Skip to content

Commit a3056ea

Browse files
Mickaël SchoentgenBoboTiG
authored andcommitted
tests: fix tests on macOS with Retina display
1 parent cecafb2 commit a3056ea

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ History:
55
5.0.1 2020/xx/xx
66
- produce wheels for Python 3 only
77
- tools: force write of file when saving a PNG file
8+
- tests: fix tests on macOS with Retina display
89

910
5.0.0 2019/12/31
1011
- removed support for Python 2.7

mss/tests/conftest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import glob
77
import os
88

9-
import mss
109
import pytest
10+
import mss
1111

1212

1313
def purge_files():
@@ -47,3 +47,15 @@ def raw():
4747
file = os.path.join(here, "res", "monitor-1024x768.raw")
4848
with open(file, "rb") as f:
4949
yield f.read()
50+
51+
52+
@pytest.fixture(scope="module")
53+
def pixel_ratio(sct):
54+
"""Get the pixel, used to adapt test checks."""
55+
# Grab a 1x1 screenshot
56+
region = {"top": 0, "left": 0, "width": 1, "height": 1}
57+
58+
# On macOS with Retina display,the width will be 2 instead of 1
59+
pixel_size = sct.grab(region).size[0]
60+
61+
return pixel_size

mss/tests/test_get_pixels.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ def test_grab_monitor(sct):
1616
assert isinstance(image.rgb, bytes)
1717

1818

19-
def test_grab_part_of_screen(sct):
19+
def test_grab_part_of_screen(sct, pixel_ratio):
2020
monitor = {"top": 160, "left": 160, "width": 160, "height": 160}
2121
image = sct.grab(monitor)
2222
assert isinstance(image, ScreenShot)
2323
assert isinstance(image.raw, bytearray)
2424
assert isinstance(image.rgb, bytes)
2525
assert image.top == 160
2626
assert image.left == 160
27-
assert image.width == 160
28-
assert image.height == 160
27+
assert image.width == 160 * pixel_ratio
28+
assert image.height == 160 * pixel_ratio
2929

3030

31-
def test_grab_part_of_screen_rounded(sct):
31+
def test_grab_part_of_screen_rounded(sct, pixel_ratio):
3232
monitor = {"top": 160, "left": 160, "width": 161, "height": 159}
3333
image = sct.grab(monitor)
3434
assert isinstance(image, ScreenShot)
3535
assert isinstance(image.raw, bytearray)
3636
assert isinstance(image.rgb, bytes)
3737
assert image.top == 160
3838
assert image.left == 160
39-
assert image.width == 161
40-
assert image.height == 159
39+
assert image.width == 161 * pixel_ratio
40+
assert image.height == 159 * pixel_ratio
4141

4242

4343
def test_grab_individual_pixels(sct):

mss/tests/test_implementation.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ def test_incomplete_class():
6363
sct.grab(sct.shot(mon=222))
6464

6565

66-
def test_repr(sct):
66+
def test_repr(sct, pixel_ratio):
6767
box = {"top": 0, "left": 0, "width": 10, "height": 10}
68+
expected_box = {
69+
"top": 0,
70+
"left": 0,
71+
"width": 10 * pixel_ratio,
72+
"height": 10 * pixel_ratio,
73+
}
6874
img = sct.grab(box)
69-
ref = ScreenShot(bytearray(b"42"), box)
75+
ref = ScreenShot(bytearray(b"42"), expected_box)
7076
assert repr(img) == repr(ref)
7177

7278

@@ -140,7 +146,7 @@ def test_entry_point(capsys, sct):
140146
assert out == "Coordinates syntax: top, left, width, height\n"
141147

142148

143-
def test_grab_with_tuple(sct):
149+
def test_grab_with_tuple(sct, pixel_ratio):
144150
left = 100
145151
top = 100
146152
right = 500
@@ -151,7 +157,7 @@ def test_grab_with_tuple(sct):
151157
# PIL like
152158
box = (left, top, right, lower)
153159
im = sct.grab(box)
154-
assert im.size == (width, height)
160+
assert im.size == (width * pixel_ratio, height * pixel_ratio)
155161

156162
# MSS like
157163
box2 = {"left": left, "top": top, "width": width, "height": height}
@@ -161,7 +167,7 @@ def test_grab_with_tuple(sct):
161167
assert im.rgb == im2.rgb
162168

163169

164-
def test_grab_with_tuple_percents(sct):
170+
def test_grab_with_tuple_percents(sct, pixel_ratio):
165171
monitor = sct.monitors[1]
166172
left = monitor["left"] + monitor["width"] * 5 // 100 # 5% from the left
167173
top = monitor["top"] + monitor["height"] * 5 // 100 # 5% from the top
@@ -173,7 +179,7 @@ def test_grab_with_tuple_percents(sct):
173179
# PIL like
174180
box = (left, top, right, lower)
175181
im = sct.grab(box)
176-
assert im.size == (width, height)
182+
assert im.size == (width * pixel_ratio, height * pixel_ratio)
177183

178184
# MSS like
179185
box2 = {"left": left, "top": top, "width": width, "height": height}

mss/tests/test_third_party.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222

2323
@pytest.mark.skipif(numpy is None, reason="Numpy module not available.")
24-
def test_numpy(sct):
24+
def test_numpy(sct, pixel_ratio):
2525
box = {"top": 0, "left": 0, "width": 10, "height": 10}
2626
img = numpy.array(sct.grab(box))
27-
assert len(img) == 10
27+
assert len(img) == 10 * pixel_ratio
2828

2929

3030
@pytest.mark.skipif(Image is None, reason="PIL module not available.")

0 commit comments

Comments
 (0)