From 6f712c9fdec50dcf45af613888bbaf52bbb742a1 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Sun, 5 Sep 2021 16:26:33 +0800 Subject: [PATCH 1/3] Factor out several task synchronization help functions for unit tests --- lib/ui/painting/image_decoder_unittests.cc | 91 +++++----------------- testing/BUILD.gn | 3 + testing/post_task_sync.cc | 43 ++++++++++ testing/post_task_sync.h | 32 ++++++++ 4 files changed, 96 insertions(+), 73 deletions(-) create mode 100644 testing/post_task_sync.cc create mode 100644 testing/post_task_sync.h diff --git a/lib/ui/painting/image_decoder_unittests.cc b/lib/ui/painting/image_decoder_unittests.cc index dd774db831aa4..bc4322819faf5 100644 --- a/lib/ui/painting/image_decoder_unittests.cc +++ b/lib/ui/painting/image_decoder_unittests.cc @@ -13,6 +13,7 @@ #include "flutter/testing/dart_isolate_runner.h" #include "flutter/testing/elf_loader.h" #include "flutter/testing/fixture_test.h" +#include "flutter/testing/post_task_sync.h" #include "flutter/testing/test_dart_native_resolver.h" #include "flutter/testing/test_gl_surface.h" #include "flutter/testing/testing.h" @@ -134,14 +135,11 @@ TEST_F(ImageDecoderFixtureTest, CanCreateImageDecoder) { ); - fml::AutoResetWaitableEvent latch; - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { TestIOManager manager(runners.GetIOTaskRunner()); ImageDecoder decoder(std::move(runners), loop->GetTaskRunner(), manager.GetWeakIOManager()); - latch.Signal(); }); - latch.Wait(); } /// An Image generator that pretends it can't recognize the data it was given. @@ -401,20 +399,15 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { std::unique_ptr image_decoder; // Setup the IO manager. - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); - latch.Signal(); }); - latch.Wait(); // Setup the image decoder. - runners.GetUITaskRunner()->PostTask([&]() { + PostUITaskSync(runners, [&]() { image_decoder = std::make_unique( runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager()); - - latch.Signal(); }); - latch.Wait(); // Setup a generic decoding utility that gives us the final decoded size. auto decoded_size = [&](uint32_t target_width, @@ -451,18 +444,10 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100)); // Destroy the IO manager - runners.GetIOTaskRunner()->PostTask([&]() { - io_manager.reset(); - latch.Signal(); - }); - latch.Wait(); + PostIOTaskSync(runners, [&]() { io_manager.reset(); }); // Destroy the image decoder - runners.GetUITaskRunner()->PostTask([&]() { - image_decoder.reset(); - latch.Signal(); - }); - latch.Wait(); + PostUITaskSync(runners, [&]() { image_decoder.reset(); }); } // TODO(https://github.com/flutter/flutter/issues/81232) - disabled due to @@ -506,20 +491,15 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) { std::unique_ptr image_decoder; // Setup the IO manager. - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); - latch.Signal(); }); - latch.Wait(); // Setup the image decoder. - runners.GetUITaskRunner()->PostTask([&]() { + PostUITaskSync(runners, [&]() { image_decoder = std::make_unique( runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager()); - - latch.Signal(); }); - latch.Wait(); // Setup a generic decoding utility that gives us the final decoded size. auto decoded_size = [&](uint32_t target_width, @@ -549,18 +529,10 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) { ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100)); // Destroy the IO manager - runners.GetIOTaskRunner()->PostTask([&]() { - io_manager.reset(); - latch.Signal(); - }); - latch.Wait(); + PostIOTaskSync(runners, [&]() { io_manager.reset(); }); // Destroy the image decoder - runners.GetUITaskRunner()->PostTask([&]() { - image_decoder.reset(); - latch.Signal(); - }); - latch.Wait(); + PostUITaskSync(runners, [&]() { image_decoder.reset(); }); } // Verifies https://skia-review.googlesource.com/c/skia/+/259161 is present in @@ -673,16 +645,13 @@ TEST_F(ImageDecoderFixtureTest, CreateNewThread("io") // io ); - fml::AutoResetWaitableEvent latch; fml::AutoResetWaitableEvent io_latch; std::unique_ptr io_manager; // Setup the IO manager. - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); - latch.Signal(); }); - latch.Wait(); auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {}, GetDefaultKernelFilePath(), @@ -691,7 +660,7 @@ TEST_F(ImageDecoderFixtureTest, // Latch the IO task runner. runners.GetIOTaskRunner()->PostTask([&]() { io_latch.Wait(); }); - runners.GetUITaskRunner()->PostTask([&]() { + PostUITaskSync(runners, [&]() { fml::AutoResetWaitableEvent isolate_latch; fml::RefPtr codec; EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool { @@ -718,17 +687,10 @@ TEST_F(ImageDecoderFixtureTest, EXPECT_FALSE(codec); io_latch.Signal(); - - latch.Signal(); }); - latch.Wait(); // Destroy the IO manager - runners.GetIOTaskRunner()->PostTask([&]() { - io_manager.reset(); - latch.Signal(); - }); - latch.Wait(); + PostIOTaskSync(runners, [&]() { io_manager.reset(); }); } TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { @@ -752,22 +714,19 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { CreateNewThread("io") // io ); - fml::AutoResetWaitableEvent latch; std::unique_ptr io_manager; fml::RefPtr codec; // Setup the IO manager. - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); - latch.Signal(); }); - latch.Wait(); auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {}, GetDefaultKernelFilePath(), io_manager->GetWeakIOManager()); - runners.GetUITaskRunner()->PostTask([&]() { + PostUITaskSync(runners, [&]() { fml::AutoResetWaitableEvent isolate_latch; EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool { @@ -790,34 +749,20 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { return true; })); isolate_latch.Wait(); - - latch.Signal(); }); - latch.Wait(); - runners.GetIOTaskRunner()->PostTask([&]() { + PostIOTaskSync(runners, [&]() { EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_); - latch.Signal(); }); - latch.Wait(); // Destroy the Isolate isolate = nullptr; // Destroy the MultiFrameCodec - runners.GetUITaskRunner()->PostTask([&]() { - codec = nullptr; - latch.Signal(); - }); - latch.Wait(); + PostUITaskSync(runners, [&]() { codec = nullptr; }); // Destroy the IO manager - runners.GetIOTaskRunner()->PostTask([&]() { - io_manager.reset(); - latch.Signal(); - }); - - latch.Wait(); + PostIOTaskSync(runners, [&]() { io_manager.reset(); }); } } // namespace testing diff --git a/testing/BUILD.gn b/testing/BUILD.gn index 00bfc0ff16b0c..9a06860a20be4 100644 --- a/testing/BUILD.gn +++ b/testing/BUILD.gn @@ -17,6 +17,8 @@ source_set("testing_lib") { sources = [ "assertions.h", + "post_task_sync.cc", + "post_task_sync.h", "testing.cc", "testing.h", "thread_test.cc", @@ -24,6 +26,7 @@ source_set("testing_lib") { ] public_deps = [ + "//flutter/common:common", "//flutter/fml", "//third_party/googletest:gmock", "//third_party/googletest:gtest", diff --git a/testing/post_task_sync.cc b/testing/post_task_sync.cc new file mode 100644 index 0000000000000..49ba9f6d80d1d --- /dev/null +++ b/testing/post_task_sync.cc @@ -0,0 +1,43 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/testing/post_task_sync.h" + +#include "flutter/fml/synchronization/waitable_event.h" + +namespace flutter { +namespace testing { + +void PostPlatformTaskSync(const TaskRunners& task_runners, + const std::function& function) { + PostTaskSync(*task_runners.GetPlatformTaskRunner(), function); +} + +void PostUITaskSync(const TaskRunners& task_runners, + const std::function& function) { + PostTaskSync(*task_runners.GetUITaskRunner(), function); +} + +void PostRasterTaskSync(const TaskRunners& task_runners, + const std::function& function) { + PostTaskSync(*task_runners.GetRasterTaskRunner(), function); +} + +void PostIOTaskSync(const TaskRunners& task_runners, + const std::function& function) { + PostTaskSync(*task_runners.GetIOTaskRunner(), function); +} + +void PostTaskSync(fml::TaskRunner& task_runner, + const std::function& function) { + fml::AutoResetWaitableEvent latch; + task_runner.PostTask([&] { + function(); + latch.Signal(); + }); + latch.Wait(); +} + +} // namespace testing +} // namespace flutter diff --git a/testing/post_task_sync.h b/testing/post_task_sync.h new file mode 100644 index 0000000000000..db2dcc2081d52 --- /dev/null +++ b/testing/post_task_sync.h @@ -0,0 +1,32 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_TESTING_POST_TASK_SYNC_H_ +#define FLUTTER_TESTING_POST_TASK_SYNC_H_ + +#include "flutter/common/task_runners.h" +#include "flutter/fml/task_runner.h" + +namespace flutter { +namespace testing { + +void PostPlatformTaskSync(const TaskRunners& task_runners, + const std::function& function); + +void PostUITaskSync(const TaskRunners& task_runners, + const std::function& function); + +void PostRasterTaskSync(const TaskRunners& task_runners, + const std::function& function); + +void PostIOTaskSync(const TaskRunners& task_runners, + const std::function& function); + +void PostTaskSync(fml::TaskRunner& task_runner, + const std::function& function); + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_TESTING_POST_TASK_SYNC_H_ From d21eb8e687073b62b6655f9fb849150b51e3ab75 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Wed, 8 Sep 2021 23:36:04 +0800 Subject: [PATCH 2/3] Remove redundant functions --- lib/ui/painting/image_decoder_unittests.cc | 34 +++++++++++----------- testing/post_task_sync.cc | 24 ++------------- testing/post_task_sync.h | 14 +-------- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/lib/ui/painting/image_decoder_unittests.cc b/lib/ui/painting/image_decoder_unittests.cc index bc4322819faf5..6b945b6f91766 100644 --- a/lib/ui/painting/image_decoder_unittests.cc +++ b/lib/ui/painting/image_decoder_unittests.cc @@ -135,7 +135,7 @@ TEST_F(ImageDecoderFixtureTest, CanCreateImageDecoder) { ); - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { TestIOManager manager(runners.GetIOTaskRunner()); ImageDecoder decoder(std::move(runners), loop->GetTaskRunner(), manager.GetWeakIOManager()); @@ -399,12 +399,12 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { std::unique_ptr image_decoder; // Setup the IO manager. - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); }); // Setup the image decoder. - PostUITaskSync(runners, [&]() { + PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder = std::make_unique( runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager()); }); @@ -444,10 +444,10 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) { ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100)); // Destroy the IO manager - PostIOTaskSync(runners, [&]() { io_manager.reset(); }); + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); }); // Destroy the image decoder - PostUITaskSync(runners, [&]() { image_decoder.reset(); }); + PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder.reset(); }); } // TODO(https://github.com/flutter/flutter/issues/81232) - disabled due to @@ -491,12 +491,12 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) { std::unique_ptr image_decoder; // Setup the IO manager. - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); }); // Setup the image decoder. - PostUITaskSync(runners, [&]() { + PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder = std::make_unique( runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager()); }); @@ -529,10 +529,10 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) { ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100)); // Destroy the IO manager - PostIOTaskSync(runners, [&]() { io_manager.reset(); }); + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); }); // Destroy the image decoder - PostUITaskSync(runners, [&]() { image_decoder.reset(); }); + PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder.reset(); }); } // Verifies https://skia-review.googlesource.com/c/skia/+/259161 is present in @@ -649,7 +649,7 @@ TEST_F(ImageDecoderFixtureTest, std::unique_ptr io_manager; // Setup the IO manager. - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); }); @@ -660,7 +660,7 @@ TEST_F(ImageDecoderFixtureTest, // Latch the IO task runner. runners.GetIOTaskRunner()->PostTask([&]() { io_latch.Wait(); }); - PostUITaskSync(runners, [&]() { + PostTaskSync(runners.GetUITaskRunner(), [&]() { fml::AutoResetWaitableEvent isolate_latch; fml::RefPtr codec; EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool { @@ -690,7 +690,7 @@ TEST_F(ImageDecoderFixtureTest, }); // Destroy the IO manager - PostIOTaskSync(runners, [&]() { io_manager.reset(); }); + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); }); } TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { @@ -718,7 +718,7 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { fml::RefPtr codec; // Setup the IO manager. - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); }); @@ -726,7 +726,7 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { GetDefaultKernelFilePath(), io_manager->GetWeakIOManager()); - PostUITaskSync(runners, [&]() { + PostTaskSync(runners.GetUITaskRunner(), [&]() { fml::AutoResetWaitableEvent isolate_latch; EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool { @@ -751,7 +751,7 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { isolate_latch.Wait(); }); - PostIOTaskSync(runners, [&]() { + PostTaskSync(runners.GetIOTaskRunner(), [&]() { EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_); }); @@ -759,10 +759,10 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { isolate = nullptr; // Destroy the MultiFrameCodec - PostUITaskSync(runners, [&]() { codec = nullptr; }); + PostTaskSync(runners.GetUITaskRunner(), [&]() { codec = nullptr; }); // Destroy the IO manager - PostIOTaskSync(runners, [&]() { io_manager.reset(); }); + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); }); } } // namespace testing diff --git a/testing/post_task_sync.cc b/testing/post_task_sync.cc index 49ba9f6d80d1d..b5a6e36f5df87 100644 --- a/testing/post_task_sync.cc +++ b/testing/post_task_sync.cc @@ -9,30 +9,10 @@ namespace flutter { namespace testing { -void PostPlatformTaskSync(const TaskRunners& task_runners, - const std::function& function) { - PostTaskSync(*task_runners.GetPlatformTaskRunner(), function); -} - -void PostUITaskSync(const TaskRunners& task_runners, - const std::function& function) { - PostTaskSync(*task_runners.GetUITaskRunner(), function); -} - -void PostRasterTaskSync(const TaskRunners& task_runners, - const std::function& function) { - PostTaskSync(*task_runners.GetRasterTaskRunner(), function); -} - -void PostIOTaskSync(const TaskRunners& task_runners, - const std::function& function) { - PostTaskSync(*task_runners.GetIOTaskRunner(), function); -} - -void PostTaskSync(fml::TaskRunner& task_runner, +void PostTaskSync(fml::RefPtr task_runner, const std::function& function) { fml::AutoResetWaitableEvent latch; - task_runner.PostTask([&] { + task_runner->PostTask([&] { function(); latch.Signal(); }); diff --git a/testing/post_task_sync.h b/testing/post_task_sync.h index db2dcc2081d52..ceeb544cfd0b3 100644 --- a/testing/post_task_sync.h +++ b/testing/post_task_sync.h @@ -11,19 +11,7 @@ namespace flutter { namespace testing { -void PostPlatformTaskSync(const TaskRunners& task_runners, - const std::function& function); - -void PostUITaskSync(const TaskRunners& task_runners, - const std::function& function); - -void PostRasterTaskSync(const TaskRunners& task_runners, - const std::function& function); - -void PostIOTaskSync(const TaskRunners& task_runners, - const std::function& function); - -void PostTaskSync(fml::TaskRunner& task_runner, +void PostTaskSync(fml::RefPtr task_runner, const std::function& function); } // namespace testing From 66f6a6abf1d4c7e2f3b6ee0176a209e1cc74f34e Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 9 Sep 2021 00:34:38 +0800 Subject: [PATCH 3/3] Delete unused header file --- testing/BUILD.gn | 1 - testing/post_task_sync.h | 1 - 2 files changed, 2 deletions(-) diff --git a/testing/BUILD.gn b/testing/BUILD.gn index 9a06860a20be4..697f7a971e15c 100644 --- a/testing/BUILD.gn +++ b/testing/BUILD.gn @@ -26,7 +26,6 @@ source_set("testing_lib") { ] public_deps = [ - "//flutter/common:common", "//flutter/fml", "//third_party/googletest:gmock", "//third_party/googletest:gtest", diff --git a/testing/post_task_sync.h b/testing/post_task_sync.h index ceeb544cfd0b3..7e5d362dc9c1c 100644 --- a/testing/post_task_sync.h +++ b/testing/post_task_sync.h @@ -5,7 +5,6 @@ #ifndef FLUTTER_TESTING_POST_TASK_SYNC_H_ #define FLUTTER_TESTING_POST_TASK_SYNC_H_ -#include "flutter/common/task_runners.h" #include "flutter/fml/task_runner.h" namespace flutter {