From 057fd966c97dcadfcd58d18d53d50d17c41bf1bb Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 22 Jul 2014 12:34:04 +0100 Subject: [PATCH] Added tests for crop_to_point --- wagtail/wagtailimages/tests.py | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index f7d4cbff5..0db9549c3 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -17,6 +17,8 @@ from wagtail.wagtailimages.formats import ( from wagtail.wagtailimages.backends import get_image_backend from wagtail.wagtailimages.backends.pillow import PillowBackend +from wagtail.wagtailimages.utils.crop import crop_to_point, CropBox +from wagtail.wagtailimages.utils.focal_point import FocalPoint def get_test_image_file(): @@ -471,3 +473,60 @@ class TestFormat(TestCase): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format) + + +class TestCropToPoint(TestCase): + def test_basic(self): + "Test basic cropping in the centre of the image" + self.assertEqual( + crop_to_point((640, 480), (100, 100), FocalPoint(x=320, y=240)), + CropBox(270, 190, 370, 290), + ) + + def test_doesnt_exit_top_left(self): + "Test that the cropbox doesn't exit the image at the top left" + self.assertEqual( + crop_to_point((640, 480), (100, 100), FocalPoint(x=0, y=0)), + CropBox(0, 0, 100, 100), + ) + + def test_doesnt_exit_bottom_right(self): + "Test that the cropbox doesn't exit the image at the bottom right" + self.assertEqual( + crop_to_point((640, 480), (100, 100), FocalPoint(x=640, y=480)), + CropBox(540, 380, 640, 480), + ) + + def test_doesnt_get_smaller_than_focal_point(self): + "Test that the cropbox doesn't get any smaller than the focal point" + self.assertEqual( + crop_to_point((640, 480), (10, 10), FocalPoint(x=320, y=240, width=100, height=100)), + CropBox(270, 190, 370, 290), + ) + + def test_keeps_composition(self): + "Test that the cropbox tries to keep the composition of the original image as much as it can" + self.assertEqual( + crop_to_point((300, 300), (150, 150), FocalPoint(x=100, y=200)), + CropBox(50, 100, 200, 250), # Focal point is 1/3 across and 2/3 down in the crop box + ) + + def test_keeps_focal_point_in_view_bottom_left(self): + """ + Even though it tries to keep the composition of the image, + it shouldn't let that get in the way of keeping the entire subject in view + """ + self.assertEqual( + crop_to_point((300, 300), (150, 150), FocalPoint(x=100, y=200, width=150, height=150)), + CropBox(25, 125, 175, 275), + ) + + def test_keeps_focal_point_in_view_top_right(self): + """ + Even though it tries to keep the composition of the image, + it shouldn't let that get in the way of keeping the entire subject in view + """ + self.assertEqual( + crop_to_point((300, 300), (150, 150), FocalPoint(x=200, y=100, width=150, height=150)), + CropBox(125, 25, 275, 175), + )