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 base64
0007 import functools
0008 import os
0009 import subprocess
0010 import sys
0011 import tempfile
0012 import time
0013 import unittest
0014 
0015 import cv2 as cv
0016 import gi
0017 import numpy as np
0018 from appium import webdriver
0019 from appium.options.common.base import AppiumOptions
0020 from appium.webdriver.common.appiumby import AppiumBy
0021 from desktoptest import start_kactivitymanagerd, start_kded
0022 from selenium.webdriver.support import expected_conditions as EC
0023 from selenium.webdriver.support.ui import WebDriverWait
0024 
0025 gi.require_version('GdkPixbuf', '2.0')
0026 from gi.repository import GdkPixbuf, GLib
0027 
0028 
0029 class FolderViewTest(unittest.TestCase):
0030     """
0031     Tests for the desktop folder view layout
0032     """
0033 
0034     driver: webdriver.Remote
0035     kactivitymanagerd: subprocess.Popen | None = None
0036     kded: subprocess.Popen | None = None
0037     desktop_dir: str = ""
0038 
0039     @classmethod
0040     def setUpClass(cls) -> None:
0041         """
0042         Initializes the webdriver and the desktop folder
0043         """
0044         cls.desktop_dir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP)
0045         if not os.path.exists(cls.desktop_dir):
0046             os.makedirs(cls.desktop_dir)
0047         # Create an image file
0048         bits_per_sample = 8
0049         width = height = 16
0050         pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, True, bits_per_sample, width, height)
0051         pixbuf.fill(0xff0000ff)
0052         assert pixbuf.savev(os.path.join(cls.desktop_dir, "test.png"), "png")
0053         cls.addClassCleanup(functools.partial(os.remove, os.path.join(cls.desktop_dir, "test.png")))
0054 
0055         cls.kactivitymanagerd = start_kactivitymanagerd()
0056         cls.kded = start_kded()
0057 
0058         options = AppiumOptions()
0059         options.set_capability("app", "plasmashell -p org.kde.plasma.desktop --no-respawn")
0060         options.set_capability("timeouts", {'implicit': 30000})
0061         cls.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', options=options)
0062 
0063     def tearDown(self) -> None:
0064         """
0065         Take screenshot when the current test fails
0066         """
0067         if not self._outcome.result.wasSuccessful():
0068             self.driver.get_screenshot_as_file(f"failed_test_shot_folderview_#{self.id()}.png")
0069 
0070     @classmethod
0071     def tearDownClass(cls) -> None:
0072         """
0073         Make sure to terminate the driver again, lest it dangles.
0074         """
0075         subprocess.check_output(["kquitapp6", "plasmashell"], stderr=sys.stderr)
0076         if cls.kded:
0077             cls.kded.kill()
0078         if cls.kactivitymanagerd:
0079             cls.kactivitymanagerd.kill()
0080         cls.driver.quit()
0081 
0082     def test_0_folderview_ready(self) -> None:
0083         """
0084         Waits until the folder view is ready
0085         """
0086         WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((AppiumBy.NAME, "test.png")))
0087         time.sleep(3)  # Make sure the desktop is ready
0088 
0089     def test_1_image_preview(self) -> None:
0090         """
0091         test.png is filled with red color
0092         """
0093         with tempfile.TemporaryDirectory() as temp_dir:
0094             # Take desktop screenshot
0095             saved_image_path: str = os.path.join(temp_dir, "desktop.png")
0096             subprocess.check_call(["import", "-window", "root", saved_image_path])
0097             cv_first_image = cv.imread(saved_image_path, cv.IMREAD_COLOR)
0098             first_image = base64.b64encode(cv.imencode('.png', cv_first_image)[1].tobytes()).decode()
0099         cv_second_image = np.zeros((16, 16, 3), dtype=np.uint8)
0100         cv_second_image[:, :] = [0, 0, 255]
0101         second_image = base64.b64encode(cv.imencode('.png', cv_second_image)[1].tobytes()).decode()
0102         self.driver.find_image_occurrence(first_image, second_image)
0103 
0104     def test_2_bug469260_new_file_appear_without_refresh(self) -> None:
0105         """
0106         A new file on desktop created by other applications should show up immediately
0107         """
0108         with open(os.path.join(self.desktop_dir, "test.txt"), "w", encoding="utf-8") as handler:
0109             handler.write("\n")
0110 
0111         self.addCleanup(functools.partial(os.remove, os.path.join(self.desktop_dir, "test.txt")))
0112         WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((AppiumBy.NAME, "test.txt")))
0113 
0114 
0115 if __name__ == '__main__':
0116     unittest.main()