Skip to content

Commit 4bc23de

Browse files
committed
Tests: a lot of tests added for better coverage
1 parent 2e0fb99 commit 4bc23de

File tree

6 files changed

+222
-9
lines changed

6 files changed

+222
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ before_install:
1717
- xpra --xvfb="Xorg +extension RANDR -config `pwd`/tests/res/dummy.xorg.conf -logfile ${HOME}/.xpra/xorg.log" start :42
1818

1919
install:
20-
- pip install flake8 pylint
20+
- pip install flake8 numpy pylint
2121
- python setup.py install
2222

2323
script:

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ History:
55
dev 2017/xx/xx
66
- add the 'Say Thanks' button
77
- doc: several fixes (fix #22)
8+
- tests: a lot of tests added for better coverage
89
- MSS: possibility to use custom class to handle screen shot data
910
- Mac: handle screenshot's width not divisible by 16 (fix #14, #19, #21)
1011

mss/linux.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def __init__(self, display=None):
149149
self._set_argtypes()
150150
self._set_restypes()
151151

152+
# TODO: check if `display` is openable, else SEGFAULT
152153
self.display = self.xlib.XOpenDisplay(display)
153154
try:
154155
self.display.contents

tests/test_get_pixels.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88

99
def test_grab_monitor(sct):
10-
mon1 = sct.monitors[1]
11-
image = sct.grab(mon1)
12-
assert isinstance(image, ScreenShot)
13-
assert isinstance(image.raw, bytearray)
14-
assert isinstance(image.rgb, bytes)
10+
for mon in sct.monitors:
11+
image = sct.grab(mon)
12+
assert isinstance(image, ScreenShot)
13+
assert isinstance(image.raw, bytearray)
14+
assert isinstance(image.rgb, bytes)
1515

1616

1717
def test_grab_part_of_screen(sct):

tests/test_implementation.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# coding: utf-8
2+
3+
import ctypes.util
4+
import os
5+
import os.path
6+
import platform
7+
import sys
8+
9+
import pytest
10+
11+
import mss
12+
from mss.base import MSSBase
13+
from mss.exception import ScreenShotError
14+
from mss.screenshot import ScreenShot
15+
16+
try:
17+
import numpy
18+
except ImportError:
19+
numpy = None
20+
21+
try:
22+
from PIL import Image
23+
except ImportError:
24+
Image = None
25+
26+
27+
class MSS0(MSSBase):
28+
""" Nothing implemented. """
29+
pass
30+
31+
32+
class MSS1(MSSBase):
33+
""" Emulate no monitors. """
34+
@property
35+
def monitors(self):
36+
return []
37+
38+
39+
class MSS2(MSSBase):
40+
""" Emulate one monitor. """
41+
@property
42+
def monitors(self):
43+
return [{'top': 0, 'left': 0, 'width': 10, 'height': 10}]
44+
45+
46+
def test_incomplete_class():
47+
# `monitors` property not implemented
48+
with pytest.raises(NotImplementedError):
49+
for filename in MSS0().save():
50+
assert os.path.isfile(filename)
51+
52+
# `monitors` property is empty
53+
with pytest.raises(ScreenShotError):
54+
for filename in MSS1().save():
55+
assert os.path.isfile(filename)
56+
57+
# `grab()` not implemented
58+
sct = MSS2()
59+
with pytest.raises(NotImplementedError):
60+
sct.grab(sct.monitors[0])
61+
62+
# Bad monitor
63+
with pytest.raises(ScreenShotError):
64+
sct.grab(sct.shot(mon=222))
65+
66+
67+
def test_repr(sct):
68+
box = {'top': 0, 'left': 0, 'width': 10, 'height': 10}
69+
img = sct.grab(box)
70+
ref = ScreenShot(bytearray(b'42'), box)
71+
assert repr(img) == repr(ref)
72+
73+
74+
@pytest.mark.skipif(
75+
numpy is None,
76+
reason='Numpy module not available.')
77+
def test_numpy(sct):
78+
box = {'top': 0, 'left': 0, 'width': 10, 'height': 10}
79+
img = numpy.array(sct.grab(box))
80+
assert len(img) == 10
81+
82+
83+
@pytest.mark.skipif(
84+
Image is None,
85+
reason='PIL module not available.')
86+
def test_pil(sct):
87+
box = {'top': 0, 'left': 0, 'width': 10, 'height': 10}
88+
sct_img = sct.grab(box)
89+
90+
img = Image.frombytes('RGB', sct_img.size, sct_img.rgb)
91+
assert img.mode == 'RGB'
92+
assert img.size == sct_img.size
93+
94+
for x in range(10):
95+
for y in range(10):
96+
assert img.getpixel((x, y)) == sct_img.pixel(x, y)
97+
98+
img.save('box.png')
99+
assert os.path.isfile('box.png')
100+
101+
102+
def test_factory_basics(monkeypatch):
103+
# Current system
104+
sct = mss.mss()
105+
assert isinstance(sct, MSSBase)
106+
107+
# Unknown
108+
monkeypatch.setattr(platform, 'system', lambda: 'Chuck Norris')
109+
with pytest.raises(ScreenShotError) as exc:
110+
mss.mss()
111+
assert exc.value[0] == 'System not (yet?) implemented.'
112+
113+
114+
@pytest.mark.skipif(
115+
platform.system().lower() != 'linux',
116+
reason='To hard to maintain the test for all platforms.')
117+
def test_factory_systems(monkeypatch):
118+
""" Here, we are testing all systems. """
119+
120+
# GNU/Linux
121+
monkeypatch.setattr(platform, 'system', lambda: 'LINUX')
122+
sct = mss.mss()
123+
assert isinstance(sct, MSSBase)
124+
125+
# macOS
126+
monkeypatch.setattr(platform, 'system', lambda: 'Darwin')
127+
with pytest.raises(ScreenShotError) as exc:
128+
mss.mss()
129+
assert isinstance(exc.value[1]['self'], MSSBase)
130+
131+
# Windows
132+
monkeypatch.setattr(platform, 'system', lambda: 'wInDoWs')
133+
with pytest.raises(ValueError):
134+
# wintypes.py:19: ValueError: _type_ 'v' not supported
135+
mss.mss()
136+
137+
138+
def test_python_call():
139+
# __import__('mss.__main__')
140+
pytest.skip('Dunno how to test mss/__main__.py.')
141+
142+
143+
@pytest.mark.skipif(
144+
platform.system().lower() != 'linux',
145+
reason='GNU/Linux test only.')
146+
def test_gnu_linux(monkeypatch):
147+
text = str if sys.version[0] > '2' else unicode
148+
149+
# Bad `display` type
150+
mss.mss(display=text(':0'))
151+
152+
# TODO: SEGFAULT
153+
#with pytest.raises(ScreenShotError):
154+
# mss.mss(display=text('0'))
155+
156+
# No `DISPLAY` in envars
157+
monkeypatch.delenv('DISPLAY')
158+
with pytest.raises(ScreenShotError):
159+
mss.mss()
160+
monkeypatch.undo()
161+
162+
# No `X11` library
163+
x11 = ctypes.util.find_library('X11')
164+
monkeypatch.setattr(ctypes.util, 'find_library', lambda x: None)
165+
with pytest.raises(ScreenShotError):
166+
mss.mss()
167+
monkeypatch.undo()
168+
169+
def find_lib(lib):
170+
"""
171+
Returns None to emulate no Xrandr library.
172+
Returns the previous found X11 library else.
173+
174+
It is a naive approach, but works for now.
175+
"""
176+
177+
if lib == 'Xrandr':
178+
return None
179+
return x11
180+
181+
# No `Xrandr` library
182+
monkeypatch.setattr(ctypes.util, 'find_library', find_lib)
183+
with pytest.raises(ScreenShotError):
184+
mss.mss()
185+
monkeypatch.undo()
186+
187+
# Bad display data
188+
import mss.linux
189+
monkeypatch.setattr(mss.linux, 'Display', lambda: None)
190+
mss.mss()

tests/test_save.py

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

3-
from os.path import isfile
3+
import os.path
4+
import mss
45

56

67
def test_at_least_2_monitors(sct):
@@ -9,5 +10,25 @@ def test_at_least_2_monitors(sct):
910

1011

1112
def test_files_exist(sct):
12-
for filename in sct.save(mon=0):
13-
assert isfile(filename)
13+
for filename in sct.save():
14+
assert os.path.isfile(filename)
15+
16+
assert os.path.isfile(sct.shot())
17+
18+
sct.shot(mon=-1, output='fullscreen.png')
19+
assert os.path.isfile('fullscreen.png')
20+
21+
22+
def test_callback():
23+
24+
def on_exists(fname):
25+
if os.path.isfile(fname):
26+
new_file = fname + '.old'
27+
os.rename(fname, new_file)
28+
29+
with mss.mss() as sct:
30+
filename = sct.shot(mon=0, output='mon0.png', callback=on_exists)
31+
assert os.path.isfile(filename)
32+
33+
filename = sct.shot(output='mon1.png', callback=on_exists)
34+
assert os.path.isfile(filename)

0 commit comments

Comments
 (0)