| 1 |
"""Tests for issue #627: Profile loader must not apply HDR calibration profile in SDR mode. |
| 2 |
|
| 3 |
The Windows HDR Calibration app stores its profile under the ICMProfileAC |
| 4 |
registry value. When the display is in SDR mode that value must be ignored so |
| 5 |
the correct SDR profile (ICMProfile) is used instead. |
| 6 |
""" |
| 7 |
|
| 8 |
import os |
| 9 |
import sys |
| 10 |
import types |
| 11 |
from unittest import mock |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from DisplayCAL import icc_profile as _icc |
| 16 |
|
| 17 |
|
| 18 |
# --------------------------------------------------------------------------- |
| 19 |
# Helpers |
| 20 |
# --------------------------------------------------------------------------- |
| 21 |
|
| 22 |
def _make_fake_winreg(registry_values): |
| 23 |
"""Return a minimal fake winreg module seeded with the given values. |
| 24 |
|
| 25 |
registry_values: sequence of (name, value, type_) tuples as EnumValue |
| 26 |
would return. |
| 27 |
""" |
| 28 |
m = types.ModuleType("winreg") |
| 29 |
m.HKEY_CURRENT_USER = 0x80000001 |
| 30 |
m.HKEY_LOCAL_MACHINE = 0x80000002 |
| 31 |
m.REG_BINARY = 3 |
| 32 |
m.REG_MULTI_SZ = 7 |
| 33 |
|
| 34 |
fake_key = mock.MagicMock() |
| 35 |
m.OpenKey = mock.Mock(return_value=fake_key) |
| 36 |
m.CloseKey = mock.Mock() |
| 37 |
m.QueryInfoKey = mock.Mock(return_value=(0, len(registry_values), 0)) |
| 38 |
m.EnumValue = mock.Mock(side_effect=lambda key, i: registry_values[i]) |
| 39 |
return m |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture() |
| 43 |
def registry_with_both_profiles(tmp_path): |
| 44 |
"""Inject a fake winreg with both ICMProfile and ICMProfileAC into icc_profile.""" |
| 45 |
sdr_file = tmp_path / "sdr_profile.icm" |
| 46 |
hdr_file = tmp_path / "windows_hdr_calibration.icm" |
| 47 |
sdr_file.write_bytes(b"") |
| 48 |
hdr_file.write_bytes(b"") |
| 49 |
|
| 50 |
registry_values = [ |
| 51 |
("ICMProfile", [sdr_file.name], 7), |
| 52 |
("ICMProfileAC", [hdr_file.name], 7), |
| 53 |
] |
| 54 |
fake_winreg = _make_fake_winreg(registry_values) |
| 55 |
|
| 56 |
orig_winreg = getattr(_icc, "winreg", None) |
| 57 |
orig_profiles = _icc.ICCPROFILES |
| 58 |
|
| 59 |
_icc.winreg = fake_winreg |
| 60 |
_icc.ICCPROFILES = [str(tmp_path)] |
| 61 |
|
| 62 |
yield _icc, sdr_file.name, hdr_file.name |
| 63 |
|
| 64 |
if orig_winreg is None: |
| 65 |
if hasattr(_icc, "winreg"): |
| 66 |
del _icc.winreg |
| 67 |
else: |
| 68 |
_icc.winreg = orig_winreg |
| 69 |
_icc.ICCPROFILES = orig_profiles |
| 70 |
|
| 71 |
|
| 72 |
# Monkey-key that represents the last two components of a typical device key. |
| 73 |
_MONKEY = ["{4d36e96e-e325-11ce-bfc1-08002be10318}", "0002"] |
| 74 |
|
| 75 |
|
| 76 |
# --------------------------------------------------------------------------- |
| 77 |
# _winreg_get_display_profiles |
| 78 |
# --------------------------------------------------------------------------- |
| 79 |
|
| 80 |
class TestWinregGetDisplayProfiles: |
| 81 |
def test_returns_both_profiles_by_default(self, registry_with_both_profiles): |
| 82 |
"""Both ICMProfile and ICMProfileAC are returned when filtering is off.""" |
| 83 |
icc, sdr, hdr = registry_with_both_profiles |
| 84 |
profiles = icc._winreg_get_display_profiles(_MONKEY) |
| 85 |
assert sdr in profiles |
| 86 |
assert hdr in profiles |
| 87 |
|
| 88 |
def test_exclude_advanced_color_removes_hdr_entry(self, registry_with_both_profiles): |
| 89 |
"""With exclude_advanced_color=True, ICMProfileAC entries are dropped.""" |
| 90 |
icc, sdr, hdr = registry_with_both_profiles |
| 91 |
profiles = icc._winreg_get_display_profiles( |
| 92 |
_MONKEY, exclude_advanced_color=True |
| 93 |
) |
| 94 |
assert sdr in profiles |
| 95 |
assert hdr not in profiles |
| 96 |
|
| 97 |
def test_exclude_false_keeps_hdr_entry(self, registry_with_both_profiles): |
| 98 |
"""Explicitly passing exclude_advanced_color=False keeps both entries.""" |
| 99 |
icc, sdr, hdr = registry_with_both_profiles |
| 100 |
profiles = icc._winreg_get_display_profiles( |
| 101 |
_MONKEY, exclude_advanced_color=False |
| 102 |
) |
| 103 |
assert hdr in profiles |
| 104 |
|
| 105 |
|
| 106 |
# --------------------------------------------------------------------------- |
| 107 |
# _winreg_get_display_profile |
| 108 |
# --------------------------------------------------------------------------- |
| 109 |
|
| 110 |
class TestWinregGetDisplayProfile: |
| 111 |
def test_sdr_mode_returns_sdr_profile(self, registry_with_both_profiles): |
| 112 |
"""advanced_color_active=False must exclude ICMProfileAC and pick ICMProfile.""" |
| 113 |
icc, sdr, hdr = registry_with_both_profiles |
| 114 |
result = icc._winreg_get_display_profile( |
| 115 |
_MONKEY, path_only=True, advanced_color_active=False |
| 116 |
) |
| 117 |
assert result is not None |
| 118 |
assert sdr in result |
| 119 |
assert hdr not in result |
| 120 |
|
| 121 |
def test_hdr_mode_returns_hdr_profile(self, registry_with_both_profiles): |
| 122 |
"""advanced_color_active=True must keep ICMProfileAC (last entry wins).""" |
| 123 |
icc, sdr, hdr = registry_with_both_profiles |
| 124 |
result = icc._winreg_get_display_profile( |
| 125 |
_MONKEY, path_only=True, advanced_color_active=True |
| 126 |
) |
| 127 |
assert result is not None |
| 128 |
assert hdr in result |
| 129 |
|
| 130 |
def test_unknown_hdr_state_returns_hdr_profile(self, registry_with_both_profiles): |
| 131 |
"""advanced_color_active=None (unknown) must not filter; last entry wins.""" |
| 132 |
icc, sdr, hdr = registry_with_both_profiles |
| 133 |
result = icc._winreg_get_display_profile( |
| 134 |
_MONKEY, path_only=True, advanced_color_active=None |
| 135 |
) |
| 136 |
assert result is not None |
| 137 |
assert hdr in result |
| 138 |
|
| 139 |
def test_only_sdr_profile_in_registry(self, tmp_path): |
| 140 |
"""When no ICMProfileAC exists, SDR profile is still returned correctly.""" |
| 141 |
sdr_file = tmp_path / "sdr_only.icm" |
| 142 |
sdr_file.write_bytes(b"") |
| 143 |
|
| 144 |
fake_winreg = _make_fake_winreg([("ICMProfile", [sdr_file.name], 7)]) |
| 145 |
orig_winreg = getattr(_icc, "winreg", None) |
| 146 |
orig_profiles = _icc.ICCPROFILES |
| 147 |
_icc.winreg = fake_winreg |
| 148 |
_icc.ICCPROFILES = [str(tmp_path)] |
| 149 |
try: |
| 150 |
result = _icc._winreg_get_display_profile( |
| 151 |
_MONKEY, path_only=True, advanced_color_active=False |
| 152 |
) |
| 153 |
assert result is not None |
| 154 |
assert sdr_file.name in result |
| 155 |
finally: |
| 156 |
if orig_winreg is None: |
| 157 |
if hasattr(_icc, "winreg"): |
| 158 |
del _icc.winreg |
| 159 |
else: |
| 160 |
_icc.winreg = orig_winreg |
| 161 |
_icc.ICCPROFILES = orig_profiles |