#!/usr/bin/python
# -*- 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()

        # 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")

        # Lets try changing the language
        # Translations are not ready for RHEL yet
        if "rhel" not in m.image:
            b.switch_to_top()
            b.click("#content-user-name")
            b.click(".display-language-menu a")
            b.wait_popup('display-language')
            b.set_val("#display-language select", "de")
            b.click("#display-language-select-button")
            b.expect_load()
            b.wait_present("#content")
            b.go("/system")
            b.enter_page("/system")
            b.wait_present("#server")
            b.wait_in_text("#server", "Neustarten")

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

    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.expect_load()
        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()
