Refactor PDFium python test utilities.
[pdfium.git] / testing / tools / run_pixel_tests.py
1 #!/usr/bin/env python
2 # Copyright 2015 The PDFium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import optparse
7 import os
8 import re
9 import subprocess
10 import sys
11
12 import common
13 import pngdiffer
14 import suppressor
15
16 # Nomenclature:
17 #   x_root - "x"
18 #   x_filename - "x.ext"
19 #   x_path - "path/to/a/b/c/x.ext"
20 #   c_dir - "path/to/a/b/c"
21
22 def generate_and_test(input_filename, source_dir, working_dir,
23                       fixup_path, pdfium_test_path, image_differ):
24   input_root, _ = os.path.splitext(input_filename)
25   input_path = os.path.join(source_dir, input_root + '.in')
26   pdf_path = os.path.join(working_dir, input_root + '.pdf')
27   try:
28     sys.stdout.flush()
29     subprocess.check_call(
30         [sys.executable, fixup_path, '--output-dir=' + working_dir, input_path])
31     subprocess.check_call([pdfium_test_path, '--png', pdf_path])
32   except subprocess.CalledProcessError as e:
33     print "FAILURE: " + input_filename + "; " + str(e)
34     return False
35   if image_differ.HasDifferences(input_filename, source_dir, working_dir):
36     print "FAILURE: " + input_filename
37     return False
38   return True
39
40 def main():
41   parser = optparse.OptionParser()
42   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
43                     help='relative path from the base source directory')
44   options, args = parser.parse_args()
45   finder = common.DirectoryFinder(options.build_dir)
46   fixup_path = finder.ScriptPath('fixup_pdf_template.py')
47   source_dir = finder.TestingDir(os.path.join('resources', 'pixel'))
48   pdfium_test_path = finder.ExecutablePath('pdfium_test')
49   working_dir = finder.WorkingDir(os.path.join('testing', 'pixel'))
50   if not os.path.exists(working_dir):
51     os.makedirs(working_dir)
52
53   test_suppressor = suppressor.Suppressor(finder)
54   image_differ = pngdiffer.PNGDiffer(finder)
55
56   failures = []
57   input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$')
58   for input_filename in os.listdir(source_dir):
59     if input_file_re.match(input_filename):
60       input_path = os.path.join(source_dir, input_filename)
61       if os.path.isfile(input_path):
62         if test_suppressor.IsSuppressed(input_filename):
63           continue
64         if not generate_and_test(input_filename, source_dir, working_dir,
65                                  fixup_path, pdfium_test_path, image_differ):
66           failures.append(input_path)
67
68   if failures:
69     print '\n\nSummary of Failures:'
70     for failure in failures:
71       print failure
72     return 1
73   return 0
74
75 if __name__ == '__main__':
76   sys.exit(main())