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 
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.
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
virtual void SetLifecycleState(AppLifecycleState state)
TEST_F(CompositorOpenGLTest, CreateBackingStore)
static void WaitUntilUpdated(const WindowsLifecycleManager &manager)