Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,76 @@ | ||
import subprocess | ||
import sys | ||
|
||
import pytest | ||
from PIL import Image, ImageGrab | ||
|
||
from .helper import PillowTestCase, assert_image, unittest | ||
|
||
|
||
class TestImageGrab(PillowTestCase): | ||
@unittest.skipUnless( | ||
sys.platform in ("win32", "darwin"), "requires Windows or macOS" | ||
) | ||
def test_grab(self): | ||
for im in [ | ||
ImageGrab.grab(), | ||
ImageGrab.grab(include_layered_windows=True), | ||
ImageGrab.grab(all_screens=True), | ||
]: | ||
@pytest.mark.skipif( | ||
not sys.platform in ("win32", "darwin"), reason="requires Windows or macOS" | ||
) | ||
def test_grab(): | ||
for im in [ | ||
ImageGrab.grab(), | ||
ImageGrab.grab(include_layered_windows=True), | ||
ImageGrab.grab(all_screens=True), | ||
]: | ||
assert_image(im, im.mode, im.size) | ||
|
||
im = ImageGrab.grab(bbox=(10, 20, 50, 80)) | ||
assert_image(im, im.mode, (40, 60)) | ||
|
||
|
||
@pytest.mark.skipif(not Image.core.HAVE_XCB, reason="requires XCB") | ||
def test_grab_x11(): | ||
try: | ||
if sys.platform not in ("win32", "darwin"): | ||
im = ImageGrab.grab() | ||
assert_image(im, im.mode, im.size) | ||
|
||
im = ImageGrab.grab(bbox=(10, 20, 50, 80)) | ||
assert_image(im, im.mode, (40, 60)) | ||
im2 = ImageGrab.grab(xdisplay="") | ||
assert_image(im2, im2.mode, im2.size) | ||
except IOError as e: | ||
pytest.skip(str(e)) | ||
|
||
@unittest.skipUnless(Image.core.HAVE_XCB, "requires XCB") | ||
def test_grab_x11(self): | ||
try: | ||
if sys.platform not in ("win32", "darwin"): | ||
im = ImageGrab.grab() | ||
assert_image(im, im.mode, im.size) | ||
|
||
im2 = ImageGrab.grab(xdisplay="") | ||
assert_image(im2, im2.mode, im2.size) | ||
except IOError as e: | ||
self.skipTest(str(e)) | ||
@pytest.mark.skipif(not Image.core.HAVE_XCB, reason="tests missing XCB") | ||
def test_grab_no_xcb(): | ||
if sys.platform not in ("win32", "darwin"): | ||
with pytest.raises(IOError) as e: | ||
ImageGrab.grab() | ||
assert str(e.value).startswith("Pillow was built without XCB support") | ||
|
||
@unittest.skipIf(Image.core.HAVE_XCB, "tests missing XCB") | ||
def test_grab_no_xcb(self): | ||
if sys.platform not in ("win32", "darwin"): | ||
with self.assertRaises(IOError) as e: | ||
ImageGrab.grab() | ||
self.assertTrue( | ||
str(e.exception).startswith("Pillow was built without XCB support") | ||
) | ||
|
||
with self.assertRaises(IOError) as e: | ||
ImageGrab.grab(xdisplay="") | ||
self.assertTrue( | ||
str(e.exception).startswith("Pillow was built without XCB support") | ||
) | ||
with pytest.raises(IOError) as e: | ||
ImageGrab.grab(xdisplay="") | ||
assert str(e.value).startswith("Pillow was built without XCB support") | ||
|
||
@unittest.skipUnless(Image.core.HAVE_XCB, "requires XCB") | ||
def test_grab_invalid_xdisplay(self): | ||
with self.assertRaises(IOError) as e: | ||
ImageGrab.grab(xdisplay="error.test:0.0") | ||
assert str(e.exception).startswith("X connection failed") | ||
|
||
def test_grabclipboard(self): | ||
if sys.platform == "darwin": | ||
subprocess.call(["screencapture", "-cx"]) | ||
elif sys.platform == "win32": | ||
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE) | ||
p.stdin.write( | ||
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | ||
|
||
@pytest.mark.skipif(not Image.core.HAVE_XCB, reason="requires XCB") | ||
def test_grab_invalid_xdisplay(): | ||
with pytest.raises(IOError) as e: | ||
ImageGrab.grab(xdisplay="error.test:0.0") | ||
assert str(e.value).startswith("X connection failed") | ||
|
||
|
||
def test_grabclipboard(): | ||
if sys.platform == "darwin": | ||
subprocess.call(["screencapture", "-cx"]) | ||
elif sys.platform == "win32": | ||
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() | ||
else: | ||
with self.assertRaises(NotImplementedError) as e: | ||
ImageGrab.grabclipboard() | ||
self.assertEqual( | ||
str(e.exception), "ImageGrab.grabclipboard() is macOS and Windows only" | ||
) | ||
return | ||
|
||
im = ImageGrab.grabclipboard() | ||
assert_image(im, im.mode, im.size) | ||
) | ||
p.communicate() | ||
else: | ||
with pytest.raises(NotImplementedError) as e: | ||
ImageGrab.grabclipboard() | ||
assert str(e.value) == "ImageGrab.grabclipboard() is macOS and Windows only" | ||
return | ||
|
||
im = ImageGrab.grabclipboard() | ||
assert_image(im, im.mode, im.size) |