| Flutter Impeller |

golden_tests.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
5 #include "gtest/gtest.h"
6
7 #include <sstream>
8
9 #include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
10 #include "impeller/aiks/aiks_context.h"
11 #include "impeller/aiks/canvas.h"
12 #include "impeller/entity/contents/conical_gradient_contents.h"
13 #include "impeller/geometry/path_builder.h"
14 #include "impeller/golden_tests/golden_digest.h"
15 #include "impeller/golden_tests/metal_screenshot.h"
16 #include "impeller/golden_tests/metal_screenshoter.h"
17 #include "impeller/golden_tests/working_directory.h"
18
19 namespace impeller {
20 namespace testing {
21
22 namespace {
23 std::string GetTestName() {
24 std::string suite_name =
25 ::testing::UnitTest::GetInstance()->current_test_suite()->name();
26 std::string test_name =
27 ::testing::UnitTest::GetInstance()->current_test_info()->name();
28 std::stringstream ss;
29 ss << "impeller_" << suite_name << "_" << test_name;
30 return ss.str();
31 }
32
33 std::string GetGoldenFilename() {
34 return GetTestName() + ".png";
35 }
36
37 bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot) {
38 if (!screenshot || !screenshot->GetBytes()) {
39 return false;
40 }
41 std::string test_name = GetTestName();
42 std::string filename = GetGoldenFilename();
43 GoldenDigest::Instance()->AddImage(
44 test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
45 return screenshot->WriteToPNG(
46 WorkingDirectory::Instance()->GetFilenamePath(filename));
47 }
48
49 } // namespace
50
51 class GoldenTests : public ::testing::Test {
52 public:
53 GoldenTests() : screenshoter_(new MetalScreenshoter()) {}
54
55 MetalScreenshoter& Screenshoter() { return *screenshoter_; }
56
57 void SetUp() override {
58 testing::GoldenDigest::Instance()->AddDimension(
59 "gpu_string",
60 Screenshoter().GetPlayground().GetContext()->DescribeGpuModel());
61 }
62
63 private:
64 // This must be placed before any other members that may use the
65 // autorelease pool.
66 fml::ScopedNSAutoreleasePool autorelease_pool_;
67
68 std::unique_ptr<MetalScreenshoter> screenshoter_;
69 };
70
71 TEST_F(GoldenTests, ConicalGradient) {
72 Canvas canvas;
73 Paint paint;
74
75 paint.color_source = ColorSource::MakeConicalGradient(
76 {125, 125}, 125, {Color(1.0, 0.0, 0.0, 1.0), Color(0.0, 0.0, 1.0, 1.0)},
77 {0, 1}, {180, 180}, 0, Entity::TileMode::kClamp, {});
78
79 paint.stroke_width = 0.0;
80 paint.style = Paint::Style::kFill;
81 canvas.DrawRect(Rect(10, 10, 250, 250), paint);
82 Picture picture = canvas.EndRecordingAsPicture();
83
84 auto aiks_context =
85 AiksContext(Screenshoter().GetPlayground().GetContext(), nullptr);
86 auto screenshot = Screenshoter().MakeScreenshot(aiks_context, picture);
87 ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
88 }
89 } // namespace testing
90 } // namespace impeller
impeller::Entity::TileMode::kClamp
@ kClamp
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:519
impeller::testing::GoldenTests
Definition: golden_tests.cc:51
impeller::AiksContext
Definition: aiks_context.h:20
impeller::Paint
Definition: paint.h:25
impeller::Color
Definition: color.h:122
golden_digest.h
aiks_context.h
impeller::Canvas
Definition: canvas.h:50
impeller::testing::GoldenTests::Screenshoter
MetalScreenshoter & Screenshoter()
Definition: golden_tests.cc:55
metal_screenshoter.h
impeller::testing::GoldenDigest::AddImage
void AddImage(const std::string &test_name, const std::string &filename, int32_t width, int32_t height)
Definition: golden_digest.cc:34
impeller::Paint::color_source
ColorSource color_source
Definition: paint.h:55
path_builder.h
impeller::Picture
Definition: picture.h:19
impeller::testing::TEST_F
TEST_F(ArchiveTest, SimpleInitialization)
Definition: archivist_unittests.cc:108
impeller::ColorSource::MakeConicalGradient
static ColorSource MakeConicalGradient(Point center, Scalar radius, std::vector< Color > colors, std::vector< Scalar > stops, Point focus_center, Scalar focus_radius, Entity::TileMode tile_mode, Matrix effect_transform)
Definition: color_source.cc:75
metal_screenshot.h
conical_gradient_contents.h
impeller::Paint::style
Style style
Definition: paint.h:62
impeller::Paint::Style::kFill
@ kFill
canvas.h
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
working_directory.h
impeller::testing::MetalScreenshoter
Converts Pictures to MetalScreenshots with the playground backend.
Definition: metal_screenshoter.h:16
impeller::testing::GoldenDigest::Instance
static GoldenDigest * Instance()
Definition: golden_digest.cc:18
impeller::testing::GoldenDigest::AddDimension
void AddDimension(const std::string &name, const std::string &value)
Definition: golden_digest.cc:27
impeller::Canvas::DrawRect
void DrawRect(Rect rect, const Paint &paint)
Definition: canvas.cc:235
impeller::testing::WorkingDirectory::Instance
static WorkingDirectory * Instance()
Definition: working_directory.cc:16
impeller::testing::GoldenTests::GoldenTests
GoldenTests()
Definition: golden_tests.cc:53
impeller::testing::GoldenTests::SetUp
void SetUp() override
Definition: golden_tests.cc:57
impeller
Definition: aiks_context.cc:10
impeller::Paint::stroke_width
Scalar stroke_width
Definition: paint.h:58