OLD | NEW |
| (Empty) |
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. | |
4 | |
5 import os | |
6 from PIL import Image | |
7 import sys | |
8 import unittest | |
9 | |
10 import cloud_bucket | |
11 import image_tools | |
12 import ispy_utils | |
13 import mock_cloud_bucket | |
14 | |
15 | |
16 class ISpyUtilsUnitTest(unittest.TestCase): | |
17 | |
18 def setUp(self): | |
19 # Set up structures that will be reused throughout testing. | |
20 self.bucket = mock_cloud_bucket.MockCloudBucket() | |
21 self.ispy_utils = ispy_utils.ISpyUtils(self.bucket) | |
22 self.white = Image.new('RGBA', (25, 25), (255, 255, 255, 255)) | |
23 self.red = Image.new('RGBA', (25, 25), (255, 0, 0, 255)) | |
24 self.black = Image.new('RGBA', (25, 25), (0, 0, 0, 255)) | |
25 self.masked = Image.new('RGBA', (25, 25), (210, 0, 0, 255)) | |
26 | |
27 def testUploadImage(self): | |
28 self.bucket.Reset() | |
29 # Upload some images to the datastore. | |
30 self.ispy_utils.UploadImage('path/to/white.png', self.white) | |
31 self.ispy_utils.UploadImage('path/to/black.png', self.black) | |
32 self.ispy_utils.UploadImage('path/to/red.png', self.red) | |
33 # Confirm that the images actually got uploaded. | |
34 self.assertEquals(self.bucket.datastore['path/to/white.png'], | |
35 image_tools.EncodePNG(self.white)) | |
36 self.assertEquals(self.bucket.datastore['path/to/black.png'], | |
37 image_tools.EncodePNG(self.black)) | |
38 self.assertEquals(self.bucket.datastore['path/to/red.png'], | |
39 image_tools.EncodePNG(self.red)) | |
40 | |
41 def testDownloadImage(self): | |
42 self.bucket.Reset() | |
43 # Upload some images to the datastore. | |
44 self.ispy_utils.UploadImage('path/to/white.png', self.white) | |
45 self.ispy_utils.UploadImage('path/to/black.png', self.black) | |
46 self.ispy_utils.UploadImage('path/to/red.png', self.red) | |
47 # Check that the DownloadImage function gets the correct images. | |
48 self.assertEquals( | |
49 image_tools.EncodePNG( | |
50 self.ispy_utils.DownloadImage('path/to/white.png')), | |
51 image_tools.EncodePNG(self.white)) | |
52 self.assertEquals( | |
53 image_tools.EncodePNG( | |
54 self.ispy_utils.DownloadImage('path/to/black.png')), | |
55 image_tools.EncodePNG(self.black)) | |
56 self.assertEquals( | |
57 image_tools.EncodePNG( | |
58 self.ispy_utils.DownloadImage('path/to/red.png')), | |
59 image_tools.EncodePNG(self.red)) | |
60 # Check that the DownloadImage function throws an error for a | |
61 # nonexistant image. | |
62 self.assertRaises(cloud_bucket.FileNotFoundError, | |
63 self.ispy_utils.DownloadImage, | |
64 'path/to/yellow.png') | |
65 | |
66 def testUpdateImage(self): | |
67 self.bucket.Reset() | |
68 # Upload some images to the datastore. | |
69 self.ispy_utils.UploadImage('path/to/image.png', self.white) | |
70 self.assertEquals(self.bucket.datastore['path/to/image.png'], | |
71 image_tools.EncodePNG(self.white)) | |
72 self.ispy_utils.UpdateImage('path/to/image.png', self.black) | |
73 # Confirm that the image actually got updated. | |
74 self.assertEquals(self.bucket.datastore['path/to/image.png'], | |
75 image_tools.EncodePNG(self.black)) | |
76 | |
77 def testGenerateExpectation(self): | |
78 self.bucket.Reset() | |
79 # Upload some tests to the datastore. | |
80 self.ispy_utils.GenerateExpectation('test', [self.white, self.black]) | |
81 self.ispy_utils.GenerateExpectation('test1', [self.black, self.black]) | |
82 self.ispy_utils.GenerateExpectation('test2', [self.black]) | |
83 # Confirm that the tests were successfully uploaded. | |
84 self.assertEquals(self.bucket.datastore[ | |
85 ispy_utils.GetExpectationPath('test', 'expected.png')], | |
86 image_tools.EncodePNG(self.white)) | |
87 self.assertEquals(self.bucket.datastore[ | |
88 ispy_utils.GetExpectationPath('test', 'mask.png')], | |
89 image_tools.EncodePNG(self.white)) | |
90 self.assertEquals(self.bucket.datastore[ | |
91 ispy_utils.GetExpectationPath('test1', 'expected.png')], | |
92 image_tools.EncodePNG(self.black)) | |
93 self.assertEquals(self.bucket.datastore[ | |
94 ispy_utils.GetExpectationPath('test1', 'mask.png')], | |
95 image_tools.EncodePNG(self.black)) | |
96 self.assertEquals(self.bucket.datastore[ | |
97 ispy_utils.GetExpectationPath('test2', 'expected.png')], | |
98 image_tools.EncodePNG(self.black)) | |
99 self.assertEquals(self.bucket.datastore[ | |
100 ispy_utils.GetExpectationPath('test2', 'mask.png')], | |
101 image_tools.EncodePNG(self.black)) | |
102 | |
103 def testPerformComparison(self): | |
104 self.bucket.Reset() | |
105 self.ispy_utils.GenerateExpectation('test1', [self.red, self.red]) | |
106 self.ispy_utils.PerformComparison('test', 'test1', self.black) | |
107 self.assertEquals(self.bucket.datastore[ | |
108 ispy_utils.GetFailurePath('test', 'test1', 'actual.png')], | |
109 image_tools.EncodePNG(self.black)) | |
110 self.ispy_utils.PerformComparison('test', 'test1', self.red) | |
111 self.assertTrue(self.bucket.datastore.has_key( | |
112 ispy_utils.GetFailurePath('test', 'test1', 'actual.png'))) | |
113 | |
114 def testGetExpectation(self): | |
115 self.bucket.Reset() | |
116 # Upload some tests to the datastore | |
117 self.ispy_utils.GenerateExpectation('test1', [self.white, self.black]) | |
118 self.ispy_utils.GenerateExpectation('test2', [self.red, self.white]) | |
119 test1 = self.ispy_utils.GetExpectation('test1') | |
120 test2 = self.ispy_utils.GetExpectation('test2') | |
121 # Check that GetExpectation gets the appropriate tests. | |
122 self.assertEquals(image_tools.EncodePNG(test1.expected), | |
123 image_tools.EncodePNG(self.white)) | |
124 self.assertEquals(image_tools.EncodePNG(test1.mask), | |
125 image_tools.EncodePNG(self.white)) | |
126 self.assertEquals(image_tools.EncodePNG(test2.expected), | |
127 image_tools.EncodePNG(self.red)) | |
128 self.assertEquals(image_tools.EncodePNG(test2.mask), | |
129 image_tools.EncodePNG(self.white)) | |
130 # Check that GetExpectation throws an error for a nonexistant test. | |
131 self.assertRaises( | |
132 cloud_bucket.FileNotFoundError, self.ispy_utils.GetExpectation, 'test3') | |
133 | |
134 def testExpectationExists(self): | |
135 self.bucket.Reset() | |
136 self.ispy_utils.GenerateExpectation('test1', [self.white, self.black]) | |
137 self.ispy_utils.GenerateExpectation('test2', [self.white, self.black]) | |
138 self.assertTrue(self.ispy_utils.ExpectationExists('test1')) | |
139 self.assertTrue(self.ispy_utils.ExpectationExists('test2')) | |
140 self.assertFalse(self.ispy_utils.ExpectationExists('test3')) | |
141 | |
142 def testFailureExists(self): | |
143 self.bucket.Reset() | |
144 self.ispy_utils.GenerateExpectation('test1', [self.white, self.white]) | |
145 self.ispy_utils.PerformComparison('test', 'test1', self.black) | |
146 self.ispy_utils.PerformComparison('test', 'test1', self.white) | |
147 self.assertTrue(self.ispy_utils.FailureExists('test', 'test1')) | |
148 self.assertFalse(self.ispy_utils.FailureExists('test', 'test2')) | |
149 | |
150 def testRemoveExpectation(self): | |
151 self.bucket.Reset() | |
152 self.ispy_utils.GenerateExpectation('test1', [self.white, self.white]) | |
153 self.ispy_utils.GenerateExpectation('test2', [self.white, self.white]) | |
154 self.assertTrue(self.ispy_utils.ExpectationExists('test1')) | |
155 self.assertTrue(self.ispy_utils.ExpectationExists('test2')) | |
156 self.ispy_utils.RemoveExpectation('test1') | |
157 self.assertFalse(self.ispy_utils.ExpectationExists('test1')) | |
158 self.assertTrue(self.ispy_utils.ExpectationExists('test2')) | |
159 self.ispy_utils.RemoveExpectation('test2') | |
160 self.assertFalse(self.ispy_utils.ExpectationExists('test1')) | |
161 self.assertFalse(self.ispy_utils.ExpectationExists('test2')) | |
162 | |
163 def testRemoveFailure(self): | |
164 self.bucket.Reset() | |
165 self.ispy_utils.GenerateExpectation('test1', [self.white, self.white]) | |
166 self.ispy_utils.GenerateExpectation('test2', [self.white, self.white]) | |
167 self.ispy_utils.PerformComparison('test', 'test1', self.black) | |
168 self.ispy_utils.RemoveFailure('test', 'test1') | |
169 self.assertFalse(self.ispy_utils.FailureExists('test', 'test1')) | |
170 self.assertTrue(self.ispy_utils.ExpectationExists('test1')) | |
171 self.assertFalse(self.ispy_utils.FailureExists('test', 'test2')) | |
172 self.assertTrue(self.ispy_utils.ExpectationExists('test2')) | |
173 | |
174 def testGetFailure(self): | |
175 self.bucket.Reset() | |
176 # Upload a result | |
177 self.ispy_utils.GenerateExpectation('test1', [self.red, self.red]) | |
178 self.ispy_utils.PerformComparison('test', 'test1', self.black) | |
179 res = self.ispy_utils.GetFailure('test', 'test1') | |
180 # Check that the function correctly got the result. | |
181 self.assertEquals(image_tools.EncodePNG(res.expected), | |
182 image_tools.EncodePNG(self.red)) | |
183 self.assertEquals(image_tools.EncodePNG(res.diff), | |
184 image_tools.EncodePNG(self.masked)) | |
185 self.assertEquals(image_tools.EncodePNG(res.actual), | |
186 image_tools.EncodePNG(self.black)) | |
187 # Check that the function raises an error when given non-existant results. | |
188 self.assertRaises(cloud_bucket.FileNotFoundError, | |
189 self.ispy_utils.GetFailure, 'test', 'test2') | |
190 | |
191 def testGetAllPaths(self): | |
192 self.bucket.Reset() | |
193 # Upload some tests. | |
194 self.ispy_utils.GenerateExpectation('test1', [self.white, self.black]) | |
195 # Check that the function gets all urls matching the prefix. | |
196 self.assertEquals( | |
197 set(self.ispy_utils.GetAllPaths( | |
198 ispy_utils.GetExpectationPath('test1'))), | |
199 set([ispy_utils.GetExpectationPath('test1', 'expected.png'), | |
200 ispy_utils.GetExpectationPath('test1', 'mask.png')])) | |
201 self.assertEquals( | |
202 set(self.ispy_utils.GetAllPaths( | |
203 ispy_utils.GetExpectationPath('test3'))), set()) | |
204 | |
205 | |
206 if __name__ == '__main__': | |
207 unittest.main() | |
OLD | NEW |