blob: b1d534a5e06ccb7b463809eaee6492691725e202 [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.org44546f82014-02-11 18:21:26 +000022IMG_URL_BASE = ('http://chromium-skia-gm.commondatastorage.googleapis.com/gm/'
23 'bitmap-64bitMD5/')
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000024
25
26class ImageDiffDbTest(unittest.TestCase):
27
28 def setUp(self):
29 self._temp_dir = tempfile.mkdtemp()
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000030 self.maxDiff = None
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000031
32 def tearDown(self):
33 shutil.rmtree(self._temp_dir)
34
35 def shortDescription(self):
36 """Tell unittest framework to not print docstrings for test cases."""
37 return None
38
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000039 def test_sanitize_locator(self):
40 """Test _sanitize_locator()."""
41 self.assertEqual(imagediffdb._sanitize_locator('simple'), 'simple')
42 self.assertEqual(imagediffdb._sanitize_locator(1234), '1234')
43 self.assertEqual(imagediffdb._sanitize_locator('one/two'), 'one_two')
44 self.assertEqual(imagediffdb._sanitize_locator('one\\two'), 'one_two')
45 self.assertEqual(imagediffdb._sanitize_locator('one_two'), 'one_two')
46
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000047 def test_simple(self):
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000048 """Test ImageDiffDB, downloading real known images from Google Storage.
49
50 TODO(epoger): Instead of hitting Google Storage, we should read image
51 files from local disk using a file:// IMG_URL_BASE.
52 """
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000053 # params for each self-test:
54 # 0. expected image locator
55 # 1. expected image URL
56 # 2. actual image locator
57 # 3. actual image URL
58 # 4. expected percent_pixels_differing (as a string, to 4 decimal places)
59 # 5. expected weighted_diff_measure (as a string, to 4 decimal places)
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000060 # 6. expected perceptual difference (as a string, to 4 decimal places)
61 # 7. expected max_diff_per_channel
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000062 selftests = [
63 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000064 'arcofzorro/16206093933823793653',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000065 IMG_URL_BASE + 'arcofzorro/16206093933823793653.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000066 'arcofzorro/13786535001616823825',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000067 IMG_URL_BASE + 'arcofzorro/13786535001616823825.png',
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000068 '0.0662', '0.0113', '0.0662', [255, 255, 247],
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000069 ],
70 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000071 'gradients_degenerate_2pt/10552995703607727960',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000072 IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000073 'gradients_degenerate_2pt/11198253335583713230',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000074 IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png',
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000075 '100.0000', '66.6667', '100.0000', [255, 0, 255],
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000076 ],
77 ]
78
79 # Add all image pairs to the database
80 db = imagediffdb.ImageDiffDB(self._temp_dir)
81 for selftest in selftests:
82 retval = db.add_image_pair(
83 expected_image_locator=selftest[0], expected_image_url=selftest[1],
84 actual_image_locator=selftest[2], actual_image_url=selftest[3])
85
86 # Fetch each image pair from the database
87 for selftest in selftests:
88 record = db.get_diff_record(expected_image_locator=selftest[0],
89 actual_image_locator=selftest[2])
90 self.assertEqual('%.4f' % record.get_percent_pixels_differing(),
91 selftest[4])
92 self.assertEqual('%.4f' % record.get_weighted_diff_measure(), selftest[5])
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000093 self.assertEqual('%.4f' % record.get_perceptual_difference(), selftest[6])
94 self.assertEqual(record.get_max_diff_per_channel(), selftest[7])
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000095
epoger@google.com9dddf6f2013-11-08 16:25:25 +000096
97def main():
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000098 suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest)
99 unittest.TextTestRunner(verbosity=2).run(suite)
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000100
101
102if __name__ == '__main__':
103 main()