xref: /aosp_15_r20/external/pdfium/testing/tools/suppressor.py (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1#!/usr/bin/env python3
2# Copyright 2015 The PDFium Authors
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
7
8import common
9import pngdiffer
10
11
12class Suppressor:
13
14  def __init__(self, finder, features, js_disabled, xfa_disabled,
15               rendering_option):
16    self.has_v8 = not js_disabled and 'V8' in features
17    self.has_xfa = not js_disabled and not xfa_disabled and 'XFA' in features
18    self.rendering_option = rendering_option
19    self.suppression_set = self._LoadSuppressedSet('SUPPRESSIONS', finder)
20    self.image_suppression_set = self._LoadSuppressedSet(
21        'SUPPRESSIONS_IMAGE_DIFF', finder)
22    self.exact_matching_suppression_set = self._LoadSuppressedSet(
23        'SUPPRESSIONS_EXACT_MATCHING', finder)
24
25  def _LoadSuppressedSet(self, suppressions_filename, finder):
26    v8_option = "v8" if self.has_v8 else "nov8"
27    xfa_option = "xfa" if self.has_xfa else "noxfa"
28    with open(os.path.join(finder.TestingDir(), suppressions_filename)) as f:
29      return set(
30          self._FilterSuppressions(common.os_name(), v8_option, xfa_option,
31                                   self.rendering_option,
32                                   self._ExtractSuppressions(f)))
33
34  def _ExtractSuppressions(self, f):
35    return [
36        y.split(' ') for y in [x.split('#')[0].strip()
37                               for x in f.readlines()] if y
38    ]
39
40  def _FilterSuppressions(self, os_name, js, xfa, rendering_option,
41                          unfiltered_list):
42    return [
43        x[0]
44        for x in unfiltered_list
45        if self._MatchSuppression(x, os_name, js, xfa, rendering_option)
46    ]
47
48  def _MatchSuppression(self, item, os_name, js, xfa, rendering_option):
49    os_column = item[1].split(",")
50    js_column = item[2].split(",")
51    xfa_column = item[3].split(",")
52    rendering_option_column = item[4].split(",")
53    return (('*' in os_column or os_name in os_column) and
54            ('*' in js_column or js in js_column) and
55            ('*' in xfa_column or xfa in xfa_column) and
56            ('*' in rendering_option_column or
57             rendering_option in rendering_option_column))
58
59  def IsResultSuppressed(self, input_filename):
60    if input_filename in self.suppression_set:
61      print("%s result is suppressed" % input_filename)
62      return True
63    return False
64
65  def IsExecutionSuppressed(self, input_filepath):
66    if "xfa_specific" in input_filepath and not self.has_xfa:
67      print("%s execution is suppressed" % input_filepath)
68      return True
69    return False
70
71  def IsImageDiffSuppressed(self, input_filename):
72    if input_filename in self.image_suppression_set:
73      print("%s image diff comparison is suppressed" % input_filename)
74      return True
75    return False
76
77  def GetImageMatchingAlgorithm(self, input_filename):
78    if input_filename in self.exact_matching_suppression_set:
79      print(f"{input_filename} image diff comparison is fuzzy")
80      return pngdiffer.FUZZY_MATCHING
81    return pngdiffer.EXACT_MATCHING
82