Skip to content

Commit 1553f0c

Browse files
Mickaël SchoentgenBoboTiG
authored andcommitted
Tests: restructuration
1 parent bc5095e commit 1553f0c

File tree

8 files changed

+287
-174
lines changed

8 files changed

+287
-174
lines changed

CHANGES.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
3.1.0 (2017-xx-xx)
1+
3.1.0 (2017-08-xx)
22
==================
33

4+
base.py
5+
-------
6+
- Moved `ScreenShot` class to screenshot.py
7+
48
darwin.py
59
---------
610
- Removed `get_infinity()` function

mss/darwin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def resize(data, monitor):
208208
end = start + monitor['width'] * 4
209209
cropped.extend(data[start:end])
210210

211-
cropped.extend(b'\00' * (monitor['width'] - rounded_width) * 4)
211+
if len(cropped) < monitor['width'] * monitor['width'] * 4:
212+
cropped.extend(b'\00' * (monitor['width'] - rounded_width) * 4)
212213

213214
return cropped

tests/conftest.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
# coding: utf-8
22

3+
from __future__ import print_function
4+
5+
import glob
36
import os
47

58
import pytest
69

7-
from mss import mss
10+
import mss
811

912

1013
def pytest_addoption(parser):
1114
txt = 'Display to use (examples: ":0.1", ":0" [default])'
1215
parser.addoption('--display', action='store', default=':0', help=txt)
1316

1417

18+
def purge_files():
19+
""" Remove all generated files from previous runs. """
20+
21+
for fname in glob.glob('*.png'):
22+
print('Deleting {0} ...'.format(fname))
23+
os.unlink(fname)
24+
25+
for fname in glob.glob('*.png.old'):
26+
print('Deleting {0} ...'.format(fname))
27+
os.unlink(fname)
28+
29+
30+
@pytest.fixture(scope='module', autouse=True)
31+
def before_tests(request):
32+
request.addfinalizer(purge_files)
33+
34+
1535
@pytest.fixture(scope='session')
1636
def display(request):
1737
return request.config.getoption('--display')
@@ -20,10 +40,10 @@ def display(request):
2040
@pytest.fixture(scope='module')
2141
def sct(display):
2242
try:
23-
# display keyword is only for GNU/Linux
24-
return mss(display=display)
43+
# `display` keyword is only for GNU/Linux
44+
return mss.mss(display=display)
2545
except TypeError:
26-
return mss()
46+
return mss.mss()
2747

2848

2949
@pytest.fixture(scope='session')

tests/test_gnu_linux.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# coding: utf-8
2+
3+
import ctypes.util
4+
import platform
5+
import sys
6+
7+
import pytest
8+
9+
import mss
10+
from mss.base import MSSBase
11+
from mss.exception import ScreenShotError
12+
13+
14+
if platform.system().lower() != 'linux':
15+
pytestmark = pytest.mark.skip
16+
17+
18+
TEXT = str if sys.version[0] > '2' else unicode
19+
PY3 = sys.version[0] > '2'
20+
21+
22+
def test_factory_systems(monkeypatch):
23+
"""
24+
Here, we are testing all systems.
25+
26+
Too hard to maintain the test for all platforms,
27+
so est only on GNU/Linux.
28+
"""
29+
30+
# GNU/Linux
31+
monkeypatch.setattr(platform, 'system', lambda: 'LINUX')
32+
sct = mss.mss()
33+
assert isinstance(sct, MSSBase)
34+
35+
# macOS
36+
monkeypatch.setattr(platform, 'system', lambda: 'Darwin')
37+
with pytest.raises(ScreenShotError) as exc:
38+
mss.mss()
39+
if not PY3:
40+
error = exc.value[1]['self']
41+
else:
42+
error = exc.value.args[1]['self']
43+
assert isinstance(error, MSSBase)
44+
45+
# Windows
46+
monkeypatch.setattr(platform, 'system', lambda: 'wInDoWs')
47+
with pytest.raises(ValueError):
48+
# wintypes.py:19: ValueError: _type_ 'v' not supported
49+
mss.mss()
50+
51+
52+
def test_implementation(monkeypatch):
53+
import mss
54+
55+
# Bad `display` type
56+
mss.mss(display=TEXT(':0'))
57+
58+
# TODO: SEGFAULT
59+
#with pytest.raises(ScreenShotError):
60+
# mss.mss(display=text('0'))
61+
62+
# No `DISPLAY` in envars
63+
monkeypatch.delenv('DISPLAY')
64+
with pytest.raises(ScreenShotError):
65+
mss.mss()
66+
monkeypatch.undo()
67+
68+
# No `X11` library
69+
x11 = ctypes.util.find_library('X11')
70+
monkeypatch.setattr(ctypes.util, 'find_library', lambda x: None)
71+
with pytest.raises(ScreenShotError):
72+
mss.mss()
73+
monkeypatch.undo()
74+
75+
def find_lib(lib):
76+
"""
77+
Returns None to emulate no Xrandr library.
78+
Returns the previous found X11 library else.
79+
80+
It is a naive approach, but works for now.
81+
"""
82+
83+
if lib == 'Xrandr':
84+
return None
85+
return x11
86+
87+
# No `Xrandr` library
88+
monkeypatch.setattr(ctypes.util, 'find_library', find_lib)
89+
with pytest.raises(ScreenShotError):
90+
mss.mss()
91+
monkeypatch.undo()
92+
93+
# Bad display data
94+
import mss.linux
95+
monkeypatch.setattr(mss.linux, 'Display', lambda: None)
96+
with pytest.raises(TypeError):
97+
mss.mss()

tests/test_implementation.py

Lines changed: 6 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# coding: utf-8
22

3-
import ctypes.util
43
import os
54
import os.path
65
import platform
@@ -13,15 +12,8 @@
1312
from mss.exception import ScreenShotError
1413
from mss.screenshot import ScreenShot
1514

16-
try:
17-
import numpy
18-
except ImportError:
19-
numpy = None
2015

21-
try:
22-
from PIL import Image
23-
except ImportError:
24-
Image = None
16+
PY3 = sys.version[0] > '2'
2517

2618

2719
class MSS0(MSSBase):
@@ -71,61 +63,6 @@ def test_repr(sct):
7163
assert repr(img) == repr(ref)
7264

7365

74-
@pytest.mark.skipif(
75-
platform.system().lower() != 'darwin',
76-
reason='Specific to macOS.')
77-
def test_repr():
78-
from mss.darwin import CGSize, CGPoint, CGRect
79-
80-
point = CGPoint(2.0, 1.0)
81-
ref = CGPoint()
82-
ref.x = 2.0
83-
ref.y = 1.0
84-
assert repr(point) == repr(ref)
85-
86-
size = CGSize(2.0, 1.0)
87-
ref = CGSize()
88-
ref.width = 2.0
89-
ref.height = 1.0
90-
assert repr(size) == repr(ref)
91-
92-
rect = CGRect(point, size)
93-
ref = CGRect()
94-
ref.origin.x = 2.0
95-
ref.origin.y = 1.0
96-
ref.size.width = 2.0
97-
ref.size.height = 1.0
98-
assert repr(rect) == repr(ref)
99-
100-
101-
@pytest.mark.skipif(
102-
numpy is None,
103-
reason='Numpy module not available.')
104-
def test_numpy(sct):
105-
box = {'top': 0, 'left': 0, 'width': 10, 'height': 10}
106-
img = numpy.array(sct.grab(box))
107-
assert len(img) == 10
108-
109-
110-
@pytest.mark.skipif(
111-
Image is None,
112-
reason='PIL module not available.')
113-
def test_pil(sct):
114-
box = {'top': 0, 'left': 0, 'width': 10, 'height': 10}
115-
sct_img = sct.grab(box)
116-
117-
img = Image.frombytes('RGB', sct_img.size, sct_img.rgb)
118-
assert img.mode == 'RGB'
119-
assert img.size == sct_img.size
120-
121-
for x in range(10):
122-
for y in range(10):
123-
assert img.getpixel((x, y)) == sct_img.pixel(x, y)
124-
125-
img.save('box.png')
126-
assert os.path.isfile('box.png')
127-
128-
12966
def test_factory_basics(monkeypatch):
13067
# Current system
13168
sct = mss.mss()
@@ -135,112 +72,13 @@ def test_factory_basics(monkeypatch):
13572
monkeypatch.setattr(platform, 'system', lambda: 'Chuck Norris')
13673
with pytest.raises(ScreenShotError) as exc:
13774
mss.mss()
138-
assert exc.value[0] == 'System not (yet?) implemented.'
139-
140-
141-
@pytest.mark.skipif(
142-
platform.system().lower() != 'linux',
143-
reason='Too hard to maintain the test for all platforms.')
144-
def test_factory_systems(monkeypatch):
145-
""" Here, we are testing all systems. """
146-
147-
# GNU/Linux
148-
monkeypatch.setattr(platform, 'system', lambda: 'LINUX')
149-
sct = mss.mss()
150-
assert isinstance(sct, MSSBase)
151-
152-
# macOS
153-
monkeypatch.setattr(platform, 'system', lambda: 'Darwin')
154-
with pytest.raises(ScreenShotError) as exc:
155-
mss.mss()
156-
assert isinstance(exc.value[1]['self'], MSSBase)
157-
158-
# Windows
159-
monkeypatch.setattr(platform, 'system', lambda: 'wInDoWs')
160-
with pytest.raises(ValueError):
161-
# wintypes.py:19: ValueError: _type_ 'v' not supported
162-
mss.mss()
75+
if not PY3:
76+
error = exc.value[0]
77+
else:
78+
error = exc.value.args[0]
79+
assert error == 'System not (yet?) implemented.'
16380

16481

16582
def test_python_call():
16683
# __import__('mss.__main__')
16784
pytest.skip('Dunno how to test mss/__main__.py.')
168-
169-
170-
@pytest.mark.skipif(
171-
platform.system().lower() != 'linux',
172-
reason='Specific to GNU/Linux.')
173-
def test_gnu_linux(monkeypatch):
174-
text = str if sys.version[0] > '2' else unicode
175-
176-
# Bad `display` type
177-
mss.mss(display=text(':0'))
178-
179-
# TODO: SEGFAULT
180-
#with pytest.raises(ScreenShotError):
181-
# mss.mss(display=text('0'))
182-
183-
# No `DISPLAY` in envars
184-
monkeypatch.delenv('DISPLAY')
185-
with pytest.raises(ScreenShotError):
186-
mss.mss()
187-
monkeypatch.undo()
188-
189-
# No `X11` library
190-
x11 = ctypes.util.find_library('X11')
191-
monkeypatch.setattr(ctypes.util, 'find_library', lambda x: None)
192-
with pytest.raises(ScreenShotError):
193-
mss.mss()
194-
monkeypatch.undo()
195-
196-
def find_lib(lib):
197-
"""
198-
Returns None to emulate no Xrandr library.
199-
Returns the previous found X11 library else.
200-
201-
It is a naive approach, but works for now.
202-
"""
203-
204-
if lib == 'Xrandr':
205-
return None
206-
return x11
207-
208-
# No `Xrandr` library
209-
monkeypatch.setattr(ctypes.util, 'find_library', find_lib)
210-
with pytest.raises(ScreenShotError):
211-
mss.mss()
212-
monkeypatch.undo()
213-
214-
# Bad display data
215-
import mss.linux
216-
monkeypatch.setattr(mss.linux, 'Display', lambda: None)
217-
mss.mss()
218-
219-
220-
@pytest.mark.skipif(
221-
platform.system().lower() != 'darwin',
222-
reason='Specific to macOS.')
223-
def test_macos(monkeypatch):
224-
# No `CoreGraphics` library
225-
monkeypatch.setattr(ctypes.util, 'find_library', lambda x: None)
226-
with pytest.raises(ScreenShotError):
227-
mss.mss()
228-
monkeypatch.undo()
229-
230-
with mss.mss() as sct:
231-
# Test monitor's rotation
232-
original = sct.monitors[1]
233-
monkeypatch.setattr(sct.core, 'CGDisplayRotation',
234-
lambda x: -90.0)
235-
sct._monitors = []
236-
modified = sct.monitors[1]
237-
assert original['width'] == modified['height']
238-
assert original['height'] == modified['width']
239-
monkeypatch.undo()
240-
241-
# Test bad data retreival
242-
monkeypatch.setattr(sct.core, 'CGWindowListCreateImage',
243-
lambda *args: None)
244-
with pytest.raises(ScreenShotError):
245-
sct.grab(sct.monitors[1])
246-
monkeypatch.undo()

0 commit comments

Comments
 (0)