| 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..cea34ed6c9f580e339a5883c0063a0b1de51e470
|
| --- /dev/null
|
| +++ b/chrome/test/functional/ispy/image_tools_test.py
|
| @@ -0,0 +1,224 @@
|
| +# 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.
|
| +
|
| +import unittest
|
| +from PIL import Image
|
| +import image_tools
|
| +
|
| +
|
| +def _GenImage(size, color):
|
| + return Image.new('RGB', size, color)
|
| +
|
| +
|
| +def _AllPixelsOfColor(image, color):
|
| + return not any(px != color for px in image.getdata())
|
| +
|
| +
|
| +class ImageToolsTest(unittest.TestCase):
|
| +
|
| + def testAreTheSameSize(self):
|
| + black25x25 = _GenImage((25, 25), (0, 0, 0))
|
| + white25x25 = _GenImage((25, 25), (255, 255, 255))
|
| + black50x50 = _GenImage((50, 50), (0, 0, 0))
|
| + white50x50 = _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(Exception, image_tools._AreTheSameSize, [])
|
| + self.assertRaises(Exception, 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 = _GenImage((25, 25), (0, 0, 0))
|
| + black50x50 = _GenImage((50, 50), (0, 0, 0))
|
| + white25x25 = _GenImage((25, 25), (255, 255, 255))
|
| + gray25x25 = _GenImage((25, 25), (127, 127, 127))
|
| +
|
| + m1 = image_tools.CreateMask([black25x25, white25x25], True, 1)
|
| + self.assertTrue(_AllPixelsOfColor(m1, (255, 255, 255)))
|
| + m2 = image_tools.CreateMask([black25x25, black25x25], True, 1)
|
| + self.assertTrue(_AllPixelsOfColor(m2, (0, 0, 0)))
|
| + m3 = image_tools.CreateMask([white25x25, white25x25], True, 1)
|
| + self.assertTrue(_AllPixelsOfColor(m3, (0, 0, 0)))
|
| +
|
| + m4 = image_tools.CreateMask([white25x25, gray25x25], False, 0)
|
| + self.assertFalse(_AllPixelsOfColor(m4, (0, 0, 0)) or
|
| + _AllPixelsOfColor(m4, (255, 255, 255)))
|
| +
|
| + m5 = image_tools.CreateMask([white25x25, white25x25], True, -1)
|
| + self.assertTrue(_AllPixelsOfColor(m5, (255, 255, 255)))
|
| + m6 = image_tools.CreateMask([white25x25, black25x25], True, -1)
|
| + self.assertTrue(_AllPixelsOfColor(m6, (255, 255, 255)))
|
| +
|
| + self.assertRaises(Exception, image_tools.CreateMask,
|
| + [white25x25, black50x50], True, 1)
|
| +
|
| + def testComputeLargestDifference(self):
|
| + white25x25 = _GenImage((25, 25), (255, 255, 255))
|
| + black25x25 = _GenImage((25, 25), (0, 0, 0))
|
| + wd = white25x25.getdata()
|
| + bd = black25x25.getdata()
|
| +
|
| + self.assertEquals(list(image_tools._ComputeLargestDifference(wd, bd, bd)),
|
| + list(wd))
|
| + self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, bd)),
|
| + list(wd))
|
| + self.assertEquals(list(image_tools._ComputeLargestDifference(wd, wd, wd)),
|
| + list(wd))
|
| + self.assertEquals(list(image_tools._ComputeLargestDifference(bd, bd, bd)),
|
| + list(bd))
|
| + self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, wd)),
|
| + list(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(Exception, image_tools._MaskToValue, px3)
|
| +
|
| + def testTotalDifferentPixels(self):
|
| + white25x25 = _GenImage((25, 25), (255, 255, 255))
|
| + white50x50 = _GenImage((50, 50), (255, 255, 255))
|
| + black25x25 = _GenImage((25, 25), (0, 0, 0))
|
| +
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + white25x25),
|
| + 0)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(black25x25,
|
| + black25x25),
|
| + 0)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + black25x25),
|
| + 25*25)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + black25x25,
|
| + mask=white25x25),
|
| + 0)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + white25x25,
|
| + mask=white25x25),
|
| + 0)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + black25x25,
|
| + mask=black25x25),
|
| + 25*25)
|
| + self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
|
| + white25x25,
|
| + mask=black25x25),
|
| + 0)
|
| + self.assertRaises(Exception, image_tools.TotalDifferentPixels,
|
| + white25x25, white50x50)
|
| + self.assertRaises(Exception, image_tools.TotalDifferentPixels,
|
| + white25x25, white25x25, mask=white50x50)
|
| +
|
| + def testSameImage(self):
|
| + white25x25 = _GenImage((25, 25), (255, 255, 255))
|
| + black25x25 = _GenImage((25, 25), (0, 0, 0))
|
| + white50x50 = _GenImage((50, 50), (255, 255, 255))
|
| +
|
| + self.assertTrue(image_tools.SameImage(white25x25, white25x25,
|
| + max_different_pixels=0))
|
| + self.assertTrue(image_tools.SameImage(white25x25, black25x25,
|
| + max_different_pixels=25*25))
|
| + self.assertFalse(image_tools.SameImage(black25x25, black25x25,
|
| + max_different_pixels=-1))
|
| +
|
| + self.assertTrue(image_tools.SameImage(white25x25, black25x25,
|
| + max_different_pixels=25*25,
|
| + mask=white25x25))
|
| + self.assertTrue(image_tools.SameImage(white25x25, white25x25,
|
| + max_different_pixels=0,
|
| + mask=white25x25))
|
| + self.assertFalse(image_tools.SameImage(white25x25, white25x25,
|
| + max_different_pixels=-1,
|
| + mask=white25x25))
|
| +
|
| + self.assertRaises(Exception, image_tools.SameImage,
|
| + white25x25, white50x50,
|
| + max_different_pixels=1)
|
| + self.assertRaises(Exception, image_tools.SameImage,
|
| + white25x25, white25x25,
|
| + max_different_pixels=1, mask=white50x50)
|
| +
|
| +
|
| +if __name__ == '__main__':
|
| + unittest.main()
|
| +
|
| +
|
|
|