- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / functional / ispy / common / ispy_utils_unittest.py
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 testUploadExpectation(self):
78     self.bucket.Reset()
79     # Upload some tests to the datastore.
80     self.ispy_utils.UploadExpectation('test', [self.white, self.black])
81     self.ispy_utils.UploadExpectation('test1', [self.black, self.black])
82     # Confirm that the tests were successfully uploaded.
83     self.assertEquals(self.bucket.datastore[
84         ispy_utils.GetExpectationPath('test', 'expected.png')],
85         image_tools.EncodePNG(self.white))
86     self.assertEquals(self.bucket.datastore[
87         ispy_utils.GetExpectationPath('test', 'mask.png')],
88         image_tools.EncodePNG(self.white))
89     self.assertEquals(self.bucket.datastore[
90         ispy_utils.GetExpectationPath('test1', 'expected.png')],
91         image_tools.EncodePNG(self.black))
92     self.assertEquals(self.bucket.datastore[
93         ispy_utils.GetExpectationPath('test1', 'mask.png')],
94         image_tools.EncodePNG(self.black))
95
96   def testRunTest(self):
97     self.bucket.Reset()
98     self.ispy_utils.UploadExpectation('test1', [self.red, self.red])
99     self.ispy_utils.RunTest('test', 'test1', self.black)
100     self.assertEquals(self.bucket.datastore[
101         ispy_utils.GetFailurePath('test', 'test1', 'actual.png')],
102         image_tools.EncodePNG(self.black))
103     self.ispy_utils.RunTest('test', 'test1', self.red)
104     self.assertTrue(self.bucket.datastore.has_key(
105             ispy_utils.GetFailurePath('test', 'test1', 'actual.png')))
106
107   def testGetExpectation(self):
108     self.bucket.Reset()
109     # Upload some tests to the datastore
110     self.ispy_utils.UploadExpectation('test1', [self.white, self.black])
111     self.ispy_utils.UploadExpectation('test2', [self.red, self.white])
112     test1 = self.ispy_utils.GetExpectation('test1')
113     test2 = self.ispy_utils.GetExpectation('test2')
114     # Check that GetExpectation gets the appropriate tests.
115     self.assertEquals(image_tools.EncodePNG(test1.expected),
116                       image_tools.EncodePNG(self.white))
117     self.assertEquals(image_tools.EncodePNG(test1.mask),
118                       image_tools.EncodePNG(self.white))
119     self.assertEquals(image_tools.EncodePNG(test2.expected),
120                       image_tools.EncodePNG(self.red))
121     self.assertEquals(image_tools.EncodePNG(test2.mask),
122                       image_tools.EncodePNG(self.white))
123     # Check that GetExpectation throws an error for a nonexistant test.
124     self.assertRaises(
125         cloud_bucket.FileNotFoundError, self.ispy_utils.GetExpectation, 'test3')
126
127   def testExpectationExists(self):
128     self.bucket.Reset()
129     self.ispy_utils.UploadExpectation('test1', [self.white, self.black])
130     self.ispy_utils.UploadExpectation('test2', [self.white, self.black])
131     self.assertTrue(self.ispy_utils.ExpectationExists('test1'))
132     self.assertTrue(self.ispy_utils.ExpectationExists('test2'))
133     self.assertFalse(self.ispy_utils.ExpectationExists('test3'))
134
135   def testFailureExists(self):
136     self.bucket.Reset()
137     self.ispy_utils.UploadExpectation('test1', [self.white, self.white])
138     self.ispy_utils.RunTest('test', 'test1', self.black)
139     self.ispy_utils.RunTest('test', 'test1', self.white)
140     self.assertTrue(self.ispy_utils.FailureExists('test', 'test1'))
141     self.assertFalse(self.ispy_utils.FailureExists('test', 'test2'))
142
143   def testRemoveExpectation(self):
144     self.bucket.Reset()
145     self.ispy_utils.UploadExpectation('test1', [self.white, self.white])
146     self.ispy_utils.UploadExpectation('test2', [self.white, self.white])
147     self.assertTrue(self.ispy_utils.ExpectationExists('test1'))
148     self.assertTrue(self.ispy_utils.ExpectationExists('test2'))
149     self.ispy_utils.RemoveExpectation('test1')
150     self.assertFalse(self.ispy_utils.ExpectationExists('test1'))
151     self.assertTrue(self.ispy_utils.ExpectationExists('test2'))
152     self.ispy_utils.RemoveExpectation('test2')
153     self.assertFalse(self.ispy_utils.ExpectationExists('test1'))
154     self.assertFalse(self.ispy_utils.ExpectationExists('test2'))
155
156   def testRemoveFailure(self):
157     self.bucket.Reset()
158     self.ispy_utils.UploadExpectation('test1', [self.white, self.white])
159     self.ispy_utils.UploadExpectation('test2', [self.white, self.white])
160     self.ispy_utils.RunTest('test', 'test1', self.black)
161     self.ispy_utils.RemoveFailure('test', 'test1')
162     self.assertFalse(self.ispy_utils.FailureExists('test', 'test1'))
163     self.assertTrue(self.ispy_utils.ExpectationExists('test1'))
164     self.assertFalse(self.ispy_utils.FailureExists('test', 'test2'))
165     self.assertTrue(self.ispy_utils.ExpectationExists('test2'))
166
167   def testGetFailure(self):
168     self.bucket.Reset()
169     # Upload a result
170     self.ispy_utils.UploadExpectation('test1', [self.red, self.red])
171     self.ispy_utils.RunTest('test', 'test1', self.black)
172     res = self.ispy_utils.GetFailure('test', 'test1')
173     # Check that the function correctly got the result.
174     self.assertEquals(image_tools.EncodePNG(res.expected),
175                       image_tools.EncodePNG(self.red))
176     self.assertEquals(image_tools.EncodePNG(res.diff),
177                       image_tools.EncodePNG(self.masked))
178     self.assertEquals(image_tools.EncodePNG(res.actual),
179                       image_tools.EncodePNG(self.black))
180     # Check that the function raises an error when given non-existant results.
181     self.assertRaises(cloud_bucket.FileNotFoundError,
182                       self.ispy_utils.GetFailure, 'test', 'test2')
183
184   def testGetAllPaths(self):
185     self.bucket.Reset()
186     # Upload some tests.
187     self.ispy_utils.UploadExpectation('test1', [self.white, self.black])
188     # Check that the function gets all urls matching the prefix.
189     self.assertEquals(
190         set(self.ispy_utils.GetAllPaths(
191             ispy_utils.GetExpectationPath('test1'))),
192         set([ispy_utils.GetExpectationPath('test1', 'expected.png'),
193              ispy_utils.GetExpectationPath('test1', 'mask.png')]))
194     self.assertEquals(
195         set(self.ispy_utils.GetAllPaths(
196             ispy_utils.GetExpectationPath('test3'))), set())
197
198
199 if __name__ == '__main__':
200   unittest.main()