| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
from _pytest.monkeypatch import MonkeyPatch |
| 8 |
|
| 9 |
from DisplayCAL import real_display_size_mm, argyll, config |
| 10 |
from DisplayCAL.dev.mocks import check_call |
| 11 |
from tests.data.display_data import DisplayData |
| 12 |
|
| 13 |
try: |
| 14 |
from tests.data.fake_dbus import FakeDBusObject |
| 15 |
except ImportError: |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture(scope="function") |
| 20 |
def patch_subprocess_on_rdsmm(monkeypatch, patch_subprocess): |
| 21 |
"""Patch DisplayCAL.real_display_size_mm.subprocess to return whatever we want.""" |
| 22 |
monkeypatch.setattr("DisplayCAL.real_display_size_mm.subprocess", patch_subprocess) |
| 23 |
yield patch_subprocess |
| 24 |
|
| 25 |
|
| 26 |
def test_real_display_size_mm(clear_displays): |
| 27 |
"""Test real_display_size_mm() function.""" |
| 28 |
with check_call( |
| 29 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 30 |
): |
| 31 |
with check_call(config, "getcfg", DisplayData.CFG_DATA, call_count=2): |
| 32 |
display_size = real_display_size_mm.real_display_size_mm(0) |
| 33 |
assert display_size != (0, 0) |
| 34 |
assert display_size[0] > 1 |
| 35 |
assert display_size[1] > 1 |
| 36 |
|
| 37 |
|
| 38 |
def test_xrandr_output_x_id_1(clear_displays): |
| 39 |
"""Test GetXRandROutputXID() function.""" |
| 40 |
with check_call( |
| 41 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 42 |
): |
| 43 |
with check_call(config, "getcfg", DisplayData.CFG_DATA, call_count=2): |
| 44 |
result = real_display_size_mm.get_xrandr_output_xid(0) |
| 45 |
assert result != 0 |
| 46 |
|
| 47 |
|
| 48 |
def test_enumerate_displays(clear_displays): |
| 49 |
"""Test enumerate_displays() function.""" |
| 50 |
with check_call( |
| 51 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 52 |
): |
| 53 |
result = real_display_size_mm.enumerate_displays() |
| 54 |
assert result[0]["description"] != "" |
| 55 |
assert result[0]["edid"] != "" |
| 56 |
assert result[0]["icc_profile_atom_id"] != "" |
| 57 |
assert result[0]["icc_profile_output_atom_id"] != "" |
| 58 |
assert result[0]["name"] != "" |
| 59 |
assert result[0]["output"] != "" |
| 60 |
assert result[0]["pos"] != "" |
| 61 |
assert result[0]["ramdac_screen"] != "" |
| 62 |
assert result[0]["screen"] != "" |
| 63 |
assert result[0]["size"] != "" |
| 64 |
assert isinstance(result[0]["size"][0], int) |
| 65 |
assert isinstance(result[0]["size"][1], int) |
| 66 |
assert result[0]["size_mm"] != "" |
| 67 |
assert isinstance(result[0]["size_mm"][0], int) |
| 68 |
assert isinstance(result[0]["size_mm"][1], int) |
| 69 |
assert result[0]["x11_screen"] != "" |
| 70 |
# assert result[0]["xrandr_name"] != "" |
| 71 |
assert real_display_size_mm._displays is not None |
| 72 |
|
| 73 |
|
| 74 |
def test__enumerate_displays_dispwin_path_is_none(monkeypatch, clear_displays): |
| 75 |
"""_enumerate_displays() dispwin path is None returns empty list.""" |
| 76 |
monkeypatch.setattr( |
| 77 |
"DisplayCAL.real_display_size_mm.argyll.get_argyll_util", lambda x: None |
| 78 |
) |
| 79 |
result = real_display_size_mm._enumerate_displays() |
| 80 |
assert result == [] |
| 81 |
|
| 82 |
|
| 83 |
def test__enumerate_displays_uses_argyll_dispwin( |
| 84 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 85 |
): |
| 86 |
"""_enumerate_displays() uses dispwin.""" |
| 87 |
PatchedSubprocess = patch_subprocess_on_rdsmm |
| 88 |
PatchedSubprocess.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_1 |
| 89 |
PatchedArgyll = patch_argyll_util |
| 90 |
assert PatchedSubprocess.passed_args == [] |
| 91 |
assert PatchedSubprocess.passed_kwargs == {} |
| 92 |
result = real_display_size_mm._enumerate_displays() |
| 93 |
# assert result == DisplayData.DISPWIN_OUTPUT_1 |
| 94 |
assert PatchedSubprocess.passed_args != [] |
| 95 |
assert "dispwin" in PatchedSubprocess.passed_args[0][0] |
| 96 |
assert PatchedSubprocess.passed_args[0][1] == "-v" |
| 97 |
assert PatchedSubprocess.passed_args[0][2] == "-d0" |
| 98 |
|
| 99 |
|
| 100 |
def test__enumerate_displays_uses_argyll_dispwin_output_1( |
| 101 |
clear_displays, patch_subprocess_on_rdsmm, patch_argyll_util |
| 102 |
): |
| 103 |
"""_enumerate_displays() uses dispwin.""" |
| 104 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_1 |
| 105 |
result = real_display_size_mm._enumerate_displays() |
| 106 |
assert isinstance(result, list) |
| 107 |
assert len(result) == 1 |
| 108 |
assert ( |
| 109 |
result[0]["description"] |
| 110 |
== b"Built-in Retina Display, at 0, 0, width 1728, height 1117 (Primary Display)" |
| 111 |
) |
| 112 |
assert result[0]["name"] == b"Built-in Retina Display" |
| 113 |
assert result[0]["size"] == (1728, 1117) |
| 114 |
assert result[0]["pos"] == (0, 0) |
| 115 |
|
| 116 |
|
| 117 |
def test__enumerate_displays_uses_argyll_dispwin_output_2( |
| 118 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 119 |
): |
| 120 |
"""_enumerate_displays() uses dispwin.""" |
| 121 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_2 |
| 122 |
result = real_display_size_mm._enumerate_displays() |
| 123 |
assert isinstance(result, list) |
| 124 |
assert len(result) == 2 |
| 125 |
assert ( |
| 126 |
result[0]["description"] |
| 127 |
== b"Built-in Retina Display, at 0, 0, width 1728, height 1117 (Primary Display)" |
| 128 |
) |
| 129 |
assert result[0]["name"] == b"Built-in Retina Display" |
| 130 |
assert result[0]["size"] == (1728, 1117) |
| 131 |
assert result[0]["pos"] == (0, 0) |
| 132 |
assert ( |
| 133 |
result[1]["description"] |
| 134 |
== b"DELL U2720Q, at 1728, -575, width 3008, height 1692" |
| 135 |
) |
| 136 |
assert result[1]["name"] == b"DELL U2720Q" |
| 137 |
assert result[1]["size"] == (3008, 1692) |
| 138 |
assert result[1]["pos"] == (1728, -575) |
| 139 |
|
| 140 |
|
| 141 |
def test__enumerate_displays_uses_argyll_dispwin_output_6( |
| 142 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 143 |
): |
| 144 |
"""_enumerate_displays() uses dispwin.""" |
| 145 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_7 |
| 146 |
result = real_display_size_mm._enumerate_displays() |
| 147 |
assert isinstance(result, list) |
| 148 |
assert len(result) == 2 |
| 149 |
assert ( |
| 150 |
result[0]["description"] |
| 151 |
== b"Built-in Retina Display, at 0, 0, width 1728, height 1117 (Primary Display)" |
| 152 |
) |
| 153 |
assert result[0]["name"] == b"Built-in Retina Display" |
| 154 |
assert result[0]["size"] == (1728, 1117) |
| 155 |
assert result[0]["pos"] == (0, 0) |
| 156 |
assert ( |
| 157 |
result[1]["description"] |
| 158 |
== b"DELL UP2516D, at -2560, -323, width 2560, height 1440" |
| 159 |
) |
| 160 |
assert result[1]["name"] == b"DELL UP2516D" |
| 161 |
assert result[1]["size"] == (2560, 1440) |
| 162 |
assert result[1]["pos"] == (-2560, -323) |
| 163 |
|
| 164 |
|
| 165 |
def test__enumerate_displays_without_a_proper_dispwin_output_missing_lines( |
| 166 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 167 |
): |
| 168 |
"""_enumerate_displays() return empty list when dispwin returns no usable data.""" |
| 169 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_3 |
| 170 |
result = real_display_size_mm._enumerate_displays() |
| 171 |
assert isinstance(result, list) |
| 172 |
assert len(result) == 0 |
| 173 |
|
| 174 |
|
| 175 |
def test__enumerate_displays_with_argyll_3_1_style_output( |
| 176 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 177 |
): |
| 178 |
"""_enumerate_displays() can enumerate ArgyllCMS 3.1.0 dispwin output format.""" |
| 179 |
from DisplayCAL import localization as lang |
| 180 |
|
| 181 |
lang.init() |
| 182 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_6 |
| 183 |
result = real_display_size_mm._enumerate_displays() |
| 184 |
assert result[0]["name"] == b"Monitor 1, Output Virtual-1" |
| 185 |
assert ( |
| 186 |
result[0]["description"] |
| 187 |
== b"Monitor 1, Output Virtual-1 at 0, 0, width 1920, height 1080" |
| 188 |
) |
| 189 |
assert result[0]["size"] == (1920, 1080) |
| 190 |
assert result[0]["pos"] == (0, 0) |
| 191 |
|
| 192 |
|
| 193 |
def test__enumerate_displays_without_a_proper_dispwin_output_with_partial_match( |
| 194 |
patch_subprocess_on_rdsmm, patch_argyll_util |
| 195 |
): |
| 196 |
"""_enumerate_displays() return empty list when dispwin returns no usable data.""" |
| 197 |
from DisplayCAL import localization as lang |
| 198 |
|
| 199 |
lang.init() |
| 200 |
patch_subprocess_on_rdsmm.output["dispwin-v-d0"] = DisplayData.DISPWIN_OUTPUT_5 |
| 201 |
with pytest.raises(ValueError) as cm: |
| 202 |
result = real_display_size_mm._enumerate_displays() |
| 203 |
assert str(cm.value) == ( |
| 204 |
"An internal error occurred.\n" |
| 205 |
"Error code: -1\n" |
| 206 |
"Error message: dispwin returns no usable data while enumerating displays." |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
def test_get_display(clear_displays): |
| 211 |
"""Test DisplayCAL.real_display_size_mm.get_display() function.""" |
| 212 |
with check_call( |
| 213 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 214 |
): |
| 215 |
with check_call(config, "getcfg", DisplayData.CFG_DATA, call_count=2): |
| 216 |
display = real_display_size_mm.get_display() |
| 217 |
assert real_display_size_mm._displays is not None |
| 218 |
assert isinstance(display, dict) |
| 219 |
|
| 220 |
|
| 221 |
def test_get_x_display(clear_displays): |
| 222 |
"""Test DisplayCAL.real_display_size_mm.get_x_display() function.""" |
| 223 |
with check_call( |
| 224 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 225 |
): |
| 226 |
with check_call(config, "getcfg", DisplayData.CFG_DATA, call_count=2): |
| 227 |
display = real_display_size_mm.get_x_display(0) |
| 228 |
assert isinstance(display, tuple) |
| 229 |
assert len(display) == 3 |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.parametrize( |
| 233 |
"function", |
| 234 |
( |
| 235 |
real_display_size_mm.get_x_icc_profile_atom_id, |
| 236 |
real_display_size_mm.get_x_icc_profile_output_atom_id, |
| 237 |
), |
| 238 |
) |
| 239 |
def test_get_x_icc_profile_atom_id(clear_displays, function) -> None: |
| 240 |
"""Test DisplayCAL.real_display_size_mm.get_x_icc_profile_atom_id() function.""" |
| 241 |
with check_call( |
| 242 |
real_display_size_mm, "_enumerate_displays", DisplayData.enumerate_displays() |
| 243 |
): |
| 244 |
with check_call(config, "getcfg", DisplayData.CFG_DATA, call_count=2): |
| 245 |
result = function(0) |
| 246 |
assert result is not None |
| 247 |
assert isinstance(result, int) |
| 248 |
|
| 249 |
|
| 250 |
@pytest.mark.skipif("fake_dbus" not in sys.modules, reason="requires the DBus library") |
| 251 |
def test_get_wayland_display(monkeypatch: MonkeyPatch) -> None: |
| 252 |
"""Test if wayland display is returned.""" |
| 253 |
with mock.patch.object(real_display_size_mm, "DBusObject", new=FakeDBusObject): |
| 254 |
display = real_display_size_mm.get_wayland_display(0, 0, 0, 0) |
| 255 |
assert display["xrandr_name"] == "DP-2" |
| 256 |
assert display["size_mm"] == (597, 336) |
| 257 |
|
| 258 |
|
| 259 |
def test_get_dispwin_output_dispwin_path_is_none_returns_empty_bytes( |
| 260 |
clear_displays, monkeypatch |
| 261 |
): |
| 262 |
"""get_dispwin_output() argyll.get_argyll_util("dispwin") returns None.""" |
| 263 |
|
| 264 |
def patched_get_argyll_util(*args): |
| 265 |
return None |
| 266 |
|
| 267 |
monkeypatch.setattr( |
| 268 |
"DisplayCAL.real_display_size_mm.argyll.get_argyll_util", patched_get_argyll_util |
| 269 |
) |
| 270 |
assert real_display_size_mm.get_dispwin_output() == b"" |
| 271 |
|
| 272 |
|
| 273 |
@pytest.mark.parametrize( |
| 274 |
"dispwin_data_file_name", [ |
| 275 |
"dispwin_output_1.txt", |
| 276 |
"dispwin_output_2.txt", |
| 277 |
"dispwin_output_3.txt", |
| 278 |
"dispwin_output_4.txt", |
| 279 |
] |
| 280 |
) |
| 281 |
def test_get_dispwin_output_returns_dispwin_output_as_bytes( |
| 282 |
clear_displays, data_files, patch_subprocess_on_rdsmm, dispwin_data_file_name |
| 283 |
): |
| 284 |
"""get_dispwin_output() returns bytes.""" |
| 285 |
# patch dispwin |
| 286 |
with open(data_files[dispwin_data_file_name], "rb") as dispwin_data_file: |
| 287 |
dispwin_data = dispwin_data_file.read() |
| 288 |
dispwin_path = argyll.get_argyll_util("dispwin") |
| 289 |
patch_subprocess_on_rdsmm.output[f"{dispwin_path}-v-d0"] = dispwin_data |
| 290 |
|
| 291 |
result = real_display_size_mm.get_dispwin_output() |
| 292 |
assert isinstance(result, bytes) |