Home | History | Annotate | Download | only in rebaseline_server
      1 #!/usr/bin/python
      2 
      3 """
      4 Copyright 2014 Google Inc.
      5 
      6 Use of this source code is governed by a BSD-style license that can be
      7 found in the LICENSE file.
      8 
      9 Test imagepair.py
     10 """
     11 
     12 # System-level imports
     13 import shutil
     14 import tempfile
     15 import unittest
     16 
     17 # Local imports
     18 import imagediffdb
     19 import imagepair
     20 
     21 
     22 IMG_URL_BASE = 'http://chromium-skia-gm.commondatastorage.googleapis.com/gm/bitmap-64bitMD5/'
     23 
     24 
     25 class ImagePairTest(unittest.TestCase):
     26 
     27   def setUp(self):
     28     self._temp_dir = tempfile.mkdtemp()
     29     self.maxDiff = None
     30 
     31   def tearDown(self):
     32     shutil.rmtree(self._temp_dir)
     33 
     34   def shortDescription(self):
     35     """Tells unittest framework to not print docstrings for test cases."""
     36     return None
     37 
     38   def test_endToEnd(self):
     39     """Tests ImagePair, using a real ImageDiffDB to download real images.
     40 
     41     TODO(epoger): Either in addition to or instead of this end-to-end test,
     42     we should perform some tests using either:
     43     1. a mock ImageDiffDB, or
     44     2. a real ImageDiffDB that doesn't hit Google Storage looking for input
     45        image files (maybe a file:// IMG_URL_BASE)
     46     """
     47     # params for each self-test:
     48     #
     49     # inputs:
     50     #  0. imageA_relative_URL
     51     #  1. imageB_relative_URL
     52     #  2. expectations dict
     53     #  3. extra_columns dict
     54     # expected output:
     55     #  4. expected result of ImagePair.as_dict()
     56     selftests = [
     57         [
     58             # inputs:
     59             'arcofzorro/16206093933823793653.png',
     60             'arcofzorro/16206093933823793653.png',
     61             None,
     62             {
     63                 'builder': 'MyBuilder',
     64                 'test': 'MyTest',
     65             },
     66             # expected output:
     67             {
     68                 'extraColumns': {
     69                     'builder': 'MyBuilder',
     70                     'test': 'MyTest',
     71                 },
     72                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
     73                 'imageBUrl': 'arcofzorro/16206093933823793653.png',
     74                 'isDifferent': False,
     75             },
     76         ],
     77 
     78         [
     79             # inputs:
     80             'arcofzorro/16206093933823793653.png',
     81             'arcofzorro/13786535001616823825.png',
     82             None,
     83             None,
     84             # expected output:
     85             {
     86                 'differenceData': {
     87                     'maxDiffPerChannel': [255, 255, 247],
     88                     'numDifferingPixels': 662,
     89                     'percentDifferingPixels': 0.0662,
     90                     'perceptualDifference': 0.06620000000000914,
     91                 },
     92                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
     93                 'imageBUrl': 'arcofzorro/13786535001616823825.png',
     94                 'isDifferent': True,
     95             },
     96         ],
     97 
     98         [
     99             # inputs:
    100             'gradients_degenerate_2pt/10552995703607727960.png',
    101             'gradients_degenerate_2pt/11198253335583713230.png',
    102             {
    103                 'ignoreFailure': True,
    104                 'bugs': [1001, 1002],
    105             },
    106             {
    107                 'builder': 'MyBuilder',
    108                 'test': 'MyTest',
    109             },
    110             # expected output:
    111             {
    112                 'differenceData': {
    113                     'maxDiffPerChannel': [255, 0, 255],
    114                     'numDifferingPixels': 102400,
    115                     'percentDifferingPixels': 100.00,
    116                     'perceptualDifference': 100.00,
    117                 },
    118                 'expectations': {
    119                     'bugs': [1001, 1002],
    120                     'ignoreFailure': True,
    121                 },
    122                 'extraColumns': {
    123                     'builder': 'MyBuilder',
    124                     'test': 'MyTest',
    125                 },
    126                 'imageAUrl':
    127                     'gradients_degenerate_2pt/10552995703607727960.png',
    128                 'imageBUrl':
    129                     'gradients_degenerate_2pt/11198253335583713230.png',
    130                 'isDifferent': True,
    131             },
    132         ],
    133 
    134         # Test fix for http://skbug.com/2368 -- how do we handle an ImagePair
    135         # missing one of its images?
    136         [
    137             # inputs:
    138             'arcofzorro/16206093933823793653.png',
    139             'nonexistentDir/111111.png',
    140             {
    141                 'ignoreFailure': True,
    142                 'bugs': [1001, 1002],
    143             },
    144             {
    145                 'builder': 'MyBuilder',
    146                 'test': 'MyTest',
    147             },
    148             # expected output:
    149             {
    150                 'expectations': {
    151                     'bugs': [1001, 1002],
    152                     'ignoreFailure': True,
    153                 },
    154                 'extraColumns': {
    155                     'builder': 'MyBuilder',
    156                     'test': 'MyTest',
    157                 },
    158                 'imageAUrl': 'arcofzorro/16206093933823793653.png',
    159                 'imageBUrl': 'nonexistentDir/111111.png',
    160                 'isDifferent': True,
    161             },
    162         ],
    163     ]
    164 
    165     db = imagediffdb.ImageDiffDB(self._temp_dir)
    166     for selftest in selftests:
    167       image_pair = imagepair.ImagePair(
    168           image_diff_db=db,
    169           base_url=IMG_URL_BASE,
    170           imageA_relative_url=selftest[0],
    171           imageB_relative_url=selftest[1],
    172           expectations=selftest[2],
    173           extra_columns=selftest[3])
    174       self.assertEqual(image_pair.as_dict(), selftest[4])
    175 
    176 
    177 def main():
    178   suite = unittest.TestLoader().loadTestsFromTestCase(ImagePairTest)
    179   unittest.TextTestRunner(verbosity=2).run(suite)
    180 
    181 
    182 if __name__ == '__main__':
    183   main()
    184