2013-11-08 16:25:25 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
"""
|
|
|
|
Copyright 2013 Google Inc.
|
|
|
|
|
|
|
|
Use of this source code is governed by a BSD-style license that can be
|
|
|
|
found in the LICENSE file.
|
|
|
|
|
|
|
|
Test imagediffdb.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
# System-level imports
|
2014-01-06 18:33:19 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2013-11-08 16:25:25 +00:00
|
|
|
|
|
|
|
# Local imports
|
|
|
|
import imagediffdb
|
|
|
|
|
|
|
|
|
2014-02-11 18:21:26 +00:00
|
|
|
IMG_URL_BASE = ('http://chromium-skia-gm.commondatastorage.googleapis.com/gm/'
|
|
|
|
'bitmap-64bitMD5/')
|
2014-01-06 18:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImageDiffDbTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-07-17 19:54:16 +00:00
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
2014-02-10 18:19:30 +00:00
|
|
|
self.maxDiff = None
|
2014-01-06 18:33:19 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2014-07-17 19:54:16 +00:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2014-01-06 18:33:19 +00:00
|
|
|
|
|
|
|
def shortDescription(self):
|
|
|
|
"""Tell unittest framework to not print docstrings for test cases."""
|
|
|
|
return None
|
|
|
|
|
2014-02-10 18:19:30 +00:00
|
|
|
def test_sanitize_locator(self):
|
|
|
|
"""Test _sanitize_locator()."""
|
2014-07-17 19:54:16 +00:00
|
|
|
# pylint: disable=W0212
|
2014-02-10 18:19:30 +00:00
|
|
|
self.assertEqual(imagediffdb._sanitize_locator('simple'), 'simple')
|
|
|
|
self.assertEqual(imagediffdb._sanitize_locator(1234), '1234')
|
|
|
|
self.assertEqual(imagediffdb._sanitize_locator('one/two'), 'one_two')
|
|
|
|
self.assertEqual(imagediffdb._sanitize_locator('one\\two'), 'one_two')
|
|
|
|
self.assertEqual(imagediffdb._sanitize_locator('one_two'), 'one_two')
|
|
|
|
|
2014-01-06 18:33:19 +00:00
|
|
|
def test_simple(self):
|
2014-02-10 18:19:30 +00:00
|
|
|
"""Test ImageDiffDB, downloading real known images from Google Storage.
|
|
|
|
|
|
|
|
TODO(epoger): Instead of hitting Google Storage, we should read image
|
|
|
|
files from local disk using a file:// IMG_URL_BASE.
|
|
|
|
"""
|
2014-01-06 18:33:19 +00:00
|
|
|
# params for each self-test:
|
|
|
|
# 0. expected image locator
|
|
|
|
# 1. expected image URL
|
|
|
|
# 2. actual image locator
|
|
|
|
# 3. actual image URL
|
|
|
|
# 4. expected percent_pixels_differing (as a string, to 4 decimal places)
|
2014-06-12 03:35:59 +00:00
|
|
|
# 5. expected perceptual difference (as a string, to 4 decimal places)
|
|
|
|
# 6. expected max_diff_per_channel
|
2014-01-06 18:33:19 +00:00
|
|
|
selftests = [
|
|
|
|
[
|
2014-02-10 18:19:30 +00:00
|
|
|
'arcofzorro/16206093933823793653',
|
2014-01-06 18:33:19 +00:00
|
|
|
IMG_URL_BASE + 'arcofzorro/16206093933823793653.png',
|
2014-02-10 18:19:30 +00:00
|
|
|
'arcofzorro/13786535001616823825',
|
2014-01-06 18:33:19 +00:00
|
|
|
IMG_URL_BASE + 'arcofzorro/13786535001616823825.png',
|
2014-06-12 03:35:59 +00:00
|
|
|
'0.0662', '0.0662', [255, 255, 247],
|
2014-01-06 18:33:19 +00:00
|
|
|
],
|
|
|
|
[
|
2014-02-10 18:19:30 +00:00
|
|
|
'gradients_degenerate_2pt/10552995703607727960',
|
2014-01-06 18:33:19 +00:00
|
|
|
IMG_URL_BASE + 'gradients_degenerate_2pt/10552995703607727960.png',
|
2014-02-10 18:19:30 +00:00
|
|
|
'gradients_degenerate_2pt/11198253335583713230',
|
2014-01-06 18:33:19 +00:00
|
|
|
IMG_URL_BASE + 'gradients_degenerate_2pt/11198253335583713230.png',
|
2014-06-12 03:35:59 +00:00
|
|
|
'100.0000', '100.0000', [255, 0, 255],
|
2014-01-06 18:33:19 +00:00
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
# Add all image pairs to the database
|
2014-07-17 19:54:16 +00:00
|
|
|
db = imagediffdb.ImageDiffDB(self.temp_dir)
|
2014-01-06 18:33:19 +00:00
|
|
|
for selftest in selftests:
|
2014-07-17 19:54:16 +00:00
|
|
|
db.add_image_pair(
|
2014-01-06 18:33:19 +00:00
|
|
|
expected_image_locator=selftest[0], expected_image_url=selftest[1],
|
|
|
|
actual_image_locator=selftest[2], actual_image_url=selftest[3])
|
|
|
|
|
|
|
|
# Fetch each image pair from the database
|
|
|
|
for selftest in selftests:
|
|
|
|
record = db.get_diff_record(expected_image_locator=selftest[0],
|
|
|
|
actual_image_locator=selftest[2])
|
|
|
|
self.assertEqual('%.4f' % record.get_percent_pixels_differing(),
|
|
|
|
selftest[4])
|
2014-06-12 03:35:59 +00:00
|
|
|
self.assertEqual('%.4f' % record.get_perceptual_difference(), selftest[5])
|
|
|
|
self.assertEqual(record.get_max_diff_per_channel(), selftest[6])
|
2014-01-06 18:33:19 +00:00
|
|
|
|
2013-11-08 16:25:25 +00:00
|
|
|
|
|
|
|
def main():
|
2014-01-06 18:33:19 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(ImageDiffDbTest)
|
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite)
|
2013-11-08 16:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|