Home | History | Annotate | Download | only in tools
      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 os
      7 import sys
      8 
      9 import common
     10 
     11 class PNGDiffer():
     12   ACTUAL_TEMPLATE = '.pdf.%d.png'
     13   EXPECTED_TEMPLATE = '_expected' + ACTUAL_TEMPLATE
     14   PLATFORM_EXPECTED_TEMPLATE = '_expected_%s' + ACTUAL_TEMPLATE
     15 
     16   def __init__(self, finder):
     17     self.pdfium_diff_path = finder.ExecutablePath('pdfium_diff')
     18     self.os_name = finder.os_name
     19 
     20   def GetActualFiles(self, input_filename, source_dir, working_dir):
     21     actual_paths = []
     22     template_paths = self._GetTemplatePaths(
     23         input_filename, source_dir, working_dir)
     24     actual_path_template = template_paths[0];
     25     expected_path_template = template_paths[1]
     26     platform_expected_path_template = template_paths[2]
     27     i = 0
     28     while True:
     29       actual_path = actual_path_template % i
     30       expected_path = expected_path_template % i
     31       platform_expected_path = (
     32           platform_expected_path_template % (self.os_name, i))
     33       if os.path.exists(platform_expected_path):
     34         expected_path = platform_expected_path
     35       elif not os.path.exists(expected_path):
     36         break
     37       actual_paths.append(actual_path)
     38       i += 1
     39     return actual_paths
     40 
     41   def HasDifferences(self, input_filename, source_dir, working_dir):
     42     template_paths = self._GetTemplatePaths(
     43         input_filename, source_dir, working_dir)
     44     actual_path_template = template_paths[0];
     45     expected_path_template = template_paths[1]
     46     platform_expected_path_template = template_paths[2]
     47     i = 0
     48     while True:
     49       actual_path = actual_path_template % i
     50       expected_path = expected_path_template % i
     51       # PDFium tests should be platform independent. Platform based results are
     52       # used to capture platform dependent implementations.
     53       platform_expected_path = (
     54           platform_expected_path_template % (self.os_name, i))
     55       if (not os.path.exists(expected_path) and
     56           not os.path.exists(platform_expected_path)):
     57         if i == 0:
     58           print "WARNING: no expected results files for " + input_filename
     59         break
     60       print "Checking " + actual_path
     61       sys.stdout.flush()
     62       if os.path.exists(expected_path):
     63         error = common.RunCommand(
     64             [self.pdfium_diff_path, expected_path, actual_path])
     65       else:
     66         error = 1;
     67       if error:
     68         # When failed, we check against platform based results.
     69         if os.path.exists(platform_expected_path):
     70           error = common.RunCommand(
     71               [self.pdfium_diff_path, platform_expected_path, actual_path])
     72         if error:
     73           print "FAILURE: " + input_filename + "; " + str(error)
     74           return True
     75       i += 1
     76     return False
     77 
     78   def _GetTemplatePaths(self, input_filename, source_dir, working_dir):
     79     input_root, _ = os.path.splitext(input_filename)
     80     actual_path = os.path.join(working_dir, input_root + self.ACTUAL_TEMPLATE)
     81     expected_path = os.path.join(
     82         source_dir, input_root + self.EXPECTED_TEMPLATE)
     83     platform_expected_path = os.path.join(
     84         source_dir, input_root + self.PLATFORM_EXPECTED_TEMPLATE)
     85     return (actual_path, expected_path, platform_expected_path)
     86