| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 3 |
# |
| 4 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 5 |
# later. See the COPYING file in the top-level directory. |
| 6 |
|
| 7 |
""" |
| 8 |
Test cases for GuiUtil module. |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
import os |
| 13 |
import time |
| 14 |
import unittest |
| 15 |
from pathlib import Path |
| 16 |
|
| 17 |
from tests.common import pytest |
| 18 |
|
| 19 |
from bleachbit import General, logger |
| 20 |
from bleachbit.GtkShim import is_gtk_available |
| 21 |
|
| 22 |
from tests import common |
| 23 |
|
| 24 |
HAVE_GTK = is_gtk_available() |
| 25 |
if HAVE_GTK: |
| 26 |
from bleachbit.GtkShim import Gdk, Gtk # pylint: disable=ungrouped-imports |
| 27 |
from bleachbit.GuiUtil import (clear_clipboard, flush_gtk_events, |
| 28 |
get_clipboard_paths, get_font_size_from_name) |
| 29 |
|
| 30 |
CLIPBOARD_TIMEOUT_SECONDS = 5 |
| 31 |
CLIPBOARD_SLEEP_SECONDS = 0.05 |
| 32 |
|
| 33 |
|
| 34 |
@unittest.skipUnless(HAVE_GTK, 'requires GTK+ module and a display environment') |
| 35 |
class GUIUtilClipboardTestCase(common.BleachbitTestCase): |
| 36 |
"""Test case for module GUI Util""" |
| 37 |
|
| 38 |
def setUp(self): |
| 39 |
"""Set up before each test method.""" |
| 40 |
super().setUp() |
| 41 |
self.paths = [ |
| 42 |
self.write_file('clipboard-path-1'), |
| 43 |
self.write_file('clipboard-path-2'), |
| 44 |
] |
| 45 |
|
| 46 |
def tearDown(self): |
| 47 |
"""Clean up after each test method.""" |
| 48 |
super().tearDown() |
| 49 |
# Verify that clearing the clipboard works. |
| 50 |
clear_clipboard() |
| 51 |
self.assertEqual([], list(get_clipboard_paths())) |
| 52 |
|
| 53 |
def _wait_for_clipboard_text(self, clipboard, text): |
| 54 |
"""Wait for GTK to publish clipboard text.""" |
| 55 |
start_time = time.time() |
| 56 |
deadline = start_time + CLIPBOARD_TIMEOUT_SECONDS |
| 57 |
if clipboard.wait_for_text() == text: |
| 58 |
logger.debug( |
| 59 |
'clipboard text available after first wait attempt at %.1fs', time.time() - start_time) |
| 60 |
return True |
| 61 |
while time.time() < deadline: |
| 62 |
flush_gtk_events() |
| 63 |
if clipboard.wait_for_text() == text: |
| 64 |
elapsed = time.time() - start_time |
| 65 |
logger.info( |
| 66 |
"clipboard text became available after %.1fs", elapsed) |
| 67 |
return True |
| 68 |
time.sleep(CLIPBOARD_SLEEP_SECONDS) |
| 69 |
elapsed = time.time() - start_time |
| 70 |
logger.warning( |
| 71 |
"clipboard text was still not available after %.1fs", elapsed) |
| 72 |
return False |
| 73 |
|
| 74 |
def _copy_paths_to_windows_clipboard(self): |
| 75 |
"""Copy test paths using the shell clipboard, like Explorer.""" |
| 76 |
pattern = os.path.join(self.tempdir, 'clipboard-path-*') |
| 77 |
args = ('powershell.exe', 'Set-Clipboard', '-Path', pattern) |
| 78 |
rc, _stdout, stderr = General.run_external(args) |
| 79 |
self.assertEqual(0, rc, stderr) |
| 80 |
|
| 81 |
def _wait_for_windows_clipboard_paths(self, clipboard, paths): |
| 82 |
"""Wait for the production clipboard path to return file paths.""" |
| 83 |
expected = sorted(paths) |
| 84 |
start_time = time.time() |
| 85 |
deadline = start_time + CLIPBOARD_TIMEOUT_SECONDS |
| 86 |
while time.time() < deadline: |
| 87 |
flush_gtk_events() |
| 88 |
got = sorted(get_clipboard_paths()) |
| 89 |
if got == expected: |
| 90 |
elapsed = time.time() - start_time |
| 91 |
logger.info( |
| 92 |
"clipboard paths became available after %.1fs", elapsed) |
| 93 |
return |
| 94 |
time.sleep(CLIPBOARD_SLEEP_SECONDS) |
| 95 |
got = sorted(get_clipboard_paths()) |
| 96 |
_has_targets, targets = clipboard.wait_for_targets() |
| 97 |
target_names = [target.name() for target in targets] if targets else [] |
| 98 |
elapsed = time.time() - start_time |
| 99 |
self.fail( |
| 100 |
'clipboard file paths were still not available after ' |
| 101 |
f'{elapsed:.1f}s: expected {expected}, got {got}, ' |
| 102 |
f'targets={target_names}') |
| 103 |
|
| 104 |
@pytest.mark.xdist_group('gui') |
| 105 |
def test_get_clipboard_paths_text_plain(self): |
| 106 |
"""Get text/plain paths from the real clipboard.""" |
| 107 |
|
| 108 |
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) |
| 109 |
clipboard_text = '\n'.join([f' {self.paths[0]} ', self.paths[1], '']) |
| 110 |
clipboard.set_text(clipboard_text, -1) |
| 111 |
if not self._wait_for_clipboard_text(clipboard, clipboard_text): |
| 112 |
self.skipTest('clipboard text is unavailable') |
| 113 |
|
| 114 |
# Getting should not affect the clipboard state. |
| 115 |
get1 = get_clipboard_paths() |
| 116 |
get2 = get_clipboard_paths() |
| 117 |
self.assertIsInstance(get1, list) |
| 118 |
self.assertIsInstance(get2, list) |
| 119 |
self.assertEqual(get1, get2) |
| 120 |
self.assertEqual(self.paths, get1) |
| 121 |
|
| 122 |
@common.skipIfWindows |
| 123 |
def test_get_clipboard_paths_uri_list(self): |
| 124 |
"""Get text/uri-list paths like those copied from a file manager.""" |
| 125 |
uris = [Path(path).as_uri() for path in self.paths] |
| 126 |
|
| 127 |
class ClipboardContents: |
| 128 |
"""Mock clipboard contents for testing""" |
| 129 |
|
| 130 |
def get_uris(self): |
| 131 |
"""Return the URIs""" |
| 132 |
return uris |
| 133 |
|
| 134 |
class Clipboard: |
| 135 |
"""Mock clipboard for testing""" |
| 136 |
|
| 137 |
def wait_for_contents(self, _target): |
| 138 |
"""Return mock clipboard contents""" |
| 139 |
return ClipboardContents() |
| 140 |
|
| 141 |
targets = [Gdk.Atom.intern('text/uri-list', False)] |
| 142 |
|
| 143 |
get1 = get_clipboard_paths(Clipboard(), targets) |
| 144 |
get2 = get_clipboard_paths(Clipboard(), targets) |
| 145 |
self.assertIsInstance(get1, list) |
| 146 |
self.assertIsInstance(get2, list) |
| 147 |
self.assertEqual(get1, get2) |
| 148 |
self.assertEqual(self.paths, get1) |
| 149 |
|
| 150 |
@common.skipIfWindows |
| 151 |
def test_get_clipboard_paths_matches_target_names(self): |
| 152 |
"""Match targets by name because Gdk.Atom objects may not compare equal""" |
| 153 |
uris = [Path(path).as_uri() for path in self.paths] |
| 154 |
text = '\n'.join(self.paths) |
| 155 |
|
| 156 |
class Target: |
| 157 |
"""Stand-in for a Gdk.Atom that only exposes its name""" |
| 158 |
|
| 159 |
def __init__(self, name): |
| 160 |
self._name = name |
| 161 |
|
| 162 |
def name(self): |
| 163 |
"""Return the target name""" |
| 164 |
return self._name |
| 165 |
|
| 166 |
class ClipboardContents: |
| 167 |
"""Mock clipboard contents for testing""" |
| 168 |
|
| 169 |
def get_uris(self): |
| 170 |
"""Return the URIs""" |
| 171 |
return uris |
| 172 |
|
| 173 |
class Clipboard: |
| 174 |
"""Mock clipboard for testing""" |
| 175 |
|
| 176 |
def wait_for_contents(self, _target): |
| 177 |
"""Return mock clipboard contents""" |
| 178 |
return ClipboardContents() |
| 179 |
|
| 180 |
def wait_for_text(self): |
| 181 |
"""Return the paths as plain text""" |
| 182 |
return text |
| 183 |
|
| 184 |
for target_name in ('text/uri-list', 'text/plain', 'UTF8_STRING'): |
| 185 |
with self.subTest(target=target_name): |
| 186 |
self.assertEqual(self.paths, get_clipboard_paths( |
| 187 |
Clipboard(), [Target(target_name)])) |
| 188 |
|
| 189 |
@common.skipIfWindows |
| 190 |
def test_get_clipboard_paths_unusable_target_name(self): |
| 191 |
"""Fall back to a fresh atom when a target's name cannot be decoded. |
| 192 |
|
| 193 |
Regression test for a real-world bug where Gdk.atom_intern_static_string() |
| 194 |
returned an atom whose name() raised UnicodeDecodeError. |
| 195 |
""" |
| 196 |
uris = [Path(path).as_uri() for path in self.paths] |
| 197 |
|
| 198 |
class UnusableTarget: |
| 199 |
"""Stand-in for a Gdk.Atom with a corrupted, undecodable name""" |
| 200 |
|
| 201 |
def name(self): |
| 202 |
"""Simulate a corrupted atom name""" |
| 203 |
raise UnicodeDecodeError( |
| 204 |
'utf-8', b'\xff', 0, 1, 'invalid start byte') |
| 205 |
|
| 206 |
class ClipboardContents: |
| 207 |
"""Mock clipboard contents for testing""" |
| 208 |
|
| 209 |
def get_uris(self): |
| 210 |
"""Return the URIs""" |
| 211 |
return uris |
| 212 |
|
| 213 |
class Clipboard: |
| 214 |
"""Mock clipboard for testing""" |
| 215 |
|
| 216 |
def wait_for_contents(self, _target): |
| 217 |
"""Return mock clipboard contents""" |
| 218 |
return ClipboardContents() |
| 219 |
|
| 220 |
result = get_clipboard_paths(Clipboard(), [UnusableTarget()]) |
| 221 |
self.assertEqual(self.paths, result) |
| 222 |
|
| 223 |
def test_get_clipboard_paths_none_target(self): |
| 224 |
"""Fall back to a fresh atom when a target is None. |
| 225 |
|
| 226 |
Regression test for a real-world crash on macOS/Quartz where a |
| 227 |
clipboard target whose NSPasteboard type could not be mapped to |
| 228 |
a Gdk.Atom appeared as None in the target list (alongside a |
| 229 |
'gdk_atom_intern: assertion atom_name != NULL failed' GDK |
| 230 |
warning) instead of a valid atom object, crashing on |
| 231 |
target.name() with AttributeError. |
| 232 |
""" |
| 233 |
uris = [Path(path).as_uri() for path in self.paths] |
| 234 |
|
| 235 |
class ClipboardContents: |
| 236 |
"""Mock clipboard contents for testing""" |
| 237 |
|
| 238 |
def get_uris(self): |
| 239 |
"""Return the URIs""" |
| 240 |
return uris |
| 241 |
|
| 242 |
class Clipboard: |
| 243 |
"""Mock clipboard for testing""" |
| 244 |
|
| 245 |
def wait_for_contents(self, _target): |
| 246 |
"""Return mock clipboard contents""" |
| 247 |
return ClipboardContents() |
| 248 |
|
| 249 |
result = get_clipboard_paths(Clipboard(), [None]) |
| 250 |
self.assertEqual(self.paths, result) |
| 251 |
|
| 252 |
@common.skipUnlessWindows |
| 253 |
@pytest.mark.xdist_group('gui') |
| 254 |
def test_get_clipboard_paths_windows(self): |
| 255 |
"""Get file paths from the clipboard on Windows.""" |
| 256 |
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) |
| 257 |
self._copy_paths_to_windows_clipboard() |
| 258 |
self._wait_for_windows_clipboard_paths(clipboard, self.paths) |
| 259 |
|
| 260 |
# Getting should not affect the clipboard state. |
| 261 |
get1 = get_clipboard_paths() |
| 262 |
get2 = get_clipboard_paths() |
| 263 |
self.assertIsInstance(get1, list) |
| 264 |
self.assertIsInstance(get2, list) |
| 265 |
self.assertEqual(get1, get2) |
| 266 |
self.assertEqual(sorted(self.paths), sorted(get1)) |
| 267 |
|
| 268 |
|
| 269 |
@unittest.skipUnless(HAVE_GTK, 'requires GTK+ module and a display environment') |
| 270 |
class GUIUtilFontTestCase(common.BleachbitTestCase): |
| 271 |
"""Test case for font size utility""" |
| 272 |
|
| 273 |
def test_get_font_size_from_name_valid(self): |
| 274 |
"""Extract font size from valid font names.""" |
| 275 |
tests = ( |
| 276 |
('Sans 12', 12), |
| 277 |
('Monospace Bold 14', 14), |
| 278 |
('12', 12), |
| 279 |
('Arial 10.5', 10), |
| 280 |
) |
| 281 |
for font_name, expected in tests: |
| 282 |
self.assertEqual(get_font_size_from_name(font_name), expected, |
| 283 |
f"Font name '{font_name}' should return {expected}") |
| 284 |
|
| 285 |
def test_get_font_size_from_name_invalid(self): |
| 286 |
"""Return None for invalid inputs.""" |
| 287 |
tests = ( |
| 288 |
None, |
| 289 |
123, |
| 290 |
'', |
| 291 |
'Sans', |
| 292 |
'Sans abc', |
| 293 |
'Sans 0', |
| 294 |
'Sans -1', |
| 295 |
) |
| 296 |
for font_name in tests: |
| 297 |
self.assertIsNone(get_font_size_from_name(font_name), |
| 298 |
f"Font name '{font_name}' should return None") |
| 299 |
|