forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_implementation.py
More file actions
183 lines (142 loc) · 4.6 KB
/
test_implementation.py
File metadata and controls
183 lines (142 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
"""
import os
import os.path
import platform
import sys
import mss
import mss.tools
from mss.base import MSSMixin
from mss.exception import ScreenShotError
from mss.screenshot import ScreenShot
import pytest
PY3 = sys.version[0] > "2"
class MSS0(MSSMixin):
""" Nothing implemented. """
pass
class MSS1(MSSMixin):
""" Emulate no monitors. """
@property
def monitors(self):
return []
class MSS2(MSSMixin):
""" Emulate one monitor. """
@property
def monitors(self):
return [{"top": 0, "left": 0, "width": 10, "height": 10}]
def test_incomplete_class():
# `monitors` property not implemented
with pytest.raises(NotImplementedError):
for filename in MSS0().save():
assert os.path.isfile(filename)
# `monitors` property is empty
with pytest.raises(ScreenShotError):
for filename in MSS1().save():
assert os.path.isfile(filename)
# `grab()` not implemented
sct = MSS2()
with pytest.raises(NotImplementedError):
sct.grab(sct.monitors[0])
# Bad monitor
with pytest.raises(ScreenShotError):
sct.grab(sct.shot(mon=222))
def test_repr(sct):
box = {"top": 0, "left": 0, "width": 10, "height": 10}
img = sct.grab(box)
ref = ScreenShot(bytearray(b"42"), box)
assert repr(img) == repr(ref)
def test_factory(monkeypatch):
# Current system
with mss.mss() as sct:
assert isinstance(sct, MSSMixin)
# Unknown
monkeypatch.setattr(platform, "system", lambda: "Chuck Norris")
with pytest.raises(ScreenShotError) as exc:
mss.mss()
monkeypatch.undo()
if not PY3:
error = exc.value[0]
else:
error = exc.value.args[0]
assert error == "System 'chuck norris' not (yet?) implemented."
def test_entry_point(capsys, sct):
from mss.__main__ import main
from datetime import datetime
for opt in ("-m", "--monitor"):
main([opt, "1"])
out, _ = capsys.readouterr()
assert out.endswith("monitor-1.png\n")
assert os.path.isfile("monitor-1.png")
os.remove("monitor-1.png")
for opt in zip(("-m 1", "--monitor=1"), ("-q", "--quiet")):
main(opt)
out, _ = capsys.readouterr()
assert not out
assert os.path.isfile("monitor-1.png")
os.remove("monitor-1.png")
fmt = "sct-{width}x{height}.png"
for opt in ("-o", "--out"):
main([opt, fmt])
filename = fmt.format(**sct.monitors[1])
out, _ = capsys.readouterr()
assert out.endswith(filename + "\n")
assert os.path.isfile(filename)
os.remove(filename)
fmt = "sct_{mon}-{date:%Y-%m-%d}.png"
for opt in ("-o", "--out"):
main(["-m 1", opt, fmt])
filename = fmt.format(mon=1, date=datetime.now())
out, _ = capsys.readouterr()
assert out.endswith(filename + "\n")
assert os.path.isfile(filename)
os.remove(filename)
coordinates = "2,12,40,67"
for opt in ("-c", "--coordinates"):
main([opt, coordinates])
filename = "sct-2x12_40x67.png"
out, _ = capsys.readouterr()
assert out.endswith(filename + "\n")
assert os.path.isfile(filename)
os.remove(filename)
coordinates = "2,12,40"
for opt in ("-c", "--coordinates"):
main([opt, coordinates])
out, _ = capsys.readouterr()
assert out == "Coordinates syntax: top, left, width, height\n"
def test_grab_with_tuple(sct):
left = 100
top = 100
right = 500
lower = 500
width = right - left # 400px width
height = lower - top # 400px height
# PIL like
box = (left, top, right, lower)
im = sct.grab(box)
assert im.size == (width, height)
# MSS like
box2 = {"left": left, "top": top, "width": width, "height": height}
im2 = sct.grab(box2)
assert im.size == im2.size
assert im.pos == im2.pos
assert im.rgb == im2.rgb
def test_grab_with_tuple_percents(sct):
monitor = sct.monitors[1]
left = monitor["left"] + monitor["width"] * 5 // 100 # 5% from the left
top = monitor["top"] + monitor["height"] * 5 // 100 # 5% from the top
right = left + 500 # 500px
lower = top + 500 # 500px
width = right - left
height = lower - top
# PIL like
box = (left, top, right, lower)
im = sct.grab(box)
assert im.size == (width, height)
# MSS like
box2 = {"left": left, "top": top, "width": width, "height": height}
im2 = sct.grab(box2)
assert im.size == im2.size
assert im.pos == im2.pos
assert im.rgb == im2.rgb