1e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#!/usr/bin/env python
2e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov# Copyright 2015 The PDFium Authors. All rights reserved.
3e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov# Use of this source code is governed by a BSD-style license that can be
4e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov# found in the LICENSE file.
5e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
6e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovimport os
7e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovimport subprocess
8e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovimport sys
9e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
10e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovclass PNGDiffer():
11e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  ACTUAL_TEMPLATE = '.pdf.%d.png'
12e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  EXPECTED_TEMPLATE = '_expected' + ACTUAL_TEMPLATE
13e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  PLATFORM_EXPECTED_TEMPLATE = '_expected_%s' + ACTUAL_TEMPLATE
14e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
15e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  def __init__(self, finder):
16e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    self.pdfium_diff_path = finder.ExecutablePath('pdfium_diff')
17e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    self.os_name = finder.os_name
18e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
19e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov  def HasDifferences(self, input_filename, source_dir, working_dir):
20e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    input_root, _ = os.path.splitext(input_filename)
21e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    actual_path_template = os.path.join(
22e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        working_dir, input_root + self.ACTUAL_TEMPLATE)
23e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    expected_path_template = os.path.join(
24e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        source_dir, input_root + self.EXPECTED_TEMPLATE)
25e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    platform_expected_path_template = os.path.join(
26e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        source_dir, input_root + self.PLATFORM_EXPECTED_TEMPLATE)
27e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    i = 0
28e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    try:
29e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      while True:
30e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        actual_path = actual_path_template % i
31e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        expected_path = expected_path_template % i
32e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        platform_expected_path = (
33e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov            platform_expected_path_template % (self.os_name, i))
34e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        if os.path.exists(platform_expected_path):
35e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov          expected_path = platform_expected_path
36e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        elif not os.path.exists(expected_path):
37e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov          if i == 0:
38e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov            print "WARNING: no expected results files for " + input_filename
39e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov          break
40e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        print "Checking " + actual_path
41e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        sys.stdout.flush()
42e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        subprocess.check_call(
43e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov            [self.pdfium_diff_path, expected_path, actual_path])
44e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov        i += 1
45e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    except subprocess.CalledProcessError as e:
46e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      print "FAILURE: " + input_filename + "; " + str(e)
47e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov      return True
48e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov    return False
49