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"
11 #include "impeller/aiks/canvas.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();
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() : screenshotter_(new MetalScreenshotter()) {}
54 
55  MetalScreenshotter& Screenshotter() { return *screenshotter_; }
56 
57  void SetUp() override {
59  "gpu_string",
60  Screenshotter().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<MetalScreenshotter> screenshotter_;
69 };
70 
71 TEST_F(GoldenTests, ConicalGradient) {
72  Canvas canvas;
73  Paint paint;
74 
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::MakeXYWH(10, 10, 250, 250), paint);
82  Picture picture = canvas.EndRecordingAsPicture();
83 
84  auto aiks_context =
85  AiksContext(Screenshotter().GetPlayground().GetContext(), nullptr);
86  auto screenshot = Screenshotter().MakeScreenshot(aiks_context, picture);
87  ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
88 }
89 } // namespace testing
90 } // 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:756
impeller::testing::GoldenTests
Definition: golden_tests.cc:51
impeller::AiksContext
Definition: aiks_context.h:20
impeller::testing::GoldenTests::Screenshotter
MetalScreenshotter & Screenshotter()
Definition: golden_tests.cc:55
impeller::Paint
Definition: paint.h:23
impeller::testing::Screenshotter
Definition: screenshotter.h:18
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:58
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:56
impeller::Canvas::DrawRect
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:441
path_builder.h
impeller::Picture
Definition: picture.h:20
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:74
metal_screenshot.h
conical_gradient_contents.h
impeller::Paint::style
Style style
Definition: paint.h:63
impeller::Paint::Style::kFill
@ kFill
canvas.h
impeller::testing::TEST_F
TEST_F(GoldenTests, ConicalGradient)
Definition: golden_tests.cc:71
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
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:19
impeller::testing::GoldenTests::SetUp
void SetUp() override
Definition: golden_tests.cc:57
metal_screenshotter.h
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Paint::stroke_width
Scalar stroke_width
Definition: paint.h:59