Debian Codegraph @

tests/test_edid.py

raw
1 """This module contains tests for the EDID parsing functionality in DisplayCAL."""
2
3 import binascii
4 import codecs
5 import json
6 import platform
7
8 import pytest
9
10 from DisplayCAL import real_display_size_mm, config
11 from DisplayCAL.config import getcfg
12 from DisplayCAL.dev.mocks import check_call
13 from DisplayCAL.edid import (
14 get_display_name_from_system_profiler,
15 get_edid,
16 get_edid_darwin,
17 get_edid_from_xrandr,
18 parse_edid,
19 parse_manufacturer_id,
20 )
21
22 from tests.data.display_data import DisplayData
23
24
25 # @pytest.mark.skipif(
26 # platform.system() == "Darwin", reason="Not working as expected on MacOS"
27 # )
28 def test_get_edid_1(clear_displays, monkeypatch, patch_subprocess, data_files):
29 """Testing DisplayCAL.colord.device_id_from_edid() function."""
30 # patch xrandr
31 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
32 monkeypatch.setattr("DisplayCAL.edid.sys.platform", "linux")
33 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
34 xrandr_data_file_name = "xrandr_output_4.txt"
35 with open(data_files[xrandr_data_file_name], "rb") as xrandr_data_file:
36 xrandr_data = xrandr_data_file.read()
37 patch_subprocess.output["xrandr--verbose"] = xrandr_data
38
39 with check_call(
40 config,
41 "getcfg",
42 DisplayData.CFG_DATA,
43 call_count=-1,
44 ):
45 with check_call(
46 real_display_size_mm,
47 "_enumerate_displays",
48 DisplayData.enumerate_displays(),
49 call_count=-1,
50 ):
51 result = get_edid(0)
52
53 assert isinstance(result, dict)
54 assert "blue_x" in result
55 assert isinstance(result["blue_y"], float)
56 assert "blue_y" in result
57 assert isinstance(result["blue_y"], float)
58 assert "checksum" in result
59 assert result["checksum"] > 0
60 assert "checksum_valid" in result
61 assert result["checksum_valid"] is True
62 assert "edid" in result
63 assert isinstance(result["edid"], bytes)
64 assert "edid_revision" in result
65 assert isinstance(result["edid_revision"], int)
66 assert "edid_version" in result
67 assert isinstance(result["edid_version"], int)
68 assert "ext_flag" in result
69 assert isinstance(result["ext_flag"], int)
70 assert "features" in result
71 assert isinstance(result["features"], int)
72 assert "gamma" in result
73 assert isinstance(result["gamma"], float)
74 assert "green_x" in result
75 assert isinstance(result["green_x"], float)
76 assert "green_y" in result
77 assert isinstance(result["green_y"], float)
78 assert "hash" in result
79 assert isinstance(result["hash"], str)
80 assert "header" in result
81 assert isinstance(result["header"], bytes)
82 assert "manufacturer" in result
83 assert isinstance(result["manufacturer"], str)
84 assert "manufacturer_id" in result
85 assert isinstance(result["manufacturer_id"], str)
86 assert "max_h_size_cm" in result
87 assert isinstance(result["max_h_size_cm"], int)
88 assert "max_v_size_cm" in result
89 assert isinstance(result["max_v_size_cm"], int)
90 assert "product_id" in result
91 assert isinstance(result["product_id"], int)
92 assert "red_x" in result
93 assert isinstance(result["red_x"], float)
94 assert "red_y" in result
95 assert isinstance(result["red_y"], float)
96 assert "serial_32" in result
97 assert isinstance(result["serial_32"], int)
98 assert "week_of_manufacture" in result
99 assert isinstance(result["week_of_manufacture"], int)
100 assert "white_x" in result
101 assert isinstance(result["white_x"], float)
102 assert "white_y" in result
103 assert isinstance(result["white_y"], float)
104 assert "year_of_manufacture" in result
105 assert isinstance(result["year_of_manufacture"], int)
106
107
108 # def test_get_edid_3(clear_displays):
109 # """Testing DisplayCAL.colord.device_id_from_edid() function."""
110 # config.initcfg()
111 # display = RDSMM.get_display(0)
112 # edid = display.get("edid")
113 # assert isinstance(edid, str)
114 # edid = edid.encode("utf-8")
115 #
116 # # assert len(edid) == 256
117 # assert edid == (
118 # b"\x00\xff\xff\xff\xff\xff\xff\x00\x10\xac\xe0@L405\x05\x1b\x01\x04\xb57\x1fx:U"
119 # b"\xc5\xafO3\xb8%\x0bPT\xa5K\x00qO\xa9@\x81\x80\xd1\xc0\x01\x01\x01\x01\x01\x01"
120 # b"\x01\x01V^\x00\xa0\xa0\xa0)P0 5\x00)7!\x00\x00\x1a\x00\x00\x00\xff\x00"
121 # b"TYPR371U504L\n\x00\x00\x00\xfc\x00DELL UP2516D\n\x00\x00\x00\xfd\x002K\x1eX"
122 # b"\x19\x01\n \x01,\x02\x03\x1c\xf1O\x90\x05\x04\x03\x02\x07\x16\x01\x06"
123 # b"\x11\x12\x15\x13\x14\x1f#\t\x1f\x07\x83\x01\x00\x00\x02:\x80\x18q8-@X,E"
124 # b"\x00)7!\x00\x00\x1e~9\x00\xa0\x808\x1f@0 :\x00)7!\x00\x00\x1a\x01\x1d\x00rQ"
125 # b"\xd0\x1e n(U\x00)7!\x00\x00\x1e\xbf\x16\x00\xa0\x808\x13@0 :\x00)7!\x00\x00"
126 # b"\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
127 # b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x86"
128 # )
129
130
131 _VP2768A_HEX = (
132 "00ffffffffffff005a633a7a0f010101"
133 "311e0104b53c22783bb091ab524ea026"
134 "0f5054bfef80e1c0d100d1c0b300a940"
135 "8180810081c0565e00a0a0a029503020"
136 "350055502100001a000000ff00573855"
137 "3230343930303130340a000000fd0018"
138 "4b0f5a1e000a202020202020000000fc"
139 "00565032373638610a2020202020017b"
140 "020322f155901f05145a5904131e1d0f"
141 "0e07061211161503020123097f078301"
142 "0000023a801871382d40582c45005550"
143 "2100001e011d8018711c1620582c2500"
144 "55502100009e023a80d072382d40102c"
145 "458055502100001e011d007251d01e20"
146 "6e28550055502100001e584d00b8a138"
147 "1440f82c4b0055502100001e000000d2"
148 )
149 _VP2768A_EDID = binascii.unhexlify(_VP2768A_HEX)
150
151 _U28E590_HEX = (
152 "00ffffffffffff004c2d4d0c46584d30"
153 "231a0104b53d23783a5fb1a2574fa228"
154 "0f5054bfef80714f810081c08180a9c0"
155 "b300950001014dd000a0f0703e803020"
156 "35005f592100001a000000fd00384b1e"
157 "873c000a202020202020000000fc0055"
158 "3238453539300a2020202020000000ff"
159 "00485450483930303130330a20200166"
160 "02030ef041102309070783010000023a"
161 "801871382d40582c45005f592100001e"
162 "565e00a0a0a02950302035005f592100"
163 "001a04740030f2705a80b0588a005f59"
164 "2100001e000000000000000000000000"
165 "00000000000000000000000000000000"
166 "00000000000000000000000000000000"
167 "00000000000000000000000000000052"
168 )
169 _U28E590_EDID = binascii.unhexlify(_U28E590_HEX)
170
171 _ACER_ET430K_HEX = (
172 "00ffffffffffff0004725805436e6072"
173 "1a1b0103805e35782aa191a9544d9c26"
174 "0f5054bfef80714f8140818081c08100"
175 "9500b300d1c04dd000a0f0703e803020"
176 "3500ad113200001a565e00a0a0a02950"
177 "2f203500ad113200001a000000fd0032"
178 "3c1e8c3c000a202020202020000000fc"
179 "00416365722045543433304b0a200129"
180 "020341f1506101600304121305141f10"
181 "0706026b5f23090707830100006b030c"
182 "002000383c2000200167d85dc4017880"
183 "00e305e001e40f050000e60607016060"
184 "45023a801871382d40582c4500ad1132"
185 "00001e011d007251d01e206e285500ad"
186 "113200001e8c0ad08a20e02d10103e96"
187 "00ad1132000018000000000000000088"
188 )
189 _ACER_ET430K_EDID = binascii.unhexlify(_ACER_ET430K_HEX)
190
191 _VP2768A_RESULT = {
192 "edid": _VP2768A_EDID,
193 "hash": "c809a7de5f47319307d2358f3d578078",
194 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
195 "manufacturer": "ViewSonic Corporation",
196 "manufacturer_id": "VSC",
197 "product_id": 31290,
198 "serial_32": 16843023,
199 "serial_ascii": "W8U204900104",
200 "week_of_manufacture": 49,
201 "year_of_manufacture": 2020,
202 "edid_version": 1,
203 "edid_revision": 4,
204 "max_h_size_cm": 60,
205 "max_v_size_cm": 34,
206 "gamma": 2.2,
207 "features": 59,
208 "red_x": 0.669921875,
209 "red_y": 0.3232421875,
210 "green_x": 0.3046875,
211 "green_y": 0.625,
212 "blue_x": 0.150390625,
213 "blue_y": 0.0595703125,
214 "white_x": 0.3125,
215 "white_y": 0.3291015625,
216 "ext_flag": 1,
217 "checksum": 123,
218 "checksum_valid": True,
219 "monitor_name": "VP2768a",
220 }
221
222
223 @pytest.mark.parametrize(
224 "xrandr_data_file_name,dispwin_data_file_name,getcfg_displays_output,display_no,expected_result",
225 [
226 [
227 "xrandr_output_1.txt",
228 "dispwin_output_1.txt",
229 ["DP-4 @ 0, 0, 2560x1440 [PRIMARY]"],
230 0,
231 _VP2768A_RESULT,
232 ],
233 [
234 "xrandr_output_2.txt",
235 "dispwin_output_2.txt",
236 ["DP-4 @ 0, 0, 2560x1440 [PRIMARY]", "DP-2 @ 2160, 0, 3840x2160"],
237 0,
238 _VP2768A_RESULT,
239 ],
240 [
241 "xrandr_output_2.txt",
242 "dispwin_output_2.txt",
243 ["DP-4 @ 0, 0, 2560x1440 [PRIMARY]", "DP-2 @ 2160, 0, 3840x2160"],
244 1,
245 {
246 "edid": _U28E590_EDID,
247 "hash": "a719d259d3e729176b9f56e1c875e8c1",
248 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
249 "manufacturer": "Samsung Electric Company",
250 "manufacturer_id": "SAM",
251 "product_id": 3149,
252 "serial_32": 810375238,
253 "serial_ascii": "HTPH900103",
254 "week_of_manufacture": 35,
255 "year_of_manufacture": 2016,
256 "edid_version": 1,
257 "edid_revision": 4,
258 "max_h_size_cm": 61,
259 "max_v_size_cm": 35,
260 "gamma": 2.2,
261 "features": 58,
262 "red_x": 0.6337890625,
263 "red_y": 0.3408203125,
264 "green_x": 0.3115234375,
265 "green_y": 0.6357421875,
266 "blue_x": 0.158203125,
267 "blue_y": 0.0615234375,
268 "white_x": 0.3125,
269 "white_y": 0.3291015625,
270 "ext_flag": 1,
271 "checksum": 102,
272 "checksum_valid": True,
273 "monitor_name": "U28E590",
274 },
275 ],
276 [
277 "xrandr_output_3.txt",
278 "dispwin_output_3.txt",
279 ["HDMI-A-0 @ 0, 0, 3840x2160 [PRIMARY]"],
280 0,
281 {
282 "edid": _ACER_ET430K_EDID,
283 "hash": "23d07c7921998829a4b68374e1000cfe",
284 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
285 "manufacturer": "Acer Technologies",
286 "manufacturer_id": "ACR",
287 "product_id": 1368,
288 "serial_32": 1918922307,
289 "week_of_manufacture": 26,
290 "year_of_manufacture": 2017,
291 "edid_version": 1,
292 "edid_revision": 3,
293 "max_h_size_cm": 94,
294 "max_v_size_cm": 53,
295 "gamma": 2.2,
296 "features": 42,
297 "red_x": 0.662109375,
298 "red_y": 0.330078125,
299 "green_x": 0.30078125,
300 "green_y": 0.6103515625,
301 "blue_x": 0.150390625,
302 "blue_y": 0.0595703125,
303 "white_x": 0.3125,
304 "white_y": 0.3291015625,
305 "ext_flag": 1,
306 "checksum": 41,
307 "checksum_valid": True,
308 "monitor_name": "Acer ET430K",
309 },
310 ],
311 # ArgyllCMS >= 3.3.0 reports just the xrandr output name ("DP-2"), but
312 # xrandr may show the output as "Monitor 1, Output DP-2 connected".
313 # Verify that the ", Output <name> connected" fallback matching works.
314 [
315 "xrandr_output_4.txt",
316 "dispwin_output_5.txt",
317 ["DP-2 @ 0, 0, 1280x1024 [PRIMARY]"],
318 0,
319 _VP2768A_RESULT,
320 ],
321 ],
322 )
323 def test_get_edid_4(
324 monkeypatch,
325 patch_subprocess,
326 patch_argyll_util,
327 clear_displays,
328 data_files,
329 xrandr_data_file_name,
330 dispwin_data_file_name,
331 getcfg_displays_output,
332 display_no,
333 expected_result,
334 ):
335 """DisplayCAL.edid.get_edid() gets the EDID data from xrandr --verbose command."""
336 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
337 monkeypatch.setattr("DisplayCAL.real_display_size_mm.subprocess", patch_subprocess)
338 monkeypatch.setattr("DisplayCAL.real_display_size_mm.sys.platform", "linux")
339 monkeypatch.setattr("DisplayCAL.edid.sys.platform", "linux")
340 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
341
342 # patch xrandr
343 with open(data_files[xrandr_data_file_name], "rb") as xrandr_data_file:
344 xrandr_data = xrandr_data_file.read()
345 patch_subprocess.output["xrandr--verbose"] = xrandr_data
346
347 # patch dispwin
348 with open(data_files[dispwin_data_file_name], "rb") as dispwin_data_file:
349 dispwin_data = dispwin_data_file.read()
350 patch_subprocess.output["dispwin-v-d0"] = dispwin_data
351
352 # patch real_display_size_mm.getcfg("displays")
353 orig_getcfg = getcfg
354
355 def patched_getcfg(config_value):
356 if config_value == "displays":
357 return getcfg_displays_output + [
358 "Web @ localhost",
359 "madVR",
360 "Prisma",
361 "Resolve",
362 "Untethered",
363 ]
364 else:
365 return orig_getcfg(config_value)
366
367 monkeypatch.setattr("DisplayCAL.config.getcfg", patched_getcfg)
368
369 result = get_edid(display_no=display_no)
370 assert result == expected_result
371
372
373 def test_parse_edid_1():
374 """Testing DisplayCAL.edid.parse_edid() function."""
375 raw_edid = (
376 b"\x00\xff\xff\xff\xff\xff\xff\x00\x10\xac\xe0@L405\x05\x1b\x01\x04\xb57\x1fx:U"
377 b"\xc5\xafO3\xb8%\x0bPT\xa5K\x00qO\xa9@\x81\x80\xd1\xc0\x01\x01\x01\x01\x01\x01"
378 b"\x01\x01V^\x00\xa0\xa0\xa0)P0 5\x00)7!\x00\x00\x1a\x00\x00\x00\xff\x00"
379 b"TYPR371U504L\n\x00\x00\x00\xfc\x00DELL UP2516D\n\x00\x00\x00\xfd\x002K\x1eX"
380 b"\x19\x01\n \x01,\x02\x03\x1c\xf1O\x90\x05\x04\x03\x02\x07\x16\x01\x06"
381 b"\x11\x12\x15\x13\x14\x1f#\t\x1f\x07\x83\x01\x00\x00\x02:\x80\x18q8-@X,E"
382 b"\x00)7!\x00\x00\x1e~9\x00\xa0\x808\x1f@0 :\x00)7!\x00\x00\x1a\x01\x1d\x00rQ"
383 b"\xd0\x1e n(U\x00)7!\x00\x00\x1e\xbf\x16\x00\xa0\x808\x13@0 :\x00)7!\x00\x00"
384 b"\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
385 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x86"
386 )
387 result = parse_edid(raw_edid)
388 expected_result = {
389 "blue_x": 0.1474609375,
390 "blue_y": 0.04296875,
391 "checksum": 44,
392 "checksum_valid": True,
393 "edid": b"\x00\xff\xff\xff\xff\xff\xff\x00\x10\xac\xe0@L405\x05\x1b\x01\x04"
394 b"\xb57\x1fx:U\xc5\xafO3\xb8%\x0bPT\xa5K\x00qO\xa9@\x81\x80"
395 b"\xd1\xc0\x01\x01\x01\x01\x01\x01\x01\x01V^\x00\xa0\xa0\xa0)P0 "
396 b"5\x00)7!\x00\x00\x1a\x00\x00\x00\xff\x00TYPR371U504L\n\x00\x00"
397 b"\x00\xfc\x00DELL UP2516D\n\x00\x00\x00\xfd\x002K\x1eX\x19\x01\n "
398 b" \x01,\x02\x03\x1c\xf1O\x90\x05\x04\x03\x02\x07\x16\x01\x06\x11\x12"
399 b"\x15\x13\x14\x1f#\t\x1f\x07\x83\x01\x00\x00\x02:\x80\x18q8-@X,E\x00"
400 b")7!\x00\x00\x1e~9\x00\xa0\x808\x1f@0 :\x00)7!\x00\x00\x1a"
401 b"\x01\x1d\x00rQ\xd0\x1e n(U\x00)7!\x00\x00\x1e\xbf\x16\x00\xa0\x808"
402 b"\x13@0 :\x00)7!\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00"
403 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
404 b"\x00\x00\x00\x86",
405 "edid_revision": 4,
406 "edid_version": 1,
407 "ext_flag": 1,
408 "features": 58,
409 "gamma": 2.2,
410 "green_x": 0.2001953125,
411 "green_y": 0.7197265625,
412 "hash": "40cf706d53476076b828fb8a78af796d",
413 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
414 "manufacturer": "Dell, Inc.",
415 "manufacturer_id": "DEL",
416 "max_h_size_cm": 55,
417 "max_v_size_cm": 31,
418 "monitor_name": "DELL UP2516D",
419 "product_id": 16608,
420 "red_x": 0.6845703125,
421 "red_y": 0.3095703125,
422 "serial_32": 892351564,
423 "serial_ascii": "TYPR371U504L",
424 "week_of_manufacture": 5,
425 "white_x": 0.3134765625,
426 "white_y": 0.3291015625,
427 "year_of_manufacture": 2017,
428 }
429 assert result == expected_result
430
431
432 def test_parse_edid_2():
433 """Testing DisplayCAL.edid.parse_edid() function. for #50."""
434 xrandr_edid_data = """
435 00ffffffffffff0004725805436e6072
436 1a1b0103805e35782aa191a9544d9c26
437 0f5054bfef80714f8140818081c08100
438 9500b300d1c04dd000a0f0703e803020
439 3500ad113200001a565e00a0a0a02950
440 2f203500ad113200001a000000fd0032
441 3c1e8c3c000a202020202020000000fc
442 00416365722045543433304b0a200129
443 020341f1506101600304121305141f10
444 0706026b5f23090707830100006b030c
445 002000383c2000200167d85dc4017880
446 00e305e001e40f050000e60607016060
447 45023a801871382d40582c4500ad1132
448 00001e011d007251d01e206e285500ad
449 113200001e8c0ad08a20e02d10103e96
450 00ad1132000018000000000000000088"""
451 xrandr_edid_data = "".join(xrandr_edid_data.split("\n")).replace(" ", "").strip()
452 raw_edid = codecs.decode(xrandr_edid_data, "hex")
453 result = parse_edid(raw_edid)
454 expected_result = {
455 "blue_x": 0.150390625,
456 "blue_y": 0.0595703125,
457 "checksum": 41,
458 "checksum_valid": True,
459 "edid": b"\x00\xff\xff\xff\xff\xff\xff\x00\x04rX\x05Cn`r\x1a\x1b\x01\x03"
460 b"\x80^5x*\xa1\x91\xa9TM\x9c&\x0fPT\xbf\xef\x80qO\x81@\x81\x80"
461 b"\x81\xc0\x81\x00\x95\x00\xb3\x00\xd1\xc0M\xd0\x00\xa0\xf0p>\x800 "
462 b"5\x00\xad\x112\x00\x00\x1aV^\x00\xa0\xa0\xa0)P/ 5\x00\xad\x112\x00"
463 b"\x00\x1a\x00\x00\x00\xfd\x002<\x1e\x8c<\x00\n \x00\x00\x00\xfc"
464 b"\x00Acer ET430K\n \x01)\x02\x03A\xf1Pa\x01`\x03\x04\x12\x13"
465 b"\x05\x14\x1f\x10\x07\x06\x02k_#\t\x07\x07\x83\x01\x00\x00k\x03\x0c"
466 b"\x00 \x008< \x00 \x01g\xd8]\xc4\x01x\x80\x00\xe3\x05\xe0"
467 b"\x01\xe4\x0f\x05\x00\x00\xe6\x06\x07\x01``E\x02:\x80\x18q8-@X,E"
468 b"\x00\xad\x112\x00\x00\x1e\x01\x1d\x00rQ\xd0\x1e n(U\x00\xad"
469 b"\x112\x00\x00\x1e\x8c\n\xd0\x8a \xe0-\x10\x10>\x96\x00\xad\x112"
470 b"\x00\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x88",
471 "edid_revision": 3,
472 "edid_version": 1,
473 "ext_flag": 1,
474 "features": 42,
475 "gamma": 2.2,
476 "green_x": 0.30078125,
477 "green_y": 0.6103515625,
478 "hash": "23d07c7921998829a4b68374e1000cfe",
479 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
480 "manufacturer": "Acer Technologies",
481 "manufacturer_id": "ACR",
482 "max_h_size_cm": 94,
483 "max_v_size_cm": 53,
484 "monitor_name": "Acer ET430K",
485 "product_id": 1368,
486 "red_x": 0.662109375,
487 "red_y": 0.330078125,
488 "serial_32": 1918922307,
489 "week_of_manufacture": 26,
490 "white_x": 0.3125,
491 "white_y": 0.3291015625,
492 "year_of_manufacture": 2017,
493 }
494 assert result == expected_result
495
496
497 def test_parse_edid_3():
498 """Testing DisplayCAL.edid.parse_edid() function. for #119."""
499 xrandr_edid_data = """
500 00ffffffffffff0009e5120800000000
501 1f1c0104a5221378030980955c5a9129
502 21505400000001010101010101010101
503 010101010101033a803671381e403020
504 360058c21000001a0000000000000000
505 00000000000000000000000000fe0042
506 4f452043510a202020202020000000fe
507 004e5431353646484d2d4e36310a00ed
508 """
509 xrandr_edid_data = "".join(xrandr_edid_data.split("\n")).replace(" ", "").strip()
510 raw_edid = codecs.decode(xrandr_edid_data, "hex")
511 expected_raw_edid = (
512 b"\x00\xff\xff\xff\xff\xff\xff\x00\t\xe5\x12\x08\x00\x00\x00\x00"
513 b'\x1f\x1c\x01\x04\xa5"\x13x\x03\t\x80\x95\\Z\x91)!PT\x00'
514 b"\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
515 b"\x01\x01\x03:\x806q8\x1e@0 6\x00X\xc2\x10\x00\x00\x1a"
516 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
517 b"\x00\x00\x00\x00\x00\xfe\x00BOE CQ\n \x00\x00\x00\xfe\x00NT1"
518 b"56FHM-N61\n\x00\xed"
519 )
520 assert raw_edid == expected_raw_edid
521 assert len(raw_edid) == 128
522
523 result = parse_edid(raw_edid)
524 expected_result = {
525 "ascii": "NT156FHM-N61",
526 "blue_x": 0.162109375,
527 "blue_y": 0.12890625,
528 "checksum": 237,
529 "checksum_valid": True,
530 "edid": b"\x00\xff\xff\xff\xff\xff\xff\x00\t\xe5\x12\x08\x00\x00\x00\x00"
531 b'\x1f\x1c\x01\x04\xa5"\x13x\x03\t\x80\x95\\Z\x91)!PT\x00'
532 b"\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
533 b"\x01\x01\x03:\x806q8\x1e@0 6\x00X\xc2\x10\x00\x00\x1a"
534 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
535 b"\x00\x00\x00\x00\x00\xfe\x00BOE CQ\n \x00\x00\x00\xfe\x00NT1"
536 b"56FHM-N61\n\x00\xed",
537 "edid_revision": 4,
538 "edid_version": 1,
539 "ext_flag": 0,
540 "features": 3,
541 "gamma": 2.2,
542 "green_x": 0.353515625,
543 "green_y": 0.5673828125,
544 "hash": "db067630cf478ff8638db83f2724a40b",
545 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
546 "manufacturer": "BOE",
547 "manufacturer_id": "BOE",
548 "max_h_size_cm": 34,
549 "max_v_size_cm": 19,
550 "product_id": 2066,
551 "red_x": 0.58203125,
552 "red_y": 0.359375,
553 "serial_32": 0,
554 "week_of_manufacture": 31,
555 "white_x": 0.3125,
556 "white_y": 0.328125,
557 "year_of_manufacture": 2018,
558 }
559 assert result == expected_result
560
561
562 def test_parse_edid_4():
563 """Testing DisplayCAL.edid.parse_edid() function. for #119."""
564 raw_edid = (
565 b"\x00\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\xc3\xbf\x00\x10\xc2"
566 b"\xac\xc3\xa0@L405\x05\x1b\x01\x04\xc2\xb57\x1fx:U\xc3\x85\xc2\xafO3\xc2\xb8%"
567 b"\x0bPT\xc2\xa5K\x00qO\xc2\xa9@\xc2\x81\xc2\x80\xc3\x91\xc3\x80"
568 b"\x01\x01\x01\x01\x01\x01\x01\x01V^\x00\xc2\xa0\xc2\xa0\xc2\xa0)P0 5\x00)"
569 b"7!\x00\x00\x1a\x00\x00\x00\xc3\xbf\x00TYPR371U504L\n\x00\x00\x00\xc3"
570 b"\xbc\x00DELL UP2516D\n\x00\x00\x00\xc3\xbd\x002K\x1eX\x19\x01\n \x01,"
571 b"\x02\x03\x1c\xc3\xb1O\xc2\x90\x05\x04\x03\x02\x07\x16\x01\x06"
572 b"\x11\x12\x15\x13\x14\x1f#\t\x1f\x07\xc2\x83\x01\x00\x00\x02:\xc2\x80\x18q8-@"
573 b"X,E\x00)7!\x00\x00\x1e~9\x00\xc2\xa0\xc2\x808\x1f@0 :\x00)7!\x00"
574 b"\x00\x1a\x01\x1d\x00rQ\xc3\x90\x1e n(U\x00)7!\x00\x00\x1e\xc2\xbf\x16"
575 b"\x00\xc2\xa0\xc2\x808\x13@0 :\x00)7!\x00\x00\x1a\x00\x00\x00\x00\x00\x00"
576 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
577 b"\x00\x00\x00\x00\x00\xc2\x86"
578 )
579 result = parse_edid(raw_edid)
580 expected_result = {
581 "blue_x": 0.1474609375,
582 "blue_y": 0.04296875,
583 "checksum": 44,
584 "checksum_valid": True,
585 "edid": (
586 b"\x00\xff\xff\xff\xff\xff\xff\x00\x10\xac\xe0@L405\x05\x1b\x01\x04"
587 b"\xb57\x1fx:U\xc5\xafO3\xb8%\x0bPT\xa5K\x00qO\xa9@\x81\x80"
588 b"\xd1\xc0\x01\x01\x01\x01\x01\x01\x01\x01V^\x00\xa0\xa0\xa0)P0 "
589 b"5\x00)7!\x00\x00\x1a\x00\x00\x00\xff\x00TYPR371U504L\n\x00\x00"
590 b"\x00\xfc\x00DELL UP2516D\n\x00\x00\x00\xfd\x002K\x1eX\x19\x01\n "
591 b" \x01,\x02\x03\x1c\xf1O\x90\x05\x04\x03\x02\x07\x16\x01\x06\x11\x12"
592 b"\x15\x13\x14\x1f#\t\x1f\x07\x83\x01\x00\x00\x02:\x80\x18q8-@X,E\x00"
593 b")7!\x00\x00\x1e~9\x00\xa0\x808\x1f@0 :\x00)7!\x00\x00\x1a"
594 b"\x01\x1d\x00rQ\xd0\x1e n(U\x00)7!\x00\x00\x1e\xbf\x16\x00\xa0\x808"
595 b"\x13@0 :\x00)7!\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00"
596 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
597 b"\x00\x00\x00\x86"
598 ),
599 "edid_revision": 4,
600 "edid_version": 1,
601 "ext_flag": 1,
602 "features": 58,
603 "gamma": 2.2,
604 "green_x": 0.2001953125,
605 "green_y": 0.7197265625,
606 "hash": "40cf706d53476076b828fb8a78af796d",
607 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
608 "manufacturer": "Dell, Inc.",
609 "manufacturer_id": "DEL",
610 "max_h_size_cm": 55,
611 "max_v_size_cm": 31,
612 "monitor_name": "DELL UP2516D",
613 "product_id": 16608,
614 "red_x": 0.6845703125,
615 "red_y": 0.3095703125,
616 "serial_32": 892351564,
617 "serial_ascii": "TYPR371U504L",
618 "week_of_manufacture": 5,
619 "white_x": 0.3134765625,
620 "white_y": 0.3291015625,
621 "year_of_manufacture": 2017,
622 }
623 assert result == expected_result
624
625
626 def test_parse_edid_5():
627 """Testing DisplayCAL.edid.parse_edid() function with a 384 byte EDID."""
628 xrandr_edid_data = """
629 00ffffffffffff004c2d5c10564a5843
630 0c1f0104b53c22783a2eb5ae4f46a626
631 115054bfef8081c0810081809500a9c0
632 b300714f0101565e00a0a0a029503020
633 350055502100001a000000fd0832f01e
634 6762000a202020202020000000fc004c
635 433237473778540a20202020000000ff
636 0048345a523330323437300a20200231
637 02031cf147903f1f0413120323090707
638 83010000e305c000e30605015a8780a0
639 70384d403020350055502100001a23e8
640 8078703887401c20980c55502100001a
641 6fc200a0a0a055503020350055502100
642 001a98e200a0a0a02950084035005550
643 2100001a023a801871382d40582c4500
644 56502100001e00000000000000000088
645 7012170000030114e17b0188ff099f00
646 2f801f009f053100020004008a000000
647 00000000000000000000000000000000
648 00000000000000000000000000000000
649 00000000000000000000000000000000
650 00000000000000000000000000000000
651 00000000000000000000000000000000
652 00000000000000000000000000000090"""
653 xrandr_edid_data = "".join(xrandr_edid_data.split("\n")).replace(" ", "").strip()
654 raw_edid = codecs.decode(xrandr_edid_data, "hex")
655 result = parse_edid(raw_edid)
656 expected_result = {
657 "blue_x": 0.150390625,
658 "blue_y": 0.0693359375,
659 "checksum": 49,
660 "checksum_valid": True,
661 "edid": b'\x00\xff\xff\xff\xff\xff\xff\x00L-\\\x10VJXC\x0c\x1f\x01\x04\xb5<"x'
662 b":.\xb5\xaeOF\xa6&\x11PT\xbf\xef\x80\x81\xc0\x81\x00\x81\x80"
663 b"\x95\x00\xa9\xc0\xb3\x00qO\x01\x01V^\x00\xa0\xa0\xa0)P0 5\x00UP"
664 b"!\x00\x00\x1a\x00\x00\x00\xfd\x082\xf0\x1egb\x00\n \x00\x00"
665 b"\x00\xfc\x00LC27G7xT\n \x00\x00\x00\xff\x00H4ZR302470\n \x021"
666 b"\x02\x03\x1c\xf1G\x90?\x1f\x04\x13\x12\x03#\t\x07\x07"
667 b"\x83\x01\x00\x00\xe3\x05\xc0\x00\xe3\x06\x05\x01Z\x87\x80\xa0p8M@"
668 b"0 5\x00UP!\x00\x00\x1a#\xe8\x80xp8\x87@\x1c \x98\x0cUP!\x00\x00\x1a"
669 b"o\xc2\x00\xa0\xa0\xa0UP0 5\x00UP!\x00\x00\x1a\x98\xe2"
670 b"\x00\xa0\xa0\xa0)P\x08@5\x00UP!\x00\x00\x1a\x02:\x80\x18q8-@X,E\x00"
671 b"VP!\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x88p\x12\x17\x00"
672 b"\x00\x03\x01\x14\xe1{\x01\x88\xff\t\x9f\x00/\x80\x1f\x00"
673 b"\x9f\x051\x00\x02\x00\x04\x00\x8a\x00\x00\x00\x00\x00\x00\x00"
674 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
675 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
676 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
677 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
678 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
679 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90",
680 "edid_revision": 4,
681 "edid_version": 1,
682 "ext_flag": 2,
683 "features": 58,
684 "gamma": 2.2,
685 "green_x": 0.2763671875,
686 "green_y": 0.650390625,
687 "hash": "c0c868ec1d10057c60a6f0cd40282568",
688 "header": b"\x00\xff\xff\xff\xff\xff\xff\x00",
689 "manufacturer": "Samsung Electric Company",
690 "manufacturer_id": "SAM",
691 "max_h_size_cm": 60,
692 "max_v_size_cm": 34,
693 "monitor_name": "LC27G7xT",
694 "product_id": 4188,
695 "red_x": 0.6796875,
696 "red_y": 0.310546875,
697 "serial_32": 1129859670,
698 "serial_ascii": "H4ZR302470",
699 "week_of_manufacture": 12,
700 "white_x": 0.3134765625,
701 "white_y": 0.3291015625,
702 "year_of_manufacture": 2021,
703 }
704 assert result == expected_result
705
706
707 def test_parse_edid_6():
708 """parse_edid() with test data."""
709 edid = DisplayData.DISPLAY_DATA_2["edid"]
710 result = parse_edid(edid)
711 assert result == DisplayData.DISPLAY_DATA_2
712
713
714 def test_parse_manufacturer_id_1():
715 """Test parse_manufacturer_id."""
716 manufacturer_id_raw = b"\x10\xac"
717 manufacturer_id = parse_manufacturer_id(manufacturer_id_raw)
718 assert manufacturer_id == "DEL"
719
720
721 def test_get_edid_windows_wmi_returns_bytes():
722 """get_edid_windows_wmi must return bytes not str (Python 3 fix for issue #703).
723
724 WMI returns EDID as a tuple of ints. The old code used
725 ''.join(chr(i) for i in edid[0]) which produces a unicode str in Python 3,
726 causing md5() in parse_edid to raise TypeError (silently suppressed),
727 leaving the monitor with its generic 'Plug and play' device string instead
728 of the real EDID monitor name.
729 """
730 from DisplayCAL.edid import get_edid_windows_wmi
731
732 raw_edid = (
733 b"\x00\xff\xff\xff\xff\xff\xff\x00\x10\xac\xe0@L405\x05\x1b\x01\x04\xb57\x1fx:U"
734 b"\xc5\xafO3\xb8%\x0bPT\xa5K\x00qO\xa9@\x81\x80\xd1\xc0\x01\x01\x01\x01\x01\x01"
735 b"\x01\x01V^\x00\xa0\xa0\xa0)P0 5\x00)7!\x00\x00\x1a\x00\x00\x00\xff\x00"
736 b"TYPR371U504L\n\x00\x00\x00\xfc\x00DELL UP2516D\n\x00\x00\x00\xfd\x002K\x1eX"
737 b"\x19\x01\n \x01,\x02\x03\x1c\xf1O\x90\x05\x04\x03\x02\x07\x16\x01\x06"
738 b"\x11\x12\x15\x13\x14\x1f#\t\x1f\x07\x83\x01\x00\x00\x02:\x80\x18q8-@X,E"
739 b"\x00)7!\x00\x00\x1e~9\x00\xa0\x808\x1f@0 :\x00)7!\x00\x00\x1a\x01\x1d\x00rQ"
740 b"\xd0\x1e n(U\x00)7!\x00\x00\x1e\xbf\x16\x00\xa0\x808\x13@0 :\x00)7!\x00\x00"
741 b"\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
742 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x86"
743 )
744 edid_as_ints = tuple(raw_edid)
745
746 class MockMonitor:
747 InstanceName = "DISPLAY\\DELL_UP2516D\\0"
748
749 def WmiGetMonitorRawEEdidV1Block(self, block):
750 return (edid_as_ints, 0)
751
752 class MockWMIConnection:
753 def WmiMonitorDescriptorMethods(self):
754 return [MockMonitor()]
755
756 result = get_edid_windows_wmi("DELL_UP2516D", MockWMIConnection(), False)
757
758 assert isinstance(result, bytes), (
759 f"get_edid_windows_wmi must return bytes, got {type(result).__name__}"
760 )
761 assert result == raw_edid
762
763 parsed = parse_edid(result)
764 assert parsed.get("monitor_name") == "DELL UP2516D"
765
766
767 def _make_ioreg_output(*edid_hex_list):
768 """Build a fake ioreg output string containing the given EDID hex blobs."""
769 lines = []
770 for edid_hex in edid_hex_list:
771 lines.append(f'"IODisplayEDID" = <{edid_hex}>')
772 return "\n".join(lines).encode()
773
774
775 def test_get_edid_darwin_name_match(monkeypatch, patch_subprocess):
776 """get_edid_darwin() returns the EDID whose monitor_name matches display_name."""
777 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
778 patch_subprocess.output["ioreg-cIODisplay-S-w0"] = _make_ioreg_output(_VP2768A_HEX)
779
780 result = get_edid_darwin("VP2768a", display_no=0)
781
782 assert result == _VP2768A_EDID
783
784
785 def test_get_edid_darwin_index_fallback(monkeypatch, patch_subprocess):
786 """get_edid_darwin() falls back to display_no index when name does not match.
787
788 Covers the case where ArgyllCMS returns a generic name such as "Display #1"
789 on macOS Tahoe / Apple Silicon (issue #773).
790 """
791 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
792 patch_subprocess.output["ioreg-cIODisplay-S-w0"] = _make_ioreg_output(_VP2768A_HEX)
793
794 result = get_edid_darwin("Display #1", display_no=0)
795
796 assert result == _VP2768A_EDID
797 parsed = parse_edid(result)
798 assert parsed.get("monitor_name") == "VP2768a"
799
800
801 def test_get_edid_darwin_index_out_of_range(monkeypatch, patch_subprocess):
802 """get_edid_darwin() returns None when display_no exceeds available EDIDs."""
803 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
804 patch_subprocess.output["ioreg-cIODisplay-S-w0"] = _make_ioreg_output(_VP2768A_HEX)
805
806 result = get_edid_darwin("Display #2", display_no=1)
807
808 assert result is None
809
810
811 def test_get_edid_darwin_multi_display_index_fallback(monkeypatch, patch_subprocess):
812 """get_edid_darwin() selects the correct EDID by index for multi-display setups."""
813 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
814 patch_subprocess.output["ioreg-cIODisplay-S-w0"] = _make_ioreg_output(
815 _VP2768A_HEX, _U28E590_HEX
816 )
817
818 result0 = get_edid_darwin("Display #1", display_no=0)
819 result1 = get_edid_darwin("Display #2", display_no=1)
820
821 assert result0 == _VP2768A_EDID
822 assert result1 == _U28E590_EDID
823
824
825 # ---------------------------------------------------------------------------
826 # Linux (xrandr) fallback tests
827 # ---------------------------------------------------------------------------
828
829 def _make_xrandr_output(*entries):
830 """Build a minimal fake xrandr --verbose output.
831
832 Each entry is a (output_name, edid_hex) tuple. Connected outputs are
833 listed in order with their EDID blocks embedded.
834 """
835 lines = [b"Screen 0: minimum 8 x 8, current 2560 x 1440, maximum 32767 x 32767"]
836 for i, (output_name, edid_hex) in enumerate(entries):
837 name = output_name.encode() if isinstance(output_name, str) else output_name
838 primary = b" primary" if i == 0 else b""
839 lines.append(name + b" connected" + primary + b" 2560x1440+0+0 (normal) 597mm x 336mm")
840 lines.append(b"\tEDID:")
841 # Split hex into 32-char rows (16 bytes each) as xrandr does
842 for j in range(0, len(edid_hex), 32):
843 lines.append(b"\t\t" + edid_hex[j : j + 32].encode())
844 return b"\n".join(lines)
845
846
847 def test_get_edid_from_xrandr_index_fallback_no_display(monkeypatch, patch_subprocess):
848 """get_edid_from_xrandr() falls back to index when get_display() returns None.
849
850 This covers the case where the geometry-based display lookup fails
851 (e.g. stale getcfg('displays') on first run).
852 """
853 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
854 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
855 monkeypatch.setattr("DisplayCAL.edid.real_display_size_mm.get_display", lambda n: None)
856 patch_subprocess.output["xrandr--verbose"] = _make_xrandr_output(
857 ("DP-4", _VP2768A_HEX)
858 )
859
860 result = get_edid_from_xrandr(0)
861
862 assert result == _VP2768A_EDID
863 parsed = parse_edid(result)
864 assert parsed.get("monitor_name") == "VP2768a"
865
866
867 def test_get_edid_from_xrandr_index_fallback_name_mismatch(monkeypatch, patch_subprocess):
868 """get_edid_from_xrandr() falls back to index when xrandr name does not match.
869
870 This covers the case where ArgyllCMS provides a generic name (e.g. "Monitor 1")
871 that does not appear in xrandr output as an output name (issue #773 on Linux).
872 """
873 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
874 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
875 monkeypatch.setattr(
876 "DisplayCAL.edid.real_display_size_mm.get_display",
877 lambda n: {"name": b"Monitor 1", "description": b"Monitor 1 @ 0, 0, 2560x1440"},
878 )
879 patch_subprocess.output["xrandr--verbose"] = _make_xrandr_output(
880 ("DP-4", _VP2768A_HEX)
881 )
882
883 result = get_edid_from_xrandr(0)
884
885 assert result == _VP2768A_EDID
886 parsed = parse_edid(result)
887 assert parsed.get("monitor_name") == "VP2768a"
888
889
890 def test_get_edid_from_xrandr_index_fallback_multi_display(monkeypatch, patch_subprocess):
891 """get_edid_from_xrandr() fallback selects the Nth EDID for multi-display setups."""
892 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
893 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
894 monkeypatch.setattr("DisplayCAL.edid.real_display_size_mm.get_display", lambda n: None)
895 patch_subprocess.output["xrandr--verbose"] = _make_xrandr_output(
896 ("DP-4", _VP2768A_HEX),
897 ("DP-2", _U28E590_HEX),
898 )
899
900 result0 = get_edid_from_xrandr(0)
901 result1 = get_edid_from_xrandr(1)
902
903 assert result0 == _VP2768A_EDID
904 assert result1 == _U28E590_EDID
905
906
907 def test_get_edid_from_xrandr_index_out_of_range(monkeypatch, patch_subprocess):
908 """get_edid_from_xrandr() returns None when display_no exceeds available EDIDs."""
909 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
910 monkeypatch.setattr("DisplayCAL.edid.which", lambda x: "xrandr")
911 monkeypatch.setattr("DisplayCAL.edid.real_display_size_mm.get_display", lambda n: None)
912 patch_subprocess.output["xrandr--verbose"] = _make_xrandr_output(
913 ("DP-4", _VP2768A_HEX)
914 )
915
916 result = get_edid_from_xrandr(1)
917
918 assert result is None
919
920
921 # ---------------------------------------------------------------------------
922 # system_profiler fallback tests (macOS Tahoe / Apple Silicon)
923 # ---------------------------------------------------------------------------
924
925 _SYSTEM_PROFILER_JSON = json.dumps(
926 {
927 "SPDisplaysDataType": [
928 {
929 "_name": "Apple M4 Max",
930 "spdisplays_ndrvs": [
931 {
932 "_name": "Built-in Retina Display",
933 "spdisplays_resolution": "3024 x 1964 Retina",
934 },
935 {
936 "_name": "LG UltraFine 4K",
937 "spdisplays_resolution": "3840 x 2160 (UHD 4K)",
938 },
939 ],
940 }
941 ]
942 }
943 ).encode()
944
945 # Reflects the actual macOS Tahoe / M1 Pro format seen in the field:
946 # resolution keys are underscore-prefixed (_spdisplays_resolution, _spdisplays_pixels)
947 _SYSTEM_PROFILER_JSON_TAHOE = json.dumps(
948 {
949 "SPDisplaysDataType": [
950 {
951 "_name": "Apple M1 Pro",
952 "spdisplays_ndrvs": [
953 {
954 "_name": "Color LCD",
955 "_spdisplays_pixels": "3456 x 2234",
956 "_spdisplays_resolution": "1728 x 1117 @ 120.00Hz",
957 "spdisplays_pixelresolution": "spdisplays_3456x2234Retina",
958 "spdisplays_connection_type": "spdisplays_internal",
959 },
960 ],
961 }
962 ]
963 }
964 ).encode()
965
966
967 def test_get_display_name_from_system_profiler_match(monkeypatch, patch_subprocess):
968 """get_display_name_from_system_profiler() returns name matched by resolution."""
969 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
970 patch_subprocess.output[
971 "system_profilerSPDisplaysDataType-json"
972 ] = _SYSTEM_PROFILER_JSON
973
974 result = get_display_name_from_system_profiler(3840, 2160)
975
976 assert result == "LG UltraFine 4K"
977
978
979 def test_get_display_name_from_system_profiler_builtin(monkeypatch, patch_subprocess):
980 """get_display_name_from_system_profiler() matches built-in display by resolution."""
981 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
982 patch_subprocess.output[
983 "system_profilerSPDisplaysDataType-json"
984 ] = _SYSTEM_PROFILER_JSON
985
986 result = get_display_name_from_system_profiler(3024, 1964)
987
988 assert result == "Built-in Retina Display"
989
990
991 def test_get_display_name_from_system_profiler_no_match(monkeypatch, patch_subprocess):
992 """get_display_name_from_system_profiler() returns None when resolution not found."""
993 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
994 patch_subprocess.output[
995 "system_profilerSPDisplaysDataType-json"
996 ] = _SYSTEM_PROFILER_JSON
997
998 result = get_display_name_from_system_profiler(1920, 1080)
999
1000 assert result is None
1001
1002
1003 def test_get_display_name_from_system_profiler_hidpi_physical(
1004 monkeypatch, patch_subprocess
1005 ):
1006 """Physical (3840x2160) pixels match inside a HiDPI resolution string."""
1007 data = json.dumps(
1008 {
1009 "SPDisplaysDataType": [
1010 {
1011 "_name": "Apple M4 Max",
1012 "spdisplays_ndrvs": [
1013 {
1014 "_name": "LG UltraFine 5K",
1015 "spdisplays_resolution": "2560 x 1440 (3840 x 2160 HiDPI)",
1016 },
1017 ],
1018 }
1019 ]
1020 }
1021 ).encode()
1022 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1023 patch_subprocess.output["system_profilerSPDisplaysDataType-json"] = data
1024
1025 assert get_display_name_from_system_profiler(3840, 2160) == "LG UltraFine 5K"
1026
1027
1028 def test_get_display_name_from_system_profiler_hidpi_logical(
1029 monkeypatch, patch_subprocess
1030 ):
1031 """Logical (2560x1440) pixels match inside a HiDPI resolution string."""
1032 data = json.dumps(
1033 {
1034 "SPDisplaysDataType": [
1035 {
1036 "_name": "Apple M4 Max",
1037 "spdisplays_ndrvs": [
1038 {
1039 "_name": "LG UltraFine 5K",
1040 "spdisplays_resolution": "2560 x 1440 (3840 x 2160 HiDPI)",
1041 },
1042 ],
1043 }
1044 ]
1045 }
1046 ).encode()
1047 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1048 patch_subprocess.output["system_profilerSPDisplaysDataType-json"] = data
1049
1050 assert get_display_name_from_system_profiler(2560, 1440) == "LG UltraFine 5K"
1051
1052
1053 def test_get_display_name_from_system_profiler_pixelresolution_key(
1054 monkeypatch, patch_subprocess
1055 ):
1056 """spdisplays_pixelresolution key (format 'spdisplays_3840x2160') is used as fallback."""
1057 data = json.dumps(
1058 {
1059 "SPDisplaysDataType": [
1060 {
1061 "_name": "Apple M4 Max",
1062 "spdisplays_ndrvs": [
1063 {
1064 "_name": "ASUS ProArt PA329CV",
1065 "spdisplays_pixelresolution": "spdisplays_3840x2160",
1066 },
1067 ],
1068 }
1069 ]
1070 }
1071 ).encode()
1072 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1073 patch_subprocess.output["system_profilerSPDisplaysDataType-json"] = data
1074
1075 assert get_display_name_from_system_profiler(3840, 2160) == "ASUS ProArt PA329CV"
1076
1077
1078 def test_get_display_name_from_system_profiler_native_resolution_key(
1079 monkeypatch, patch_subprocess
1080 ):
1081 """spdisplays_native-resolution key is checked when spdisplays_resolution missing."""
1082 data = json.dumps(
1083 {
1084 "SPDisplaysDataType": [
1085 {
1086 "_name": "Apple M4 Max",
1087 "spdisplays_ndrvs": [
1088 {
1089 "_name": "Dell U2723QE",
1090 "spdisplays_native-resolution": "3840 x 2160",
1091 },
1092 ],
1093 }
1094 ]
1095 }
1096 ).encode()
1097 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1098 patch_subprocess.output["system_profilerSPDisplaysDataType-json"] = data
1099
1100 assert get_display_name_from_system_profiler(3840, 2160) == "Dell U2723QE"
1101
1102
1103 def test_get_display_name_from_system_profiler_tahoe_underscore_resolution(
1104 monkeypatch, patch_subprocess
1105 ):
1106 """macOS Tahoe uses _spdisplays_resolution (underscore prefix) for the resolution key."""
1107 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1108 patch_subprocess.output[
1109 "system_profilerSPDisplaysDataType-json"
1110 ] = _SYSTEM_PROFILER_JSON_TAHOE
1111
1112 # 1728x1117 is what Argyll (dispwin) reports; it matches _spdisplays_resolution
1113 result = get_display_name_from_system_profiler(1728, 1117)
1114
1115 assert result == "Color LCD"
1116
1117
1118 def test_get_display_name_from_system_profiler_tahoe_no_match(
1119 monkeypatch, patch_subprocess
1120 ):
1121 """_spdisplays_pixels (physical 2x) does not match Argyll logical resolution."""
1122 monkeypatch.setattr("DisplayCAL.edid.subprocess", patch_subprocess)
1123 patch_subprocess.output[
1124 "system_profilerSPDisplaysDataType-json"
1125 ] = _SYSTEM_PROFILER_JSON_TAHOE
1126
1127 # Physical pixel count (3456x2234) — Argyll reports logical (1728x1117),
1128 # so asking for physical should NOT match via _spdisplays_resolution but WILL
1129 # match via _spdisplays_pixels.
1130 result = get_display_name_from_system_profiler(3456, 2234)
1131
1132 assert result == "Color LCD"