|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This builds a html page of all images from the image comparison tests |
| 4 | +# and opens that page in the browser. |
| 5 | +# |
| 6 | +# $ python visual_tests.py |
| 7 | +# |
| 8 | + |
| 9 | +import os |
| 10 | +import time |
| 11 | +import six |
| 12 | + |
| 13 | +from collections import defaultdict |
| 14 | + |
| 15 | +def run(): |
| 16 | + # Build a website for visual comparison |
| 17 | + image_dir = "result_images" |
| 18 | + # build the website |
| 19 | + _html = "" |
| 20 | + _html += """<html><head><style media="screen" type="text/css"> |
| 21 | + img{ |
| 22 | + width:100%; |
| 23 | + max-width:800px; |
| 24 | + } |
| 25 | + </style> |
| 26 | + </head><body>\n""" |
| 27 | + _subdirs = [name for name in os.listdir(image_dir) if os.path.isdir(os.path.join(image_dir, name))] |
| 28 | + # loop over all pictures |
| 29 | + _row = '<tr><td>{0} {1}</td><td>{2}</td><td><a href="{3}"><img src="{3}"></a></td><td>{4}</td>\n' |
| 30 | + _failed = "" |
| 31 | + _failed += "<h2>Only Failed</h2>" |
| 32 | + _failed += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n" |
| 33 | + _has_failure = False |
| 34 | + _body = "" |
| 35 | + for subdir in _subdirs: |
| 36 | + if subdir == "test_compare_images": |
| 37 | + # these are the image which test the image comparison functions... |
| 38 | + continue |
| 39 | + pictures = defaultdict(dict) |
| 40 | + for file in os.listdir(os.path.join(image_dir, subdir)): |
| 41 | + if os.path.isdir(os.path.join(image_dir, subdir, file)): |
| 42 | + continue |
| 43 | + fn, fext = os.path.splitext(file) |
| 44 | + if fext != ".png": |
| 45 | + continue |
| 46 | + if "-failed-diff" in fn: |
| 47 | + pictures[fn[:-12]]["f"] = os.path.join(subdir, file) |
| 48 | + elif "-expected" in fn: |
| 49 | + pictures[fn[:-9]]["e"] = os.path.join(subdir, file) |
| 50 | + else: |
| 51 | + pictures[fn]["c"] = os.path.join(subdir, file) |
| 52 | + |
| 53 | + _body += "<h2>{0}</h2>".format(subdir) |
| 54 | + _body += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n" |
| 55 | + for name, test in six.iteritems(pictures): |
| 56 | + if test.get("f", None): |
| 57 | + # a real failure in the image generation, resulting in different images |
| 58 | + _has_failure = True |
| 59 | + s = "(failed)" |
| 60 | + failed = '<a href="{0}">diff</a>'.format(test.get("f", "")) |
| 61 | + current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", "")) |
| 62 | + _failed += _row.format(name, "", current, test.get("e", ""), failed) |
| 63 | + elif test.get("c", None) is None: |
| 64 | + # A failure in the test, resulting in no current image |
| 65 | + _has_failure = True |
| 66 | + s = "(failed)" |
| 67 | + failed = '--' |
| 68 | + current = '(Failure in test, no image produced)' |
| 69 | + _failed += _row.format(name, "", current, test.get("e", ""), failed) |
| 70 | + else: |
| 71 | + s = "(passed)" |
| 72 | + failed = '--' |
| 73 | + current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", "")) |
| 74 | + _body += _row.format(name, "", current, test.get("e", ""), failed) |
| 75 | + _body += "</table>\n" |
| 76 | + _failed += "</table>\n" |
| 77 | + if _has_failure: |
| 78 | + _html += _failed |
| 79 | + _html += _body |
| 80 | + _html += "\n</body></html>" |
| 81 | + index = os.path.join(image_dir, "index.html") |
| 82 | + with open(index, "w") as f: |
| 83 | + f.write(_html) |
| 84 | + try: |
| 85 | + import webbrowser |
| 86 | + webbrowser.open(index) |
| 87 | + except: |
| 88 | + print("Open {0} in a browser for a visual comparison.".format(str(index))) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + run() |
0 commit comments