forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gnu_linux.py
More file actions
119 lines (90 loc) · 2.87 KB
/
test_gnu_linux.py
File metadata and controls
119 lines (90 loc) · 2.87 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
# coding: utf-8
import ctypes.util
import platform
import sys
import pytest
import mss
from mss.base import MSSBase
from mss.exception import ScreenShotError
if platform.system().lower() != "linux":
pytestmark = pytest.mark.skip
try:
TEXT = unicode # Python 2
except NameError:
TEXT = str # Python 3
PY3 = sys.version[0] > "2"
def test_factory_systems(monkeypatch):
"""
Here, we are testing all systems.
Too hard to maintain the test for all platforms,
so test only on GNU/Linux.
"""
# GNU/Linux
monkeypatch.setattr(platform, "system", lambda: "LINUX")
with mss.mss() as sct:
assert isinstance(sct, MSSBase)
monkeypatch.undo()
# macOS
monkeypatch.setattr(platform, "system", lambda: "Darwin")
with pytest.raises(ScreenShotError):
mss.mss()
monkeypatch.undo()
# Windows
monkeypatch.setattr(platform, "system", lambda: "wInDoWs")
with pytest.raises(ValueError):
# wintypes.py:19: ValueError: _type_ 'v' not supported
mss.mss()
monkeypatch.undo()
def test_implementation(monkeypatch, is_travis):
import mss
# Bad `display` type
with mss.mss(display=TEXT(":42") if is_travis else TEXT(":0")):
pass
with pytest.raises(ScreenShotError):
with mss.mss(display=TEXT("0")):
pass
# No `DISPLAY` in envars
monkeypatch.delenv("DISPLAY")
with pytest.raises(ScreenShotError):
with mss.mss():
pass
monkeypatch.undo()
# No `X11` library
x11 = ctypes.util.find_library("X11")
monkeypatch.setattr(ctypes.util, "find_library", lambda x: None)
with pytest.raises(ScreenShotError):
with mss.mss():
pass
monkeypatch.undo()
def find_lib(lib):
"""
Returns None to emulate no Xrandr library.
Returns the previous found X11 library else.
It is a naive approach, but works for now.
"""
if lib == "Xrandr":
return None
return x11
# No `Xrandr` library
monkeypatch.setattr(ctypes.util, "find_library", find_lib)
with pytest.raises(ScreenShotError):
with mss.mss():
pass
monkeypatch.undo()
# Bad display data
import mss.linux
monkeypatch.setattr(mss.linux, "Display", lambda: None)
with pytest.raises(TypeError):
with mss.mss():
pass
def test_region_out_of_monitor_bounds(is_travis):
display = TEXT(":42") if is_travis else None
monitor = {"left": -30, "top": 0, "width": 100, "height": 100}
with mss.mss(display=display) as sct:
with pytest.raises(ScreenShotError) as exc:
assert sct.grab(monitor)
assert str(exc.value)
assert exc.value.details["xerror"]
assert "retval" in exc.value.details
assert "args" in exc.value.details
assert isinstance(exc.value.details["xerror_details"], dict)