Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bcc52d2

Browse files
committed
Adds the ability to specify a golden threadshold
1 parent 252f6cf commit bcc52d2

11 files changed

+40
-23
lines changed

impeller/aiks/aiks_playground.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ AiksPlayground::AiksPlayground() = default;
1414

1515
AiksPlayground::~AiksPlayground() = default;
1616

17-
bool AiksPlayground::OpenPlaygroundHere(const Picture& picture) {
17+
bool AiksPlayground::OpenPlaygroundHere(const Picture& picture,
18+
double threshold) {
1819
return OpenPlaygroundHere(
1920
[&picture](AiksContext& renderer, RenderTarget& render_target) -> bool {
2021
return renderer.Render(picture, render_target);

impeller/aiks/aiks_playground.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AiksPlayground : public PlaygroundTest {
2020

2121
~AiksPlayground();
2222

23-
bool OpenPlaygroundHere(const Picture& picture);
23+
bool OpenPlaygroundHere(const Picture& picture, double threshold = 0.01);
2424

2525
bool OpenPlaygroundHere(AiksPlaygroundCallback callback);
2626

impeller/aiks/aiks_unittests.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,9 +1317,6 @@ TEST_P(AiksTest, CanRenderTextOutsideBoundaries) {
13171317
}
13181318

13191319
TEST_P(AiksTest, TextRotated) {
1320-
#ifdef IMPELLER_GOLDEN_TESTS
1321-
GTEST_SKIP() << "Test has small differences on different mac hosts";
1322-
#endif
13231320
Canvas canvas;
13241321
canvas.Transform(Matrix(0.5, -0.3, 0, -0.002, //
13251322
0, 1, 0, 0, //
@@ -1330,7 +1327,8 @@ TEST_P(AiksTest, TextRotated) {
13301327
GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?",
13311328
"Roboto-Regular.ttf"));
13321329

1333-
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
1330+
ASSERT_TRUE(
1331+
OpenPlaygroundHere(canvas.EndRecordingAsPicture(), /*threshold=*/0.1));
13341332
}
13351333

13361334
TEST_P(AiksTest, CanDrawPaint) {

impeller/golden_tests/golden_digest.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ GoldenDigest::GoldenDigest() {}
2323
void GoldenDigest::AddImage(const std::string& test_name,
2424
const std::string& filename,
2525
int32_t width,
26-
int32_t height) {
27-
entries_.push_back({test_name, filename, width, height});
26+
int32_t height,
27+
double threshold) {
28+
entries_.push_back({test_name, filename, width, height, threshold});
2829
}
2930

3031
bool GoldenDigest::Write(WorkingDirectory* working_directory) {
@@ -46,8 +47,15 @@ bool GoldenDigest::Write(WorkingDirectory* working_directory) {
4647
<< "\"testName\" : \"" << entry.test_name << "\", "
4748
<< "\"filename\" : \"" << entry.filename << "\", "
4849
<< "\"width\" : " << entry.width << ", "
49-
<< "\"height\" : " << entry.height << " "
50-
<< "}";
50+
<< "\"height\" : " << entry.height << ", ";
51+
52+
if (entry.threshold == static_cast<int64_t>(entry.threshold)) {
53+
fout << "\"threshold\" : " << entry.threshold << ".0 ";
54+
} else {
55+
fout << "\"threshold\" : " << entry.threshold << " ";
56+
}
57+
58+
fout << "}";
5159
}
5260
fout << std::endl << "]" << std::endl;
5361

impeller/golden_tests/golden_digest.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class GoldenDigest {
2121
void AddImage(const std::string& test_name,
2222
const std::string& filename,
2323
int32_t width,
24-
int32_t height);
24+
int32_t height,
25+
double threshold);
2526

2627
/// Writes a "digest.json" file to `working_directory`.
2728
///
@@ -36,6 +37,7 @@ class GoldenDigest {
3637
std::string filename;
3738
int32_t width;
3839
int32_t height;
40+
double threshold;
3941
};
4042

4143
static GoldenDigest* instance_;

impeller/golden_tests/golden_playground_test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GoldenPlaygroundTest
2626

2727
PlaygroundBackend GetBackend() const;
2828

29-
bool OpenPlaygroundHere(const Picture& picture);
29+
bool OpenPlaygroundHere(const Picture& picture, double threshold = 0.01);
3030

3131
bool OpenPlaygroundHere(const AiksPlaygroundCallback& callback);
3232

impeller/golden_tests/golden_playground_test_mac.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ std::string GetGoldenFilename() {
2828
return GetTestName() + ".png";
2929
}
3030

31-
bool SaveScreenshot(std::unique_ptr<testing::MetalScreenshot> screenshot) {
31+
bool SaveScreenshot(std::unique_ptr<testing::MetalScreenshot> screenshot,
32+
double threshold) {
3233
if (!screenshot || !screenshot->GetBytes()) {
3334
return false;
3435
}
3536
std::string test_name = GetTestName();
3637
std::string filename = GetGoldenFilename();
3738
testing::GoldenDigest::Instance()->AddImage(
38-
test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
39+
test_name, filename, screenshot->GetWidth(), screenshot->GetHeight(),
40+
threshold);
3941
return screenshot->WriteToPNG(
4042
testing::WorkingDirectory::Instance()->GetFilenamePath(filename));
4143
}
@@ -96,10 +98,11 @@ PlaygroundBackend GoldenPlaygroundTest::GetBackend() const {
9698
return GetParam();
9799
}
98100

99-
bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture) {
101+
bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture,
102+
double threshold) {
100103
auto screenshot =
101104
pimpl_->screenshoter_->MakeScreenshot(picture, pimpl_->window_size_);
102-
return SaveScreenshot(std::move(screenshot));
105+
return SaveScreenshot(std::move(screenshot), threshold);
103106
}
104107

105108
bool GoldenPlaygroundTest::OpenPlaygroundHere(

impeller/golden_tests/golden_playground_test_stub.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ PlaygroundBackend GoldenPlaygroundTest::GetBackend() const {
1616
return GetParam();
1717
}
1818

19-
bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture) {
19+
bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture,
20+
double threshold) {
2021
return false;
2122
}
2223

impeller/golden_tests/golden_tests.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ std::string GetGoldenFilename() {
3232
return GetTestName() + ".png";
3333
}
3434

35-
bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot) {
35+
bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot,
36+
double threshold) {
3637
if (!screenshot || !screenshot->GetBytes()) {
3738
return false;
3839
}
3940
std::string test_name = GetTestName();
4041
std::string filename = GetGoldenFilename();
41-
GoldenDigest::Instance()->AddImage(
42-
test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
42+
GoldenDigest::Instance()->AddImage(test_name, filename,
43+
screenshot->GetWidth(),
44+
screenshot->GetHeight(), threshold);
4345
return screenshot->WriteToPNG(
4446
WorkingDirectory::Instance()->GetFilenamePath(filename));
4547
}
@@ -73,7 +75,7 @@ TEST_F(GoldenTests, ConicalGradient) {
7375
canvas.DrawRect(Rect(10, 10, 250, 250), paint);
7476
Picture picture = canvas.EndRecordingAsPicture();
7577
auto screenshot = Screenshoter().MakeScreenshot(picture);
76-
ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
78+
ASSERT_TRUE(SaveScreenshot(std::move(screenshot), 0.01));
7779
}
7880
} // namespace testing
7981
} // namespace impeller

impeller/golden_tests_harvester/bin/golden_tests_harvester.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FakeSkiaGoldClient implements SkiaGoldClient {
2323
{double differentPixelsRate = 0.01,
2424
int pixelColorDelta = 0,
2525
required int screenshotSize}) async {
26-
Logger.instance.log('addImg $testName ${goldenFile.path} $screenshotSize');
26+
Logger.instance.log('addImg testName:$testName goldenFile:${goldenFile.path} screenshotSize:$screenshotSize differentPixelsRate:$differentPixelsRate');
2727
}
2828

2929
@override

impeller/golden_tests_harvester/lib/golden_tests_harvester.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ Future<void> harvest(
3232
final String filename = (map['filename'] as String?)!;
3333
final int width = (map['width'] as int?)!;
3434
final int height = (map['height'] as int?)!;
35+
final double threshold = (map['threshold'] as double?)!;
3536
final File goldenImage = File(p.join(workDirectory.path, filename));
3637
final Future<void> future = skiaGoldClient
37-
.addImg(filename, goldenImage, screenshotSize: width * height)
38+
.addImg(filename, goldenImage,
39+
screenshotSize: width * height, differentPixelsRate: threshold)
3840
.catchError((dynamic err) {
3941
Logger.instance.log('skia gold comparison failed: $err');
4042
throw Exception('Failed comparison: $filename');

0 commit comments

Comments
 (0)