From f2452b265b58a58a8d9f293c646c778802377b56 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 30 Oct 2023 15:28:45 -0700 Subject: [PATCH] Consolidate all the copies of the OpenFixtureAsSkData testing helper function --- impeller/aiks/aiks_unittests.cc | 21 ++------- impeller/display_list/dl_playground.cc | 17 +------- impeller/typographer/typographer_unittests.cc | 19 +------- .../painting/image_decoder_no_gl_unittests.cc | 34 +-------------- .../painting/image_decoder_no_gl_unittests.h | 1 - lib/ui/painting/image_decoder_unittests.cc | 43 +++++++++++-------- shell/common/shell_io_manager_unittests.cc | 32 +------------- testing/testing.cc | 15 +++++++ testing/testing.h | 12 ++++++ 9 files changed, 60 insertions(+), 134 deletions(-) diff --git a/impeller/aiks/aiks_unittests.cc b/impeller/aiks/aiks_unittests.cc index 1ee111f138dc4..c045a89f23763 100644 --- a/impeller/aiks/aiks_unittests.cc +++ b/impeller/aiks/aiks_unittests.cc @@ -1348,21 +1348,6 @@ TEST_P(AiksTest, CanDrawAnOpenPathThatIsntARect) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -static sk_sp OpenFixtureAsSkData(const char* fixture_name) { - auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name); - if (!mapping) { - return nullptr; - } - auto data = SkData::MakeWithProc( - mapping->GetMapping(), mapping->GetSize(), - [](const void* ptr, void* context) { - delete reinterpret_cast(context); - }, - mapping.get()); - mapping.release(); - return data; -} - struct TextRenderOptions { Scalar font_size = 50; Scalar alpha = 1; @@ -1383,7 +1368,7 @@ bool RenderTextInCanvasSkia(const std::shared_ptr& context, Paint{.color = Color::Red().WithAlpha(0.25)}); // Construct the text blob. - auto mapping = OpenFixtureAsSkData(font_fixture.c_str()); + auto mapping = flutter::testing::OpenFixtureAsSkData(font_fixture.c_str()); if (!mapping) { return false; } @@ -1560,7 +1545,7 @@ TEST_P(AiksTest, CanRenderTextOutsideBoundaries) { canvas.Translate({200, 150}); // Construct the text blob. - auto mapping = OpenFixtureAsSkData("wtf.otf"); + auto mapping = flutter::testing::OpenFixtureAsSkData("wtf.otf"); ASSERT_NE(mapping, nullptr); Scalar font_size = 80; @@ -3366,7 +3351,7 @@ TEST_P(AiksTest, CanDrawPointsWithTextureMap) { // moved into DLDispatching. Path data requires the SkTextBlobs which are not // used in impeller::TextFrames. TEST_P(AiksTest, TextForegroundShaderWithTransform) { - auto mapping = OpenFixtureAsSkData("Roboto-Regular.ttf"); + auto mapping = flutter::testing::OpenFixtureAsSkData("Roboto-Regular.ttf"); ASSERT_NE(mapping, nullptr); Scalar font_size = 100; diff --git a/impeller/display_list/dl_playground.cc b/impeller/display_list/dl_playground.cc index aa47f81850ebe..8565e9b1f9cbc 100644 --- a/impeller/display_list/dl_playground.cc +++ b/impeller/display_list/dl_playground.cc @@ -52,24 +52,9 @@ bool DlPlayground::OpenPlaygroundHere(DisplayListPlaygroundCallback callback) { }); } -static sk_sp OpenFixtureAsSkData(const char* fixture_name) { - auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name); - if (!mapping) { - return nullptr; - } - auto data = SkData::MakeWithProc( - mapping->GetMapping(), mapping->GetSize(), - [](const void* ptr, void* context) { - delete reinterpret_cast(context); - }, - mapping.get()); - mapping.release(); - return data; -} - SkFont DlPlayground::CreateTestFontOfSize(SkScalar scalar) { static constexpr const char* kTestFontFixture = "Roboto-Regular.ttf"; - auto mapping = OpenFixtureAsSkData(kTestFontFixture); + auto mapping = flutter::testing::OpenFixtureAsSkData(kTestFontFixture); FML_CHECK(mapping); return SkFont{SkTypeface::MakeFromData(mapping), scalar}; } diff --git a/impeller/typographer/typographer_unittests.cc b/impeller/typographer/typographer_unittests.cc index eb3581d750f0b..6488783dd488a 100644 --- a/impeller/typographer/typographer_unittests.cc +++ b/impeller/typographer/typographer_unittests.cc @@ -86,26 +86,11 @@ TEST_P(TypographerTest, CanCreateGlyphAtlas) { .has_value()); } -static sk_sp OpenFixtureAsSkData(const char* fixture_name) { - auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name); - if (!mapping) { - return nullptr; - } - auto data = SkData::MakeWithProc( - mapping->GetMapping(), mapping->GetSize(), - [](const void* ptr, void* context) { - delete reinterpret_cast(context); - }, - mapping.get()); - mapping.release(); - return data; -} - TEST_P(TypographerTest, LazyAtlasTracksColor) { #if FML_OS_MACOSX - auto mapping = OpenFixtureAsSkData("Apple Color Emoji.ttc"); + auto mapping = flutter::testing::OpenFixtureAsSkData("Apple Color Emoji.ttc"); #else - auto mapping = OpenFixtureAsSkData("NotoColorEmoji.ttf"); + auto mapping = flutter::testing::OpenFixtureAsSkData("NotoColorEmoji.ttf"); #endif ASSERT_TRUE(mapping); SkFont emoji_font(SkTypeface::MakeFromData(mapping), 50.0); diff --git a/lib/ui/painting/image_decoder_no_gl_unittests.cc b/lib/ui/painting/image_decoder_no_gl_unittests.cc index 5ed339303b4da..975282328b48b 100644 --- a/lib/ui/painting/image_decoder_no_gl_unittests.cc +++ b/lib/ui/painting/image_decoder_no_gl_unittests.cc @@ -73,41 +73,11 @@ float DecodeBGR10(uint32_t x) { return (x * slope) + intercept; } -sk_sp OpenFixtureAsSkData(const char* name) { - auto fixtures_directory = - fml::OpenDirectory(GetFixturesPath(), false, fml::FilePermission::kRead); - if (!fixtures_directory.is_valid()) { - return nullptr; - } - - auto fixture_mapping = - fml::FileMapping::CreateReadOnly(fixtures_directory, name); - - if (!fixture_mapping) { - return nullptr; - } - - SkData::ReleaseProc on_release = [](const void* ptr, void* context) -> void { - delete reinterpret_cast(context); - }; - - auto data = SkData::MakeWithProc(fixture_mapping->GetMapping(), - fixture_mapping->GetSize(), on_release, - fixture_mapping.get()); - - if (!data) { - return nullptr; - } - // The data is now owned by Skia. - fixture_mapping.release(); - return data; -} - TEST(ImageDecoderNoGLTest, ImpellerWideGamutDisplayP3) { #if defined(OS_FUCHSIA) GTEST_SKIP() << "Fuchsia can't load the test fixtures."; #endif - auto data = OpenFixtureAsSkData("DisplayP3Logo.png"); + auto data = flutter::testing::OpenFixtureAsSkData("DisplayP3Logo.png"); auto image = SkImages::DeferredFromEncodedData(data); ASSERT_TRUE(image != nullptr); ASSERT_EQ(SkISize::Make(100, 100), image->dimensions()); @@ -164,7 +134,7 @@ TEST(ImageDecoderNoGLTest, ImpellerWideGamutIndexedPng) { #if defined(OS_FUCHSIA) GTEST_SKIP() << "Fuchsia can't load the test fixtures."; #endif - auto data = OpenFixtureAsSkData("WideGamutIndexed.png"); + auto data = flutter::testing::OpenFixtureAsSkData("WideGamutIndexed.png"); auto image = SkImages::DeferredFromEncodedData(data); ASSERT_TRUE(image != nullptr); ASSERT_EQ(SkISize::Make(100, 100), image->dimensions()); diff --git a/lib/ui/painting/image_decoder_no_gl_unittests.h b/lib/ui/painting/image_decoder_no_gl_unittests.h index eef8a6dd3c5c1..145771e3062b2 100644 --- a/lib/ui/painting/image_decoder_no_gl_unittests.h +++ b/lib/ui/painting/image_decoder_no_gl_unittests.h @@ -96,7 +96,6 @@ namespace testing { float HalfToFloat(uint16_t half); float DecodeBGR10(uint32_t x); -sk_sp OpenFixtureAsSkData(const char* name); } // namespace testing } // namespace flutter diff --git a/lib/ui/painting/image_decoder_unittests.cc b/lib/ui/painting/image_decoder_unittests.cc index 9c2962f843ce1..817be8a6b78c3 100644 --- a/lib/ui/painting/image_decoder_unittests.cc +++ b/lib/ui/painting/image_decoder_unittests.cc @@ -238,7 +238,7 @@ TEST_F(ImageDecoderFixtureTest, InvalidImageResultsError) { manager.GetWeakIOManager(), std::make_shared()); - auto data = OpenFixtureAsSkData("ThisDoesNotExist.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("ThisDoesNotExist.jpg"); ASSERT_FALSE(data); fml::RefPtr image_descriptor = @@ -279,7 +279,8 @@ TEST_F(ImageDecoderFixtureTest, ValidImageResultsInSuccess) { settings, runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager(), std::make_shared()); - auto data = OpenFixtureAsSkData("DashInNooglerHat.jpg"); + auto data = flutter::testing::flutter::testing::OpenFixtureAsSkData( + "DashInNooglerHat.jpg"); ASSERT_TRUE(data); ASSERT_GE(data->size(), 0u); @@ -398,7 +399,8 @@ TEST_F(ImageDecoderFixtureTest, ImpellerPixelConversion32F) { } TEST_F(ImageDecoderFixtureTest, ImpellerWideGamutDisplayP3Opaque) { - auto data = OpenFixtureAsSkData("DisplayP3Logo.jpg"); + auto data = flutter::testing::flutter::testing::OpenFixtureAsSkData( + "DisplayP3Logo.jpg"); auto image = SkImages::DeferredFromEncodedData(data); ASSERT_TRUE(image != nullptr); ASSERT_EQ(SkISize::Make(100, 100), image->dimensions()); @@ -450,7 +452,8 @@ TEST_F(ImageDecoderFixtureTest, ImpellerWideGamutDisplayP3Opaque) { } TEST_F(ImageDecoderFixtureTest, ImpellerNonWideGamut) { - auto data = OpenFixtureAsSkData("Horizontal.jpg"); + auto data = + flutter::testing::flutter::testing::OpenFixtureAsSkData("Horizontal.jpg"); auto image = SkImages::DeferredFromEncodedData(data); ASSERT_TRUE(image != nullptr); ASSERT_EQ(SkISize::Make(600, 200), image->dimensions()); @@ -501,7 +504,7 @@ TEST_F(ImageDecoderFixtureTest, ExifDataIsRespectedOnDecode) { settings, runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager(), std::make_shared()); - auto data = OpenFixtureAsSkData("Horizontal.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg"); ASSERT_TRUE(data); ASSERT_GE(data->size(), 0u); @@ -562,7 +565,7 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithoutAGPUContext) { settings, runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager(), std::make_shared()); - auto data = OpenFixtureAsSkData("DashInNooglerHat.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("DashInNooglerHat.jpg"); ASSERT_TRUE(data); ASSERT_GE(data->size(), 0u); @@ -597,9 +600,10 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithoutAGPUContext) { } TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { - const auto image_dimensions = SkImages::DeferredFromEncodedData( - OpenFixtureAsSkData("DashInNooglerHat.jpg")) - ->dimensions(); + const auto image_dimensions = + SkImages::DeferredFromEncodedData( + flutter::testing::OpenFixtureAsSkData("DashInNooglerHat.jpg")) + ->dimensions(); ASSERT_FALSE(image_dimensions.isEmpty()); @@ -635,7 +639,7 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { uint32_t target_height) -> SkISize { SkISize final_size = SkISize::MakeEmpty(); runners.GetUITaskRunner()->PostTask([&]() { - auto data = OpenFixtureAsSkData("DashInNooglerHat.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("DashInNooglerHat.jpg"); ASSERT_TRUE(data); ASSERT_GE(data->size(), 0u); @@ -676,8 +680,9 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { // Flutter. TEST(ImageDecoderTest, VerifyCodecRepeatCountsForGifAndWebPAreConsistentWithLoopCounts) { - auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); - auto webp_mapping = OpenFixtureAsSkData("hello_loop_2.webp"); + auto gif_mapping = flutter::testing::OpenFixtureAsSkData("hello_loop_2.gif"); + auto webp_mapping = + flutter::testing::OpenFixtureAsSkData("hello_loop_2.webp"); ASSERT_TRUE(gif_mapping); ASSERT_TRUE(webp_mapping); @@ -696,7 +701,7 @@ TEST(ImageDecoderTest, } TEST(ImageDecoderTest, VerifySimpleDecoding) { - auto data = OpenFixtureAsSkData("Horizontal.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg"); auto image = SkImages::DeferredFromEncodedData(data); ASSERT_TRUE(image != nullptr); ASSERT_EQ(600, image->width()); @@ -733,7 +738,7 @@ TEST(ImageDecoderTest, VerifySimpleDecoding) { } TEST(ImageDecoderTest, ImagesWithTransparencyArePremulAlpha) { - auto data = OpenFixtureAsSkData("heart_end.png"); + auto data = flutter::testing::OpenFixtureAsSkData("heart_end.png"); ASSERT_TRUE(data); ImageGeneratorRegistry registry; std::shared_ptr generator = @@ -751,7 +756,7 @@ TEST(ImageDecoderTest, ImagesWithTransparencyArePremulAlpha) { } TEST(ImageDecoderTest, VerifySubpixelDecodingPreservesExifOrientation) { - auto data = OpenFixtureAsSkData("Horizontal.jpg"); + auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg"); ImageGeneratorRegistry registry; std::shared_ptr generator = @@ -776,7 +781,7 @@ TEST(ImageDecoderTest, VerifySubpixelDecodingPreservesExifOrientation) { fml::tracing::TraceFlow("")); }; - auto expected_data = OpenFixtureAsSkData("Horizontal.png"); + auto expected_data = flutter::testing::OpenFixtureAsSkData("Horizontal.png"); ASSERT_TRUE(expected_data != nullptr); ASSERT_FALSE(expected_data->isEmpty()); @@ -809,7 +814,7 @@ TEST_F(ImageDecoderFixtureTest, auto vm_ref = DartVMRef::Create(settings); auto vm_data = vm_ref.GetVMData(); - auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); + auto gif_mapping = flutter::testing::OpenFixtureAsSkData("hello_loop_2.gif"); ASSERT_TRUE(gif_mapping); @@ -878,7 +883,7 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { auto vm_ref = DartVMRef::Create(settings); auto vm_data = vm_ref.GetVMData(); - auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); + auto gif_mapping = flutter::testing::OpenFixtureAsSkData("hello_loop_2.gif"); ASSERT_TRUE(gif_mapping); @@ -962,7 +967,7 @@ TEST_F(ImageDecoderFixtureTest, auto vm_ref = DartVMRef::Create(settings); auto vm_data = vm_ref.GetVMData(); - auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); + auto gif_mapping = flutter::testing::OpenFixtureAsSkData("hello_loop_2.gif"); ASSERT_TRUE(gif_mapping); diff --git a/shell/common/shell_io_manager_unittests.cc b/shell/common/shell_io_manager_unittests.cc index de880b16306bb..e262fd7d7840e 100644 --- a/shell/common/shell_io_manager_unittests.cc +++ b/shell/common/shell_io_manager_unittests.cc @@ -16,36 +16,6 @@ namespace flutter { namespace testing { -static sk_sp OpenFixtureAsSkData(const char* name) { - auto fixtures_directory = - fml::OpenDirectory(GetFixturesPath(), false, fml::FilePermission::kRead); - if (!fixtures_directory.is_valid()) { - return nullptr; - } - - auto fixture_mapping = - fml::FileMapping::CreateReadOnly(fixtures_directory, name); - - if (!fixture_mapping) { - return nullptr; - } - - SkData::ReleaseProc on_release = [](const void* ptr, void* context) -> void { - delete reinterpret_cast(context); - }; - - auto data = SkData::MakeWithProc(fixture_mapping->GetMapping(), - fixture_mapping->GetSize(), on_release, - fixture_mapping.get()); - - if (!data) { - return nullptr; - } - // The data is now owned by Skia. - fixture_mapping.release(); - return data; -} - class ShellIOManagerTest : public FixtureTest {}; // Regression test for https://github.com/flutter/engine/pull/32106. @@ -54,7 +24,7 @@ TEST_F(ShellIOManagerTest, auto settings = CreateSettingsForFixture(); auto vm_ref = DartVMRef::Create(settings); auto vm_data = vm_ref.GetVMData(); - auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); + auto gif_mapping = flutter::testing::OpenFixtureAsSkData("hello_loop_2.gif"); ASSERT_TRUE(gif_mapping); ImageGeneratorRegistry registry; diff --git a/testing/testing.cc b/testing/testing.cc index 06de22adb85ab..a12f6159cb29d 100644 --- a/testing/testing.cc +++ b/testing/testing.cc @@ -61,6 +61,21 @@ std::unique_ptr OpenFixtureAsMapping( return fml::FileMapping::CreateReadOnly(OpenFixture(fixture_name)); } +sk_sp OpenFixtureAsSkData(const std::string& fixture_name) { + auto mapping = flutter::testing::OpenFixtureAsMapping(fixture_name); + if (!mapping) { + return nullptr; + } + auto data = SkData::MakeWithProc( + mapping->GetMapping(), mapping->GetSize(), + [](const void* ptr, void* context) { + delete reinterpret_cast(context); + }, + mapping.get()); + mapping.release(); + return data; +} + bool MemsetPatternSetOrCheck(uint8_t* buffer, size_t size, MemsetPatternOp op) { if (buffer == nullptr) { return false; diff --git a/testing/testing.h b/testing/testing.h index 43cf512162aed..f3cc80c8e9931 100644 --- a/testing/testing.h +++ b/testing/testing.h @@ -12,6 +12,7 @@ #include "flutter/fml/mapping.h" #include "flutter/testing/assertions.h" #include "gtest/gtest.h" +#include "third_party/skia/include/core/SkData.h" namespace flutter { namespace testing { @@ -73,6 +74,17 @@ fml::UniqueFD OpenFixture(const std::string& fixture_name); std::unique_ptr OpenFixtureAsMapping( const std::string& fixture_name); +//------------------------------------------------------------------------------ +/// @brief Opens a fixture of the given file name and returns a Skia SkData +/// holding its contents. +/// +/// @param[in] fixture_name The fixture name +/// +/// @return An SkData, or null if the fixture does not exist or its contents +/// cannot be mapped in. +/// +sk_sp OpenFixtureAsSkData(const std::string& fixture_name); + //------------------------------------------------------------------------------ /// @brief Gets the name of the currently running test. This is useful in /// generating logs or assets based on test name.