2018-10-22 10:39:07 +03:00
|
|
|
from helper import unittest, PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-16 17:05:22 +04:00
|
|
|
import sys
|
2018-10-22 10:55:16 +03:00
|
|
|
import subprocess
|
2014-09-16 17:05:22 +04:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
try:
|
|
|
|
from PIL import ImageGrab
|
|
|
|
|
2014-07-19 21:21:18 +04:00
|
|
|
class TestImageGrab(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_grab(self):
|
|
|
|
im = ImageGrab.grab()
|
|
|
|
self.assert_image(im, im.mode, im.size)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2018-10-22 10:55:16 +03:00
|
|
|
def test_grabclipboard(self):
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
subprocess.call(['screencapture', '-c'])
|
|
|
|
else:
|
|
|
|
p = subprocess.Popen(['powershell', '-command', '-'],
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
p.stdin.write(b'''[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
|
|
|
|
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
|
|
|
|
$bmp = New-Object Drawing.Bitmap 200, 200
|
|
|
|
[Windows.Forms.Clipboard]::SetImage($bmp)''')
|
|
|
|
p.communicate()
|
|
|
|
|
|
|
|
im = ImageGrab.grabclipboard()
|
|
|
|
self.assert_image(im, im.mode, im.size)
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
except ImportError:
|
2014-07-19 21:21:18 +04:00
|
|
|
class TestImageGrab(PillowTestCase):
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_skip(self):
|
|
|
|
self.skipTest("ImportError")
|
|
|
|
|
|
|
|
|
2014-09-16 17:05:22 +04:00
|
|
|
class TestImageGrabImport(PillowTestCase):
|
|
|
|
|
|
|
|
def test_import(self):
|
|
|
|
# Arrange
|
|
|
|
exception = None
|
|
|
|
|
|
|
|
# Act
|
|
|
|
try:
|
|
|
|
from PIL import ImageGrab
|
|
|
|
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"]:
|
2017-06-03 06:34:52 +03:00
|
|
|
self.assertIsNone(exception)
|
2014-09-16 17:05:22 +04:00
|
|
|
else:
|
2014-09-16 17:44:51 +04:00
|
|
|
self.assertIsInstance(exception, ImportError)
|
2015-08-01 10:44:13 +03:00
|
|
|
self.assertEqual(str(exception),
|
2016-09-23 14:12:03 +03:00
|
|
|
"ImageGrab is macOS and Windows only")
|
2014-09-16 17:05:22 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|