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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
# -*- coding: utf-8 -*-
from __future__ import annotations
import io
import pathlib
import os
import shutil
import sys
import tempfile
from typing import Tuple, Dict
from urllib.error import URLError
import pytest
from DisplayCAL import config
from DisplayCAL.argyll import (
get_argyll_latest_version,
get_argyll_util,
make_argyll_compatible_path,
)
from DisplayCAL.cgats import CGATS
from DisplayCAL.config import initcfg, setcfg
from DisplayCAL.dev.mocks import check_call_str
from DisplayCAL.icc_profile import ICCProfile
from DisplayCAL.meta import DOMAIN
from DisplayCAL.worker import (
add_keywords_to_cgats,
check_cal_isfile,
check_create_dir,
check_file_isfile,
check_profile_isfile,
check_ti3_criteria1,
get_argyll_version_string,
get_options_from_profile,
Sudo,
Worker,
)
from tests.data.display_data import DisplayData
def test_get_options_from_profile_1(data_files):
"""Test ``DisplayCAL.worker.get_options_from_profile()`` function"""
profile_path = data_files[
"UP2516D #1 2022-03-23 16-06 D6500 2.2 F-S XYZLUT+MTX.icc"
].absolute()
options = get_options_from_profile(profile=profile_path)
assert options == (
[
"t6500",
"g2.2",
"f1.0",
"A4.0",
"d1",
"c1",
"yl",
"P0.48923385077616427,0.8797619047619047,1.4894179894179895",
"H",
],
["qh", "aX", 'A "Dell, Inc."'],
)
def test_get_options_from_profile_2(data_files):
"""Test ``DisplayCAL.worker.get_options_from_profile()`` function, for #69"""
profile_path = data_files["SW271 PM PenalNative_KB1_160_2022-03-17.icc"].absolute()
options = get_options_from_profile(profile=profile_path)
assert options == ([], []) # no options on that profile
def test_make_argyll_compatible_path_1():
"""make_argyll_compatible_path is working properly with bytes input."""
test_value = "C:\\Program Files\\some path\\executable.exe"
result = make_argyll_compatible_path(test_value)
if sys.platform == "win32":
expected_result = "C:\\Program Files\\some path\\executable.exe"
else:
expected_result = "C_Program Files_some path_executable.exe"
assert result == expected_result
def test_make_argyll_compatible_path_2():
"""make_argyll_compatible_path is working properly with bytes input."""
test_value = b"C:\\Program Files\\some path\\executable.exe"
result = make_argyll_compatible_path(test_value)
if sys.platform == "win32":
expected_result = b"C:\\Program Files\\some path\\executable.exe"
else:
expected_result = b"C_Program Files_some path_executable.exe"
assert result == expected_result
def test_worker_get_instrument_name_1():
"""Worker.get_instrument_name() is working properly."""
worker = Worker()
result = worker.get_instrument_name()
expected_result = ""
assert result == expected_result
def test_worker_get_instrument_features():
"""Worker.get_instrument_features() is working properly."""
worker = Worker()
result = worker.get_instrument_features()
assert result == {}
def test_worker_instrument_supports_css_1():
"""testing if Worker.instrument_supports_ccss is working properly"""
worker = Worker()
result = worker.instrument_supports_ccss()
expected_result = None
assert result == expected_result
# @pytest.mark.skip(reason="Test segfaults with python 3.12 - further investigation required.")
def test_generate_b2a_from_inverse_table(data_files, setup_argyll):
"""Test Worker.generate_B2A_from_inverse_table() method"""
worker = Worker()
icc_profile1 = ICCProfile(
profile=data_files[
"Monitor 1 #1 2022-03-09 16-13 D6500 2.2 F-S XYZLUT+MTX.icc"
].absolute()
)
logfile = io.StringIO()
result = worker.generate_B2A_from_inverse_table(icc_profile1, logfile=logfile)
assert result is True
def test_sudo_class_initialization():
"""Test worker.Sudo class initialization"""
sudo = Sudo()
assert sudo is not None
def test_download_method_1():
"""Test Worker.download() method."""
worker = Worker()
uri = f"https://{DOMAIN}/i1d3"
result = worker.download(uri)
assert result is not None
def test_download_method_2():
"""Test Worker.download() method."""
worker = Worker()
uri = f"https://{DOMAIN}/i1d3"
result = worker.download(uri, force=True)
assert result is not None
def test_download_method_3():
"""Test Worker.download() method."""
worker = Worker()
uri = f"https://{DOMAIN}/spyd2"
result = worker.download(uri)
assert result is not None
def test_download_method_4():
"""Test Worker.download() method."""
worker = Worker()
uri = f"https://{DOMAIN}/spyd2"
result = worker.download(uri, force=True)
assert result is not None
def test_get_display_name_1():
"""Testing Worker.get_display_name() method."""
initcfg()
setcfg("display.number", 1)
worker = Worker()
result = worker.get_display_name(False, True, False)
assert result == ""
def test_get_pwd():
"""Testing Worker.get_display_name() method."""
initcfg()
worker = Worker()
test_value = "test_value"
worker.pwd = test_value
assert worker.pwd == test_value
def test_update_profile_1(random_icc_profile):
"""Testing Worker.update_profile() method."""
from DisplayCAL import worker
worker.dbus_session = None
worker.dbus_system = None
initcfg()
worker = Worker()
icc_profile, icc_profile_path = random_icc_profile
with check_call_str(
"DisplayCAL.worker.Worker.get_display_edid", DisplayData.DISPLAY_DATA_2
):
worker.update_profile(icc_profile_path, tags=True)
def test_exec_cmd_1():
"""Test worker.exec_cmd() function for issue #73"""
# Command line:
cmd = "/home/eoyilmaz/.local/bin/Argyll_V2.3.0/bin/colprof"
args = [
"-v",
"-qh",
"-ax",
"-bn",
"-C",
b"No copyright. Created with DisplayCAL 3.8.9.3 and Argyll CMS 2.3.0",
"-A",
"Dell, Inc.",
"-D",
"UP2516D_#1_2022-04-01_00-26_2.2_F-S_XYZLUT+MTX",
"/tmp/DisplayCAL-i91d9z8_/UP2516D_#1_2022-04-01_00-26_2.2_F-S_XYZLUT+MTX",
]
cwd = "/tmp/DisplayCAL-i91d9z8_"
worker = Worker()
worker.exec_cmd(cmd=cmd, args=args)
def test_is_allowed_1():
"""Test Sudo.is_allowed() function for issue #76"""
sudo = Sudo()
result = sudo.is_allowed()
assert result != ""
def test_ti3_lookup_to_ti1_1(data_files, setup_argyll):
"""Test Worker.ti3_lookup_to_ti1() function for #129"""
ti3_path = data_files["0_16_from_issue_129.ti3"].absolute()
profile_path = data_files[
"UP2516D #1 2022-03-23 16-06 D6500 2.2 F-S XYZLUT+MTX.icc"
].absolute()
ti3_cgat = CGATS(ti3_path)
icc_profile = ICCProfile(profile_path)
config.initcfg()
worker = Worker()
ti1, ti3v = worker.ti3_lookup_to_ti1(ti3_cgat, icc_profile)
assert isinstance(ti1, CGATS)
assert isinstance(ti3v, CGATS)
assert ti1 == {
0: {
"COLOR_REP": b"RGB",
"DATA": {
0: {
"RGB_B": 99.9959,
"RGB_G": 100.0,
"RGB_R": 97.1526,
"SAMPLE_ID": 1,
"XYZ_X": 95.0104,
"XYZ_Y": 100.0,
"XYZ_Z": 92.7202,
},
1: {
"RGB_B": 9.1428,
"RGB_G": 5.8338,
"RGB_R": 5.842,
"SAMPLE_ID": 2,
"XYZ_X": 0.277593,
"XYZ_Y": 0.255279,
"XYZ_Z": 0.423145,
},
2: {
"RGB_B": 11.6181,
"RGB_G": 9.1081,
"RGB_R": 8.0801,
"SAMPLE_ID": 3,
"XYZ_X": 0.51238,
"XYZ_Y": 0.536117,
"XYZ_Z": 0.705578,
},
},
"DATA_FORMAT": {
0: b"SAMPLE_ID",
1: b"RGB_R",
2: b"RGB_G",
3: b"RGB_B",
4: b"XYZ_X",
5: b"XYZ_Y",
6: b"XYZ_Z",
},
"DESCRIPTOR": b"Argyll Calibration Target chart information 1",
"KEYWORDS": {0: b"COLOR_REP"},
"NUMBER_OF_FIELDS": None,
"NUMBER_OF_SETS": None,
}
}
assert ti3v == {
"COLOR_REP": b"RGB_XYZ",
"CREATED": b"Sun Jun 5 13:08:54 2022",
"DATA": {
0: {
"RGB_B": 99.9959,
"RGB_G": 100.0,
"RGB_R": 97.1526,
"SAMPLE_ID": 1,
"XYZ_X": 95.0104,
"XYZ_Y": 100.0,
"XYZ_Z": 92.7202,
},
1: {
"RGB_B": 9.1428,
"RGB_G": 5.8338,
"RGB_R": 5.842,
"SAMPLE_ID": 2,
"XYZ_X": 0.277593,
"XYZ_Y": 0.255279,
"XYZ_Z": 0.423145,
},
2: {
"RGB_B": 11.6181,
"RGB_G": 9.1081,
"RGB_R": 8.0801,
"SAMPLE_ID": 3,
"XYZ_X": 0.51238,
"XYZ_Y": 0.536117,
"XYZ_Z": 0.705578,
},
},
"DATA_FORMAT": {
0: b"SAMPLE_ID",
1: b"RGB_R",
2: b"RGB_G",
3: b"RGB_B",
4: b"XYZ_X",
5: b"XYZ_Y",
6: b"XYZ_Z",
},
"DESCRIPTOR": b"Argyll Calibration Target chart information 3",
"DEVICE_CLASS": b"DISPLAY",
"DISPLAY_TYPE_BASE_ID": 1,
"DISPLAY_TYPE_REFRESH": b"NO",
"INSTRUMENT_TYPE_SPECTRAL": b"NO",
"LUMINANCE_XYZ_CDM2": b"42.204124 44.420532 41.186805",
"NORMALIZED_TO_Y_100": b"YES",
"NUMBER_OF_FIELDS": None,
"NUMBER_OF_SETS": None,
"ORIGINATOR": b"Argyll dispread",
"TARGET_INSTRUMENT": b"Datacolor Spyder3",
"VIDEO_LUT_CALIBRATION_POSSIBLE": b"YES",
}
def test_add_keywords_to_cgats(data_files) -> None:
"""Test if keywords are added to cgats by add_keywords_to_cgats."""
path = data_files["0_16.ti3"].absolute()
cgats = CGATS(cgats=path)
assert "keyword" not in cgats[0]
options = {"keyword": "Value"}
alternated_cgats = add_keywords_to_cgats(cgats, options)
assert "keyword" in alternated_cgats[0]
def test_check_create_dir() -> None:
"""Test function 'check_create_dir'."""
assert check_create_dir("test_dir") == True
@pytest.mark.parametrize("file", (True, False))
def test_check_cal_isfile(data_files, file: bool) -> None:
"""Test 'check_cal_isfile'."""
path = data_files["Monitor.cal"].absolute() if file else "no_file"
assert check_cal_isfile(path) == True if file else "error.calibration.file_missing"
@pytest.mark.parametrize("file", (True, False))
def test_check_profile_isfile(data_files, file: bool) -> None:
"""Test 'check_profile_isfile'."""
path = data_files["Monitor.cal"].absolute() if file else "no_file"
assert check_profile_isfile(path) == True if file else "error.profile.file_missing"
# todo: test is working locally but not on CI
@pytest.mark.skip(
reason="First execution of test fails on remote CI server. "
"All following tests are positive."
)
@pytest.mark.parametrize("silent", (True, False), ids=("silent", "not silent"))
@pytest.mark.parametrize(
"path,result",
(
("data/cgats0.txt", ("True", "True")),
("no_file", ("False", "file.missing")),
(".", ("False", "file.notfile")),
),
)
def test_check_file_isfile(
data_files, silent: bool, path: str, result: Tuple[str, str]
) -> None:
"""Test if file gets detected."""
assert (
str(check_file_isfile(path, silent=silent)) == result[0]
if silent
else result[1]
)
@pytest.mark.parametrize(
"sample,result",
(
(
{
"SAMPLE_ID": 1,
"RGB_R": 50,
"RGB_G": 50,
"RGB_B": 50,
"XYZ_X": 0.5,
"XYZ_Y": 0.5,
"XYZ_Z": 0.5,
},
True,
),
(
{
"SAMPLE_ID": 2,
"RGB_R": 6,
"RGB_G": 6,
"RGB_B": 6,
"XYZ_X": 0.5,
"XYZ_Y": 0.5,
"XYZ_Z": 0.5,
},
False,
),
),
)
def test_check_ti3_criteria1(sample: Dict[str:float], result: bool) -> None:
"""Test for ti3 criteria1 check."""
black = (0, 0, 0)
white = (110, 110, 110)
criteria = check_ti3_criteria1(
(sample["RGB_R"], sample["RGB_G"], sample["RGB_B"]),
(sample["XYZ_X"], sample["XYZ_Y"], sample["XYZ_Z"]),
black,
white,
print_debuginfo=True,
)
assert criteria[3] == result
def test_prepare_colprof_for_271(monkeypatch, data_path):
"""Bug report 271."""
assert isinstance(data_path, pathlib.Path)
def patched_getcfg(key):
"""patched getcfg()"""
cfg = {
"argyll.version": "2.3.1",
"profile.name.expanded": "test_profile",
"profile.quality": "m",
"profile.type": "l",
"gamap_saturation": False,
"gamap_perceptual": False,
"profile.quality.b2a": "h",
"profile.b2a.hires": True,
"copyright": "",
"extra_args.colprof": "",
"profile.black_point_compensation": False,
"profile.black_point_correction": False,
"profile.b2a.hires.size": 17,
"profile.b2a.hires.smooth": True,
"measure.override_min_display_update_delay_ms": False,
"measure.override_display_settle_time_mult": False,
"patterngenerator.ffp_insertion": False,
"testchart.patch_sequence": "",
"3dlut.create": False,
}
return cfg[key]
monkeypatch.setattr("DisplayCAL.worker.getcfg", patched_getcfg)
def patched_os_path_exists(filepath):
return True
monkeypatch.setattr("DisplayCAL.worker.os.path.exists", patched_os_path_exists)
def patched_os_path_isfile(filepath):
return True
monkeypatch.setattr("DisplayCAL.worker.os.path.isfile", patched_os_path_isfile)
worker = Worker()
in_out_file = pathlib.Path(worker.setup_inout("test_profile")).with_suffix(".ti3")
# copy the test file to the target path
test_file_path = data_path / "sample" / "issue271" / "test_profile.ti3"
os.makedirs(in_out_file.parent, exist_ok=True)
shutil.copy(test_file_path, in_out_file)
# This should not raise the:
# TypeError: startswith first arg must be bytes or a tuple of bytes, not str
worker.prepare_colprof()
def test_prepare_dispcal_1():
"""Worker.prepare_dispcal() return value should be quoted properly."""
worker = Worker()
return_val = worker.prepare_dispcal()
expected_result = [
"-v2",
"-d0",
"-c1",
return_val[1][3], # '-yl',
return_val[1][4], # '-P0.5,0.5,1.0',
"-ql",
return_val[1][6], # '-t',
"-g2.2",
"-f1.0",
return_val[1][9], # '-k0.0',
"/var/folders/8l/xy1__ym94nn35x86xyg56xq80000gn/T/DisplayCAL-2fdjtyql/",
]
assert return_val[0] == get_argyll_util("dispcal")
assert isinstance(return_val[1], list)
assert return_val[1][:-1] == expected_result[:-1] # don't check the final part
assert tempfile.gettempdir() in return_val[1][-1] # this should be in a temp path
@pytest.mark.skipif(
os.getenv("GITHUB_ACTIONS") == "true",
reason="Not working properly on GitHub.",
)
def test_get_argyll_version_string_returns_a_proper_value():
"""get_argyll_version_string() returns a proper value."""
import wx
config.initcfg()
app = wx.GetApp() or wx.App()
assert "0.0.0" != get_argyll_version_string(name="ccxxmake", silent=False)
def test_get_argyll_latest_version_returns_str():
"""get_argyll_latest_version() returns a str."""
result = get_argyll_latest_version()
assert isinstance(result, str)
def test_get_argyll_latest_version_returns_latest_argyll_cms_version():
"""get_argyll_latest_version() returns the latest argyll cms version."""
result = get_argyll_latest_version()
assert result == "3.4.1"
def test_get_argyll_latest_version_returns_the_default_version_if_no_internet_connect(
monkeypatch,
):
"""get_argyll_latest_version() returns the default argyll cms version if no internet connection."""
def patched_urlopen(*args, **kwargs):
raise URLError(
"<urlopen error [Errno 8] nodename nor servname provided, or not known>"
)
monkeypatch.setattr("DisplayCAL.argyll.urllib.request.urlopen", patched_urlopen)
# print(dir(get_argyll_latest_version))
# clear the cache
get_argyll_latest_version.cache_clear()
result = get_argyll_latest_version()
assert result == config.defaults.get("argyll.version")
# assert False
@pytest.mark.skipif(
os.getenv("GITHUB_ACTIONS") == "true" and sys.platform == "linux",
reason="Not working properly on GitHub on Linux machines.",
)
def test_get_technology_strings_returns_dict(setup_argyll):
"""Test get_technology_strings() returns a dict."""
worker = Worker()
result = worker.get_technology_strings()
assert isinstance(result, dict)
def test_get_technology_strings_without_argyll_returns_from_argyll_17():
"""Test get_technology_strings() returns a dictionary from argyll 1.7."""
get_argyll_latest_version.cache_clear()
worker = Worker()
worker.argyll_version = [0, 0, 0]
result = worker.get_technology_strings()
assert result == {
"c": "CRT",
"m": "Plasma",
"l": "LCD",
"1": "LCD CCFL",
"2": "LCD CCFL IPS",
"3": "LCD CCFL VPA",
"4": "LCD CCFL TFT",
"L": "LCD CCFL Wide Gamut",
"5": "LCD CCFL Wide Gamut IPS",
"6": "LCD CCFL Wide Gamut VPA",
"7": "LCD CCFL Wide Gamut TFT",
"e": "LCD White LED",
"8": "LCD White LED IPS",
"9": "LCD White LED VPA",
"d": "LCD White LED TFT",
"b": "LCD RGB LED",
"f": "LCD RGB LED IPS",
"g": "LCD RGB LED VPA",
"i": "LCD RGB LED TFT",
"h": "LCD RG Phosphor",
"j": "LCD RG Phosphor IPS",
"k": "LCD RG Phosphor VPA",
"n": "LCD RG Phosphor TFT",
"o": "LED OLED",
"a": "LED AMOLED",
"p": "DLP Projector",
"q": "DLP Projector RGB Filter Wheel",
"r": "DPL Projector RGBW Filter Wheel",
"s": "DLP Projector RGBCMY Filter Wheel",
"u": "Unknown",
}
@pytest.mark.skipif(
os.getenv("GITHUB_ACTIONS") == "true" and sys.platform == "linux",
reason="Not working properly on GitHub on Linux machines.",
)
def test_get_technology_strings_with_argyll_returns_expected_data(setup_argyll):
"""Test get_technology_strings() returns a dict with correct data."""
get_argyll_latest_version.cache_clear()
worker = Worker()
assert worker.argyll_version != [0, 0, 0]
result = worker.get_technology_strings()
expected = {
"c": "CRT",
"m": "Plasma",
"l": "LCD",
"1": "LCD CCFL",
"2": "LCD CCFL IPS",
"3": "LCD CCFL PVA",
"4": "LCD CCFL TFT",
"L": "LCD CCFL Wide Gamut",
"5": "LCD CCFL Wide Gamut IPS",
"6": "LCD CCFL Wide Gamut PVA",
"7": "LCD CCFL Wide Gamut TFT",
"e": "LCD White LED",
"8": "LCD White LED IPS",
"9": "LCD White LED PVA",
"d": "LCD White LED TFT",
"b": "LCD RGB LED",
"f": "LCD RGB LED IPS",
"g": "LCD RGB LED PVA",
"j": "LCD RGB LED TFT",
"h": "LCD RG Phosphor",
"k": "LCD RG Phosphor IPS",
"n": "LCD RG Phosphor PVA",
"q": "LCD RG Phosphor TFT",
"r": "LCD PFS Phosphor",
"s": "LCD PFS Phosphor IPS",
"t": "LCD PFS Phosphor PVA",
"v": "LCD PFS Phosphor TFT",
"i": "LCD GB-R Phosphor",
"x": "LCD GB-R Phosphor IPS",
"y": "LCD GB-R Phosphor PVA",
"z": "LCD GB-R Phosphor TFT",
"o": "LED OLED",
"a": "LED AMOLED",
"w": "LED WOLED",
"p": "DLP Projector",
"A": "DLP Projector RGB Filter Wheel",
"B": "DLP Projector RGBW Filter Wheel",
"C": "DLP Projector RGBCMY Filter Wheel",
"u": "Unknown",
}
assert result == expected
|