blob: 186b2f132419344619138806f12e6ce63e8e31fc [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
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000013import shutil
14import tempfile
15import unittest
epoger@google.com9dddf6f2013-11-08 16:25:25 +000016
17# Local imports
18import imagediffdb
19
20
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000021IMG_URL_BASE = ('http://chromium-skia-gm.commondatastorage.googleapis.com/gm/'
22 'bitmap-64bitMD5/')
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000023
24
25class ImageDiffDbTest(unittest.TestCase):
26
27 def setUp(self):
epoger66ed8dc2014-07-17 12:54:16 -070028 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):
epoger66ed8dc2014-07-17 12:54:16 -070032 shutil.rmtree(self.temp_dir)
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000033
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()."""
epoger66ed8dc2014-07-17 12:54:16 -070040 # pylint: disable=W0212
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000041 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)
epoger984b97c2014-06-11 20:35:59 -070059 # 5. expected perceptual difference (as a string, to 4 decimal places)
60 # 6. expected max_diff_per_channel
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000061 selftests = [
62 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000063 'arcofzorro/16206093933823793653',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000064 IMG_URL_BASE + 'arcofzorro/16206093933823793653.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000065 'arcofzorro/13786535001616823825',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000066 IMG_URL_BASE + 'arcofzorro/13786535001616823825.png',
epoger984b97c2014-06-11 20:35:59 -070067 '0.0662', '0.0662', [255, 255, 247],
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000068 ],
69 [
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000070 'gradients_degenerate_2pt/10552995703607727960',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000071 IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png',
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000072 'gradients_degenerate_2pt/11198253335583713230',
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000073 IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png',
epoger984b97c2014-06-11 20:35:59 -070074 '100.0000', '100.0000', [255, 0, 255],
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000075 ],
76 ]
77
78 # Add all image pairs to the database
epoger66ed8dc2014-07-17 12:54:16 -070079 db = imagediffdb.ImageDiffDB(self.temp_dir)
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000080 for selftest in selftests:
epoger66ed8dc2014-07-17 12:54:16 -070081 db.add_image_pair(
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000082 expected_image_locator=selftest[0], expected_image_url=selftest[1],
83 actual_image_locator=selftest[2], actual_image_url=selftest[3])
84
85 # Fetch each image pair from the database
86 for selftest in selftests:
87 record = db.get_diff_record(expected_image_locator=selftest[0],
88 actual_image_locator=selftest[2])
89 self.assertEqual('%.4f' % record.get_percent_pixels_differing(),
90 selftest[4])
epoger984b97c2014-06-11 20:35:59 -070091 self.assertEqual('%.4f' % record.get_perceptual_difference(), selftest[5])
92 self.assertEqual(record.get_max_diff_per_channel(), selftest[6])
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000093
epoger@google.com9dddf6f2013-11-08 16:25:25 +000094
95def main():
commit-bot@chromium.org2a78e822014-01-06 18:33:19 +000096 suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest)
97 unittest.TextTestRunner(verbosity=2).run(suite)
epoger@google.com9dddf6f2013-11-08 16:25:25 +000098
99
100if __name__ == '__main__':
101 main()