#!/usr/bin/python2
# -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import parent
from testlib import *

class TestPages(MachineCase):
    def testBasic(self):
        m = self.machine
        b = self.browser
        m.write("/etc/systemd/system/test.service",
"""
[Unit]
Description=Test Service

[Service]
ExecStart=/bin/true

[Install]
WantedBy=default.target
""")

        self.allow_restart_journal_messages()
        self.allow_journal_messages("Failed to get realtime timestamp: Cannot assign requested address")

        # On Debian and Ubuntu we have to generate the other locales :S
        if m.image in ["debian-stable", "debian-testing" ]:
            m.execute("echo 'de_DE.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen && update-locale")
        elif m.image in [ "ubuntu-1604", "ubuntu-stable" ]:
            m.execute("locale-gen de_DE && locale-gen de_DE.UTF-8 && update-locale")

        # On Atomic no locales other than en_US are supported on the host itself
        # HACK: https://bugzilla.redhat.com/show_bug.cgi?id=1186757
        elif m.image in [ "continuous-atomic", "fedora-atomic", "rhel-atomic" ]:
            self.allow_journal_messages("invalid or unusable locale: de_DE.UTF-8")

        # login so that we have a cookie.
        self.login_and_go("/system/services#/test.service")

        # check that reloading a page with parameters works
        b.enter_page("/system/services")
        b.reload()
        b.enter_page("/system/services")
        b.wait_present("#service-unit .panel-heading")
        b.wait_text("#service-unit .panel-heading", "Test Service")

        m.restart_cockpit()
        b.relogin("/system/services")
        b.wait_present("#service-unit .panel-heading")
        b.wait_text("#service-unit .panel-heading", "Test Service")

        # check that navigating away and back preserves place
        b.switch_to_top();
        b.click("a[href='/system']")
        b.enter_page("/system")
        b.wait_present("#system_information_hostname_button")
        b.switch_to_top();
        b.click("a[href='/system/services']")
        b.enter_page("/system/services")
        b.wait_present("#service-unit .panel-heading")
        b.wait_visible("#service-unit")
        b.wait_present("ol.breadcrumb")
        b.wait_text("#service-unit .panel-heading", "Test Service")
        b.switch_to_top();
        b.wait_js_cond('window.location.pathname === "/system/services"')
        b.wait_js_cond('window.location.hash === "#/test.service"')

        # check that when inside the component clicking the navbar
        # takes you home
        b.switch_to_top()
        b.click("a[href='/system/services']")
        b.enter_page("/system/services")
        b.wait_present("#services-list-enabled")
        b.wait_not_visible("#service-unit")
        b.switch_to_top();
        b.wait_js_cond('window.location.pathname === "/system/services"')
        b.wait_js_cond('window.location.hash === ""')

        # Navigate inside an iframe
        b.switch_to_top()
        b.go("/@localhost/playground/test")
        b.enter_page("/playground/test")
        b.click("button:contains('Go down')")
        b.click("button:contains('Go down')")
        b.switch_to_top()
        b.wait_js_cond("window.location.hash == '#/0/1?length=1'")

        # This should be visible now
        b.switch_to_frame("cockpit1:localhost/playground/test")
        b.wait_text("#hidden", "visible")

        # This should now say invisible
        b.switch_to_top()
        b.go("/@localhost/system/services")
        b.switch_to_frame("cockpit1:localhost/playground/test")
        b.wait_text("#hidden", "hidden")

        b.switch_to_top()

        if m.image != "rhel-7-4":
            # The top bar should not be visible on fedora-atomic because no dashboards
            b.wait_present(".area-ct-body")
            if "atomic" in m.image:
                b.wait_present(".area-ct-body.single-nav")
            else:
                b.wait_not_present(".area-ct-body.single-nav")
            b.wait_present("#host-nav")

        # Lets try changing the language

        b.click("#content-user-name")
        b.click(".display-language-menu a")
        b.wait_popup('display-language')
        b.set_val("#display-language select", "de-de")
        b.click("#display-language-select-button")
        b.expect_load()
        b.wait_present("#content")

        # Check that the system page is translated
        b.go("/system")
        b.enter_page("/system")
        b.wait_present("#server")
        b.wait_in_text("#server", "Neustarten")

        # Check the playground page
        b.switch_to_top()
        b.go("/playground/translate")
        b.enter_page("/playground/translate")

        # HTML section
        self.assertEqual(b.text("#translate-html"), "Bereit")
        self.assertEqual(b.text("#translate-html-context"), "Bereiten")
        self.assertEqual(b.text("#translate-html-yes"), "Nicht bereit")
        self.assertEqual(b.attr("#translate-html-title", "title"), u"Nicht verfügbar")
        self.assertEqual(b.text("#translate-html-title"), "Cancel")
        self.assertEqual(b.attr("#translate-html-yes-title", "title"), u"Nicht verfügbar")
        self.assertEqual(b.text("#translate-html-yes-title"), "Abbrechen")

        # Glade section
        self.assertEqual(b.text("#translatable-glade"), "Leer")
        self.assertEqual(b.text("#translatable-glade-context"), "Leeren")

        # Mustache section
        self.assertEqual(b.text("#translatable-mustache"), "Benutzer")
        self.assertEqual(b.text("#translate-mustache"), "Speicher")
        self.assertEqual(b.text("#translate-mustache-yes"), "Netzwerk")
        self.assertEqual(b.attr("#translate-mustache-title", "title"), "Fehler")
        self.assertEqual(b.text("#translate-mustache-title"), "User")
        self.assertEqual(b.attr("#translate-mustache-yes-title", "title"), "Fehler")
        self.assertEqual(b.text("#translate-mustache-yes-title"), "Benutzer")

        # Javascript
        self.assertEqual(b.text("#underscore-empty"), "Leer")
        self.assertEqual(b.text("#underscore-context-empty"), "Leeren")
        self.assertEqual(b.text("#cunderscore-context-empty"), "Leeren")
        self.assertEqual(b.text("#gettext-control"), "Steuerung")
        self.assertEqual(b.text("#gettext-context-control"), "Strg")
        self.assertEqual(b.text("#ngettext-disks-1"), "$0 Festplatte fehlt")
        self.assertEqual(b.text("#ngettext-disks-2"), "$0 Festplatten fehlen")
        self.assertEqual(b.text("#ngettext-context-disks-1"), u"$0 Datenträger fehlt")
        self.assertEqual(b.text("#ngettext-context-disks-2"), u"$0 Datenträger fehlen")

        # Angular
        self.assertEqual(b.text("#angular-translate"), "Bereit")
        self.assertEqual(b.text("#angular-context"), "Bereiten")
        self.assertEqual(b.text("#angular-interpolate"), u"'Marmalade' löschen!!!")

        # Log out and check that login page is translated now
        b.logout()
        b.wait_visible('#password-group')
        b.wait_text("#password-group > label", "Passwort")

    def testFrameReload(self):
        b = self.browser
        frame = "cockpit1:localhost/playground/test"

        self.login_and_go("/playground/test")

        b.wait_present('#file-content')
        b.wait_text('#file-content', "0")
        b.click("#modify-file")
        b.wait_text('#file-content', "1")

        b.switch_to_top()
        b.eval_js('ph_set_attr("iframe[name=\'%s\']", "data-ready", null)' % frame)
        b.eval_js('ph_set_attr("iframe[name=\'%s\']", "src", "../playground/test.html?i=1#/")' % frame)
        b.wait_present("iframe.container-frame[name='%s'][data-ready]" % frame)

        b.enter_page("/playground/test")

        b.wait_present('#file-content')
        b.wait_text('#file-content', "1")

if __name__ == '__main__':
    test_main()
