Skip to content

Commit f98cefb

Browse files
author
Jonah Williams
authored
[engine] force semantics action to post a task. (flutter#56514)
Repro: Testing WIP. b/373761573 ```dart import 'package:flutter/material.dart'; void main() { runApp(const Example()); } class Example extends StatefulWidget { const Example({super.key}); @OverRide State<Example> createState() => _ExampleState(); } class _ExampleState extends State<Example> { bool value = true; @OverRide Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Semantics( child: Text( value ? 'Hello, world!' : 'Goodbye, World!', ), button: true, onTap: () async { await null; setState(() { value = !value; }); }, ), ), ), ); } } ```
1 parent d99a438 commit f98cefb

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

shell/common/fixtures/shell_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// ignore_for_file: avoid_print
66

7+
import 'dart:async';
78
import 'dart:convert' show json, utf8;
89
import 'dart:isolate';
910
import 'dart:typed_data';
@@ -629,3 +630,13 @@ void renderWarmUpView1and2() {
629630
}
630631
});
631632
}
633+
634+
@pragma('vm:entry-point')
635+
void testSemanticsActions() {
636+
PlatformDispatcher.instance.onSemanticsActionEvent = (SemanticsActionEvent action) async {
637+
await null;
638+
Future<void>.value().then((_) {
639+
notifyNative();
640+
});
641+
};
642+
}

shell/common/shell.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,7 @@ void Shell::OnPlatformViewDispatchSemanticsAction(int32_t node_id,
11221122
FML_DCHECK(is_set_up_);
11231123
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
11241124

1125-
fml::TaskRunner::RunNowOrPostTask(
1126-
task_runners_.GetUITaskRunner(),
1125+
task_runners_.GetUITaskRunner()->PostTask(
11271126
fml::MakeCopyable([engine = engine_->GetWeakPtr(), node_id, action,
11281127
args = std::move(args)]() mutable {
11291128
if (engine) {

shell/common/shell_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ void ShellTest::SendPlatformMessage(Shell* shell,
6565
shell->OnPlatformViewDispatchPlatformMessage(std::move(message));
6666
}
6767

68+
void ShellTest::SendSemanticsAction(Shell* shell,
69+
int32_t node_id,
70+
SemanticsAction action,
71+
fml::MallocMapping args) {
72+
shell->OnPlatformViewDispatchSemanticsAction(node_id, action,
73+
std::move(args));
74+
}
75+
6876
void ShellTest::SendEnginePlatformMessage(
6977
Shell* shell,
7078
std::unique_ptr<PlatformMessage> message) {

shell/common/shell_test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class ShellTest : public FixtureTest {
9090
void SendPlatformMessage(Shell* shell,
9191
std::unique_ptr<PlatformMessage> message);
9292

93+
void SendSemanticsAction(Shell* shell,
94+
int32_t node_id,
95+
SemanticsAction action,
96+
fml::MallocMapping args);
97+
9398
void SendEnginePlatformMessage(Shell* shell,
9499
std::unique_ptr<PlatformMessage> message);
95100

shell/common/shell_unittests.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
#include "flutter/shell/common/vsync_waiters_test.h"
4646
#include "flutter/shell/version/version.h"
4747
#include "flutter/testing/testing.h"
48+
#include "fml/mapping.h"
4849
#include "gmock/gmock.h"
4950
#include "impeller/core/runtime_types.h"
51+
#include "lib/ui/semantics/semantics_node.h"
5052
#include "third_party/rapidjson/include/rapidjson/writer.h"
5153
#include "third_party/skia/include/codec/SkCodecAnimation.h"
5254
#include "third_party/tonic/converter/dart_converter.h"
@@ -4311,6 +4313,41 @@ TEST_F(ShellTest, NavigationMessageDispachedImmediately) {
43114313
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
43124314
}
43134315

4316+
TEST_F(ShellTest, SemanticsActionsPostTask) {
4317+
Settings settings = CreateSettingsForFixture();
4318+
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
4319+
ThreadHost::Type::kPlatform);
4320+
auto task_runner = thread_host.platform_thread->GetTaskRunner();
4321+
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
4322+
task_runner);
4323+
4324+
EXPECT_EQ(task_runners.GetPlatformTaskRunner(),
4325+
task_runners.GetUITaskRunner());
4326+
auto shell = CreateShell(settings, task_runners);
4327+
auto configuration = RunConfiguration::InferFromSettings(settings);
4328+
configuration.SetEntrypoint("testSemanticsActions");
4329+
4330+
RunEngine(shell.get(), std::move(configuration));
4331+
4332+
task_runners.GetPlatformTaskRunner()->PostTask([&] {
4333+
SendSemanticsAction(shell.get(), 0, SemanticsAction::kTap,
4334+
fml::MallocMapping(nullptr, 0));
4335+
});
4336+
4337+
// Fulfill native function for the second Shell's entrypoint.
4338+
fml::CountDownLatch latch(1);
4339+
AddNativeCallback(
4340+
// The Dart native function names aren't very consistent but this is
4341+
// just the native function name of the second vm entrypoint in the
4342+
// fixture.
4343+
"NotifyNative",
4344+
CREATE_NATIVE_ENTRY([&](auto args) { latch.CountDown(); }));
4345+
latch.Wait();
4346+
4347+
DestroyShell(std::move(shell), task_runners);
4348+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
4349+
}
4350+
43144351
TEST_F(ShellTest, DiesIfSoftwareRenderingAndImpellerAreEnabledDeathTest) {
43154352
#if defined(OS_FUCHSIA)
43164353
GTEST_SKIP() << "Fuchsia";

0 commit comments

Comments
 (0)