blob: 3cd9a0a0603fa5b3e87fd1a687db93858b9f0ff1 [file] [log] [blame]
Tom Sepezb7cb36a2015-02-13 16:54:48 -08001#!/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
6import optparse
7import os
8import re
9import subprocess
10import sys
11
12# Nomenclature:
13# x_root - "x"
14# x_filename - "x.ext"
15# x_path - "path/to/a/b/c/x.ext"
16# c_dir - "path/to/a/b/c"
17
18def generate_and_test(input_filename, source_dir, working_dir,
Tom Sepezdf16baf2015-02-24 12:47:58 -080019 fixup_path, pdfium_test_path, pdfium_diff_path):
Tom Sepezb7cb36a2015-02-13 16:54:48 -080020 input_root, _ = os.path.splitext(input_filename)
21 input_path = os.path.join(source_dir, input_root + '.in')
22 pdf_path = os.path.join(working_dir, input_root + '.pdf')
Tom Sepezdf16baf2015-02-24 12:47:58 -080023 actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.png')
Tom Sepezb7cb36a2015-02-13 16:54:48 -080024 expected_path_template = os.path.join(source_dir,
Tom Sepezdf16baf2015-02-24 12:47:58 -080025 input_root + '_expected.pdf.%d.png')
Tom Sepezb7cb36a2015-02-13 16:54:48 -080026 try:
27 subprocess.check_call(
28 [fixup_path, '--output-dir=' + working_dir, input_path])
Tom Sepezdf16baf2015-02-24 12:47:58 -080029 subprocess.check_call([pdfium_test_path, '--png', pdf_path])
Tom Sepezb7cb36a2015-02-13 16:54:48 -080030 i = 0;
31 while True:
32 expected_path = expected_path_template % i;
33 actual_path = actual_path_template % i;
34 if not os.path.exists(expected_path):
Tom Sepezdf16baf2015-02-24 12:47:58 -080035 if i == 0:
36 print "WARNING: no expected results files found for " + input_filename
Tom Sepezb7cb36a2015-02-13 16:54:48 -080037 break
Tom Sepezdf16baf2015-02-24 12:47:58 -080038 print "Checking " + actual_path
39 subprocess.check_call([pdfium_diff_path, expected_path, actual_path])
Tom Sepezb7cb36a2015-02-13 16:54:48 -080040 i += 1
41 except subprocess.CalledProcessError as e:
42 print "FAILURE: " + input_filename + "; " + str(e)
43
44def main():
45 parser = optparse.OptionParser()
46 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
47 help='relative path from the base source directory')
48 options, args = parser.parse_args()
49
50 # Expect |my_dir| to be .../pdfium/testing/tools.
51 my_dir = os.path.dirname(os.path.realpath(__file__))
52 testing_dir = os.path.dirname(my_dir)
53 pdfium_dir = os.path.dirname(testing_dir)
54 if (os.path.basename(my_dir) != 'tools' or
55 os.path.basename(testing_dir) != 'testing'):
56 print 'Confused, can not find pdfium root directory, aborting.'
57 return 1
58
59 # Other scripts are found in the same directory as this one.
60 fixup_path = os.path.join(my_dir, 'fixup_pdf_template.py')
61
62 # test files are in .../pdfium/testing/resources/pixel.
63 source_dir = os.path.join(testing_dir, 'resources', 'pixel')
64
65 # Find path to build directory. This depends on whether this is a
66 # standalone build vs. a build as part of a chromium checkout. For
67 # standalone, we expect a path like .../pdfium/out/Debug, but for
68 # chromium, we expect a path like .../src/out/Debug two levels
69 # higher (to skip over the third_party/pdfium path component under
70 # which chromium sticks pdfium).
71 base_dir = pdfium_dir
72 one_up_dir = os.path.dirname(base_dir)
73 two_up_dir = os.path.dirname(one_up_dir)
74 if (os.path.basename(two_up_dir) == 'src' and
75 os.path.basename(one_up_dir) == 'third_party'):
76 base_dir = two_up_dir
77 build_dir = os.path.join(base_dir, options.build_dir)
78
79 # Compiled binaries are found under the build path.
80 pdfium_test_path = os.path.join(build_dir, 'pdfium_test')
Tom Sepezdf16baf2015-02-24 12:47:58 -080081 pdfium_diff_path = os.path.join(build_dir, 'pdfium_diff')
Tom Sepezb7cb36a2015-02-13 16:54:48 -080082 if sys.platform.startswith('win'):
83 pdfium_test_path = pdfium_test_path + '.exe'
Tom Sepezdf16baf2015-02-24 12:47:58 -080084 pdfium_diff_path = pdfium_diff_path + '.exe'
Tom Sepezb7cb36a2015-02-13 16:54:48 -080085 # TODO(tsepez): Mac may require special handling here.
86
87 # Place generated files under the build directory, not source directory.
88 gen_dir = os.path.join(build_dir, 'gen', 'pdfium')
89 working_dir = os.path.join(gen_dir, 'testing', 'pixel')
90 if not os.path.exists(working_dir):
91 os.makedirs(working_dir)
92
93 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$')
94 for input_filename in os.listdir(source_dir):
95 if input_file_re.match(input_filename):
96 input_path = os.path.join(source_dir, input_filename)
97 if os.path.isfile(input_path):
98 generate_and_test(input_filename, source_dir, working_dir,
Tom Sepezdf16baf2015-02-24 12:47:58 -080099 fixup_path, pdfium_test_path, pdfium_diff_path)
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800100 return 0
101
102
103if __name__ == '__main__':
104 sys.exit(main())