-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_convert.py
521 lines (413 loc) · 21.1 KB
/
test_convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import unittest
import sys
import shutil
import hashlib
from pathlib import Path
import platform
from unittest.mock import patch
from PySide6.QtGui import (
QDropEvent,
QCloseEvent,
)
from PySide6.QtWidgets import (
QApplication,
)
from PySide6.QtTest import (
QTest,
)
from PySide6.QtCore import (
Qt,
QMimeData,
QUrl,
QPoint
)
from PIL import Image, ImageDraw
from main import MainWindow
from data.constants import *
import core.controller as controller
# CONFIG
SAMPLE_IMG_FOLDER = Path(".").resolve() / "sample_img"
TMP_IMG_FOLDER = Path(".").resolve() / "unit_tests_tmp"
app = QApplication(sys.argv)
# ---------------------------------------------------------------
# Tools
# ---------------------------------------------------------------
def scan_dir(path):
items = set() # No duplicates
for i in Path(path).rglob("*"):
if i.is_file():
items.add(i.resolve())
return list(items) # So it can be accessed by an index
def rmtree(path):
path = Path(path).resolve()
if path.is_dir():
shutil.rmtree(path)
def blake2(path):
hasher = hashlib.blake2b()
with open(path, "rb") as file:
buff_size = 8192
for buf in iter(lambda: file.read(buff_size), b""):
hasher.update(buf)
return hasher.hexdigest()
def sleep(ms):
QTest.qWait(ms)
def test_dict(data):
"""Recursively verify dictionary's integrity"""
for key, value in data.items():
if isinstance(value, dict):
if test_dict(value) is None:
return False
else:
if value is None:
return False
return True
def create_sample_img():
sample_img_path = SAMPLE_IMG_FOLDER / "sample_img.png"
if sample_img_path.exists():
return
w, h = 1920, 1080
img = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(img)
for i in range(h):
r = int(32 + (241 - 32) * i / h)
g = int(33 + (128 - 33) * i / h)
b = int(36 + (0 - 36) * i / h)
draw.line([(0, i), (w, i)], fill=(r, g, b))
SAMPLE_IMG_FOLDER.mkdir(exist_ok=True)
img.save(sample_img_path)
class Data:
"""Sample data and tmp folder management system."""
def __init__(self, sample_img_folder, tmp_img_folder):
self.sample_img_folder = sample_img_folder
self.tmp_img_folder = tmp_img_folder
self.cleanup()
def cleanup(self):
rmtree(self.tmp_img_folder)
# Tmp (output) Directory
def get_tmp_folder_path(self, path = None):
if path == None:
return self.tmp_img_folder
else:
return Path(self.tmp_img_folder) / path
def get_tmp_folder_content(self, subfolder=None):
if subfolder:
return scan_dir(Path(self.tmp_img_folder) / subfolder)
else:
return scan_dir(self.tmp_img_folder)
def make_tmp_subfolder(self, name):
path = Path(self.tmp_img_folder) / name
path.mkdir(parents=True)
return path
# Samples Directory
def get_sample_img(self):
return scan_dir(self.sample_img_folder)[0]
def get_sample_imgs(self):
return scan_dir(self.sample_img_folder)
def get_sample_img_folder(self):
return self.sample_img_folder
class Interact:
"""Translation layer between unit tests and the application."""
def __init__(self, main_window):
self.main_window = main_window
self.root = self.main_window.input_tab.file_view.invisibleRootItem()
def tear_down(self):
self.main_window.closeEvent(QCloseEvent())
def add_item(self, path):
path = Path(path)
self.main_window.input_tab._addItems(
[
(path, path.parent)
]
)
def add_items(self, paths):
tmp = []
for i in paths:
path = Path(i)
tmp.append(
(
path,
path.parent,
)
)
self.main_window.input_tab._addItems(tmp)
def get_items(self):
return [self.root.child(i).text(2) for i in range(self.root.childCount())]
def get_item_count(self):
return self.main_window.input_tab.file_view.invisibleRootItem().childCount()
def clear_list(self):
self.main_window.input_tab.clearInput()
def set_custom_output(self, path):
self.main_window.output_tab.wm.getWidget("choose_output_ct_rb").setChecked(True)
path = str(path.resolve())
self.main_window.output_tab.wm.getWidget("choose_output_ct_le").setText(path)
def set_lossless(self, checked):
self.main_window.output_tab.wm.getWidget("lossless_cb").setChecked(checked)
def reset_to_default(self):
self.main_window.output_tab.resetToDefault()
self.main_window.modify_tab.resetToDefault()
self.main_window.settings_tab.resetToDefault()
self.main_window.output_tab.wm.getWidget("threads_sl").setValue(self.main_window.output_tab.MAX_THREAD_COUNT) # To speed up testing
def convert_preset(self, src, dst, format, lossless=False, effort=7, jpg_encoder="JPEGLI"):
self.clear_list()
self.set_format(format)
self.set_custom_output(dst)
self.add_item(src)
self.set_lossless(lossless)
self.set_effort(effort)
if format == "JPEG":
self.set_jpg_encoder(jpg_encoder)
self.convert()
def convert(self):
mock_CheckStatus = controller.CheckStatus()
with patch.object(self.main_window.controller, "checkProcessingRequirements", return_value=mock_CheckStatus): # Without this activeThreadCount can return > 0 on Windows.
self.main_window.convert()
self.wait_for_done()
def wait_for_done(self):
while True:
sleep(100)
if self.main_window.controller.getCompletedItemCount() == self.main_window.controller.getItemCount():
break
def set_effort(self, effort):
self.main_window.output_tab.effort_sb.setValue(effort)
def set_duplicate_handling(self, mode):
self.main_window.output_tab.duplicates_cmb.setCurrentText(mode)
def set_format(self, _format):
self.main_window.output_tab.format_cmb.setCurrentText(_format)
def set_preserve_attributes(self, enabled):
self.main_window.modify_tab.date_time_cb.setChecked(enabled)
def set_jpg_encoder(self, encoder: str):
self.main_window.settings_tab.jpg_encoder_cmb.setCurrentText(encoder)
def drag_and_drop(self, urls):
mime_data = QMimeData()
mime_data.setUrls([QUrl.fromLocalFile(url) for url in urls])
drop_event = QDropEvent(QPoint(), Qt.CopyAction, mime_data, Qt.LeftButton, Qt.NoModifier)
QTest.mousePress(self.main_window, Qt.LeftButton, pos=QPoint(), delay=100)
self.main_window.dropEvent(drop_event)
QTest.mouseRelease(self.main_window, Qt.LeftButton, pos=QPoint(), delay=100)
QTest.qWait(100)
def get_settings(self, tab):
match tab:
case "output_tab":
return self.main_window.output_tab.getSettings()
case "modify_tab":
return self.main_window.modify_tab.getSettings()
case "settings_tab":
return self.main_window.settings_tab.getSettings()
def set_metadata_mode(self, mode):
self.main_window.modify_tab.metadata_cmb.setCurrentText(mode)
def set_downscaling_mode(self,
mode,
width=None,
height=None,
percent=None,
shortest=None,
longest=None,
file_size=None,
):
self.main_window.modify_tab.downscale_cb.setChecked(True)
self.main_window.modify_tab.mode_cmb.setCurrentText(mode)
def set_value(widget, value):
if value is not None:
widget.setValue(value)
set_value(self.main_window.modify_tab.pixel_w_sb, width)
set_value(self.main_window.modify_tab.pixel_h_sb, height)
set_value(self.main_window.modify_tab.percent_sb, percent)
set_value(self.main_window.modify_tab.shortest_sb, shortest)
set_value(self.main_window.modify_tab.longest_sb, longest)
set_value(self.main_window.modify_tab.file_size_sb, file_size)
# ---------------------------------------------------------------
# Unit Tests
# ---------------------------------------------------------------
def windows_only(test_func):
def wrapper(*args, **kwargs):
if os.name != 'nt':
raise unittest.SkipTest()
return test_func(*args, **kwargs)
return wrapper
class TestMainWindow(unittest.TestCase):
def setUp(self):
self.app = Interact(MainWindow())
self.data = Data(SAMPLE_IMG_FOLDER, TMP_IMG_FOLDER)
self.app.reset_to_default()
self.app.clear_list()
def tearDown(self):
self.data.cleanup()
self.app.tear_down()
def test_dependencies(self):
FILES = (
ICON_SVG,
LICENSE_PATH,
LICENSE_3RD_PARTY_PATH,
CJXL_PATH,
DJXL_PATH,
JXLINFO_PATH,
IMAGE_MAGICK_PATH,
AVIFENC_PATH,
AVIFDEC_PATH,
OXIPNG_PATH,
EXIFTOOL_PATH,
)
for i in FILES:
if platform.system() == "Linux" and i == EXIFTOOL_PATH:
continue
assert Path(i).is_file(), f"File not found ({i})"
def test_clear_list(self):
self.app.add_items(self.data.get_sample_imgs())
assert self.app.get_item_count() > 0, "List shouldn't be empty"
self.app.clear_list()
assert self.app.get_item_count() == 0, "List should be empty"
def test_drag_n_drop_files(self):
self.app.drag_and_drop(self.data.get_sample_imgs())
assert self.app.get_item_count() == len(self.data.get_sample_imgs())
def test_drag_n_drop_folders(self):
self.app.drag_and_drop([self.data.get_sample_img_folder()])
assert self.app.get_item_count() == len(self.data.get_sample_imgs())
def test_duplicate_detection(self):
items = self.data.get_sample_imgs()
self.app.add_items(items)
self.app.add_items(items)
assert self.app.get_item_count() == len(items)
def test_jpeg_xl_lossy(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG XL")
assert len(self.data.get_tmp_folder_content() ) > 0, "Converted image not found"
def test_jpeg_xl_effort(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG XL", effort=7)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG XL", effort=9)
converted = self.data.get_tmp_folder_content()
assert blake2(converted[0]) != blake2(converted[1]), "Images should not be the same"
def test_jpg_reconstruction(self):
# Source -> JPG
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("jpg"), "JPEG")
# JPG -> JXL
self.app.convert_preset(self.data.get_tmp_folder_content("jpg")[0], self.data.make_tmp_subfolder("jxl"), "Lossless JPEG Transcoding")
# JXL -> JPG
self.app.convert_preset(self.data.get_tmp_folder_content("jxl")[0], self.data.make_tmp_subfolder("reconstructed"), "JPEG Reconstruction")
assert blake2(self.data.get_tmp_folder_content("jpg")[0]) == blake2(self.data.get_tmp_folder_content("reconstructed")[0]), "Hash mismatch for reconstructed JPG"
def test_avif(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("avif"), "AVIF")
assert Path(self.data.get_tmp_folder_content()[0]).suffix == ".avif", "AVIF file not found"
def test_webp(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("webp"), "WebP")
assert Path(self.data.get_tmp_folder_content()[0]).suffix == ".webp", "WebP file not found"
def test_jpg(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("jpg"), "JPEG")
assert Path(self.data.get_tmp_folder_content()[0]).suffix == ".jpg", "JPG file not found"
def test_jpeg_xl_decode(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("jxl"), "JPEG XL")
self.app.convert_preset(self.data.get_tmp_folder_content("jxl")[0], self.data.make_tmp_subfolder("jxl_decoded"), "PNG")
assert len(self.data.get_tmp_folder_content("jxl_decoded")) > 0, "Decoded JPEG XL file not found"
def test_avif_decode(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("avif"), "AVIF")
self.app.convert_preset(self.data.get_tmp_folder_content("avif")[0], self.data.make_tmp_subfolder("avif_decoded"), "PNG")
assert len(self.data.get_tmp_folder_content("avif_decoded")) > 0, "Decoded AVIF file not found"
def test_webp_decode(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("webp"), "WebP")
self.app.convert_preset(self.data.get_tmp_folder_content("webp")[0], self.data.make_tmp_subfolder("webp_decoded"), "PNG")
assert len(self.data.get_tmp_folder_content("webp_decoded")) > 0, "Decoded WebP file not found"
def test_decode_jpg(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("jpg"), "JPEG")
self.app.convert_preset(self.data.get_tmp_folder_content("jpg")[0], self.data.make_tmp_subfolder("jpg_decoded"), "PNG")
assert len(self.data.get_tmp_folder_content("jpg_decoded")) > 0, "Decoded JPG file not found"
def test_proxy(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("jpg"), "JPEG")
assert Path(self.data.get_tmp_folder_content("jpg")[0]).suffix == ".jpg", "JPG file not found"
self.app.convert_preset(self.data.get_tmp_folder_content("jpg")[0], self.data.make_tmp_subfolder("webp"), "WebP")
assert Path(self.data.get_tmp_folder_content("webp")[0]).suffix == ".webp", "WebP file not found"
self.app.convert_preset(self.data.get_tmp_folder_content("webp")[0], self.data.make_tmp_subfolder("jxl"), "JPEG XL")
assert Path(self.data.get_tmp_folder_content("jxl")[0]).suffix == ".jxl", "JPEG XL file not found"
self.app.convert_preset(self.data.get_tmp_folder_content("jxl")[0], self.data.make_tmp_subfolder("avif"), "AVIF")
assert Path(self.data.get_tmp_folder_content("avif")[0]).suffix == ".avif", "AVIF file not found"
def test_smallest_lossless(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "Smallest Lossless")
assert len(self.data.get_tmp_folder_content()) > 0, "Smallest Lossless did not generate any files"
def test_rename(self):
self.app.set_duplicate_handling("Rename")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
assert len(self.data.get_tmp_folder_content()) == 2, "File amount mismatch"
def test_replace(self):
self.app.set_duplicate_handling("Replace")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
assert len(self.data.get_tmp_folder_content()) == 1, "File amount mismatch"
def test_skip(self):
self.app.set_duplicate_handling("Skip")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
assert len(self.data.get_tmp_folder_content()) == 1, "File amount mismatch"
def test_get_settings(self):
assert test_dict(self.app.get_settings("output_tab")), "output_tab.getSettings contains None"
assert test_dict(self.app.get_settings("modify_tab")), "modify_tab.getSettings contains None"
assert test_dict(self.app.get_settings("settings_tab")), "settings.getSettings contains None"
def test_preserve_attributes(self):
self.app.set_preserve_attributes(True)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_preserve_attributes(False)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
# If modification times are more than 15 sec apart
files = self.data.get_tmp_folder_content()
assert abs(files[0].stat().st_mtime - files[1].stat().st_mtime) > 15, "Range too narrow"
def test_metadata_exiftool(self):
self.app.set_metadata_mode("ExifTool - Preserve")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_metadata_mode("ExifTool - Wipe")
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
files = self.data.get_tmp_folder_content()
assert files[0].stat().st_size != files[1].stat().st_size, "No change detected" # This is expected to fail if your Windows username contains Unicode specific characters. See core/metadata.py for more info.
def test_downscaling_resolution(self):
self.app.set_downscaling_mode("Resolution", width = 100, height = 2000)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_downscaling_mode("Resolution", width = 2000, height = 100)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
converted = self.data.get_tmp_folder_content()
assert converted[0].stat().st_size != converted[1].stat().st_size, "No change detected"
def test_downscaling_percent(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_downscaling_mode("Percent", percent=50)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
converted = self.data.get_tmp_folder_content()
assert converted[0].stat().st_size != converted[1].stat().st_size, "No change detected"
def test_downscaling_shortest(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_downscaling_mode("Shortest Side", shortest=1)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
converted = self.data.get_tmp_folder_content()
assert converted[0].stat().st_size != converted[1].stat().st_size, "No change detected"
def test_downscaling_longest(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
self.app.set_downscaling_mode("Longest Side", longest=1)
self.app.convert_preset(self.data.get_sample_img(), self.data.get_tmp_folder_path(), "JPEG")
converted = self.data.get_tmp_folder_content()
assert converted[0].stat().st_size != converted[1].stat().st_size, "No change detected"
# POSIX is fine
@windows_only
def test_jxl_utf8_support(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("漢字0"), "JPEG XL")
converted = self.data.get_tmp_folder_content()
assert "jxl" == str(converted[0])[-3:]
self.app.convert_preset(converted[0], self.data.make_tmp_subfolder("漢字1"), "PNG")
assert len(self.data.get_tmp_folder_content()) == 2
@windows_only
def test_avif_utf8_support(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("漢字0"), "AVIF")
converted = self.data.get_tmp_folder_content()
assert "avif" == str(converted[0])[-4:]
self.app.convert_preset(converted[0], self.data.make_tmp_subfolder("漢字1"), "PNG")
assert len(self.data.get_tmp_folder_content()) == 2
@windows_only
def test_jpegli_utf8_support(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("漢字0"), "JPEG", jpg_encoder="JPEGLI")
converted = self.data.get_tmp_folder_content()
assert "jpg" == str(converted[0])[-3:]
self.app.convert_preset(converted[0], self.data.make_tmp_subfolder("漢字1"), "PNG")
assert len(self.data.get_tmp_folder_content()) == 2
@windows_only
def test_imagemagick_utf8_support(self):
self.app.convert_preset(self.data.get_sample_img(), self.data.make_tmp_subfolder("漢字0"), "WebP")
converted = self.data.get_tmp_folder_content()
assert "webp" == str(converted[0])[-4:]
self.app.convert_preset(converted[0], self.data.make_tmp_subfolder("漢字1"), "PNG")
assert len(self.data.get_tmp_folder_content()) == 2
if __name__ == "__main__":
create_sample_img()
unittest.main(failfast=True)