| 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_screenshotter.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<Screenshot> 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()
54 : screenshotter_(new MetalScreenshotter(/*enable_wide_gamut=*/false)) {}
55
56 MetalScreenshotter& Screenshotter() { return *screenshotter_; }
57
58 void SetUp() override {
59 testing::GoldenDigest::Instance()->AddDimension(
60 "gpu_string",
61 Screenshotter().GetPlayground().GetContext()->DescribeGpuModel());
62 }
63
64 private:
65 // This must be placed before any other members that may use the
66 // autorelease pool.
67 fml::ScopedNSAutoreleasePool autorelease_pool_;
68
69 std::unique_ptr<MetalScreenshotter> screenshotter_;
70 };
71
72 TEST_F(GoldenTests, ConicalGradient) {
73 Canvas canvas;
74 Paint paint;
75
76 paint.color_source = ColorSource::MakeConicalGradient(
77 {125, 125}, 125, {Color(1.0, 0.0, 0.0, 1.0), Color(0.0, 0.0, 1.0, 1.0)},
78 {0, 1}, {180, 180}, 0, Entity::TileMode::kClamp, {});
79
80 paint.stroke_width = 0.0;
81 paint.style = Paint::Style::kFill;
82 canvas.DrawRect(Rect::MakeXYWH(10, 10, 250, 250), paint);
83 Picture picture = canvas.EndRecordingAsPicture();
84
85 auto aiks_context =
86 AiksContext(Screenshotter().GetPlayground().GetContext(), nullptr);
87 auto screenshot = Screenshotter().MakeScreenshot(aiks_context, picture);
88 ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
89 }
90 } // namespace testing
91 } // namespace impeller
impeller::testing::Screenshotter::MakeScreenshot
virtual std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const Picture &picture, const ISize &size={300, 300}, bool scale_content=true)=0
impeller::Entity::TileMode::kClamp
@ kClamp
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:804
impeller::testing::GoldenTests
Definition: golden_tests.cc:51
impeller::AiksContext
Definition: aiks_context.h:19
impeller::testing::GoldenTests::Screenshotter
MetalScreenshotter & Screenshotter()
Definition: golden_tests.cc:56
impeller::Paint
Definition: paint.h:23
impeller::testing::Screenshotter
Definition: screenshotter.h:17
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
impeller::Color
Definition: color.h:124
golden_digest.h
aiks_context.h
impeller::Canvas
Definition: canvas.h:60
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::Canvas::DrawRect
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:495
path_builder.h
impeller::Picture
Definition: picture.h:17
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:180
metal_screenshot.h
conical_gradient_contents.h
impeller::Paint::Style::kFill
@ kFill
canvas.h
impeller::testing::TEST_F
TEST_F(GoldenTests, ConicalGradient)
Definition: golden_tests.cc:72
working_directory.h
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
paint
const Paint & paint
Definition: color_source.cc:38
impeller::testing::WorkingDirectory::Instance
static WorkingDirectory * Instance()
Definition: working_directory.cc:16
impeller::testing::GoldenTests::GoldenTests
GoldenTests()
Definition: golden_tests.cc:53
impeller::testing::MetalScreenshotter
Definition: metal_screenshotter.h:18
impeller::testing::GoldenTests::SetUp
void SetUp() override
Definition: golden_tests.cc:58
metal_screenshotter.h
impeller
Definition: aiks_blend_unittests.cc:18