blob: bc4ad053e8fdda2c482b6e83131a7725880511c9 [file] [log] [blame]
dsinclair2a8a20c2016-04-25 09:46:17 -07001#!/usr/bin/env python
2# Copyright 2016 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
12import common
13import pngdiffer
14import 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
22class TestRunner:
23 def __init__(self, dirname):
24 self.test_dir = dirname
25
26 def GenerateAndTest(self, input_filename, source_dir):
27 input_root, _ = os.path.splitext(input_filename)
28 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
29
30 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
31
32 # Remove any existing generated images from previous runs.
33 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
34 self.working_dir)
35 for image in actual_images:
36 if os.path.exists(image):
37 os.remove(image)
38
39 sys.stdout.flush()
40
41 raised_exception = self.Generate(source_dir, input_filename, input_root,
42 pdf_path)
43
44 if raised_exception != None:
45 print "FAILURE: " + input_filename + "; " + str(raised_exception)
46 return False
47
48 if os.path.exists(expected_txt_path):
49 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path)
50 else:
51 raised_exception = self.TestPixel(input_root, pdf_path)
52
53 if raised_exception != None:
54 print "FAILURE: " + input_filename + "; " + str(raised_exception)
55 return False
56
57 if len(actual_images):
58 if self.image_differ.HasDifferences(input_filename, source_dir,
59 self.working_dir):
60 return False
61
62 return True
63
64 def Generate(self, source_dir, input_filename, input_root, pdf_path):
65 original_path = os.path.join(source_dir, input_filename)
66 input_path = os.path.join(source_dir, input_root + '.in')
67
68 if not os.path.exists(input_path):
69 if os.path.exists(original_path):
70 shutil.copyfile(original_path, pdf_path)
71 return None
72
73 sys.stdout.flush()
74 return common.RunCommand(
75 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
76 input_path])
77
78
79 def TestText(self, input_root, expected_txt_path, pdf_path):
80 txt_path = os.path.join(self.working_dir, input_root + '.txt')
81
82 with open(txt_path, 'w') as outfile:
83 # add Dr. Memory wrapper if exist
84 cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root)
85 cmd_to_run.extend([self.pdfium_test_path, pdf_path])
86 subprocess.check_call(cmd_to_run, stdout=outfile)
87
88 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
89 return common.RunCommand(cmd)
90
91
92 def TestPixel(self, input_root, pdf_path):
93 cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root)
tsepez10b01bf2016-05-04 12:52:42 -070094 cmd_to_run.extend([self.pdfium_test_path, '--send-events', '--png',
95 pdf_path])
dsinclair2a8a20c2016-04-25 09:46:17 -070096 return common.RunCommand(cmd_to_run)
97
98
99 def HandleResult(self, input_filename, input_path, result):
100 if self.test_suppressor.IsResultSuppressed(input_filename):
101 if result:
102 self.surprises.append(input_path)
103 else:
104 if not result:
105 self.failures.append(input_path)
106
107
108 def Run(self):
109 parser = optparse.OptionParser()
110 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
111 help='relative path from the base source directory')
112 parser.add_option('-j', default=1,
113 dest='num_workers', type='int',
114 help='run NUM_WORKERS jobs in parallel')
115 parser.add_option('--wrapper', default='', dest="wrapper",
116 help='wrapper for running test under Dr. Memory')
117 options, args = parser.parse_args()
118
119 finder = common.DirectoryFinder(options.build_dir)
120 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
121 self.text_diff_path = finder.ScriptPath('text_diff.py')
122
123 self.drmem_wrapper = options.wrapper
124
125 self.source_dir = finder.TestingDir()
126 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
127 if not os.path.exists(self.pdfium_test_path):
128 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
129 print "Use --build-dir to specify its location."
130 return 1
131
132 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
133 if not os.path.exists(self.working_dir):
134 os.makedirs(self.working_dir)
135
136 self.feature_string = subprocess.check_output([self.pdfium_test_path,
137 '--show-config'])
138 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string)
139 self.image_differ = pngdiffer.PNGDiffer(finder)
140
141 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
142 walk_from_dir = finder.TestingDir(test_dir);
143
144 test_cases = []
145 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$')
146 if len(args):
147 for file_name in args:
148 file_name.replace(".pdf", ".in")
149 input_path = os.path.join(walk_from_dir, file_name)
150 if not os.path.isfile(input_path):
151 print "Can't find test file '%s'" % file_name
152 return 1
153
154 test_cases.append((os.path.basename(input_path),
155 os.path.dirname(input_path)))
156 else:
157 for file_dir, _, filename_list in os.walk(walk_from_dir):
158 for input_filename in filename_list:
159 if input_file_re.match(input_filename):
160 input_path = os.path.join(file_dir, input_filename)
161 if not self.test_suppressor.IsExecutionSuppressed(input_path):
162 if os.path.isfile(input_path):
163 test_cases.append((input_filename, file_dir))
164
165 self.failures = []
166 self.surprises = []
167
168 for test_case in test_cases:
169 input_filename, input_file_dir = test_case
170 result = self.GenerateAndTest(input_filename, input_file_dir)
171 self.HandleResult(input_filename,
172 os.path.join(input_file_dir, input_filename), result)
173
174 if self.surprises:
175 self.surprises.sort()
176 print '\n\nUnexpected Successes:'
177 for surprise in self.surprises:
178 print surprise;
179
180 if self.failures:
181 self.failures.sort()
182 print '\n\nSummary of Failures:'
183 for failure in self.failures:
184 print failure
185 return 1
186
187 return 0