File indexing completed on 2024-05-12 05:35:22

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0004 # SPDX-License-Identifier: MIT
0005 
0006 import os
0007 import subprocess
0008 import sys
0009 import unittest
0010 
0011 from appium import webdriver
0012 from appium.options.common.base import AppiumOptions
0013 from appium.webdriver.common.appiumby import AppiumBy
0014 from desktoptest import start_plasmashell
0015 from gi.repository import Gio, GLib
0016 from selenium.webdriver.support import expected_conditions as EC
0017 from selenium.webdriver.support.ui import WebDriverWait
0018 
0019 
0020 class Bug472909Test(unittest.TestCase):
0021     """
0022     Kickoff is taking focus away after being closed with meta key
0023     """
0024 
0025     driver: webdriver.Remote
0026     kactivitymanagerd: subprocess.Popen | None = None
0027     kded: subprocess.Popen | None = None
0028     plasmashell: subprocess.Popen | None = None
0029 
0030     @classmethod
0031     def setUpClass(cls) -> None:
0032         cls.kactivitymanagerd, cls.kded, cls.plasmashell = start_plasmashell()
0033 
0034         options = AppiumOptions()
0035         options.set_capability("app", "Root")
0036         options.set_capability("timeouts", {'implicit': 30000})
0037         cls.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0038 
0039     def tearDown(self) -> None:
0040         """
0041         Take screenshot when the current test fails
0042         """
0043         if os.environ["GDK_BACKEND"] == "wayland" and not self._outcome.result.wasSuccessful():
0044             self.driver.get_screenshot_as_file(f"failed_test_shot_bug472909_#{self.id()}.png")
0045 
0046     @classmethod
0047     def tearDownClass(cls) -> None:
0048         """
0049         Make sure to terminate the driver again, lest it dangles.
0050         """
0051         subprocess.check_output(["kquitapp6", "plasmashell"], stderr=sys.stderr)
0052         cls.plasmashell.wait()
0053         if cls.kded:
0054             cls.kded.terminate()
0055             cls.kded.wait()
0056         if cls.kactivitymanagerd:
0057             cls.kactivitymanagerd.terminate()
0058             cls.kactivitymanagerd.wait()
0059         cls.driver.quit()
0060 
0061     def test_bug472909(self) -> None:
0062         wait = WebDriverWait(self.driver, 30)
0063         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Application Launcher")))
0064 
0065         # The driver doesn't support sending global keyboard shortcuts
0066         session_bus: Gio.DBusConnection = Gio.bus_get_sync(Gio.BusType.SESSION)
0067         message = Gio.DBusMessage.new_method_call("org.kde.kglobalaccel", "/component/plasmashell", "org.kde.kglobalaccel.Component", "allShortcutInfos")
0068         reply, _ = session_bus.send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE, 1000)
0069         shortcut_list = reply.get_body().get_child_value(0)
0070         launcher_shortcut_id: str = ""
0071         for i in range(shortcut_list.n_children()):
0072             shortcut_info: GLib.Variant = shortcut_list.get_child_value(i)
0073             shortcut_id: str = shortcut_info.get_child_value(0).get_string()
0074             if not shortcut_id.startswith("activate widget"):
0075                 continue
0076             shortcut_name: str = shortcut_info.get_child_value(1).get_string()
0077             if not "Application Launcher" in shortcut_name:  # from metadata.json
0078                 continue
0079             launcher_shortcut_id = shortcut_id
0080             break
0081         self.assertGreater(len(launcher_shortcut_id), 0)
0082 
0083         # Start active indicator window
0084         test_window = subprocess.Popen([os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", "bug472909_activewindow.py")], stdout=sys.stderr, stderr=sys.stderr)
0085         self.addCleanup(test_window.terminate)
0086 
0087         def activate_shortcut() -> None:
0088             message = Gio.DBusMessage.new_method_call("org.kde.kglobalaccel", "/component/plasmashell", "org.kde.kglobalaccel.Component", "invokeShortcut")
0089             message.set_body(GLib.Variant("(s)", [launcher_shortcut_id]))
0090             session_bus.send_message_with_reply_sync(message, Gio.DBusSendMessageFlags.NONE, 1000)
0091 
0092         # Activate the shortcut twice to test focus restoration
0093         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Active Window")))
0094         activate_shortcut()
0095         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Inactive Window")))
0096         activate_shortcut()
0097         wait.until(EC.presence_of_element_located((AppiumBy.NAME, "Active Window")))
0098 
0099         test_window.terminate()
0100         test_window.wait()
0101 
0102 
0103 if __name__ == '__main__':
0104     unittest.main()