Index: chrome/test/functional/ispy/image_tools_test.py |
diff --git a/chrome/test/functional/ispy/image_tools_test.py b/chrome/test/functional/ispy/image_tools_test.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..250866362fbd0270cf31564703b3f7e08e0b7dae |
--- /dev/null |
+++ b/chrome/test/functional/ispy/image_tools_test.py |
@@ -0,0 +1,209 @@ |
+# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+from google3.pyglib import flags |
craigdh
2013/06/13 19:28:31
Chrome is open source. You can't use google3. Inst
|
+from google3.testing.pybase import googletest |
+from PIL import Image |
+import image_tools |
+ |
+ |
+class ImageToolsTest(googletest.TestCase): |
+ |
+ def testAreTheSameSize(self): |
+ black25x25 = self._GenImage((25, 25), (0, 0, 0)) |
+ white25x25 = self._GenImage((25, 25), (255, 255, 255)) |
+ black50x50 = self._GenImage((50, 50), (0, 0, 0)) |
+ white50x50 = self._GenImage((50, 50), (255, 255, 255)) |
+ |
+ self.assertTrue(image_tools.AreTheSameSize(black25x25, black25x25)) |
+ self.assertTrue(image_tools.AreTheSameSize(white25x25, white25x25)) |
+ self.assertTrue(image_tools.AreTheSameSize(black50x50, black50x50)) |
+ self.assertTrue(image_tools.AreTheSameSize(white50x50, white50x50)) |
+ self.assertTrue(image_tools.AreTheSameSize(black25x25, white25x25)) |
+ self.assertTrue(image_tools.AreTheSameSize(black50x50, white50x50)) |
+ |
+ self.assertFalse(image_tools.AreTheSameSize(black50x50, black25x25)) |
+ self.assertFalse(image_tools.AreTheSameSize(white50x50, white25x25)) |
+ self.assertFalse(image_tools.AreTheSameSize(black25x25, white50x50)) |
+ self.assertFalse(image_tools.AreTheSameSize(black50x50, white25x25)) |
+ |
+ self.assertRaises(image_tools.Error, image_tools.AreTheSameSize) |
+ self.assertRaises(image_tools.Error, image_tools.AreTheSameSize, black50x50) |
+ |
+ def testGetColorDist(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ px3 = (0, 255, 255) |
+ px4 = (0, 0, 255) |
+ |
+ self.assertAlmostEquals(image_tools.GetColorDist(px1, px1), 0., places=4) |
+ self.assertAlmostEquals(image_tools.GetColorDist(px1, px2), 1., places=4) |
+ self.assertAlmostEquals(image_tools.GetColorDist(px2, px2), 0., places=4) |
+ self.assertAlmostEquals(image_tools.GetColorDist(px1, px3), 2./3, places=4) |
+ self.assertAlmostEquals(image_tools.GetColorDist(px1, px4), 1./3, places=4) |
+ self.assertAlmostEquals(image_tools.GetColorDist(px3, px4), 1./3, places=4) |
+ |
+ def testGetColorDistAsColor(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ |
+ self.assertEquals(image_tools.GetColorDistAsColor(px1, px2), |
+ (255, 255, 255)) |
+ self.assertEquals(image_tools.GetColorDistAsColor(px1, px1), |
+ (0, 0, 0)) |
+ self.assertEquals(image_tools.GetColorDistAsColor(px2, px2), |
+ (0, 0, 0)) |
+ |
+ def testBrightness(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ px3 = (1, 2, 3) |
+ |
+ self.assertEquals(image_tools.Brightness(px1), 0) |
+ self.assertEquals(image_tools.Brightness(px2), 255*3) |
+ self.assertEquals(image_tools.Brightness(px3), 6) |
+ |
+ def testMinPixel(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ px3 = (1, 2, 3) |
+ |
+ self.assertEquals(image_tools.MinPixel(px1, px2), px1) |
+ self.assertEquals(image_tools.MinPixel(px1, px3), px1) |
+ self.assertEquals(image_tools.MinPixel(px2, px3), px3) |
+ self.assertEquals(image_tools.MinPixel(px1, px1), px1) |
+ |
+ def testMaxPixel(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ px3 = (1, 2, 3) |
+ |
+ self.assertEquals(image_tools.MaxPixel(px1, px2), px2) |
+ self.assertEquals(image_tools.MaxPixel(px1, px3), px3) |
+ self.assertEquals(image_tools.MaxPixel(px2, px3), px2) |
+ self.assertEquals(image_tools.MaxPixel(px1, px1), px1) |
+ |
+ def testCreateMask(self): |
+ black25x25 = self._GenImage((25, 25), (0, 0, 0)) |
+ black50x50 = self._GenImage((50, 50), (0, 0, 0)) |
+ white25x25 = self._GenImage((25, 25), (255, 255, 255)) |
+ gray25x25 = self._GenImage((25, 25), (127, 127, 127)) |
+ |
+ m1 = image_tools.CreateMask(True, 1, black25x25, white25x25) |
+ self.assertTrue(self._AllPixelsOfColor(m1, (255, 255, 255))) |
+ m2 = image_tools.CreateMask(True, 1, black25x25, black25x25) |
+ self.assertTrue(self._AllPixelsOfColor(m2, (0, 0, 0))) |
+ m3 = image_tools.CreateMask(True, 1, white25x25, white25x25) |
+ self.assertTrue(self._AllPixelsOfColor(m3, (0, 0, 0))) |
+ |
+ m4 = image_tools.CreateMask(False, 0, white25x25, gray25x25) |
+ self.assertFalse(self._AllPixelsOfColor(m4, (0, 0, 0)) or |
+ self._AllPixelsOfColor(m4, (255, 255, 255))) |
+ |
+ m5 = image_tools.CreateMask(True, -1, white25x25, white25x25) |
+ self.assertTrue(self._AllPixelsOfColor(m5, (255, 255, 255))) |
+ m6 = image_tools.CreateMask(True, -1, white25x25, black25x25) |
+ self.assertTrue(self._AllPixelsOfColor(m6, (255, 255, 255))) |
+ |
+ self.assertRaises(image_tools.Error, image_tools.CreateMask, |
+ True, 1, white25x25, black50x50) |
+ |
+ def testComputeLargestDifference(self): |
+ white25x25 = self._GenImage((25, 25), (255, 255, 255)) |
+ black25x25 = self._GenImage((25, 25), (0, 0, 0)) |
+ wd = list(white25x25.getdata()) |
+ bd = list(black25x25.getdata()) |
+ |
+ self.assertEquals(image_tools.ComputeLargestDifference(wd, bd, bd), wd) |
+ self.assertEquals(image_tools.ComputeLargestDifference(bd, wd, bd), wd) |
+ self.assertEquals(image_tools.ComputeLargestDifference(wd, wd, wd), wd) |
+ self.assertEquals(image_tools.ComputeLargestDifference(bd, bd, bd), bd) |
+ self.assertEquals(image_tools.ComputeLargestDifference(bd, wd, wd), bd) |
+ |
+ def testThresholdColorDiff(self): |
+ p1 = ((0, 0, 0), (1, 2, 3)) |
+ p2 = ((0, 0, 0), (0, 0, 0)) |
+ |
+ self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1], 0), 1.) |
+ self.assertEquals(image_tools.ThresholdColorDiff(p2[0], p2[1], 0), 0.) |
+ self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1], |
+ 14./(3*255**2)), 0.) |
+ self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1], |
+ 13./(3*255**2)), 1.) |
+ |
+ def testMaskToValue(self): |
+ px1 = (0, 0, 0) |
+ px2 = (255, 255, 255) |
+ px3 = (100, 100, 100) |
+ |
+ self.assertEquals(image_tools.MaskToValue(px1), 1.) |
+ self.assertEquals(image_tools.MaskToValue(px2), 0.) |
+ self.assertRaises(image_tools.Error, image_tools.MaskToValue, px3) |
+ |
+ def testSimilarity(self): |
+ white25x25 = self._GenImage((25, 25), (255, 255, 255)) |
+ white50x50 = self._GenImage((50, 50), (255, 255, 255)) |
+ black25x25 = self._GenImage((25, 25), (0, 0, 0)) |
+ |
+ self.assertEquals(image_tools.Similarity(white25x25, white25x25), 1.) |
+ self.assertEquals(image_tools.Similarity(black25x25, black25x25), 1.) |
+ self.assertEquals(image_tools.Similarity(white25x25, black25x25), 0.) |
+ self.assertEquals(image_tools.Similarity(white25x25, |
+ black25x25, |
+ mask=white25x25), |
+ 1.) |
+ self.assertEquals(image_tools.Similarity(white25x25, |
+ white25x25, |
+ mask=white25x25), |
+ 1.) |
+ self.assertEquals(image_tools.Similarity(white25x25, |
+ black25x25, |
+ mask=black25x25), |
+ 0.) |
+ self.assertEquals(image_tools.Similarity(white25x25, |
+ white25x25, |
+ mask=black25x25), |
+ 1.) |
+ self.assertRaises(image_tools.Error, image_tools.Similarity, |
+ white25x25, white50x50) |
+ self.assertRaises(image_tools.Error, image_tools.Similarity, |
+ white25x25, white25x25, mask=white50x50) |
+ |
+ def testSameImage(self): |
+ white25x25 = self._GenImage((25, 25), (255, 255, 255)) |
+ black25x25 = self._GenImage((25, 25), (0, 0, 0)) |
+ white50x50 = self._GenImage((50, 50), (255, 255, 255)) |
+ |
+ self.assertTrue(image_tools.SameImage(white25x25, white25x25, 0.97)) |
+ self.assertTrue(image_tools.SameImage(white25x25, black25x25, 0.)) |
+ self.assertFalse(image_tools.SameImage(black25x25, black25x25, 1.01)) |
+ |
+ self.assertTrue(image_tools.SameImage(white25x25, black25x25, |
+ 0., mask=white25x25)) |
+ self.assertTrue(image_tools.SameImage(white25x25, white25x25, |
+ 1., mask=white25x25)) |
+ self.assertFalse(image_tools.SameImage(white25x25, white25x25, |
+ 1.01, mask=white25x25)) |
+ |
+ self.assertRaises(image_tools.Error, image_tools.SameImage, |
+ white25x25, white50x50, 1.) |
+ self.assertRaises(image_tools.Error, image_tools.SameImage, |
+ white25x25, white25x25, 1., mask=white50x50) |
+ |
+ def _GenImage(self, size, color): |
craigdh
2013/06/13 19:28:31
This can be a module level function.
|
+ return Image.new('RGB', size, color) |
+ |
+ def _AllPixelsOfColor(self, image, color): |
craigdh
2013/06/13 19:28:31
ditto
|
+ data = image.getdata() |
+ for i in data: |
craigdh
2013/06/13 19:28:31
Just use any()
|
+ if i == color: |
+ continue |
+ else: |
+ return False |
+ return True |
+ |
+if __name__ == '__main__': |
+ googletest.main() |
+ |
+ |