Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e296708

Browse files
Factor out a task synchronization help function for unit tests (#28467)
1 parent 79818c6 commit e296708

File tree

4 files changed

+62
-73
lines changed

4 files changed

+62
-73
lines changed

lib/ui/painting/image_decoder_unittests.cc

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/testing/dart_isolate_runner.h"
1414
#include "flutter/testing/elf_loader.h"
1515
#include "flutter/testing/fixture_test.h"
16+
#include "flutter/testing/post_task_sync.h"
1617
#include "flutter/testing/test_dart_native_resolver.h"
1718
#include "flutter/testing/test_gl_surface.h"
1819
#include "flutter/testing/testing.h"
@@ -134,14 +135,11 @@ TEST_F(ImageDecoderFixtureTest, CanCreateImageDecoder) {
134135

135136
);
136137

137-
fml::AutoResetWaitableEvent latch;
138-
runners.GetIOTaskRunner()->PostTask([&]() {
138+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
139139
TestIOManager manager(runners.GetIOTaskRunner());
140140
ImageDecoder decoder(std::move(runners), loop->GetTaskRunner(),
141141
manager.GetWeakIOManager());
142-
latch.Signal();
143142
});
144-
latch.Wait();
145143
}
146144

147145
/// An Image generator that pretends it can't recognize the data it was given.
@@ -401,20 +399,15 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) {
401399
std::unique_ptr<ImageDecoder> image_decoder;
402400

403401
// Setup the IO manager.
404-
runners.GetIOTaskRunner()->PostTask([&]() {
402+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
405403
io_manager = std::make_unique<TestIOManager>(runners.GetIOTaskRunner());
406-
latch.Signal();
407404
});
408-
latch.Wait();
409405

410406
// Setup the image decoder.
411-
runners.GetUITaskRunner()->PostTask([&]() {
407+
PostTaskSync(runners.GetUITaskRunner(), [&]() {
412408
image_decoder = std::make_unique<ImageDecoder>(
413409
runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager());
414-
415-
latch.Signal();
416410
});
417-
latch.Wait();
418411

419412
// Setup a generic decoding utility that gives us the final decoded size.
420413
auto decoded_size = [&](uint32_t target_width,
@@ -451,18 +444,10 @@ TEST_F(ImageDecoderFixtureTest, CanDecodeWithResizes) {
451444
ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100));
452445

453446
// Destroy the IO manager
454-
runners.GetIOTaskRunner()->PostTask([&]() {
455-
io_manager.reset();
456-
latch.Signal();
457-
});
458-
latch.Wait();
447+
PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); });
459448

460449
// Destroy the image decoder
461-
runners.GetUITaskRunner()->PostTask([&]() {
462-
image_decoder.reset();
463-
latch.Signal();
464-
});
465-
latch.Wait();
450+
PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder.reset(); });
466451
}
467452

468453
// TODO(https://github.com/flutter/flutter/issues/81232) - disabled due to
@@ -506,20 +491,15 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) {
506491
std::unique_ptr<ImageDecoder> image_decoder;
507492

508493
// Setup the IO manager.
509-
runners.GetIOTaskRunner()->PostTask([&]() {
494+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
510495
io_manager = std::make_unique<TestIOManager>(runners.GetIOTaskRunner());
511-
latch.Signal();
512496
});
513-
latch.Wait();
514497

515498
// Setup the image decoder.
516-
runners.GetUITaskRunner()->PostTask([&]() {
499+
PostTaskSync(runners.GetUITaskRunner(), [&]() {
517500
image_decoder = std::make_unique<ImageDecoder>(
518501
runners, loop->GetTaskRunner(), io_manager->GetWeakIOManager());
519-
520-
latch.Signal();
521502
});
522-
latch.Wait();
523503

524504
// Setup a generic decoding utility that gives us the final decoded size.
525505
auto decoded_size = [&](uint32_t target_width,
@@ -549,18 +529,10 @@ TEST_F(ImageDecoderFixtureTest, DISABLED_CanResizeWithoutDecode) {
549529
ASSERT_EQ(decoded_size(100, 100), SkISize::Make(100, 100));
550530

551531
// Destroy the IO manager
552-
runners.GetIOTaskRunner()->PostTask([&]() {
553-
io_manager.reset();
554-
latch.Signal();
555-
});
556-
latch.Wait();
532+
PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); });
557533

558534
// Destroy the image decoder
559-
runners.GetUITaskRunner()->PostTask([&]() {
560-
image_decoder.reset();
561-
latch.Signal();
562-
});
563-
latch.Wait();
535+
PostTaskSync(runners.GetUITaskRunner(), [&]() { image_decoder.reset(); });
564536
}
565537

566538
// Verifies https://skia-review.googlesource.com/c/skia/+/259161 is present in
@@ -673,16 +645,13 @@ TEST_F(ImageDecoderFixtureTest,
673645
CreateNewThread("io") // io
674646
);
675647

676-
fml::AutoResetWaitableEvent latch;
677648
fml::AutoResetWaitableEvent io_latch;
678649
std::unique_ptr<TestIOManager> io_manager;
679650

680651
// Setup the IO manager.
681-
runners.GetIOTaskRunner()->PostTask([&]() {
652+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
682653
io_manager = std::make_unique<TestIOManager>(runners.GetIOTaskRunner());
683-
latch.Signal();
684654
});
685-
latch.Wait();
686655

687656
auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {},
688657
GetDefaultKernelFilePath(),
@@ -691,7 +660,7 @@ TEST_F(ImageDecoderFixtureTest,
691660
// Latch the IO task runner.
692661
runners.GetIOTaskRunner()->PostTask([&]() { io_latch.Wait(); });
693662

694-
runners.GetUITaskRunner()->PostTask([&]() {
663+
PostTaskSync(runners.GetUITaskRunner(), [&]() {
695664
fml::AutoResetWaitableEvent isolate_latch;
696665
fml::RefPtr<MultiFrameCodec> codec;
697666
EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool {
@@ -718,17 +687,10 @@ TEST_F(ImageDecoderFixtureTest,
718687
EXPECT_FALSE(codec);
719688

720689
io_latch.Signal();
721-
722-
latch.Signal();
723690
});
724-
latch.Wait();
725691

726692
// Destroy the IO manager
727-
runners.GetIOTaskRunner()->PostTask([&]() {
728-
io_manager.reset();
729-
latch.Signal();
730-
});
731-
latch.Wait();
693+
PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); });
732694
}
733695

734696
TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) {
@@ -752,22 +714,19 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) {
752714
CreateNewThread("io") // io
753715
);
754716

755-
fml::AutoResetWaitableEvent latch;
756717
std::unique_ptr<TestIOManager> io_manager;
757718
fml::RefPtr<MultiFrameCodec> codec;
758719

759720
// Setup the IO manager.
760-
runners.GetIOTaskRunner()->PostTask([&]() {
721+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
761722
io_manager = std::make_unique<TestIOManager>(runners.GetIOTaskRunner());
762-
latch.Signal();
763723
});
764-
latch.Wait();
765724

766725
auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {},
767726
GetDefaultKernelFilePath(),
768727
io_manager->GetWeakIOManager());
769728

770-
runners.GetUITaskRunner()->PostTask([&]() {
729+
PostTaskSync(runners.GetUITaskRunner(), [&]() {
771730
fml::AutoResetWaitableEvent isolate_latch;
772731

773732
EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool {
@@ -790,34 +749,20 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) {
790749
return true;
791750
}));
792751
isolate_latch.Wait();
793-
794-
latch.Signal();
795752
});
796-
latch.Wait();
797753

798-
runners.GetIOTaskRunner()->PostTask([&]() {
754+
PostTaskSync(runners.GetIOTaskRunner(), [&]() {
799755
EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_);
800-
latch.Signal();
801756
});
802-
latch.Wait();
803757

804758
// Destroy the Isolate
805759
isolate = nullptr;
806760

807761
// Destroy the MultiFrameCodec
808-
runners.GetUITaskRunner()->PostTask([&]() {
809-
codec = nullptr;
810-
latch.Signal();
811-
});
812-
latch.Wait();
762+
PostTaskSync(runners.GetUITaskRunner(), [&]() { codec = nullptr; });
813763

814764
// Destroy the IO manager
815-
runners.GetIOTaskRunner()->PostTask([&]() {
816-
io_manager.reset();
817-
latch.Signal();
818-
});
819-
820-
latch.Wait();
765+
PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); });
821766
}
822767

823768
} // namespace testing

testing/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ source_set("testing_lib") {
1717

1818
sources = [
1919
"assertions.h",
20+
"post_task_sync.cc",
21+
"post_task_sync.h",
2022
"testing.cc",
2123
"testing.h",
2224
"thread_test.cc",

testing/post_task_sync.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 "flutter/testing/post_task_sync.h"
6+
7+
#include "flutter/fml/synchronization/waitable_event.h"
8+
9+
namespace flutter {
10+
namespace testing {
11+
12+
void PostTaskSync(fml::RefPtr<fml::TaskRunner> task_runner,
13+
const std::function<void()>& function) {
14+
fml::AutoResetWaitableEvent latch;
15+
task_runner->PostTask([&] {
16+
function();
17+
latch.Signal();
18+
});
19+
latch.Wait();
20+
}
21+
22+
} // namespace testing
23+
} // namespace flutter

testing/post_task_sync.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#ifndef FLUTTER_TESTING_POST_TASK_SYNC_H_
6+
#define FLUTTER_TESTING_POST_TASK_SYNC_H_
7+
8+
#include "flutter/fml/task_runner.h"
9+
10+
namespace flutter {
11+
namespace testing {
12+
13+
void PostTaskSync(fml::RefPtr<fml::TaskRunner> task_runner,
14+
const std::function<void()>& function);
15+
16+
} // namespace testing
17+
} // namespace flutter
18+
19+
#endif // FLUTTER_TESTING_POST_TASK_SYNC_H_

0 commit comments

Comments
 (0)