• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6import os
7import shutil
8import sys
9
10import common
11
12class PNGDiffer():
13  def __init__(self, finder):
14    self.pdfium_diff_path = finder.ExecutablePath('pdfium_diff')
15    self.os_name = finder.os_name
16
17  def GetActualFiles(self, input_filename, source_dir, working_dir):
18    actual_paths = []
19    path_templates = PathTemplates(input_filename, source_dir, working_dir)
20
21    i = 0
22    while True:
23      actual_path = path_templates.GetActualPath(i)
24      expected_path = path_templates.GetExpectedPath(i)
25      platform_expected_path = path_templates.GetPlatformExpectedPath(
26          self.os_name, i)
27      if os.path.exists(platform_expected_path):
28        expected_path = platform_expected_path
29      elif not os.path.exists(expected_path):
30        break
31      actual_paths.append(actual_path)
32      i += 1
33    return actual_paths
34
35  def HasDifferences(self, input_filename, source_dir, working_dir):
36    path_templates = PathTemplates(input_filename, source_dir, working_dir)
37
38    i = 0
39    while True:
40      actual_path = path_templates.GetActualPath(i)
41      expected_path = path_templates.GetExpectedPath(i)
42      # PDFium tests should be platform independent. Platform based results are
43      # used to capture platform dependent implementations.
44      platform_expected_path = path_templates.GetPlatformExpectedPath(
45          self.os_name, i)
46      if (not os.path.exists(expected_path) and
47          not os.path.exists(platform_expected_path)):
48        if i == 0:
49          print "WARNING: no expected results files for " + input_filename
50        break
51      print "Checking " + actual_path
52      sys.stdout.flush()
53      if os.path.exists(expected_path):
54        error = common.RunCommand(
55            [self.pdfium_diff_path, expected_path, actual_path])
56      else:
57        error = 1;
58      if error:
59        # When failed, we check against platform based results.
60        if os.path.exists(platform_expected_path):
61          error = common.RunCommand(
62              [self.pdfium_diff_path, platform_expected_path, actual_path])
63        if error:
64          print "FAILURE: " + input_filename + "; " + str(error)
65          return True
66      i += 1
67    return False
68
69  def Regenerate(self, input_filename, source_dir, working_dir, platform_only):
70    path_templates = PathTemplates(input_filename, source_dir, working_dir)
71
72    page = 0
73    while True:
74      # Loop through the generated page images. Stop when there is a page
75      # missing a png, which means the document ended.
76      actual_path = path_templates.GetActualPath(page)
77      if not os.path.isfile(actual_path):
78        break
79
80      platform_expected_path = path_templates.GetPlatformExpectedPath(
81          self.os_name, page)
82
83      # If there is a platform expected png, we will overwrite it. Otherwise,
84      # overwrite the generic png in "all" mode, or do nothing in "platform"
85      # mode.
86      if os.path.exists(platform_expected_path):
87        expected_path = platform_expected_path
88      elif not platform_only:
89        expected_path = path_templates.GetExpectedPath(page)
90      else:
91        expected_path = None
92
93      if expected_path is not None and os.path.exists(expected_path):
94        shutil.copyfile(actual_path, expected_path)
95
96      page += 1
97
98
99ACTUAL_TEMPLATE = '.pdf.%d.png'
100EXPECTED_TEMPLATE = '_expected' + ACTUAL_TEMPLATE
101PLATFORM_EXPECTED_TEMPLATE = '_expected_%s' + ACTUAL_TEMPLATE
102
103
104class PathTemplates(object):
105
106  def __init__(self, input_filename, source_dir, working_dir):
107    input_root, _ = os.path.splitext(input_filename)
108    self.actual_path_template = os.path.join(working_dir,
109                                             input_root + ACTUAL_TEMPLATE)
110    self.expected_path = os.path.join(
111        source_dir, input_root + EXPECTED_TEMPLATE)
112    self.platform_expected_path = os.path.join(
113        source_dir, input_root + PLATFORM_EXPECTED_TEMPLATE)
114
115  def GetActualPath(self, page):
116    return self.actual_path_template % page
117
118  def GetExpectedPath(self, page):
119    return self.expected_path % page
120
121  def GetPlatformExpectedPath(self, platform, page):
122    return self.platform_expected_path % (platform, page)
123