|
| 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() |
0 commit comments