blob: 95b7e4d44d2a7421415fff9e04455026f479f68e [file] [log] [blame]
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +00001#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9Test imagepair.py
10"""
11
12# System-level imports
13import shutil
14import tempfile
15import unittest
16
17# Local imports
18import imagediffdb
19import imagepair
20
21
22IMG_URL_BASE = 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm/bitmap-64bitMD5/'
23
24
25class ImagePairTest(unittest.TestCase):
26
27 def setUp(self):
28 self._temp_dir = tempfile.mkdtemp()
29 self.maxDiff = None
30
31 def tearDown(self):
32 shutil.rmtree(self._temp_dir)
33
34 def shortDescription(self):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000035 """Tells unittest framework to not print docstrings for test cases."""
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000036 return None
37
38 def test_endToEnd(self):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000039 """Tests ImagePair, using a real ImageDiffDB to download real images.
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000040
41 TODO(epoger): Either in addition to or instead of this end-to-end test,
42 we should perform some tests using either:
43 1. a mock ImageDiffDB, or
44 2. a real ImageDiffDB that doesn't hit Google Storage looking for input
45 image files (maybe a file:// IMG_URL_BASE)
46 """
47 # params for each self-test:
48 #
49 # inputs:
50 # 0. imageA_relative_URL
51 # 1. imageB_relative_URL
52 # 2. expectations dict
53 # 3. extra_columns dict
54 # expected output:
55 # 4. expected result of ImagePair.as_dict()
56 selftests = [
57 [
58 # inputs:
59 'arcofzorro/16206093933823793653.png',
60 'arcofzorro/16206093933823793653.png',
61 None,
62 {
63 'builder': 'MyBuilder',
64 'test': 'MyTest',
65 },
66 # expected output:
67 {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000068 'extraColumns': {
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000069 'builder': 'MyBuilder',
70 'test': 'MyTest',
71 },
72 'imageAUrl': 'arcofzorro/16206093933823793653.png',
73 'imageBUrl': 'arcofzorro/16206093933823793653.png',
74 'isDifferent': False,
75 },
76 ],
77
78 [
79 # inputs:
80 'arcofzorro/16206093933823793653.png',
81 'arcofzorro/13786535001616823825.png',
82 None,
83 None,
84 # expected output:
85 {
86 'differenceData': {
87 'maxDiffPerChannel': [255, 255, 247],
88 'numDifferingPixels': 662,
89 'percentDifferingPixels': 0.0662,
commit-bot@chromium.org16f41802014-02-26 19:05:20 +000090 'perceptualDifference': 0.06620000000000914,
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000091 'weightedDiffMeasure': 0.01127756555171088,
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,
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000117 'perceptualDifference': 100.00,
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000118 'weightedDiffMeasure': 66.66666666666667,
119 },
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000120 'expectations': {
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000121 'bugs': [1001, 1002],
122 'ignoreFailure': True,
123 },
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +0000124 'extraColumns': {
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000125 'builder': 'MyBuilder',
126 'test': 'MyTest',
127 },
128 'imageAUrl':
129 'gradients_degenerate_2pt/10552995703607727960.png',
130 'imageBUrl':
131 'gradients_degenerate_2pt/11198253335583713230.png',
132 'isDifferent': True,
133 },
134 ],
commit-bot@chromium.org68449582014-04-01 22:16:33 +0000135
136 # Test fix for http://skbug.com/2368 -- how do we handle an ImagePair
137 # missing one of its images?
138 [
139 # inputs:
140 'arcofzorro/16206093933823793653.png',
141 'nonexistentDir/111111.png',
142 {
143 'ignoreFailure': True,
144 'bugs': [1001, 1002],
145 },
146 {
147 'builder': 'MyBuilder',
148 'test': 'MyTest',
149 },
150 # expected output:
151 {
152 'expectations': {
153 'bugs': [1001, 1002],
154 'ignoreFailure': True,
155 },
156 'extraColumns': {
157 'builder': 'MyBuilder',
158 'test': 'MyTest',
159 },
160 'imageAUrl': 'arcofzorro/16206093933823793653.png',
161 'imageBUrl': 'nonexistentDir/111111.png',
162 'isDifferent': True,
163 },
164 ],
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000165 ]
166
167 db = imagediffdb.ImageDiffDB(self._temp_dir)
168 for selftest in selftests:
169 image_pair = imagepair.ImagePair(
170 image_diff_db=db,
171 base_url=IMG_URL_BASE,
172 imageA_relative_url=selftest[0],
173 imageB_relative_url=selftest[1],
174 expectations=selftest[2],
175 extra_columns=selftest[3])
176 self.assertEqual(image_pair.as_dict(), selftest[4])
177
178
179def main():
180 suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairTest)
181 unittest.TextTestRunner(verbosity=2).run(suite)
182
183
184if __name__ == '__main__':
185 main()