File: skia_gold_pixel_diff.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (183 lines) | stat: -rw-r--r-- 6,788 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_TEST_SKIA_GOLD_PIXEL_DIFF_H_
#define UI_BASE_TEST_SKIA_GOLD_PIXEL_DIFF_H_

#include <optional>
#include <string>

#include "base/auto_reset.h"
#include "base/containers/flat_map.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"

namespace base {
class CommandLine;
}

class SkBitmap;

namespace testing {
class TestInfo;
}

namespace ui {
namespace test {

// Keys used to identify test environment information that can possibly affect
// pixel output. If your test depends on some new environment information, add
// it here and update |TestEnvironmentKeyToString|.
enum class TestEnvironmentKey {
  // Being default-provided keys. These should not by used by subtypes.

  // The operating system name.
  kSystem,
  // The processor architecture.
  kProcessor,

  // Begin subtype-provided keys.\

  // The version of the operating system.
  // On Windows, this is the release ID string and is used to track the DWM
  // version.
  kSystemVersion,

  // The vendor name of the GPU used for ths test.
  kGpuDriverVendor,

  // The GPU driver version.
  kGpuDriverVersion,

  // The GL_RENDERER string returned from the GLContext.
  kGlRenderer,
};

using TestEnvironmentMap = base::flat_map<TestEnvironmentKey, std::string>;

class SkiaGoldMatchingAlgorithm;
// This is the utility class for Skia Gold pixeltest.
class SkiaGoldPixelDiff final {
 public:
  // Returns the platform used to generate this image. It is appended to the
  // golden name of the images uploaded to the Skia Gold Server. This is pubic
  // to be used in tests.
  static std::string GetPlatform();

  // Resets the global session cache used by |GetSession| on creation and
  // deletion. Invalidates pointers returned by |GetSession| in both
  // cases. Used to limit reuse of sessions when testing |SkiaGoldPixelDiff|
  // itself.
  class ScopedSessionCacheForTesting {
   public:
    ScopedSessionCacheForTesting();
    ~ScopedSessionCacheForTesting();

    ScopedSessionCacheForTesting(const ScopedSessionCacheForTesting&) = delete;
    ScopedSessionCacheForTesting& operator=(
        const ScopedSessionCacheForTesting&) = delete;
    ScopedSessionCacheForTesting(ScopedSessionCacheForTesting&&) = delete;
    ScopedSessionCacheForTesting& operator=(ScopedSessionCacheForTesting&&) =
        delete;
  };

  // Get or create and initialize a |SkiaGoldPixelDiff| instance with the
  // provided parameters.
  // Args:
  // corpus The corpus (i.e. result group) that will be used to store the
  //   result in Gold. If omitted, will default to the generic corpus for
  //   results from gtest-based tests.
  // test_environment A map containing any test-specific environment information
  //   to determine whether a new screenshot is good or not. All the information
  //   that can affect the output of pixels should be filled in. Eg: operating
  //   system, graphics card, processor architecture, screen resolution, etc.
  static SkiaGoldPixelDiff* GetSession(
      const std::optional<std::string>& corpus = {},
      TestEnvironmentMap test_environment = TestEnvironmentMap());

  // Allows passing in a mocked |LaunchProcessCallback|, which is reset when the
  // return value is destructed.
  using LaunchProcessCallback =
      base::RepeatingCallback<int(const base::CommandLine&)>;
  static base::AutoReset<LaunchProcessCallback> OverrideLaunchProcessForTesting(
      LaunchProcessCallback custom_launch_process);

  SkiaGoldPixelDiff(const SkiaGoldPixelDiff&) = delete;
  SkiaGoldPixelDiff& operator=(const SkiaGoldPixelDiff&) = delete;

  ~SkiaGoldPixelDiff();

  // Generate a golden image name from the test suite and test name from
  // |test_info| with an optional suffix. If the test is parameterized, the
  // parameterization with be added after the test name and before the suffix.
  // The result will be something like |{TestSuiteName}_{TestName}_{Suffix}|.
  static std::string GetGoldenImageName(
      const std::string& test_suite_name,
      const std::string& test_name,
      const std::optional<std::string>& suffix = {});

  static std::string GetGoldenImageName(
      const ::testing::TestInfo* test_info,
      const std::optional<std::string>& suffix = {});

  // golden_image_name
  //   For every screenshot you take, it should have a unique name across
  //   Chromium. This is because Skia Gold primarily uses the image name to
  //   determine which golden images to compare against. The standard convention
  //   is to use the test suite name and test name, e.g.
  //   `ToolbarTest_BackButtonHover`. See |GetGoldenImageName|.
  bool CompareScreenshot(
      const std::string& golden_image_name,
      const SkBitmap& bitmap,
      const SkiaGoldMatchingAlgorithm* algorithm = nullptr) const;

 private:
  SkiaGoldPixelDiff();

  void Init(const std::string& corpus, TestEnvironmentMap test_environment);

  void InitSkiaGold() const;

  // Upload the local file to Skia Gold server. Return true if the screenshot
  // is the same as the remote golden image.
  bool UploadToSkiaGoldServer(const base::FilePath& local_file_path,
                              const std::string& remote_golden_image_name,
                              const SkiaGoldMatchingAlgorithm* algorithm) const;

  // Calculate a diff and save locally to aid in local development.
  void GenerateLocalDiff(const std::string& remote_golden_image_name,
                         const base::FilePath& test_output_path) const;

  bool initialized_ = false;
  // True if bot mode is not enabled.
  bool is_dry_run_ = false;
  // Use luci auth on bots. Don't use luci auth for local development.
  bool luci_auth_ = true;
  // Which corpus in the instance to associate results with.
  std::string corpus_;
  // Build revision. This is only used for CI run.
  std::string build_revision_;
  // The following 3 members are for tryjob run.
  // Chagnelist issue id.
  std::string issue_;
  // Which patchset for a changelist.
  std::string patchset_;
  // Buildbucket build id.
  std::string job_id_;
  // Which code review system is being used, typically "gerrit" for Chromium
  // and "gerrit-internal" for Chrome.
  std::string code_review_system_;
  // The working dir for goldctl. It's the dir for storing temporary files.
  base::FilePath working_dir_;
  // The key-value pairs that will be associated with screenshot baselines.
  TestEnvironmentMap test_environment_;
  // Protects calls to |CompareScreenshot|, which modifies the working dir.
  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace test
}  // namespace ui

#endif  // UI_BASE_TEST_SKIA_GOLD_PIXEL_DIFF_H_