11# coding: utf-8
22
3- import ctypes .util
43import os
54import os .path
65import platform
1312from mss .exception import ScreenShotError
1413from 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
2719class 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-
12966def 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
16582def 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