2018-10-22 10:55:16 +03:00
|
|
|
import subprocess
|
2019-07-06 23:40:53 +03:00
|
|
|
import sys
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from .helper import assert_image
|
2014-09-16 17:05:22 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
try:
|
|
|
|
from PIL import ImageGrab
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageGrab:
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_grab(self):
|
2019-07-05 00:52:35 +03:00
|
|
|
for im in [
|
|
|
|
ImageGrab.grab(),
|
|
|
|
ImageGrab.grab(include_layered_windows=True),
|
2019-09-20 18:35:08 +03:00
|
|
|
ImageGrab.grab(all_screens=True),
|
2019-07-05 00:52:35 +03:00
|
|
|
]:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, im.mode, im.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-14 12:31:25 +03:00
|
|
|
im = ImageGrab.grab(bbox=(10, 20, 50, 80))
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, im.mode, (40, 60))
|
2020-01-14 12:31:25 +03:00
|
|
|
|
2018-10-22 10:55:16 +03:00
|
|
|
def test_grabclipboard(self):
|
|
|
|
if sys.platform == "darwin":
|
2019-06-13 18:54:46 +03:00
|
|
|
subprocess.call(["screencapture", "-cx"])
|
2018-10-22 10:55:16 +03:00
|
|
|
else:
|
2019-06-13 18:54:46 +03:00
|
|
|
p = subprocess.Popen(
|
|
|
|
["powershell", "-command", "-"], stdin=subprocess.PIPE
|
|
|
|
)
|
|
|
|
p.stdin.write(
|
|
|
|
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
|
2018-10-22 10:55:16 +03:00
|
|
|
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
|
|
|
$bmp = New-Object Drawing.Bitmap 200, 200
|
2019-06-13 18:54:46 +03:00
|
|
|
[Windows.Forms.Clipboard]::SetImage($bmp)"""
|
|
|
|
)
|
2018-10-22 10:55:16 +03:00
|
|
|
p.communicate()
|
|
|
|
|
|
|
|
im = ImageGrab.grabclipboard()
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image(im, im.mode, im.size)
|
2018-10-22 10:55:16 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
except ImportError:
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageGrab:
|
|
|
|
@pytest.mark.skip(reason="ImageGrab ImportError")
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_skip(self):
|
2020-03-22 22:54:54 +03:00
|
|
|
pass
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageGrabImport:
|
2014-09-16 17:05:22 +04:00
|
|
|
def test_import(self):
|
|
|
|
# Arrange
|
|
|
|
exception = None
|
|
|
|
|
|
|
|
# Act
|
|
|
|
try:
|
|
|
|
from PIL import ImageGrab
|
2019-06-13 18:54:46 +03:00
|
|
|
|
2014-09-16 17:05:22 +04:00
|
|
|
ImageGrab.__name__ # dummy to prevent Pyflakes warning
|
2014-09-16 18:19:15 +04:00
|
|
|
except Exception as e:
|
|
|
|
exception = e
|
2014-09-16 17:05:22 +04:00
|
|
|
|
|
|
|
# Assert
|
2015-08-01 10:44:13 +03:00
|
|
|
if sys.platform in ["win32", "darwin"]:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert exception is None
|
2014-09-16 17:05:22 +04:00
|
|
|
else:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert isinstance(exception, ImportError)
|
|
|
|
assert str(exception) == "ImageGrab is macOS and Windows only"
|