blob: b76a2c0898671a8f1c4938d73bc8cb337fd28904 [file] [log] [blame]
epoger@google.com9dddf6f2013-11-08 16:25:25 +00001#!/usr/bin/python
2
3"""
4Copyright 2013 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 imagediffdb.py
epoger@google.com9dddf6f2013-11-08 16:25:25 +000010"""
11
12# System-level imports
13import logging
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000014import shutil
15import tempfile
16import unittest
epoger@google.com9dddf6f2013-11-08 16:25:25 +000017
18# Local imports
19import imagediffdb
20
21
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000022IMG_URL_BASE = 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm/bitmap-64bitMD5/'
23
24
25class ImageDiffDbTest(unittest.TestCase):
26
27 def setUp(self):
28 self._temp_dir = tempfile.mkdtemp()
29
30 def tearDown(self):
31 shutil.rmtree(self._temp_dir)
32
33 def shortDescription(self):
34 """Tell unittest framework to not print docstrings for test cases."""
35 return None
36
37 def test_simple(self):
38 # params for each self-test:
39 # 0. expected image locator
40 # 1. expected image URL
41 # 2. actual image locator
42 # 3. actual image URL
43 # 4. expected percent_pixels_differing (as a string, to 4 decimal places)
44 # 5. expected weighted_diff_measure (as a string, to 4 decimal places)
45 # 6. expected max_diff_per_channel
46 selftests = [
47 [
48 '16206093933823793653',
49 IMG_URL_BASE + 'arcofzorro/16206093933823793653.png',
50 '13786535001616823825',
51 IMG_URL_BASE + 'arcofzorro/13786535001616823825.png',
52 '0.0662', '0.0113', [255, 255, 247],
53 ],
54 [
55 '10552995703607727960',
56 IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png',
57 '11198253335583713230',
58 IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png',
59 '100.0000', '66.6667', [255, 0, 255],
60 ],
61 ]
62
63 # Add all image pairs to the database
64 db = imagediffdb.ImageDiffDB(self._temp_dir)
65 for selftest in selftests:
66 retval = db.add_image_pair(
67 expected_image_locator=selftest[0], expected_image_url=selftest[1],
68 actual_image_locator=selftest[2], actual_image_url=selftest[3])
69
70 # Fetch each image pair from the database
71 for selftest in selftests:
72 record = db.get_diff_record(expected_image_locator=selftest[0],
73 actual_image_locator=selftest[2])
74 self.assertEqual('%.4f' % record.get_percent_pixels_differing(),
75 selftest[4])
76 self.assertEqual('%.4f' % record.get_weighted_diff_measure(), selftest[5])
77 self.assertEqual(record.get_max_diff_per_channel(), selftest[6])
78
epoger@google.com9dddf6f2013-11-08 16:25:25 +000079
80def main():
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000081 suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest)
82 unittest.TextTestRunner(verbosity=2).run(suite)
epoger@google.com9dddf6f2013-11-08 16:25:25 +000083
84
85if __name__ == '__main__':
86 main()