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
124 lines (91 loc) · 3.03 KB
/
test_gnu_linux.py
File metadata and controls
124 lines (91 loc) · 3.03 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
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
"""
import ctypes.util
import os
import platform
import mss
import pytest
from mss.base import MSSMixin
from mss.exception import ScreenShotError
if platform.system().lower() != "linux":
pytestmark = pytest.mark.skip
PYPY = platform.python_implementation() == "PyPy"
@pytest.mark.skipif(PYPY, reason="Failure on PyPy")
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, MSSMixin)
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()
def test_arg_display(monkeypatch):
import mss
# Good value
display = os.getenv("DISPLAY")
with mss.mss(display=display):
pass
# Bad `display` (missing ":" in front of the number)
with pytest.raises(ScreenShotError):
with mss.mss(display="0"):
pass
# No `DISPLAY` in envars
monkeypatch.delenv("DISPLAY")
with pytest.raises(ScreenShotError):
with mss.mss():
pass
@pytest.mark.skipif(PYPY, reason="Failure on PyPy")
def test_bad_display_structure(monkeypatch):
import mss.linux
monkeypatch.setattr(mss.linux, "Display", lambda: None)
with pytest.raises(TypeError):
with mss.mss():
pass
def test_no_xlib_library(monkeypatch):
monkeypatch.setattr(ctypes.util, "find_library", lambda x: None)
with pytest.raises(ScreenShotError):
with mss.mss():
pass
def test_no_xrandr_extension(monkeypatch):
x11 = ctypes.util.find_library("X11")
def find_lib_mocked(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_mocked)
with pytest.raises(ScreenShotError):
with mss.mss():
pass
def test_region_out_of_monitor_bounds():
display = os.getenv("DISPLAY")
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 "retval" in exc.value.details
assert "args" in exc.value.details
details = sct.get_error_details()
assert details["xerror"]
assert isinstance(details["xerror_details"], dict)