Flutter Impeller
golden_digest.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <fstream>
8 #include <sstream>
9 
10 static const double kMaxDiffPixelsPercent = 0.01;
11 static const int32_t kMaxColorDelta = 8;
12 
13 namespace impeller {
14 namespace testing {
15 
16 GoldenDigest* GoldenDigest::instance_ = nullptr;
17 
19  if (!instance_) {
20  instance_ = new GoldenDigest();
21  }
22  return instance_;
23 }
24 
25 GoldenDigest::GoldenDigest() {}
26 
27 void GoldenDigest::AddDimension(const std::string& name,
28  const std::string& value) {
29  std::stringstream ss;
30  ss << "\"" << value << "\"";
31  dimensions_[name] = ss.str();
32 }
33 
34 void GoldenDigest::AddImage(const std::string& test_name,
35  const std::string& filename,
36  int32_t width,
37  int32_t height) {
38  entries_.push_back({test_name, filename, width, height, kMaxDiffPixelsPercent,
40 }
41 
42 bool GoldenDigest::Write(WorkingDirectory* working_directory) {
43  std::ofstream fout;
44  fout.open(working_directory->GetFilenamePath("digest.json"));
45  if (!fout.good()) {
46  return false;
47  }
48 
49  fout << "{" << std::endl;
50  fout << " \"dimensions\": {" << std::endl;
51  bool is_first = true;
52  for (const auto& dimension : dimensions_) {
53  if (!is_first) {
54  fout << "," << std::endl;
55  }
56  is_first = false;
57  fout << " \"" << dimension.first << "\": " << dimension.second;
58  }
59  fout << std::endl << " }," << std::endl;
60  fout << " \"entries\":" << std::endl;
61 
62  fout << " [" << std::endl;
63  is_first = true;
64  for (const auto& entry : entries_) {
65  if (!is_first) {
66  fout << "," << std::endl;
67  }
68  is_first = false;
69 
70  fout << " { " << "\"testName\" : \"" << entry.test_name << "\", "
71  << "\"filename\" : \"" << entry.filename << "\", "
72  << "\"width\" : " << entry.width << ", "
73  << "\"height\" : " << entry.height << ", ";
74 
75  if (entry.max_diff_pixels_percent ==
76  static_cast<int64_t>(entry.max_diff_pixels_percent)) {
77  fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
78  << ".0, ";
79  } else {
80  fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
81  << ", ";
82  }
83 
84  fout << "\"maxColorDelta\":" << entry.max_color_delta << " ";
85  fout << "}";
86  }
87  fout << std::endl << " ]" << std::endl;
88 
89  fout << "}" << std::endl;
90 
91  fout.close();
92  return true;
93 }
94 
95 } // namespace testing
96 } // namespace impeller
Manages a global variable for tracking instances of golden images.
Definition: golden_digest.h:18
bool Write(WorkingDirectory *working_directory)
static GoldenDigest * Instance()
void AddDimension(const std::string &name, const std::string &value)
void AddImage(const std::string &test_name, const std::string &filename, int32_t width, int32_t height)
std::string GetFilenamePath(const std::string &filename) const
int32_t value
static const double kMaxDiffPixelsPercent
static const int32_t kMaxColorDelta