forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Impeller] Adds golden image tests. (flutter#40366)
Added golden image tests to impeller
- Loading branch information
Showing
27 changed files
with
881 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2013 The Flutter Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import("//flutter/common/config.gni") | ||
import("//flutter/impeller/tools/impeller.gni") | ||
|
||
if (is_mac) { | ||
test_fixtures("impeller_golden_tests_fixtures") { | ||
fixtures = [] | ||
} | ||
|
||
impeller_component("impeller_golden_tests") { | ||
target_type = "executable" | ||
|
||
testonly = true | ||
|
||
sources = [ | ||
"golden_digest.cc", | ||
"golden_digest.h", | ||
"golden_tests.cc", | ||
"main.cc", | ||
"metal_screenshot.h", | ||
"metal_screenshot.mm", | ||
"metal_screenshoter.h", | ||
"metal_screenshoter.mm", | ||
"working_directory.cc", | ||
"working_directory.h", | ||
] | ||
|
||
deps = [ | ||
":impeller_golden_tests_fixtures", | ||
"//flutter/impeller/aiks", | ||
"//flutter/impeller/playground", | ||
"//flutter/impeller/renderer/backend/metal:metal", | ||
"//third_party/googletest:gtest", | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Impeller Golden Tests | ||
|
||
This is the executable that will generate the golden image results that can then | ||
be sent to Skia Gold vial the | ||
[golden_tests_harvester]("../golden_tests_harvester"). | ||
|
||
Running these tests should happen from | ||
[//flutter/testing/run_tests.py](../../testing/run_tests.py). That will do all | ||
the steps to generate the golden images and transmit them to Skia Gold. If you | ||
run the tests locally it will not actually upload anything. That only happens if | ||
the script is executed from LUCI. | ||
|
||
Example invocation: | ||
|
||
```sh | ||
./run_tests.py --variant="host_debug_unopt_arm64" --type="impeller-golden" | ||
``` | ||
|
||
Currently these tests are only supported on macOS and only test the Metal | ||
backend to Impeller. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "impeller/golden_tests/golden_digest.h" | ||
|
||
#include <fstream> | ||
|
||
namespace impeller { | ||
namespace testing { | ||
|
||
GoldenDigest* GoldenDigest::instance_ = nullptr; | ||
|
||
GoldenDigest* GoldenDigest::Instance() { | ||
if (!instance_) { | ||
instance_ = new GoldenDigest(); | ||
} | ||
return instance_; | ||
} | ||
|
||
GoldenDigest::GoldenDigest() {} | ||
|
||
void GoldenDigest::AddImage(const std::string& test_name, | ||
const std::string& filename, | ||
int32_t width, | ||
int32_t height) { | ||
entries_.push_back({test_name, filename, width, height}); | ||
} | ||
|
||
bool GoldenDigest::Write(WorkingDirectory* working_directory) { | ||
std::ofstream fout; | ||
fout.open(working_directory->GetFilenamePath("digest.json")); | ||
if (!fout.good()) { | ||
return false; | ||
} | ||
|
||
fout << "[" << std::endl; | ||
bool is_first = true; | ||
for (const auto& entry : entries_) { | ||
if (!is_first) { | ||
fout << "," << std::endl; | ||
is_first = false; | ||
} | ||
fout << " { " | ||
<< "\"testName\" : \"" << entry.test_name << "\", " | ||
<< "\"filename\" : \"" << entry.filename << "\", " | ||
<< "\"width\" : " << entry.width << ", " | ||
<< "\"height\" : " << entry.height << " " | ||
<< "}"; | ||
} | ||
fout << std::endl << "]" << std::endl; | ||
|
||
fout.close(); | ||
return true; | ||
} | ||
|
||
} // namespace testing | ||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "flutter/impeller/golden_tests/working_directory.h" | ||
|
||
namespace impeller { | ||
namespace testing { | ||
|
||
/// Manages a global variable for tracking instances of golden images. | ||
class GoldenDigest { | ||
public: | ||
static GoldenDigest* Instance(); | ||
|
||
void AddImage(const std::string& test_name, | ||
const std::string& filename, | ||
int32_t width, | ||
int32_t height); | ||
|
||
/// Writes a "digest.json" file to `working_directory`. | ||
/// | ||
/// Returns `true` on success. | ||
bool Write(WorkingDirectory* working_directory); | ||
|
||
private: | ||
FML_DISALLOW_COPY_AND_ASSIGN(GoldenDigest); | ||
GoldenDigest(); | ||
struct Entry { | ||
std::string test_name; | ||
std::string filename; | ||
int32_t width; | ||
int32_t height; | ||
}; | ||
|
||
static GoldenDigest* instance_; | ||
std::vector<Entry> entries_; | ||
}; | ||
} // namespace testing | ||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include <sstream> | ||
|
||
#include "impeller/aiks/canvas.h" | ||
#include "impeller/entity/contents/conical_gradient_contents.h" | ||
#include "impeller/geometry/path_builder.h" | ||
#include "impeller/golden_tests/golden_digest.h" | ||
#include "impeller/golden_tests/metal_screenshot.h" | ||
#include "impeller/golden_tests/metal_screenshoter.h" | ||
#include "impeller/golden_tests/working_directory.h" | ||
|
||
namespace impeller { | ||
namespace testing { | ||
|
||
namespace { | ||
std::string GetTestName() { | ||
std::string suite_name = | ||
::testing::UnitTest::GetInstance()->current_test_suite()->name(); | ||
std::string test_name = | ||
::testing::UnitTest::GetInstance()->current_test_info()->name(); | ||
std::stringstream ss; | ||
ss << "impeller_" << suite_name << "_" << test_name; | ||
return ss.str(); | ||
} | ||
|
||
std::string GetGoldenFilename() { | ||
return GetTestName() + ".png"; | ||
} | ||
|
||
bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot) { | ||
if (!screenshot || !screenshot->GetBytes()) { | ||
return false; | ||
} | ||
std::string test_name = GetTestName(); | ||
std::string filename = GetGoldenFilename(); | ||
GoldenDigest::Instance()->AddImage( | ||
test_name, filename, screenshot->GetWidth(), screenshot->GetHeight()); | ||
return screenshot->WriteToPNG( | ||
WorkingDirectory::Instance()->GetFilenamePath(filename)); | ||
} | ||
} // namespace | ||
|
||
class GoldenTests : public ::testing::Test { | ||
public: | ||
GoldenTests() : screenshoter_(new MetalScreenshoter()) {} | ||
|
||
MetalScreenshoter& Screenshoter() { return *screenshoter_; } | ||
|
||
private: | ||
std::unique_ptr<MetalScreenshoter> screenshoter_; | ||
}; | ||
|
||
TEST_F(GoldenTests, ConicalGradient) { | ||
Canvas canvas; | ||
Paint paint; | ||
paint.color_source_type = Paint::ColorSourceType::kConicalGradient; | ||
paint.color_source = []() { | ||
auto result = std::make_shared<ConicalGradientContents>(); | ||
result->SetCenterAndRadius(Point(125, 125), 125); | ||
result->SetColors({Color(1.0, 0.0, 0.0, 1.0), Color(0.0, 0.0, 1.0, 1.0)}); | ||
result->SetStops({0, 1}); | ||
result->SetFocus(Point(180, 180), 0); | ||
result->SetTileMode(Entity::TileMode::kClamp); | ||
return result; | ||
}; | ||
paint.stroke_width = 0.0; | ||
paint.style = Paint::Style::kFill; | ||
canvas.DrawRect(Rect(10, 10, 250, 250), paint); | ||
Picture picture = canvas.EndRecordingAsPicture(); | ||
auto screenshot = Screenshoter().MakeScreenshot(std::move(picture)); | ||
ASSERT_TRUE(SaveScreenshot(std::move(screenshot))); | ||
} | ||
} // namespace testing | ||
} // namespace impeller |
Oops, something went wrong.