Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / gm / rebaseline_server / imagepair_test.py
1 #!/usr/bin/python
2
3 """
4 Copyright 2014 Google Inc.
5
6 Use of this source code is governed by a BSD-style license that can be
7 found in the LICENSE file.
8
9 Test imagepair.py
10 """
11
12 # System-level imports
13 import shutil
14 import tempfile
15 import unittest
16
17 # Local imports
18 import imagediffdb
19 import imagepair
20
21
22 IMG_URL_BASE = ('http://chromium-skia-gm.commondatastorage.googleapis.com/'
23                 'gm/bitmap-64bitMD5/')
24
25
26 class ImagePairTest(unittest.TestCase):
27
28   def setUp(self):
29     self.temp_dir = tempfile.mkdtemp()
30     self.maxDiff = None
31
32   def tearDown(self):
33     shutil.rmtree(self.temp_dir)
34
35   def shortDescription(self):
36     """Tells unittest framework to not print docstrings for test cases."""
37     return None
38
39   def test_endToEnd(self):
40     """Tests ImagePair, using a real ImageDiffDB to download real images.
41
42     TODO(epoger): Either in addition to or instead of this end-to-end test,
43     we should perform some tests using either:
44     1. a mock ImageDiffDB, or
45     2. a real ImageDiffDB that doesn't hit Google Storage looking for input
46        image files (maybe a file:// IMG_URL_BASE)
47     """
48     # params for each self-test:
49     #
50     # inputs:
51     #  0. imageA_relative_URL
52     #  1. imageB_relative_URL
53     #  2. expectations dict
54     #  3. extra_columns dict
55     # expected output:
56     #  4. expected result of ImagePair.as_dict()
57     selftests = [
58         [
59             # inputs:
60             'arcofzorro/16206093933823793653.png',
61             'arcofzorro/16206093933823793653.png',
62             None,
63             {
64                 'builder': 'MyBuilder',
65                 'test': 'MyTest',
66             },
67             # expected output:
68             {
69                 'extraColumns': {
70                     'builder': 'MyBuilder',
71                     'test': 'MyTest',
72                 },
73                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
74                 'imageBUrl': 'arcofzorro/16206093933823793653.png',
75                 'isDifferent': False,
76             },
77         ],
78
79         [
80             # inputs:
81             'arcofzorro/16206093933823793653.png',
82             'arcofzorro/13786535001616823825.png',
83             None,
84             None,
85             # expected output:
86             {
87                 'differenceData': {
88                     'maxDiffPerChannel': [255, 255, 247],
89                     'numDifferingPixels': 662,
90                     'percentDifferingPixels': 0.0662,
91                     'perceptualDifference': 0.06620300000000157,
92                 },
93                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
94                 'imageBUrl': 'arcofzorro/13786535001616823825.png',
95                 'isDifferent': True,
96             },
97         ],
98
99         [
100             # inputs:
101             'gradients_degenerate_2pt/10552995703607727960.png',
102             'gradients_degenerate_2pt/11198253335583713230.png',
103             {
104                 'ignoreFailure': True,
105                 'bugs': [1001, 1002],
106             },
107             {
108                 'builder': 'MyBuilder',
109                 'test': 'MyTest',
110             },
111             # expected output:
112             {
113                 'differenceData': {
114                     'maxDiffPerChannel': [255, 0, 255],
115                     'numDifferingPixels': 102400,
116                     'percentDifferingPixels': 100.00,
117                     'perceptualDifference': 100.00,
118                 },
119                 'expectations': {
120                     'bugs': [1001, 1002],
121                     'ignoreFailure': True,
122                 },
123                 'extraColumns': {
124                     'builder': 'MyBuilder',
125                     'test': 'MyTest',
126                 },
127                 'imageAUrl':
128                     'gradients_degenerate_2pt/10552995703607727960.png',
129                 'imageBUrl':
130                     'gradients_degenerate_2pt/11198253335583713230.png',
131                 'isDifferent': True,
132             },
133         ],
134
135         # Test fix for http://skbug.com/2368 -- how do we handle an ImagePair
136         # missing one of its images?
137         [
138             # inputs:
139             'arcofzorro/16206093933823793653.png',
140             'nonexistentDir/111111.png',
141             {
142                 'ignoreFailure': True,
143                 'bugs': [1001, 1002],
144             },
145             {
146                 'builder': 'MyBuilder',
147                 'test': 'MyTest',
148             },
149             # expected output:
150             {
151                 'expectations': {
152                     'bugs': [1001, 1002],
153                     'ignoreFailure': True,
154                 },
155                 'extraColumns': {
156                     'builder': 'MyBuilder',
157                     'test': 'MyTest',
158                 },
159                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
160                 'imageBUrl': 'nonexistentDir/111111.png',
161                 'isDifferent': True,
162             },
163         ],
164
165         # One of the two images is missing, but download_all_images=True so we
166         # should download it anyway.
167         [
168             # inputs:
169             None,
170             'arcofzorro/13786535001616823825.png',
171             None,
172             None,
173             # expected output:
174             {
175                 'imageAUrl': None,
176                 'imageBUrl': 'arcofzorro/13786535001616823825.png',
177                 'isDifferent': True,
178             },
179         ],
180
181     ]
182
183     db = imagediffdb.ImageDiffDB(self.temp_dir)
184     for selftest in selftests:
185       image_pair = imagepair.ImagePair(
186           image_diff_db=db,
187           base_url=IMG_URL_BASE,
188           imageA_relative_url=selftest[0],
189           imageB_relative_url=selftest[1],
190           expectations=selftest[2],
191           extra_columns=selftest[3],
192           download_all_images=True)
193       self.assertEqual(image_pair.as_dict(), selftest[4])
194
195
196 def main():
197   suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairTest)
198   unittest.TextTestRunner(verbosity=2).run(suite)
199
200
201 if __name__ == '__main__':
202   main()