#!/usr/bin/python3
# Run this with --help to see available options for tracing and debugging
# See https://github.com/cockpit-project/cockpit/blob/master/test/common/testlib.py
# "class Browser" and "class MachineCase" for the available API.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))
import testlib

# Test with pre-recorded journal with tlog UID 981
class TestApplication(testlib.MachineCase):
    def testPlay(self):
        term_first_line = ".xterm-accessibility-tree div:nth-child(1)"
        b = self.browser
        m = self.machine
        self.login_and_go("/session-recording")
        b.wait_present(".content-header-extra")
        b.wait_present("#user")
        b.click(".listing-ct-item")
        b.click("#player-play-pause")
        b.wait_timeout(30000)
        b.wait_in_text(term_first_line, "localhost")

    def testFastforwardControls(self):
        last_term_line = ".xterm-accessibility-tree > div:nth-child(26)"
        slider = ".slider > .min-slider-handle"
        b = self.browser
        m = self.machine
        self.login_and_go("/session-recording")
        b.wait_present(".content-header-extra")
        b.wait_present("#user")
        b.click(".listing-ct-item")
        b.click("#player-fast-forward")
        b.wait_in_text(last_term_line, "logout")
        b.wait_attr(slider, "style", "left: 100%;")
        # test restart playback
        b.click("#player-restart")
        b.wait_text(".xterm-accessibility-tree > div:nth-child(1)", "Blank line")
        b.wait_attr(slider, "style", "left: 100%;")

    def testSpeedControls(self):
        speed_val = ".panel-footer > span:nth-child(10)"
        b = self.browser
        m = self.machine
        self.login_and_go("/session-recording")
        b.wait_present(".content-header-extra")
        b.wait_present("#user")
        b.click(".listing-ct-item")
        # increase speed
        b.wait_present("#player-speed-up")
        b.click("#player-speed-up")
        b.wait_present(speed_val)
        b.wait_text(speed_val, "x2")
        b.click("#player-speed-up")
        b.wait_text(speed_val, "x4")
        b.click("#player-speed-up")
        b.wait_text(speed_val, "x8")
        b.click("#player-speed-up")
        b.wait_text(speed_val, "x16")
        # decrease speed
        b.click("#player-speed-down")
        b.wait_text(speed_val, "x8")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "x4")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "x2")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "/2")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "/4")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "/8")
        b.click("#player-speed-down")
        b.wait_text(speed_val, "/16")
        # restore speed
        b.click("#player-speed-reset")
        b.wait_text(speed_val, "")

    def testZoomControls(self):
        default_scale_sel = '.console-ct[style^="transform: scale(1)"]'
        zoom_one_scale_sel = '.console-ct[style^="transform: scale(1.1)"]'
        zoom_two_scale_sel = '.console-ct[style^="transform: scale(1.2)"]'
        zoom_three_scale_sel = '.console-ct[style^="transform: scale(1.3)"]'
        b = self.browser
        m = self.machine
        if m.image in ["fedora-29"] or m.image in ["rhel-8.0"] or m.image in ["rhel-8.1"]:
            zoom_reset_scale_sel = '.console-ct[style*="transform: scale(0.8"]'
        else:
            zoom_reset_scale_sel = '.console-ct[style*="transform: scale(0.9"]'

        self.login_and_go("/session-recording")
        b.wait_present(".content-header-extra")
        b.wait_present("#user")
        b.click(".listing-ct-item")
        # Wait for terminal with scale(1)
        b.wait_present(default_scale_sel)
        # Zoom in x3
        b.click("#player-zoom-in")
        b.wait_present(zoom_one_scale_sel)
        b.click("#player-zoom-in")
        b.wait_present(zoom_two_scale_sel)
        b.click("#player-zoom-in")
        b.wait_present(zoom_three_scale_sel)
        # Zoom Out
        b.click("#player-zoom-out")
        b.wait_present(zoom_two_scale_sel)
        # Reset Zoom
        b.click("#player-fit-to")
        b.wait_present(zoom_reset_scale_sel)

    def testSkipFrame(self):
        term_first_line = ".xterm-accessibility-tree div:nth-child(1)"
        b = self.browser
        m = self.machine
        self.login_and_go("/session-recording")
        b.wait_present(".content-header-extra")
        b.wait_present("#user")
        b.click(".listing-ct-item")
        b.click("#player-skip-frame")
        b.click("#player-skip-frame")
        b.click("#player-skip-frame")
        b.click("#player-skip-frame")
        b.click("#player-skip-frame")
        b.wait_timeout(20)
        b.wait_in_text(term_first_line, "localhost")

if __name__ == '__main__':
    testlib.test_main()
