@@ -670,6 +670,15 @@ class MockWindowsLifecycleManager : public WindowsLifecycleManager {
670
670
MOCK_METHOD4 (DispatchMessage, void (HWND, UINT, WPARAM, LPARAM));
671
671
MOCK_METHOD0 (IsLastWindowOfProcess, bool (void ));
672
672
MOCK_METHOD1 (SetLifecycleState, void (AppLifecycleState));
673
+
674
+ void BeginProcessingLifecycle () override {
675
+ WindowsLifecycleManager::BeginProcessingLifecycle ();
676
+ if (begin_processing_callback) {
677
+ begin_processing_callback ();
678
+ }
679
+ }
680
+
681
+ std::function<void ()> begin_processing_callback = nullptr ;
673
682
};
674
683
675
684
TEST_F (FlutterWindowsEngineTest, TestExit) {
@@ -1002,5 +1011,105 @@ TEST_F(FlutterWindowsEngineTest, InnerWindowHidden) {
1002
1011
AppLifecycleState::kInactive );
1003
1012
}
1004
1013
1014
+ TEST_F (FlutterWindowsEngineTest, EnableLifecycleState) {
1015
+ FlutterWindowsEngineBuilder builder{GetContext ()};
1016
+ builder.SetDartEntrypoint (" enableLifecycleTest" );
1017
+ bool finished = false ;
1018
+
1019
+ auto window_binding_handler =
1020
+ std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
1021
+ MockFlutterWindowsView view (std::move (window_binding_handler));
1022
+ view.SetEngine (builder.Build ());
1023
+ FlutterWindowsEngine* engine = view.GetEngine ();
1024
+
1025
+ EngineModifier modifier (engine);
1026
+ modifier.embedder_api ().RunsAOTCompiledDartCode = []() { return false ; };
1027
+ auto handler = std::make_unique<MockWindowsLifecycleManager>(engine);
1028
+ ON_CALL (*handler, SetLifecycleState)
1029
+ .WillByDefault ([handler_ptr = handler.get ()](AppLifecycleState state) {
1030
+ handler_ptr->WindowsLifecycleManager ::SetLifecycleState (state);
1031
+ });
1032
+ modifier.SetLifecycleManager (std::move (handler));
1033
+
1034
+ auto binary_messenger =
1035
+ std::make_unique<BinaryMessengerImpl>(engine->messenger ());
1036
+ // Mark the test only as completed on receiving an inactive state message.
1037
+ binary_messenger->SetMessageHandler (
1038
+ " flutter/unittest" , [&finished](const uint8_t * message,
1039
+ size_t message_size, BinaryReply reply) {
1040
+ std::string contents (message, message + message_size);
1041
+ EXPECT_NE (contents.find (" AppLifecycleState.inactive" ),
1042
+ std::string::npos);
1043
+ finished = true ;
1044
+ char response[] = " " ;
1045
+ reply (reinterpret_cast <uint8_t *>(response), 0 );
1046
+ });
1047
+
1048
+ engine->Run ();
1049
+
1050
+ // Test that setting the state before enabling lifecycle does nothing.
1051
+ HWND hwnd = reinterpret_cast <HWND>(1 );
1052
+ view.OnWindowStateEvent (hwnd, WindowStateEvent::kShow );
1053
+ view.OnWindowStateEvent (hwnd, WindowStateEvent::kHide );
1054
+ EXPECT_FALSE (finished);
1055
+
1056
+ // Test that we can set the state afterwards.
1057
+ engine->OnApplicationLifecycleEnabled ();
1058
+ view.OnWindowStateEvent (hwnd, WindowStateEvent::kShow );
1059
+
1060
+ while (!finished) {
1061
+ engine->task_runner ()->ProcessTasks ();
1062
+ }
1063
+ }
1064
+
1065
+ TEST_F (FlutterWindowsEngineTest, LifecycleStateToFrom) {
1066
+ FlutterWindowsEngineBuilder builder{GetContext ()};
1067
+ builder.SetDartEntrypoint (" enableLifecycleToFrom" );
1068
+ bool enabled_lifecycle = false ;
1069
+ bool dart_responded = false ;
1070
+
1071
+ auto window_binding_handler =
1072
+ std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
1073
+ MockFlutterWindowsView view (std::move (window_binding_handler));
1074
+ view.SetEngine (builder.Build ());
1075
+ FlutterWindowsEngine* engine = view.GetEngine ();
1076
+
1077
+ EngineModifier modifier (engine);
1078
+ modifier.embedder_api ().RunsAOTCompiledDartCode = []() { return false ; };
1079
+ auto handler = std::make_unique<MockWindowsLifecycleManager>(engine);
1080
+ ON_CALL (*handler, SetLifecycleState)
1081
+ .WillByDefault ([handler_ptr = handler.get ()](AppLifecycleState state) {
1082
+ handler_ptr->WindowsLifecycleManager ::SetLifecycleState (state);
1083
+ });
1084
+ handler->begin_processing_callback = [&]() { enabled_lifecycle = true ; };
1085
+ modifier.SetLifecycleManager (std::move (handler));
1086
+
1087
+ auto binary_messenger =
1088
+ std::make_unique<BinaryMessengerImpl>(engine->messenger ());
1089
+ binary_messenger->SetMessageHandler (
1090
+ " flutter/unittest" ,
1091
+ [&](const uint8_t * message, size_t message_size, BinaryReply reply) {
1092
+ std::string contents (message, message + message_size);
1093
+ EXPECT_NE (contents.find (" AppLifecycleState." ), std::string::npos);
1094
+ dart_responded = true ;
1095
+ char response[] = " " ;
1096
+ reply (reinterpret_cast <uint8_t *>(response), 0 );
1097
+ });
1098
+
1099
+ engine->Run ();
1100
+
1101
+ while (!enabled_lifecycle) {
1102
+ engine->task_runner ()->ProcessTasks ();
1103
+ }
1104
+
1105
+ HWND hwnd = reinterpret_cast <HWND>(1 );
1106
+ view.OnWindowStateEvent (hwnd, WindowStateEvent::kShow );
1107
+ view.OnWindowStateEvent (hwnd, WindowStateEvent::kHide );
1108
+
1109
+ while (!dart_responded) {
1110
+ engine->task_runner ()->ProcessTasks ();
1111
+ }
1112
+ }
1113
+
1005
1114
} // namespace testing
1006
1115
} // namespace flutter
0 commit comments