blob: 558a816a02cbbb6a05dea1bd9884c7e6d6cd4049 [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()
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000029 self.maxDiff = None
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000030
31 def tearDown(self):
32 shutil.rmtree(self._temp_dir)
33
34 def shortDescription(self):
35 """Tell unittest framework to not print docstrings for test cases."""
36 return None
37
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000038 def test_sanitize_locator(self):
39 """Test _sanitize_locator()."""
40 self.assertEqual(imagediffdb._sanitize_locator('simple'), 'simple')
41 self.assertEqual(imagediffdb._sanitize_locator(1234), '1234')
42 self.assertEqual(imagediffdb._sanitize_locator('one/two'), 'one_two')
43 self.assertEqual(imagediffdb._sanitize_locator('one\\two'), 'one_two')
44 self.assertEqual(imagediffdb._sanitize_locator('one_two'), 'one_two')
45
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000046 def test_simple(self):
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000047 """Test ImageDiffDB, downloading real known images from Google Storage.
48
49 TODO(epoger): Instead of hitting Google Storage, we should read image
50 files from local disk using a file:// IMG_URL_BASE.
51 """
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000052 # params for each self-test:
53 # 0. expected image locator
54 # 1. expected image URL
55 # 2. actual image locator
56 # 3. actual image URL
57 # 4. expected percent_pixels_differing (as a string, to 4 decimal places)
58 # 5. expected weighted_diff_measure (as a string, to 4 decimal places)
59 # 6. expected max_diff_per_channel
60 selftests = [
61 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000062 'arcofzorro/16206093933823793653',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000063 IMG_URL_BASE + 'arcofzorro/16206093933823793653.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000064 'arcofzorro/13786535001616823825',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000065 IMG_URL_BASE + 'arcofzorro/13786535001616823825.png',
66 '0.0662', '0.0113', [255, 255, 247],
67 ],
68 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000069 'gradients_degenerate_2pt/10552995703607727960',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000070 IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000071 'gradients_degenerate_2pt/11198253335583713230',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000072 IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png',
73 '100.0000', '66.6667', [255, 0, 255],
74 ],
75 ]
76
77 # Add all image pairs to the database
78 db = imagediffdb.ImageDiffDB(self._temp_dir)
79 for selftest in selftests:
80 retval = db.add_image_pair(
81 expected_image_locator=selftest[0], expected_image_url=selftest[1],
82 actual_image_locator=selftest[2], actual_image_url=selftest[3])
83
84 # Fetch each image pair from the database
85 for selftest in selftests:
86 record = db.get_diff_record(expected_image_locator=selftest[0],
87 actual_image_locator=selftest[2])
88 self.assertEqual('%.4f' % record.get_percent_pixels_differing(),
89 selftest[4])
90 self.assertEqual('%.4f' % record.get_weighted_diff_measure(), selftest[5])
91 self.assertEqual(record.get_max_diff_per_channel(), selftest[6])
92
epoger@google.com9dddf6f2013-11-08 16:25:25 +000093
94def main():
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000095 suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest)
96 unittest.TextTestRunner(verbosity=2).run(suite)
epoger@google.com9dddf6f2013-11-08 16:25:25 +000097
98
99if __name__ == '__main__':
100 main()