Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / ispy / common / image_tools_unittest.py
blob017c172316c6cdc1d8195877639288ebc7c64d3d
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import unittest
6 import sys
7 import os
8 from PIL import Image
10 import image_tools
13 def _GenImage(size, color):
14 return Image.new('RGBA', size, color)
17 def _AllPixelsOfColor(image, color):
18 return not any(px != color for px in image.getdata())
21 class ImageToolsTest(unittest.TestCase):
23 def setUp(self):
24 self.black25 = _GenImage((25, 25), (0, 0, 0, 255))
25 self.black50 = _GenImage((50, 50), (0, 0, 0, 255))
26 self.white25 = _GenImage((25, 25), (255, 255, 255, 255))
27 self.white50 = _GenImage((50, 50), (255, 255, 255, 255))
29 def testAreTheSameSize(self):
30 self.assertTrue(image_tools._AreTheSameSize([self.black25, self.black25]))
31 self.assertTrue(image_tools._AreTheSameSize([self.white25, self.white25]))
32 self.assertTrue(image_tools._AreTheSameSize([self.black50, self.black50]))
33 self.assertTrue(image_tools._AreTheSameSize([self.white50, self.white50]))
34 self.assertTrue(image_tools._AreTheSameSize([self.black25, self.white25]))
35 self.assertTrue(image_tools._AreTheSameSize([self.black50, self.white50]))
37 self.assertFalse(image_tools._AreTheSameSize([self.black50, self.black25]))
38 self.assertFalse(image_tools._AreTheSameSize([self.white50, self.white25]))
39 self.assertFalse(image_tools._AreTheSameSize([self.black25, self.white50]))
40 self.assertFalse(image_tools._AreTheSameSize([self.black50, self.white25]))
42 self.assertRaises(Exception, image_tools._AreTheSameSize, [])
43 self.assertRaises(Exception, image_tools._AreTheSameSize, [self.black50])
45 def testGetDifferenceWithMask(self):
46 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
47 self.black25, self.black25)[0], (255, 255, 255, 255)))
48 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
49 self.white25, self.black25)[0], (210, 0, 0, 255)))
50 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
51 self.black25, self.black25, mask=self.black25)[0],
52 (255, 255, 255, 255)))
53 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
54 self.black25, self.black25, mask=self.white25)[0],
55 (225, 225, 225, 255)))
56 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
57 self.black25, self.white25, mask=self.black25)[0],
58 (210, 0, 0, 255)))
59 self.assertTrue(_AllPixelsOfColor(image_tools._GetDifferenceWithMask(
60 self.black25, self.white25, mask=self.white25)[0],
61 (225, 225, 225, 255)))
62 self.assertRaises(Exception, image_tools._GetDifferenceWithMask,
63 self.white25,
64 self.black50)
65 self.assertRaises(Exception, image_tools._GetDifferenceWithMask,
66 self.white25,
67 self.white25,
68 mask=self.black50)
70 def testCreateMask(self):
71 m1 = image_tools.CreateMask([self.black25, self.white25])
72 self.assertTrue(_AllPixelsOfColor(m1, (255, 255, 255, 255)))
73 m2 = image_tools.CreateMask([self.black25, self.black25])
74 self.assertTrue(_AllPixelsOfColor(m2, (0, 0, 0, 255)))
75 m3 = image_tools.CreateMask([self.white25, self.white25])
76 self.assertTrue(_AllPixelsOfColor(m3, (0, 0, 0, 255)))
78 def testAddMasks(self):
79 m1 = image_tools.CreateMask([self.black25, self.white25])
80 m2 = image_tools.CreateMask([self.black25, self.black25])
81 m3 = image_tools.CreateMask([self.black50, self.black50])
82 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m1]),
83 (255, 255, 255, 255)))
84 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m2]),
85 (0, 0, 0, 255)))
86 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m1, m2]),
87 (255, 255, 255, 255)))
88 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m1, m1]),
89 (255, 255, 255, 255)))
90 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m2, m2]),
91 (0, 0, 0, 255)))
92 self.assertTrue(_AllPixelsOfColor(image_tools.AddMasks([m3]),
93 (0, 0, 0, 255)))
94 self.assertRaises(Exception, image_tools.AddMasks, [])
95 self.assertRaises(Exception, image_tools.AddMasks, [m1, m3])
97 def testTotalDifferentPixels(self):
98 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
99 self.white25),
101 self.assertEquals(image_tools.TotalDifferentPixels(self.black25,
102 self.black25),
104 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
105 self.black25),
106 25*25)
107 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
108 self.black25,
109 mask=self.white25),
111 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
112 self.white25,
113 mask=self.white25),
115 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
116 self.black25,
117 mask=self.black25),
118 25*25)
119 self.assertEquals(image_tools.TotalDifferentPixels(self.white25,
120 self.white25,
121 mask=self.black25),
123 self.assertRaises(Exception, image_tools.TotalDifferentPixels,
124 self.white25, self.white50)
125 self.assertRaises(Exception, image_tools.TotalDifferentPixels,
126 self.white25, self.white25, mask=self.white50)
128 def testSameImage(self):
129 self.assertTrue(image_tools.SameImage(self.white25, self.white25))
130 self.assertFalse(image_tools.SameImage(self.white25, self.black25))
132 self.assertTrue(image_tools.SameImage(self.white25, self.black25,
133 mask=self.white25))
134 self.assertFalse(image_tools.SameImage(self.white25, self.black25,
135 mask=self.black25))
136 self.assertTrue(image_tools.SameImage(self.black25, self.black25))
137 self.assertTrue(image_tools.SameImage(self.black25, self.black25,
138 mask=self.white25))
139 self.assertTrue(image_tools.SameImage(self.white25, self.white25,
140 mask=self.white25))
141 self.assertRaises(Exception, image_tools.SameImage,
142 self.white25, self.white50)
143 self.assertRaises(Exception, image_tools.SameImage,
144 self.white25, self.white25,
145 mask=self.white50)
147 def testInflateMask(self):
148 cross_image = Image.new('RGBA', (3, 3))
149 white_image = Image.new('RGBA', (3, 3))
150 dot_image = Image.new('RGBA', (3, 3))
151 b = (0, 0, 0, 255)
152 w = (255, 255, 255, 255)
153 dot_image.putdata([b, b, b,
154 b, w, b,
155 b, b, b])
156 cross_image.putdata([b, w, b,
157 w, w, w,
158 b, w, b])
159 white_image.putdata([w, w, w,
160 w, w, w,
161 w, w, w])
162 self.assertEquals(list(image_tools.InflateMask(dot_image, 1).getdata()),
163 list(cross_image.getdata()))
164 self.assertEquals(list(image_tools.InflateMask(dot_image, 0).getdata()),
165 list(dot_image.getdata()))
166 self.assertEquals(list(image_tools.InflateMask(dot_image, 2).getdata()),
167 list(white_image.getdata()))
168 self.assertEquals(list(image_tools.InflateMask(dot_image, 3).getdata()),
169 list(white_image.getdata()))
170 self.assertEquals(list(image_tools.InflateMask(self.black25, 1).getdata()),
171 list(self.black25.getdata()))
173 def testPNGEncodeDecode(self):
174 self.assertTrue(_AllPixelsOfColor(
175 image_tools.DecodePNG(
176 image_tools.EncodePNG(self.white25)), (255, 255, 255, 255)))
177 self.assertTrue(_AllPixelsOfColor(
178 image_tools.DecodePNG(
179 image_tools.EncodePNG(self.black25)), (0, 0, 0, 255)))
182 if __name__ == '__main__':
183 unittest.main()