Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
golden_digest_manager.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#include "flutter/fml/logging.h"
11#include "flutter/fml/paths.h"
12
13static const double kMaxDiffPixelsPercent = 0.01;
14static const int32_t kMaxColorDelta = 8;
15
16namespace impeller {
17namespace testing {
18
20 const std::string& working_directory_path)
21 : working_directory_(working_directory_path) {}
22
24 if (entries_written_ < entries_.size() ||
25 dimensions_written_ < dimensions_.size()) {
26 FML_LOG(WARNING) //
27 << "golden digest at " << working_directory_ //
28 << " incomplete, with only " //
29 << dimensions_written_ << " out of " //
30 << dimensions_.size() << " dimensions " //
31 << "and " //
32 << entries_written_ << " out of " //
33 << entries_.size() << " image entries" //
34 << " written";
35 }
36}
37
38const std::string& GoldenDigestManager::GetWorkingDirectory() const {
39 return working_directory_;
40}
41
43 const std::string& filename) const {
44 return fml::paths::JoinPaths({working_directory_, filename});
45}
46
48 const std::string& value) {
49 std::stringstream ss;
50 ss << "\"" << value << "\"";
51 dimensions_[name] = ss.str();
52}
53
54void GoldenDigestManager::AddImage(const std::string& test_name,
55 const std::string& filename,
56 int32_t width,
57 int32_t height) {
58 entries_.push_back({test_name, filename, width, height, kMaxDiffPixelsPercent,
60}
61
63 std::ofstream fout;
64 fout.open(GetFullPath("digest.json"));
65 if (!fout.good()) {
66 return false;
67 }
68
69 // If we wrote some stuff before, this will be an update on top of that
70 // work, rewriting the file again from the beginning. This allows test
71 // suites to defensively write the digest after every group of tests.
72 dimensions_written_ = 0u;
73 entries_written_ = 0u;
74
75 fout << "{" << std::endl;
76 fout << " \"dimensions\": {" << std::endl;
77 bool is_first = true;
78 for (const auto& dimension : dimensions_) {
79 if (!is_first) {
80 fout << "," << std::endl;
81 }
82 is_first = false;
83 fout << " \"" << dimension.first << "\": " << dimension.second;
84 dimensions_written_++;
85 }
86 fout << std::endl << " }," << std::endl;
87 fout << " \"entries\":" << std::endl;
88
89 fout << " [" << std::endl;
90 is_first = true;
91 for (const auto& entry : entries_) {
92 if (!is_first) {
93 fout << "," << std::endl;
94 }
95 is_first = false;
96
97 fout << " { " << "\"testName\" : \"" << entry.test_name << "\", "
98 << "\"filename\" : \"" << entry.filename << "\", "
99 << "\"width\" : " << entry.width << ", "
100 << "\"height\" : " << entry.height << ", ";
101
102 if (entry.max_diff_pixels_percent ==
103 static_cast<int64_t>(entry.max_diff_pixels_percent)) {
104 fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
105 << ".0, ";
106 } else {
107 fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
108 << ", ";
109 }
110
111 fout << "\"maxColorDelta\":" << entry.max_color_delta << " ";
112 fout << "}";
113 entries_written_++;
114 }
115 fout << std::endl << " ]" << std::endl;
116
117 fout << "}" << std::endl;
118
119 fout.close();
120 return true;
121}
122
124 dimensions_.clear();
125 entries_.clear();
126
127 dimensions_written_ = 0u;
128 entries_written_ = 0u;
129}
130
131} // namespace testing
132} // namespace impeller
void AddImage(const std::string &test_name, const std::string &golden_filename, int32_t width, int32_t height)
std::string GetFullPath(const std::string &filename) const
const std::string & GetWorkingDirectory() const
void AddDimension(const std::string &name, const std::string &value)
GoldenDigestManager(const std::string &working_directory)
int32_t value
#define FML_LOG(severity)
Definition logging.h:101
static const double kMaxDiffPixelsPercent
static const int32_t kMaxColorDelta
static const double kMaxDiffPixelsPercent
static const int32_t kMaxColorDelta
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
std::string JoinPaths(std::initializer_list< std::string > components)
Definition paths.cc:14
int32_t height
int32_t width