| 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 |
""" |
| 9 |
Test case for module GUI |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
import unittest |
| 14 |
import time |
| 15 |
import types |
| 16 |
import warnings |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from tests.common import pytest |
| 20 |
|
| 21 |
import bleachbit |
| 22 |
from bleachbit.Cleaner import Cleaner, backends |
| 23 |
from bleachbit.GtkShim import Gdk, Gio, GLib, GObject, Gtk, is_gtk_available |
| 24 |
from bleachbit.Language import get_supported_language_code_name_dict |
| 25 |
from bleachbit.Language import get_text as _ |
| 26 |
from bleachbit.Options import options |
| 27 |
from tests import common |
| 28 |
|
| 29 |
HAVE_GTK = is_gtk_available() |
| 30 |
if HAVE_GTK: |
| 31 |
from bleachbit.GuiUtil import (clear_clipboard, get_font_size_from_name, |
| 32 |
get_window_info) |
| 33 |
from bleachbit.GuiTreeModels import TreeDisplayModel |
| 34 |
|
| 35 |
bleachbit.online_update_notification_enabled = False |
| 36 |
|
| 37 |
|
| 38 |
@unittest.skipUnless(HAVE_GTK, 'requires GTK+ module and a display environment') |
| 39 |
@pytest.mark.xdist_group('gui') |
| 40 |
class GUITestCase(common.BleachbitTestCase): |
| 41 |
app = None |
| 42 |
options_get_tree = options.get_tree |
| 43 |
_NEW_CLEANER_ID, _NEW_OPTION_ID = 'test_run_operations', 'test1' |
| 44 |
|
| 45 |
"""Test case for module GUI""" |
| 46 |
@classmethod |
| 47 |
def setUpClass(cls): |
| 48 |
from bleachbit.GuiApplication import Bleachbit |
| 49 |
cls.app = Bleachbit(auto_exit=False, uac=False) |
| 50 |
cls._lang_env = common.set_temporary_env('LANGUAGE', 'en') |
| 51 |
cls._lang_env.__enter__() |
| 52 |
# reminder: the set up in the parent class creates a clean |
| 53 |
# configuration file. |
| 54 |
super().setUpClass() |
| 55 |
options.get_tree = types.MethodType( |
| 56 |
lambda self, parent, child: False, options) |
| 57 |
# override, not set(): a plain set() is reverted by the per-test |
| 58 |
# tearDown reload after the first test in the class |
| 59 |
options.set_override('font_check_completed', True) |
| 60 |
try: |
| 61 |
cls.app.register() |
| 62 |
except GLib.GError as e: |
| 63 |
if "already exported" in str(e): |
| 64 |
cls.app = Gio.Application.get_default() |
| 65 |
else: |
| 66 |
raise |
| 67 |
with common.capture_glib_exceptions() as glib_errors: |
| 68 |
cls.app.activate() |
| 69 |
cls.refresh_gui() |
| 70 |
if glib_errors: |
| 71 |
_exc_type, exc_value, exc_tb = glib_errors[0] |
| 72 |
raise exc_value.with_traceback(exc_tb) |
| 73 |
|
| 74 |
@classmethod |
| 75 |
def tearDownClass(cls): |
| 76 |
super().tearDownClass() |
| 77 |
options.get_tree = cls.options_get_tree |
| 78 |
cls._lang_env.__exit__(None, None, None) |
| 79 |
window = cls.get_window() |
| 80 |
if window: |
| 81 |
window.destroy() |
| 82 |
cls.clear_window() |
| 83 |
|
| 84 |
@classmethod |
| 85 |
def get_window(cls): |
| 86 |
return getattr(cls.app, '_window', None) |
| 87 |
|
| 88 |
@classmethod |
| 89 |
def clear_window(cls): |
| 90 |
if cls.app: |
| 91 |
setattr(cls.app, '_window', None) |
| 92 |
|
| 93 |
@classmethod |
| 94 |
def refresh_gui(cls, delay=0): |
| 95 |
while Gtk.events_pending(): |
| 96 |
Gtk.main_iteration_do(blocking=False) |
| 97 |
time.sleep(delay) |
| 98 |
|
| 99 |
@classmethod |
| 100 |
def print_widget(cls, widget, indent=0): |
| 101 |
print('{}{}'.format(' ' * indent, widget)) |
| 102 |
if isinstance(widget, Gtk.Container): |
| 103 |
for c in widget.get_children(): |
| 104 |
cls.print_widget(c, indent + 2) |
| 105 |
|
| 106 |
@classmethod |
| 107 |
def find_widget(cls, widget, widget_class, widget_label=None): |
| 108 |
if isinstance(widget, widget_class): |
| 109 |
if widget_label is None or widget.get_label() == widget_label: |
| 110 |
return widget |
| 111 |
if isinstance(widget, Gtk.Container): |
| 112 |
for c in widget.get_children(): |
| 113 |
b = cls.find_widget(c, widget_class, widget_label) |
| 114 |
if b is not None: |
| 115 |
return b |
| 116 |
return None |
| 117 |
|
| 118 |
@classmethod |
| 119 |
def get_iter(cls, model, cleaner): |
| 120 |
it = model.get_iter(Gtk.TreePath(0)) |
| 121 |
while it: |
| 122 |
if model[it][2] == cleaner: |
| 123 |
return model.iter_children(it) |
| 124 |
it = model.iter_next(it) |
| 125 |
return None |
| 126 |
|
| 127 |
@classmethod |
| 128 |
def find_option(cls, model, cleaner, option): |
| 129 |
it = cls.get_iter(model, cleaner) |
| 130 |
# cls.assertIsNotNone(cls, it) |
| 131 |
while it: |
| 132 |
if model[it][2] == option: |
| 133 |
return it |
| 134 |
it = model.iter_next(it) |
| 135 |
return None |
| 136 |
|
| 137 |
def _put_checkmark_on_cleaner(self, gui, cleaner_id, option_id): |
| 138 |
model = gui.view.get_model() |
| 139 |
tree = self.find_widget(gui, Gtk.TreeView) |
| 140 |
self.assertIsNotNone(tree) |
| 141 |
it = self.find_option(model, cleaner_id, option_id) |
| 142 |
self.assertIsNotNone(it) |
| 143 |
tree.scroll_to_cell(model.get_path(it), None, False, 0, 0) |
| 144 |
model[model.iter_parent(it)][1] = True |
| 145 |
model[it][1] = True |
| 146 |
self.refresh_gui() |
| 147 |
|
| 148 |
def click_button(self, dialog, label): |
| 149 |
b = self.find_widget(dialog, Gtk.Button, label) |
| 150 |
self.assertIsNotNone(b) |
| 151 |
b.clicked() |
| 152 |
self.refresh_gui() |
| 153 |
|
| 154 |
def wait_until(self, condition, timeout=5): |
| 155 |
""" |
| 156 |
Wait until a condition is met or timeout is reached. |
| 157 |
|
| 158 |
Args: |
| 159 |
condition: A callable that returns a boolean |
| 160 |
timeout: Maximum time to wait in seconds |
| 161 |
|
| 162 |
Returns: |
| 163 |
True if condition is met, False if timeout is reached |
| 164 |
""" |
| 165 |
deadline = time.time() + timeout |
| 166 |
while time.time() < deadline: |
| 167 |
self.refresh_gui(0.05) |
| 168 |
if condition(): |
| 169 |
return True |
| 170 |
self.refresh_gui() |
| 171 |
return condition() |
| 172 |
|
| 173 |
@pytest.mark.no_xdist |
| 174 |
def test_append_text(self): |
| 175 |
"""Test append_text handles special strings""" |
| 176 |
gui = self.get_window() |
| 177 |
for test_str in common.SPECIAL_TEST_STRINGS: |
| 178 |
gui.append_text(test_str + "\n") |
| 179 |
gui.append_text(f"prefix{test_str}suffix\n") |
| 180 |
gui.append_text(test_str + "\n", tag='error') |
| 181 |
gui.append_text(f"Deleting file: /path/to/file_{test_str}.txt\n") |
| 182 |
self.refresh_gui() |
| 183 |
|
| 184 |
def test_get_window_info(self): |
| 185 |
"""Test get_window_info""" |
| 186 |
gui = self.get_window() |
| 187 |
geo = get_window_info(gui) |
| 188 |
self.assertGreaterEqual(geo.x, 0) |
| 189 |
self.assertGreaterEqual(geo.y, 0) |
| 190 |
self.assertGreaterEqual(geo.width, 0) |
| 191 |
self.assertGreaterEqual(geo.height, 0) |
| 192 |
self.assertIsInstance(geo.monitor_model, str) |
| 193 |
self.assertGreater(len(geo.monitor_model), 0) |
| 194 |
self.assertNotEqual(geo.monitor_model, "(error)") |
| 195 |
|
| 196 |
def test_GUI(self): |
| 197 |
"""Unit test for class GUI""" |
| 198 |
# there should be no crashes |
| 199 |
# app.do_startup() |
| 200 |
# pp.do_activate() Build a unit test that that does this |
| 201 |
gui = self.get_window() |
| 202 |
gui.update_progress_bar(0.0) |
| 203 |
gui.update_progress_bar(1.0) |
| 204 |
gui.update_progress_bar("status") |
| 205 |
|
| 206 |
def test_get_font_size_from_name(self): |
| 207 |
"""Test get_font_size_from_name()""" |
| 208 |
tests = (("Sans 12", 12), |
| 209 |
("Sans 10.5", 10), |
| 210 |
("Sans 0", None), |
| 211 |
("Sans -1", None), |
| 212 |
("Sans", None), |
| 213 |
("", None), |
| 214 |
(None, None)) |
| 215 |
for font_name, expected in tests: |
| 216 |
self.assertEqual(get_font_size_from_name( |
| 217 |
font_name), expected, f"Font name '{font_name}' should return {expected}") |
| 218 |
|
| 219 |
def test_preferences(self): |
| 220 |
"""Opens the preferences dialog and closes it""" |
| 221 |
|
| 222 |
# show preferences dialog |
| 223 |
pref = self.app.get_preferences_dialog() |
| 224 |
pref.dialog.show_all() |
| 225 |
self.refresh_gui() |
| 226 |
|
| 227 |
# click close button |
| 228 |
self.click_button(pref.dialog, Gtk.STOCK_CLOSE) |
| 229 |
|
| 230 |
# destroy |
| 231 |
pref.dialog.destroy() |
| 232 |
|
| 233 |
def test_preferences_language_scan_once(self): |
| 234 |
"""Opening the preferences dialog scans for languages once""" |
| 235 |
with mock.patch('bleachbit.GuiPreferences.get_supported_language_code_name_dict', |
| 236 |
wraps=get_supported_language_code_name_dict) as mock_get: |
| 237 |
pref = self.app.get_preferences_dialog() |
| 238 |
self.assertEqual(mock_get.call_count, 1) |
| 239 |
pref.dialog.destroy() |
| 240 |
|
| 241 |
def test_preferences_cookies_page(self): |
| 242 |
"""Opens the preferences dialog and navigates to cookies page""" |
| 243 |
pref = self.app.get_preferences_dialog() |
| 244 |
pref.dialog.show_all() |
| 245 |
self.refresh_gui() |
| 246 |
|
| 247 |
# Navigate to cookies page - this will trigger cookie manager loading |
| 248 |
pref.select_page('cookies') |
| 249 |
self.refresh_gui() |
| 250 |
|
| 251 |
# click close button |
| 252 |
self.click_button(pref.dialog, Gtk.STOCK_CLOSE) |
| 253 |
pref.dialog.destroy() |
| 254 |
|
| 255 |
def test_system_information(self): |
| 256 |
"""Opens the system information dialog and closes it""" |
| 257 |
dialog, _txt, _txtbuffer = self.app.get_system_information_dialog() |
| 258 |
dialog.show_all() |
| 259 |
self.refresh_gui() |
| 260 |
|
| 261 |
# click close button |
| 262 |
self.click_button(dialog, Gtk.STOCK_CLOSE) |
| 263 |
|
| 264 |
# destroy |
| 265 |
dialog.destroy() |
| 266 |
|
| 267 |
def test_about(self): |
| 268 |
"""Opens the about dialog and closes it""" |
| 269 |
about = self.app.get_about_dialog() |
| 270 |
about.show_all() |
| 271 |
self.refresh_gui() |
| 272 |
|
| 273 |
# destroy |
| 274 |
about.destroy() |
| 275 |
|
| 276 |
def test_chaff(self): |
| 277 |
"""Minimal test of the chaff dialog""" |
| 278 |
from bleachbit import Chaff, GuiChaff |
| 279 |
# each function's models_dir default is frozen at def-time, so each needs its own patch |
| 280 |
models_dir = os.path.join(self.tempdir, 'chaff_models') |
| 281 |
os.makedirs(models_dir, exist_ok=True) |
| 282 |
real_download_models = Chaff.download_models |
| 283 |
real_generate_2600 = GuiChaff.generate_2600 |
| 284 |
real_generate_emails = GuiChaff.generate_emails |
| 285 |
|
| 286 |
def isolated_download_models(*args, **kwargs): |
| 287 |
kwargs.setdefault('models_dir', models_dir) |
| 288 |
return real_download_models(*args, **kwargs) |
| 289 |
|
| 290 |
def isolated_generate_2600(*args, **kwargs): |
| 291 |
kwargs.setdefault('model_dir', models_dir) |
| 292 |
return real_generate_2600(*args, **kwargs) |
| 293 |
|
| 294 |
def isolated_generate_emails(*args, **kwargs): |
| 295 |
kwargs.setdefault('models_dir', models_dir) |
| 296 |
return real_generate_emails(*args, **kwargs) |
| 297 |
|
| 298 |
with mock.patch('bleachbit.Chaff.DEFAULT_MODELS_DIR', models_dir), \ |
| 299 |
mock.patch('bleachbit.Chaff.download_models', |
| 300 |
side_effect=isolated_download_models), \ |
| 301 |
mock.patch('bleachbit.GuiChaff.generate_2600', |
| 302 |
side_effect=isolated_generate_2600), \ |
| 303 |
mock.patch('bleachbit.GuiChaff.generate_emails', |
| 304 |
side_effect=isolated_generate_emails): |
| 305 |
if not Chaff.download_models(): |
| 306 |
self.skipTest('Unable to download chaff models for GUI test') |
| 307 |
gui = self.get_window() |
| 308 |
cd = GuiChaff.ChaffDialog(gui) |
| 309 |
cd.show_all() |
| 310 |
# Trigger missing-folder branch (no destination chosen) |
| 311 |
cd.choose_folder_button.unselect_all() |
| 312 |
self.refresh_gui() |
| 313 |
cd.on_make_files(None) |
| 314 |
self.refresh_gui() |
| 315 |
self.assertTrue(cd.infobar.get_visible()) |
| 316 |
chaff_dst_dir = os.path.join(self.tempdir, 'chaff_dst') |
| 317 |
os.mkdir(chaff_dst_dir) |
| 318 |
cd.choose_folder_button.set_filename(chaff_dst_dir) |
| 319 |
# do not delete after generation |
| 320 |
cd.when_finished_combo.set_active(1) |
| 321 |
cd.stop_value_spin.set_value(10) |
| 322 |
self.refresh_gui() |
| 323 |
self.click_button(cd, _("Make files")) |
| 324 |
self.assertIsNotNone( |
| 325 |
cd.thread, 'Chaff generation thread did not start') |
| 326 |
cd.thread.join() |
| 327 |
self.refresh_gui() |
| 328 |
self.assertEqual(len(os.listdir(chaff_dst_dir)), 10) |
| 329 |
|
| 330 |
def test_cookie_manager_bulk_actions_wait_for_loading(self): |
| 331 |
"""Bulk cookie actions should not save until loading finishes""" |
| 332 |
from bleachbit import GuiCookie |
| 333 |
|
| 334 |
class FakeThread: |
| 335 |
def __init__(self, target=None, daemon=None): |
| 336 |
self.target = target |
| 337 |
self.daemon = daemon |
| 338 |
|
| 339 |
def start(self): |
| 340 |
return None |
| 341 |
|
| 342 |
with mock.patch('bleachbit.GuiCookie.threading.Thread', FakeThread): |
| 343 |
pane = GuiCookie.CookieManagerPane() |
| 344 |
|
| 345 |
self.assertTrue(pane._is_loading) |
| 346 |
self.assertFalse(pane.select_all_btn.get_sensitive()) |
| 347 |
self.assertFalse(pane.deselect_all_btn.get_sensitive()) |
| 348 |
|
| 349 |
with mock.patch.object(pane, 'save_changes') as save_changes: |
| 350 |
pane.on_select_all_clicked(None) |
| 351 |
pane.on_deselect_all_clicked(None) |
| 352 |
save_changes.assert_not_called() |
| 353 |
|
| 354 |
pane._finish_populate(['example.com']) |
| 355 |
self.refresh_gui() |
| 356 |
|
| 357 |
self.assertFalse(pane._is_loading) |
| 358 |
self.assertTrue(pane.select_all_btn.get_sensitive()) |
| 359 |
self.assertTrue(pane.deselect_all_btn.get_sensitive()) |
| 360 |
|
| 361 |
with mock.patch.object(pane, 'save_changes') as save_changes: |
| 362 |
pane.on_select_all_clicked(None) |
| 363 |
save_changes.assert_called_once_with() |
| 364 |
self.assertTrue(all(row[0] for row in pane.cookie_store)) |
| 365 |
pane.destroy() |
| 366 |
|
| 367 |
def test_preview(self): |
| 368 |
"""Select cleaner option and clicks preview button""" |
| 369 |
gui = self.get_window() |
| 370 |
self.refresh_gui() |
| 371 |
self._put_checkmark_on_cleaner(gui, 'system', 'tmp') |
| 372 |
self.refresh_gui() |
| 373 |
self.click_button(gui, _("Preview")) |
| 374 |
self.refresh_gui() |
| 375 |
|
| 376 |
def test_notify(self): |
| 377 |
"""Test a pop-up notification""" |
| 378 |
from bleachbit.GuiUtil import notify |
| 379 |
notify('This is a test notification') |
| 380 |
time.sleep(1) # Pause for 1 second |
| 381 |
|
| 382 |
@mock.patch('bleachbit.GuiBasic.delete_confirmation_dialog') |
| 383 |
def test_confirm_delete(self, mock_delete_confirmation_dialog): |
| 384 |
gui = self.get_window() |
| 385 |
options.set('expert_mode', True) |
| 386 |
for new_delete_confirmation in [True, False]: |
| 387 |
options.set('delete_confirmation', |
| 388 |
new_delete_confirmation) |
| 389 |
gui._confirm_delete(False, False) |
| 390 |
|
| 391 |
# We should have a single call to delete_confirmation_dialog |
| 392 |
# only when delete_confirmation option is True. |
| 393 |
mock_delete_confirmation_dialog.assert_called_once() |
| 394 |
|
| 395 |
def test_shred_paths(self): |
| 396 |
dirname = self.mkdtemp(prefix='bleachbit-test-shred_paths') |
| 397 |
test_files_dirs = [ |
| 398 |
self.mkstemp(prefix="somefile", dir=dirname), |
| 399 |
dirname, |
| 400 |
self.mkstemp(prefix="somefile") |
| 401 |
] |
| 402 |
|
| 403 |
for obj in test_files_dirs: |
| 404 |
self.assertExists(obj) |
| 405 |
|
| 406 |
gui = self.get_window() |
| 407 |
self.refresh_gui() |
| 408 |
|
| 409 |
with mock.patch('bleachbit.GuiWindow.GUI._confirm_delete', return_value=True): |
| 410 |
self.assertTrue(gui._confirm_delete(False, False)) |
| 411 |
gui.shred_paths(test_files_dirs) |
| 412 |
|
| 413 |
self.refresh_gui() |
| 414 |
|
| 415 |
for obj in test_files_dirs: |
| 416 |
self.assertNotExists(obj) |
| 417 |
|
| 418 |
def test_shred_paths_cancel_cleans_temporary_backend(self): |
| 419 |
"""Test that shred_paths with cancel cleans up the temporary backend""" |
| 420 |
test_file = self.write_file('shred-cancel') |
| 421 |
gui = self.get_window() |
| 422 |
options.set('delete_confirmation', True) |
| 423 |
self.refresh_gui() |
| 424 |
|
| 425 |
try: |
| 426 |
with mock.patch.object(gui, '_confirm_delete', return_value=False): |
| 427 |
gui.shred_paths([test_file]) |
| 428 |
|
| 429 |
self.assertTrue(self.wait_until(lambda: '_gui' not in backends)) |
| 430 |
self.assertExists(test_file) |
| 431 |
finally: |
| 432 |
options.set('delete_confirmation', False) |
| 433 |
backends.pop('_gui', None) |
| 434 |
|
| 435 |
def test_shred_paths_cancel_after_preview_cleans_temporary_backend(self): |
| 436 |
"""Test cancel cleans up _gui even when the preview finishes during the dialog. |
| 437 |
|
| 438 |
In real usage the confirmation dialog runs a nested GTK main loop, so |
| 439 |
the preview worker can finish and call worker_done() before the user |
| 440 |
clicks Cancel. This test simulates that by processing GUI events |
| 441 |
inside the mocked _confirm_delete before returning False. |
| 442 |
""" |
| 443 |
test_file = self.write_file('shred-cancel-after-preview') |
| 444 |
gui = self.get_window() |
| 445 |
options.set('delete_confirmation', True) |
| 446 |
self.refresh_gui() |
| 447 |
|
| 448 |
def fake_confirm_delete(*_args, **_kwargs): |
| 449 |
# Simulate the dialog processing idle events, allowing the |
| 450 |
# preview worker to finish before the user cancels. |
| 451 |
self.wait_until(lambda: '_gui' not in backends |
| 452 |
or gui._gui_cleaner_cleanup_pending is None) |
| 453 |
self.refresh_gui() |
| 454 |
return False |
| 455 |
|
| 456 |
try: |
| 457 |
with mock.patch.object(gui, '_confirm_delete', |
| 458 |
side_effect=fake_confirm_delete): |
| 459 |
gui.shred_paths([test_file]) |
| 460 |
|
| 461 |
# _gui must be gone even though the preview finished during the |
| 462 |
# dialog (before _confirm_delete returned). |
| 463 |
self.assertNotIn('_gui', backends) |
| 464 |
self.assertExists(test_file) |
| 465 |
finally: |
| 466 |
options.set('delete_confirmation', False) |
| 467 |
backends.pop('_gui', None) |
| 468 |
|
| 469 |
def test_shred_paths_clears_clipboard_mock(self): |
| 470 |
"""Test that shred_paths with should_clear_clipboard=True clears the clipboard""" |
| 471 |
test_file = self.write_file('shred-me-via-clipboard') |
| 472 |
gui = self.get_window() |
| 473 |
self.refresh_gui() |
| 474 |
|
| 475 |
with mock.patch('bleachbit.GuiWindow.clear_clipboard') as mock_clear_clipboard: |
| 476 |
with mock.patch.object(gui, '_confirm_delete', return_value=True): |
| 477 |
gui.shred_paths([test_file], should_clear_clipboard=True) |
| 478 |
|
| 479 |
self.refresh_gui() |
| 480 |
|
| 481 |
mock_clear_clipboard.assert_called_once() |
| 482 |
self.assertNotExists(test_file) |
| 483 |
|
| 484 |
# Flaky ~1/100 on Linux: X11 CLIPBOARD owner can be transiently lost |
| 485 |
# under parallel xdist workers. assertTrue (not skipTest) lets flaky rerun. |
| 486 |
@pytest.mark.flaky(reruns=2, reruns_delay=1) |
| 487 |
def test_shred_paths_from_clipboard_menu_integration(self): |
| 488 |
"""Shred a path copied to the real clipboard""" |
| 489 |
test_file = self.write_file('shred-me-via-real-clipboard') |
| 490 |
self.refresh_gui() |
| 491 |
|
| 492 |
options.set('expert_mode', True) |
| 493 |
options.set('delete_confirmation', False) |
| 494 |
|
| 495 |
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) |
| 496 |
clipboard.set_text(test_file, -1) |
| 497 |
self.refresh_gui() |
| 498 |
self.assertTrue(self.wait_until( |
| 499 |
lambda: test_file == clipboard.wait_for_text())) |
| 500 |
|
| 501 |
targets = [Gdk.Atom.intern('text/plain', False)] |
| 502 |
self.app.cb_clipboard_uri_received(clipboard, targets, None) |
| 503 |
self.assertTrue(self.wait_until(lambda: not os.path.exists(test_file))) |
| 504 |
self.assertNotExists(test_file) |
| 505 |
|
| 506 |
def test_shred_clipboard_empty_no_glib_warnings(self): |
| 507 |
"""Empty clipboard must not trigger GLib g_array warnings.""" |
| 508 |
clear_clipboard() |
| 509 |
self.refresh_gui() |
| 510 |
|
| 511 |
with warnings.catch_warnings(record=True) as caught: |
| 512 |
warnings.simplefilter('always') |
| 513 |
self.app.cb_shred_clipboard(None, None) |
| 514 |
self.refresh_gui() |
| 515 |
glib_warnings = [ |
| 516 |
warning for warning in caught |
| 517 |
if 'g_array' in str(warning.message) |
| 518 |
] |
| 519 |
|
| 520 |
self.assertEqual([], glib_warnings) |
| 521 |
|
| 522 |
def _setup_new_cleaner(self, gui): |
| 523 |
def _create_cleaner_file_in_directory(dirname): |
| 524 |
cleaner_content = ('<?xml version="1.0" encoding="UTF-8"?>' |
| 525 |
'<cleaner id="{}">' |
| 526 |
'<label>Test run_operations</label>' |
| 527 |
'<option id="{}">' |
| 528 |
'<label>Test1</label>' |
| 529 |
'<description>Delete files in a test directory</description>' |
| 530 |
'<action command="delete" search="walk.all" path="{}"/>' |
| 531 |
'</option>' |
| 532 |
'</cleaner>'.format(self._NEW_CLEANER_ID, self._NEW_OPTION_ID, dirname)) |
| 533 |
cleaner_filename = os.path.join( |
| 534 |
dirname, 'test_run_operations_cleaner.xml') |
| 535 |
self.write_file(cleaner_filename, cleaner_content, 'w') |
| 536 |
return cleaner_filename |
| 537 |
|
| 538 |
def _load_new_cleaner_in_gui(gui): |
| 539 |
# to load the new test cleaner with id 'test_run_operations' |
| 540 |
gui.cb_refresh_operations() |
| 541 |
self.refresh_gui() |
| 542 |
|
| 543 |
dirname = self.mkdtemp(prefix='bleachbit-test-run_operations') |
| 544 |
cleaner_filename = _create_cleaner_file_in_directory(dirname) |
| 545 |
self.assertExists(cleaner_filename) |
| 546 |
with mock.patch('bleachbit.CleanerML.list_cleanerml_files') as mock_list_cleanerml_files: |
| 547 |
with mock.patch('bleachbit.RecognizeCleanerML.cleaner_change_dialog') as mock_cleaner_change_dialog: |
| 548 |
mock_list_cleanerml_files.return_value = [cleaner_filename] |
| 549 |
mock_cleaner_change_dialog.return_value = None |
| 550 |
_load_new_cleaner_in_gui(gui) |
| 551 |
file_to_clean = self.mkstemp(prefix="somefile", dir=dirname) |
| 552 |
self.assertExists(file_to_clean) |
| 553 |
return file_to_clean |
| 554 |
|
| 555 |
def test_run_operations(self): |
| 556 |
gui = self.get_window() |
| 557 |
file_to_clean = self._setup_new_cleaner(gui) |
| 558 |
self._put_checkmark_on_cleaner( |
| 559 |
gui, self._NEW_CLEANER_ID, self._NEW_OPTION_ID) |
| 560 |
|
| 561 |
with mock.patch('bleachbit.GuiWindow.GUI._confirm_delete', return_value=True): |
| 562 |
self.assertTrue(gui._confirm_delete(False, False)) |
| 563 |
# same as b = self.click_button(gui, _("Clean")) |
| 564 |
gui.run_operations(None) |
| 565 |
|
| 566 |
self.refresh_gui() |
| 567 |
self.assertNotExists(file_to_clean) |
| 568 |
|
| 569 |
def test_cb_run_option(self): |
| 570 |
gui = self.get_window() |
| 571 |
file_to_clean = self._setup_new_cleaner(gui) |
| 572 |
|
| 573 |
for really_delete, assert_method in [(False, self.assertExists), (True, self.assertNotExists)]: |
| 574 |
with mock.patch('bleachbit.GuiWindow.GUI._confirm_delete', return_value=True): |
| 575 |
self.assertTrue(gui._confirm_delete(False, False)) |
| 576 |
gui.cb_run_option( |
| 577 |
None, really_delete, self._NEW_CLEANER_ID, self._NEW_OPTION_ID |
| 578 |
) # activated from context menu |
| 579 |
|
| 580 |
self.refresh_gui() |
| 581 |
assert_method(file_to_clean) |
| 582 |
|
| 583 |
def test_context_menu_cookie_manager(self): |
| 584 |
"""Test cookie manager helper returns correct value for options""" |
| 585 |
gui = self.get_window() |
| 586 |
|
| 587 |
class _DummyAction: |
| 588 |
def __init__(self, action_key): |
| 589 |
self.action_key = action_key |
| 590 |
|
| 591 |
cookie_cleaner_id = '_unit_test_cookie_cleaner' |
| 592 |
non_cookie_cleaner_id = '_unit_test_non_cookie_cleaner' |
| 593 |
saved_cookie_cleaner = backends.get(cookie_cleaner_id) |
| 594 |
saved_non_cookie_cleaner = backends.get(non_cookie_cleaner_id) |
| 595 |
|
| 596 |
try: |
| 597 |
cookie_cleaner = Cleaner() |
| 598 |
cookie_cleaner.add_action('cookies', _DummyAction('cookie')) |
| 599 |
non_cookie_cleaner = Cleaner() |
| 600 |
non_cookie_cleaner.add_action('logs', _DummyAction('delete')) |
| 601 |
|
| 602 |
backends[cookie_cleaner_id] = cookie_cleaner |
| 603 |
backends[non_cookie_cleaner_id] = non_cookie_cleaner |
| 604 |
|
| 605 |
self.assertTrue( |
| 606 |
gui._option_has_cookie_command(cookie_cleaner_id, 'cookies'), |
| 607 |
'Expected test cookie cleaner option to be detected as cookie action') |
| 608 |
self.assertFalse( |
| 609 |
gui._option_has_cookie_command(non_cookie_cleaner_id, 'logs'), |
| 610 |
'Expected test non-cookie cleaner option to not be detected as cookie action') |
| 611 |
finally: |
| 612 |
if saved_cookie_cleaner is None: |
| 613 |
backends.pop(cookie_cleaner_id, None) |
| 614 |
else: |
| 615 |
backends[cookie_cleaner_id] = saved_cookie_cleaner |
| 616 |
if saved_non_cookie_cleaner is None: |
| 617 |
backends.pop(non_cookie_cleaner_id, None) |
| 618 |
else: |
| 619 |
backends[non_cookie_cleaner_id] = saved_non_cookie_cleaner |
| 620 |
|
| 621 |
def test_parent_unchecked_when_all_children_blocked_by_expert_mode(self): |
| 622 |
"""When all children are blocked by expert mode, parent must stay unchecked.""" |
| 623 |
|
| 624 |
model = Gtk.TreeStore( |
| 625 |
GObject.TYPE_STRING, # 0: name |
| 626 |
GObject.TYPE_BOOLEAN, # 1: active |
| 627 |
GObject.TYPE_PYOBJECT, # 2: id |
| 628 |
GObject.TYPE_STRING, # 3: size |
| 629 |
GObject.TYPE_STRING, # 4: icon |
| 630 |
) |
| 631 |
parent_iter = model.append( |
| 632 |
None, ("TestCleaner", False, "test_cleaner", "", "")) |
| 633 |
model.append(parent_iter, ("Option1", False, "option1", "", "")) |
| 634 |
model.append(parent_iter, ("Option2", False, "option2", "", "")) |
| 635 |
|
| 636 |
tdm = TreeDisplayModel() |
| 637 |
|
| 638 |
def blocked_warning(_option_id): |
| 639 |
return "This option requires expert mode." |
| 640 |
|
| 641 |
with mock.patch('bleachbit.GuiTreeModels.backends', |
| 642 |
{"test_cleaner": mock.Mock(get_warning=blocked_warning)}), \ |
| 643 |
mock.patch('bleachbit.GuiTreeModels.options') as mock_options: |
| 644 |
mock_options.get.return_value = False # expert_mode off |
| 645 |
mock_options.get_warning_preference.return_value = False |
| 646 |
|
| 647 |
parent_path = model.get_path(parent_iter) |
| 648 |
tdm.col1_toggled_cb(None, str(parent_path), model, None) |
| 649 |
|
| 650 |
self.assertFalse(model[parent_iter][1], |
| 651 |
"Parent should remain unchecked when all children are blocked by expert mode") |