@@ -51,10 +51,32 @@ class TestWindowsApi : public testing::StubFlutterWindowsApi {
5151 return true ;
5252 }
5353
54+ bool PluginRegistrarIsPlatformThread () override {
55+ return is_platform_thread_;
56+ }
57+
58+ void PluginRegistrarPostPlatformThreadTask (VoidCallback callback,
59+ VoidCallback on_cancel,
60+ void * user_data) override {
61+ post_task_callback_ = callback;
62+ post_task_on_cancel_ = on_cancel;
63+ post_task_user_data_ = user_data;
64+ }
65+
66+ void set_is_platform_thread (bool value) { is_platform_thread_ = value; }
67+
68+ VoidCallback post_task_callback () { return post_task_callback_; }
69+ VoidCallback post_task_on_cancel () { return post_task_on_cancel_; }
70+ void * post_task_user_data () { return post_task_user_data_; }
71+
5472 private:
5573 int registered_delegate_count_ = 0 ;
5674 FlutterDesktopWindowProcCallback last_registered_delegate_ = nullptr ;
5775 void * last_registered_user_data_ = nullptr ;
76+ bool is_platform_thread_ = false ;
77+ VoidCallback post_task_callback_ = nullptr ;
78+ VoidCallback post_task_on_cancel_ = nullptr ;
79+ void * post_task_user_data_ = nullptr ;
5880};
5981
6082// A test plugin that tries to access registrar state during destruction and
@@ -249,4 +271,81 @@ TEST(PluginRegistrarWindowsTest, StopsOnceHandled) {
249271 EXPECT_EQ (result, 7 );
250272}
251273
274+ TEST (PluginRegistrarWindowsTest, IsPlatformThread) {
275+ auto windows_api = std::make_unique<TestWindowsApi>();
276+ EXPECT_CALL (*windows_api, PluginRegistrarGetView)
277+ .WillRepeatedly (Return (nullptr ));
278+ testing::ScopedStubFlutterWindowsApi scoped_api_stub (std::move (windows_api));
279+ auto test_api = static_cast <TestWindowsApi*>(scoped_api_stub.stub ());
280+ PluginRegistrarWindows registrar (
281+ reinterpret_cast <FlutterDesktopPluginRegistrarRef>(1 ));
282+
283+ test_api->set_is_platform_thread (false );
284+ EXPECT_FALSE (registrar.IsPlatformThread ());
285+
286+ test_api->set_is_platform_thread (true );
287+ EXPECT_TRUE (registrar.IsPlatformThread ());
288+ }
289+
290+ TEST (PluginRegistrarWindowsTest, PostPlatformThreadTask) {
291+ auto windows_api = std::make_unique<TestWindowsApi>();
292+ EXPECT_CALL (*windows_api, PluginRegistrarGetView)
293+ .WillRepeatedly (Return (nullptr ));
294+ testing::ScopedStubFlutterWindowsApi scoped_api_stub (std::move (windows_api));
295+ auto test_api = static_cast <TestWindowsApi*>(scoped_api_stub.stub ());
296+ PluginRegistrarWindows registrar (
297+ reinterpret_cast <FlutterDesktopPluginRegistrarRef>(1 ));
298+
299+ bool called = false ;
300+ registrar.PostPlatformThreadTask ([&called]() { called = true ; });
301+
302+ // The stub should have received the task.
303+ EXPECT_NE (test_api->post_task_callback (), nullptr );
304+ EXPECT_NE (test_api->post_task_user_data (), nullptr );
305+ EXPECT_FALSE (called);
306+
307+ // Simulate executing the callback.
308+ test_api->post_task_callback ()(test_api->post_task_user_data ());
309+ EXPECT_TRUE (called);
310+ }
311+
312+ TEST (PluginRegistrarWindowsTest, PostPlatformThreadTaskNullCallback) {
313+ auto windows_api = std::make_unique<TestWindowsApi>();
314+ EXPECT_CALL (*windows_api, PluginRegistrarGetView)
315+ .WillRepeatedly (Return (nullptr ));
316+ testing::ScopedStubFlutterWindowsApi scoped_api_stub (std::move (windows_api));
317+ auto test_api = static_cast <TestWindowsApi*>(scoped_api_stub.stub ());
318+ PluginRegistrarWindows registrar (
319+ reinterpret_cast <FlutterDesktopPluginRegistrarRef>(1 ));
320+
321+ // Posting a null callback should be a no-op.
322+ registrar.PostPlatformThreadTask (nullptr );
323+ EXPECT_EQ (test_api->post_task_callback (), nullptr );
324+ }
325+
326+ TEST (PluginRegistrarWindowsTest, PostPlatformThreadTaskOnCancel) {
327+ auto windows_api = std::make_unique<TestWindowsApi>();
328+ EXPECT_CALL (*windows_api, PluginRegistrarGetView)
329+ .WillRepeatedly (Return (nullptr ));
330+ testing::ScopedStubFlutterWindowsApi scoped_api_stub (std::move (windows_api));
331+ auto test_api = static_cast <TestWindowsApi*>(scoped_api_stub.stub ());
332+ PluginRegistrarWindows registrar (
333+ reinterpret_cast <FlutterDesktopPluginRegistrarRef>(1 ));
334+
335+ bool called = false ;
336+ // Captured by the callback to detect whether the callback is freed.
337+ auto captured = std::make_shared<int >(0 );
338+ std::weak_ptr<int > weak_token = captured;
339+ registrar.PostPlatformThreadTask (
340+ [&called, token = std::move (captured)]() { called = true ; });
341+ EXPECT_FALSE (weak_token.expired ());
342+
343+ // Simulate the engine discarding the task: on_cancel must free the
344+ // callback without running it.
345+ EXPECT_NE (test_api->post_task_on_cancel (), nullptr );
346+ test_api->post_task_on_cancel ()(test_api->post_task_user_data ());
347+ EXPECT_FALSE (called);
348+ EXPECT_TRUE (weak_token.expired ());
349+ }
350+
252351} // namespace flutter
0 commit comments