Pillow/Tests/test_imagegrab.py

63 lines
1.8 KiB
Python
Raw Normal View History

2018-10-22 10:55:16 +03:00
import subprocess
import sys
from .helper import PillowTestCase
try:
from PIL import ImageGrab
class TestImageGrab(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_grab(self):
for im in [
ImageGrab.grab(),
ImageGrab.grab(include_layered_windows=True),
2019-09-20 18:35:08 +03:00
ImageGrab.grab(all_screens=True),
]:
self.assert_image(im, im.mode, im.size)
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()
self.assert_image(im, im.mode, im.size)
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
class TestImageGrab(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_skip(self):
self.skipTest("ImportError")
class TestImageGrabImport(PillowTestCase):
def test_import(self):
# Arrange
exception = None
# Act
try:
from PIL import ImageGrab
2019-06-13 18:54:46 +03:00
ImageGrab.__name__ # dummy to prevent Pyflakes warning
2014-09-16 18:19:15 +04:00
except Exception as e:
exception = e
# Assert
if sys.platform in ["win32", "darwin"]:
2017-06-03 06:34:52 +03:00
self.assertIsNone(exception)
else:
2014-09-16 17:44:51 +04:00
self.assertIsInstance(exception, ImportError)
2019-06-13 18:54:46 +03:00
self.assertEqual(str(exception), "ImageGrab is macOS and Windows only")