| Flutter Windows Embedder |

windows_lifecycle_manager_unittests.cc
Go to the documentation of this file.
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/shell/platform/windows/windows_lifecycle_manager.h"
6
7 #include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
8 #include "flutter/shell/platform/windows/testing/windows_test.h"
9 #include "gtest/gtest.h"
10
11 namespace flutter {
12 namespace testing {
13
14 class WindowsLifecycleManagerTest : public WindowsTest {};
15
16 static void WaitUntilUpdated(const WindowsLifecycleManager& manager) {
17 while (manager.IsUpdateStateScheduled()) {
18 ::MSG msg;
19 if (::GetMessage(&msg, nullptr, 0, 0)) {
20 ::TranslateMessage(&msg);
21 ::DispatchMessage(&msg);
22 }
23 }
24 }
25
26 TEST_F(WindowsLifecycleManagerTest, StateTransitions) {
27 FlutterWindowsEngineBuilder builder{GetContext()};
28 std::unique_ptr<FlutterWindowsEngine> engine = builder.Build();
29
30 WindowsLifecycleManager manager{engine.get()};
31 HWND win1 = reinterpret_cast<HWND>(1);
32 HWND win2 = reinterpret_cast<HWND>(2);
33
34 // Hidden to inactive upon window shown.
35 manager.SetLifecycleState(AppLifecycleState::kHidden);
36 manager.OnWindowStateEvent(win1, WindowStateEvent::kShow);
37 WaitUntilUpdated(manager);
38 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kInactive);
39
40 // Showing a second window does not change state.
41 manager.OnWindowStateEvent(win2, WindowStateEvent::kShow);
42 WaitUntilUpdated(manager);
43 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kInactive);
44
45 // Inactive to resumed upon window focus.
46 manager.OnWindowStateEvent(win2, WindowStateEvent::kFocus);
47 WaitUntilUpdated(manager);
48 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kResumed);
49
50 // Showing a second window does not change state.
51 manager.OnWindowStateEvent(win1, WindowStateEvent::kFocus);
52 WaitUntilUpdated(manager);
53 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kResumed);
54
55 // Unfocusing one window does not change state while another is focused.
56 manager.OnWindowStateEvent(win1, WindowStateEvent::kUnfocus);
57 WaitUntilUpdated(manager);
58 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kResumed);
59
60 // Unfocusing final remaining focused window transitions to inactive.
61 manager.OnWindowStateEvent(win2, WindowStateEvent::kUnfocus);
62 WaitUntilUpdated(manager);
63 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kInactive);
64
65 // Hiding one of two visible windows does not change state.
66 manager.OnWindowStateEvent(win2, WindowStateEvent::kHide);
67 WaitUntilUpdated(manager);
68 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kInactive);
69
70 // Hiding only visible window transitions to hidden.
71 manager.OnWindowStateEvent(win1, WindowStateEvent::kHide);
72 WaitUntilUpdated(manager);
73 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kHidden);
74
75 // Transition directly from resumed to hidden when the window is hidden.
76 manager.OnWindowStateEvent(win1, WindowStateEvent::kShow);
77 manager.OnWindowStateEvent(win1, WindowStateEvent::kFocus);
78 manager.OnWindowStateEvent(win1, WindowStateEvent::kHide);
79 WaitUntilUpdated(manager);
80 EXPECT_EQ(manager.GetLifecycleState(), AppLifecycleState::kHidden);
81 }
82
83 } // namespace testing
84 } // namespace flutter
flutter::WindowsLifecycleManager
Definition: windows_lifecycle_manager.h:37
flutter::WindowsLifecycleManager::IsUpdateStateScheduled
bool IsUpdateStateScheduled() const
Definition: windows_lifecycle_manager.h:74
flutter::WindowsLifecycleManager::SetLifecycleState
virtual void SetLifecycleState(AppLifecycleState state)
Definition: windows_lifecycle_manager.cc:195
flutter::testing::WindowsLifecycleManagerTest
Definition: windows_lifecycle_manager_unittests.cc:14
flutter::testing::TEST_F
TEST_F(AccessibilityPluginTest, DirectAnnounceCall)
Definition: accessibility_plugin_unittests.cc:102
flutter::testing::WaitUntilUpdated
static void WaitUntilUpdated(const WindowsLifecycleManager &manager)
Definition: windows_lifecycle_manager_unittests.cc:16
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowStateEvent::kHide
@ kHide
flutter::WindowStateEvent::kUnfocus
@ kUnfocus
flutter::WindowStateEvent::kShow
@ kShow
flutter::WindowStateEvent::kFocus
@ kFocus
flutter::AppLifecycleState::kHidden
@ kHidden
flutter::AppLifecycleState::kResumed
@ kResumed
flutter::AppLifecycleState::kInactive
@ kInactive
windows_lifecycle_manager.h