|
| 1 | +"""Profile reads can fail while an installed ICC file is being replaced.""" |
| 2 | + |
| 3 | +import struct |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from DisplayCAL import profile_loader |
| 9 | +from DisplayCAL.colormath import get_rgb_space |
| 10 | +from DisplayCAL.icc_profile import ICCProfile, VideoCardGammaFormulaType |
| 11 | + |
| 12 | + |
| 13 | +def make_loader(): |
| 14 | + """Use the ramp code without starting a tray icon or touching the display.""" |
| 15 | + loader = profile_loader.ProfileLoader.__new__(profile_loader.ProfileLoader) |
| 16 | + loader.profiles = {} |
| 17 | + loader.ramps = {} |
| 18 | + loader._reset_gamma_ramps = False |
| 19 | + loader._manual_restore = False |
| 20 | + loader._quantize = 65535.0 |
| 21 | + return loader |
| 22 | + |
| 23 | + |
| 24 | +def generate_ramp(loader, path, key="display-1"): |
| 25 | + return loader._generate_gamma_ramp( |
| 26 | + False, [], [], "Test display", key, str(path), False, "Test profile" |
| 27 | + )[1] |
| 28 | + |
| 29 | + |
| 30 | +def make_profile(path, calibrated=True): |
| 31 | + profile = ICCProfile.from_rgb_space(get_rgb_space("sRGB"), "Test profile") |
| 32 | + if calibrated: |
| 33 | + data = b"vcgt" + b"\0" * 4 + struct.pack(">I", 1) |
| 34 | + data += struct.pack(">III", 2 * 65536, 0, 65536) * 3 |
| 35 | + profile.tags["vcgt"] = VideoCardGammaFormulaType(data, "vcgt") |
| 36 | + profile.write(str(path)) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("failures", [1, 2]) |
| 40 | +@pytest.mark.parametrize("manual_restore", [False, True]) |
| 41 | +def test_profile_read_is_retried_without_an_association_change( |
| 42 | + tmp_path, failures, manual_restore |
| 43 | +): |
| 44 | + path = tmp_path / "calibrated.icc" |
| 45 | + make_profile(path) |
| 46 | + loader = make_loader() |
| 47 | + loader._manual_restore = manual_restore |
| 48 | + reads = 0 |
| 49 | + |
| 50 | + def read_profile(filename=None): |
| 51 | + nonlocal reads |
| 52 | + if filename: |
| 53 | + reads += 1 |
| 54 | + if reads <= failures: |
| 55 | + raise PermissionError("Profile is temporarily locked") |
| 56 | + return ICCProfile(filename) |
| 57 | + |
| 58 | + with mock.patch.object(profile_loader, "ICCProfile", side_effect=read_profile): |
| 59 | + for _ in range(failures): |
| 60 | + fallback = generate_ramp(loader, path) |
| 61 | + assert fallback[0][128] == 32896 |
| 62 | + recovered = generate_ramp(loader, path) |
| 63 | + # The file and association have not changed. Retry must replace the |
| 64 | + # temporary linear fallback with the profile's gamma 2.0 calibration. |
| 65 | + assert recovered[0][128] < 17000 |
| 66 | + assert reads == failures + 1 |
| 67 | + assert generate_ramp(loader, path) is recovered |
| 68 | + assert reads == failures + 1 |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("calibrated", [False, True]) |
| 72 | +def test_successful_profiles_are_still_cached(tmp_path, calibrated): |
| 73 | + path = tmp_path / "valid.icc" |
| 74 | + make_profile(path, calibrated) |
| 75 | + loader = make_loader() |
| 76 | + with mock.patch.object(profile_loader, "ICCProfile", wraps=ICCProfile) as read: |
| 77 | + ramp = generate_ramp(loader, path) |
| 78 | + assert generate_ramp(loader, path) is ramp |
| 79 | + read.assert_called_once_with(str(path)) |
| 80 | + |
| 81 | + |
| 82 | +def test_explicit_reset_does_not_read_the_profile(tmp_path): |
| 83 | + loader = make_loader() |
| 84 | + loader._reset_gamma_ramps = True |
| 85 | + with mock.patch.object(profile_loader, "ICCProfile", wraps=ICCProfile) as read: |
| 86 | + ramp = generate_ramp(loader, tmp_path / "unused.icc") |
| 87 | + assert ramp[0][128] == 32896 |
| 88 | + assert generate_ramp(loader, tmp_path / "unused.icc") is ramp |
| 89 | + read.assert_not_called() |
0 commit comments