Debian Codegraph @

tests/test_macos_videolut.py

raw
1 """Tests for the direct-CoreGraphics macOS VideoLUT reload path (issue #824).
2
3 Argyll's ``dispwin`` has to exit after every invocation, and macOS's own
4 cleanup of a just-exited process's transient VideoLUT claim can clobber a
5 *different*, untouched display's gamma table. These helpers apply
6 calibration directly via CoreGraphics from DisplayCAL's own long-lived
7 process instead, avoiding that OS bug entirely (see
8 DisplayCAL.worker.reload_macos_videoluts and
9 DisplayCAL.profile_loader.ProfileLoader._reload_clobbered_displays).
10 """
11
12 from unittest import mock
13
14 from DisplayCAL.worker import (
15 _get_macos_display_profile,
16 reload_macos_videoluts,
17 vcgt_values_to_videolut,
18 )
19
20
21 def test_get_macos_display_profile_prefers_coregraphics_over_applescript():
22 """CoreGraphics profile data is used without falling back to AppleScript.
23
24 ``get_display_profile`` queries the "Image Events" AppleScript scripting
25 interface, which is confirmed broken on current macOS (Tahoe). The
26 CoreGraphics-native path must be tried first and, when it succeeds, must
27 not fall back at all.
28 """
29 sentinel_profile = mock.Mock()
30 with (
31 mock.patch(
32 "DisplayCAL.worker.get_macos_display_profile_data",
33 return_value=b"fake-icc-bytes",
34 ),
35 mock.patch(
36 "DisplayCAL.worker.ICCProfile", return_value=sentinel_profile
37 ) as mock_icc_profile,
38 mock.patch("DisplayCAL.worker.get_display_profile") as mock_applescript,
39 ):
40 profile = _get_macos_display_profile(0)
41 assert profile is sentinel_profile
42 mock_icc_profile.assert_called_once_with(b"fake-icc-bytes")
43 mock_applescript.assert_not_called()
44
45
46 def test_get_macos_display_profile_falls_back_when_coregraphics_has_no_data():
47 """No CoreGraphics profile data falls back to the AppleScript path."""
48 sentinel_profile = mock.Mock()
49 with (
50 mock.patch(
51 "DisplayCAL.worker.get_macos_display_profile_data", return_value=None
52 ),
53 mock.patch(
54 "DisplayCAL.worker.get_display_profile", return_value=sentinel_profile
55 ),
56 ):
57 profile = _get_macos_display_profile(0)
58 assert profile is sentinel_profile
59
60
61 def test_get_macos_display_profile_falls_back_on_unparseable_data():
62 """Data CoreGraphics returns that fails to parse also falls back."""
63 sentinel_profile = mock.Mock()
64 with (
65 mock.patch(
66 "DisplayCAL.worker.get_macos_display_profile_data",
67 return_value=b"not-a-valid-icc-profile",
68 ),
69 mock.patch(
70 "DisplayCAL.worker.ICCProfile", side_effect=ValueError("bad profile")
71 ),
72 mock.patch(
73 "DisplayCAL.worker.get_display_profile", return_value=sentinel_profile
74 ),
75 ):
76 profile = _get_macos_display_profile(0)
77 assert profile is sentinel_profile
78
79
80 def test_vcgt_values_to_videolut_identity_same_size():
81 """A 256-entry linear vcgt interpolated to 256 entries is unchanged."""
82 points = [[i, round(i / 255.0 * 65535)] for i in range(256)]
83 out = vcgt_values_to_videolut(points, 256)
84 assert len(out) == 256
85 assert out[0] == 0.0
86 assert abs(out[-1] - 1.0) < 1e-9
87 assert abs(out[128] - points[128][1] / 65535.0) < 1e-6
88
89
90 def test_vcgt_values_to_videolut_interpolates_to_different_size():
91 """Interpolating a 256-entry table to a smaller VideoLUT stays monotonic."""
92 points = [[i, round(i / 255.0 * 65535)] for i in range(256)]
93 out = vcgt_values_to_videolut(points, 4)
94 assert len(out) == 4
95 assert out[0] == 0.0
96 assert abs(out[-1] - 1.0) < 1e-9
97 assert out == sorted(out)
98
99
100 def test_vcgt_values_to_videolut_handles_degenerate_input():
101 """A single-point or empty input doesn't crash and returns a flat table."""
102 assert vcgt_values_to_videolut([[0, 32768]], 8) == [32768 / 65535.0] * 8
103 assert vcgt_values_to_videolut([], 4) == [1.0] * 4
104
105
106 def _fake_vcgt(r_points, g_points, b_points):
107 vcgt = mock.Mock()
108 vcgt.get_values.return_value = (r_points, g_points, b_points, [])
109 return vcgt
110
111
112 def test_reload_macos_videoluts_applies_profile_vcgt():
113 """A clobbered display's profile vcgt is interpolated and applied."""
114 r_points = [[i, round(i / 255.0 * 65535)] for i in range(256)]
115 g_points = [[i, round(i / 255.0 * 65535)] for i in range(256)]
116 b_points = [[i, round(i / 255.0 * 65535)] for i in range(256)]
117 profile = mock.Mock()
118 profile.tags = {"vcgt": _fake_vcgt(r_points, g_points, b_points)}
119
120 set_calls = []
121
122 def fake_set(display_index, red, green, blue):
123 set_calls.append((display_index, len(red), len(green), len(blue)))
124 return True
125
126 with (
127 mock.patch(
128 "DisplayCAL.worker.get_macos_videolut_capacity", return_value=64
129 ),
130 mock.patch(
131 "DisplayCAL.worker.get_macos_display_profile_data", return_value=None
132 ),
133 mock.patch("DisplayCAL.worker.get_display_profile", return_value=profile),
134 mock.patch("DisplayCAL.worker.set_macos_videolut", side_effect=fake_set),
135 ):
136 reloaded = reload_macos_videoluts([0])
137 assert reloaded == [0]
138 assert set_calls == [(0, 64, 64, 64)]
139
140
141 def test_reload_macos_videoluts_falls_back_to_linear_without_vcgt():
142 """A profile without a vcgt tag gets a linear ramp instead of failing."""
143 profile = mock.Mock()
144 profile.tags = {}
145
146 set_calls = []
147
148 def fake_set(display_index, red, green, blue):
149 set_calls.append((display_index, red, green, blue))
150 return True
151
152 with (
153 mock.patch(
154 "DisplayCAL.worker.get_macos_videolut_capacity", return_value=4
155 ),
156 mock.patch(
157 "DisplayCAL.worker.get_macos_display_profile_data", return_value=None
158 ),
159 mock.patch("DisplayCAL.worker.get_display_profile", return_value=profile),
160 mock.patch("DisplayCAL.worker.set_macos_videolut", side_effect=fake_set),
161 ):
162 reloaded = reload_macos_videoluts([0])
163 assert reloaded == [0]
164 _, red, green, blue = set_calls[0]
165 assert red == green == blue == [0.0, 1 / 3, 2 / 3, 1.0]
166
167
168 def test_reload_macos_videoluts_skips_display_without_capacity():
169 """A display whose VideoLUT capacity can't be read is left alone."""
170 with (
171 mock.patch("DisplayCAL.worker.get_macos_videolut_capacity", return_value=None),
172 mock.patch("DisplayCAL.worker.set_macos_videolut") as mock_set,
173 ):
174 reloaded = reload_macos_videoluts([0])
175 assert reloaded == []
176 mock_set.assert_not_called()
177
178
179 def test_reload_macos_videoluts_reports_set_failure():
180 """A display that fails to apply the table is not reported as reloaded."""
181 profile = mock.Mock()
182 profile.tags = {}
183 logged = []
184 with (
185 mock.patch(
186 "DisplayCAL.worker.get_macos_videolut_capacity", return_value=4
187 ),
188 mock.patch(
189 "DisplayCAL.worker.get_macos_display_profile_data", return_value=None
190 ),
191 mock.patch("DisplayCAL.worker.get_display_profile", return_value=profile),
192 mock.patch("DisplayCAL.worker.set_macos_videolut", return_value=False),
193 ):
194 reloaded = reload_macos_videoluts([0], log=logged.append)
195 assert reloaded == []
196 assert logged and "display 1" in logged[0]
197
198
199 def test_reload_macos_videoluts_applies_all_displays_in_one_call():
200 """Multiple clobbered displays are all reloaded from this single call."""
201 profile = mock.Mock()
202 profile.tags = {}
203 set_calls = []
204
205 def fake_set(display_index, red, green, blue):
206 set_calls.append(display_index)
207 return True
208
209 with (
210 mock.patch(
211 "DisplayCAL.worker.get_macos_videolut_capacity", return_value=4
212 ),
213 mock.patch(
214 "DisplayCAL.worker.get_macos_display_profile_data", return_value=None
215 ),
216 mock.patch("DisplayCAL.worker.get_display_profile", return_value=profile),
217 mock.patch("DisplayCAL.worker.set_macos_videolut", side_effect=fake_set),
218 ):
219 reloaded = reload_macos_videoluts([0, 1])
220 assert reloaded == [0, 1]
221 assert set_calls == [0, 1]