Skip to content

Commit

Permalink
Consolidate all the copies of the OpenFixtureAsSkData testing helper …
Browse files Browse the repository at this point in the history
…function (#47491)
  • Loading branch information
jason-simmons authored Nov 1, 2023
1 parent a8db6d9 commit 84c53b4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 134 deletions.
21 changes: 3 additions & 18 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1349,21 +1349,6 @@ TEST_P(AiksTest, CanDrawAnOpenPathThatIsntARect) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

static sk_sp<SkData> 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<fml::Mapping*>(context);
},
mapping.get());
mapping.release();
return data;
}

struct TextRenderOptions {
Scalar font_size = 50;
Scalar alpha = 1;
Expand All @@ -1385,7 +1370,7 @@ bool RenderTextInCanvasSkia(const std::shared_ptr<Context>& 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;
}
Expand Down Expand Up @@ -1563,7 +1548,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;
Expand Down Expand Up @@ -3372,7 +3357,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;
Expand Down
17 changes: 1 addition & 16 deletions impeller/display_list/dl_playground.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,9 @@ bool DlPlayground::OpenPlaygroundHere(DisplayListPlaygroundCallback callback) {
});
}

static sk_sp<SkData> 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<fml::Mapping*>(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};
}
Expand Down
19 changes: 2 additions & 17 deletions impeller/typographer/typographer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,11 @@ TEST_P(TypographerTest, CanCreateGlyphAtlas) {
.has_value());
}

static sk_sp<SkData> 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<fml::Mapping*>(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);
Expand Down
34 changes: 2 additions & 32 deletions lib/ui/painting/image_decoder_no_gl_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,11 @@ float DecodeBGR10(uint32_t x) {
return (x * slope) + intercept;
}

sk_sp<SkData> 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<fml::FileMapping*>(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());
Expand Down Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion lib/ui/painting/image_decoder_no_gl_unittests.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ namespace testing {

float HalfToFloat(uint16_t half);
float DecodeBGR10(uint32_t x);
sk_sp<SkData> OpenFixtureAsSkData(const char* name);

} // namespace testing
} // namespace flutter
40 changes: 21 additions & 19 deletions lib/ui/painting/image_decoder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ TEST_F(ImageDecoderFixtureTest, InvalidImageResultsError) {
manager.GetWeakIOManager(),
std::make_shared<fml::SyncSwitch>());

auto data = OpenFixtureAsSkData("ThisDoesNotExist.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("ThisDoesNotExist.jpg");
ASSERT_FALSE(data);

fml::RefPtr<ImageDescriptor> image_descriptor =
Expand Down Expand Up @@ -279,7 +279,7 @@ TEST_F(ImageDecoderFixtureTest, ValidImageResultsInSuccess) {
settings, runners, loop->GetTaskRunner(),
io_manager->GetWeakIOManager(), std::make_shared<fml::SyncSwitch>());

auto data = OpenFixtureAsSkData("DashInNooglerHat.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("DashInNooglerHat.jpg");

ASSERT_TRUE(data);
ASSERT_GE(data->size(), 0u);
Expand Down Expand Up @@ -398,7 +398,7 @@ TEST_F(ImageDecoderFixtureTest, ImpellerPixelConversion32F) {
}

TEST_F(ImageDecoderFixtureTest, ImpellerWideGamutDisplayP3Opaque) {
auto data = OpenFixtureAsSkData("DisplayP3Logo.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("DisplayP3Logo.jpg");
auto image = SkImages::DeferredFromEncodedData(data);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(SkISize::Make(100, 100), image->dimensions());
Expand Down Expand Up @@ -450,7 +450,7 @@ TEST_F(ImageDecoderFixtureTest, ImpellerWideGamutDisplayP3Opaque) {
}

TEST_F(ImageDecoderFixtureTest, ImpellerNonWideGamut) {
auto data = OpenFixtureAsSkData("Horizontal.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg");
auto image = SkImages::DeferredFromEncodedData(data);
ASSERT_TRUE(image != nullptr);
ASSERT_EQ(SkISize::Make(600, 200), image->dimensions());
Expand Down Expand Up @@ -501,7 +501,7 @@ TEST_F(ImageDecoderFixtureTest, ExifDataIsRespectedOnDecode) {
settings, runners, loop->GetTaskRunner(),
io_manager->GetWeakIOManager(), std::make_shared<fml::SyncSwitch>());

auto data = OpenFixtureAsSkData("Horizontal.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg");

ASSERT_TRUE(data);
ASSERT_GE(data->size(), 0u);
Expand Down Expand Up @@ -562,7 +562,7 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithoutAGPUContext) {
settings, runners, loop->GetTaskRunner(),
io_manager->GetWeakIOManager(), std::make_shared<fml::SyncSwitch>());

auto data = OpenFixtureAsSkData("DashInNooglerHat.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("DashInNooglerHat.jpg");

ASSERT_TRUE(data);
ASSERT_GE(data->size(), 0u);
Expand Down Expand Up @@ -597,9 +597,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());

Expand Down Expand Up @@ -635,7 +636,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);
Expand Down Expand Up @@ -676,8 +677,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);
Expand All @@ -696,7 +698,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());
Expand Down Expand Up @@ -733,7 +735,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<ImageGenerator> generator =
Expand All @@ -751,7 +753,7 @@ TEST(ImageDecoderTest, ImagesWithTransparencyArePremulAlpha) {
}

TEST(ImageDecoderTest, VerifySubpixelDecodingPreservesExifOrientation) {
auto data = OpenFixtureAsSkData("Horizontal.jpg");
auto data = flutter::testing::OpenFixtureAsSkData("Horizontal.jpg");

ImageGeneratorRegistry registry;
std::shared_ptr<ImageGenerator> generator =
Expand All @@ -776,7 +778,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());

Expand Down Expand Up @@ -809,7 +811,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);

Expand Down Expand Up @@ -878,7 +880,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);

Expand Down Expand Up @@ -962,7 +964,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);

Expand Down
32 changes: 1 addition & 31 deletions shell/common/shell_io_manager_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@
namespace flutter {
namespace testing {

static sk_sp<SkData> 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<fml::FileMapping*>(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.
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions testing/testing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ std::unique_ptr<fml::Mapping> OpenFixtureAsMapping(
return fml::FileMapping::CreateReadOnly(OpenFixture(fixture_name));
}

sk_sp<SkData> 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<fml::Mapping*>(context);
},
mapping.get());
mapping.release();
return data;
}

bool MemsetPatternSetOrCheck(uint8_t* buffer, size_t size, MemsetPatternOp op) {
if (buffer == nullptr) {
return false;
Expand Down
Loading

0 comments on commit 84c53b4

Please sign in to comment.