| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "services/ui/common/types.h" | 11 #include "services/ui/common/types.h" |
| 12 #include "services/ui/common/util.h" | 12 #include "services/ui/common/util.h" |
| 13 #include "services/ui/display/viewport_metrics.h" |
| 13 #include "services/ui/public/interfaces/window_tree.mojom.h" | 14 #include "services/ui/public/interfaces/window_tree.mojom.h" |
| 14 #include "services/ui/surfaces/display_compositor.h" | 15 #include "services/ui/surfaces/display_compositor.h" |
| 15 #include "services/ui/ws/display_manager.h" | 16 #include "services/ui/ws/display_manager.h" |
| 16 #include "services/ui/ws/ids.h" | 17 #include "services/ui/ws/ids.h" |
| 17 #include "services/ui/ws/platform_display.h" | 18 #include "services/ui/ws/platform_display.h" |
| 18 #include "services/ui/ws/platform_display_factory.h" | 19 #include "services/ui/ws/platform_display_factory.h" |
| 19 #include "services/ui/ws/server_window.h" | 20 #include "services/ui/ws/server_window.h" |
| 20 #include "services/ui/ws/test_utils.h" | 21 #include "services/ui/ws/test_utils.h" |
| 21 #include "services/ui/ws/window_manager_display_root.h" | 22 #include "services/ui/ws/window_manager_display_root.h" |
| 22 #include "services/ui/ws/window_manager_state.h" | 23 #include "services/ui/ws/window_manager_state.h" |
| 23 #include "services/ui/ws/window_server.h" | 24 #include "services/ui/ws/window_server.h" |
| 24 #include "services/ui/ws/window_server_delegate.h" | 25 #include "services/ui/ws/window_server_delegate.h" |
| 25 #include "services/ui/ws/window_tree.h" | 26 #include "services/ui/ws/window_tree.h" |
| 26 #include "services/ui/ws/window_tree_binding.h" | 27 #include "services/ui/ws/window_tree_binding.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "ui/events/event.h" | 29 #include "ui/events/event.h" |
| 29 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 30 | 31 |
| 32 using display::ViewportMetrics; |
| 33 |
| 31 namespace ui { | 34 namespace ui { |
| 32 namespace ws { | 35 namespace ws { |
| 33 namespace test { | 36 namespace test { |
| 34 namespace { | 37 namespace { |
| 35 | 38 |
| 36 const UserId kTestId1 = "2"; | 39 const UserId kTestId1 = "2"; |
| 37 const UserId kTestId2 = "21"; | 40 const UserId kTestId2 = "21"; |
| 38 | 41 |
| 39 ClientWindowId ClientWindowIdForFirstRoot(WindowTree* tree) { | 42 ClientWindowId ClientWindowIdForFirstRoot(WindowTree* tree) { |
| 40 if (tree->roots().empty()) | 43 if (tree->roots().empty()) |
| 41 return ClientWindowId(); | 44 return ClientWindowId(); |
| 42 return ClientWindowIdForWindow(tree, *tree->roots().begin()); | 45 return ClientWindowIdForWindow(tree, *tree->roots().begin()); |
| 43 } | 46 } |
| 44 | 47 |
| 45 WindowManagerState* GetWindowManagerStateForUser(Display* display, | 48 WindowManagerState* GetWindowManagerStateForUser(Display* display, |
| 46 const UserId& user_id) { | 49 const UserId& user_id) { |
| 47 WindowManagerDisplayRoot* display_root = | 50 WindowManagerDisplayRoot* display_root = |
| 48 display->GetWindowManagerDisplayRootForUser(user_id); | 51 display->GetWindowManagerDisplayRootForUser(user_id); |
| 49 return display_root ? display_root->window_manager_state() : nullptr; | 52 return display_root ? display_root->window_manager_state() : nullptr; |
| 50 } | 53 } |
| 51 | 54 |
| 55 // Returns the root ServerWindow for the specified Display. |
| 56 ServerWindow* GetRootOnDisplay(WindowTree* tree, Display* display) { |
| 57 for (const ServerWindow* root : tree->roots()) { |
| 58 if (tree->GetDisplay(root) == display) |
| 59 return const_cast<ServerWindow*>(root); |
| 60 } |
| 61 return nullptr; |
| 62 } |
| 63 |
| 64 // Tracks destruction of a ServerWindow, setting a bool* to true when |
| 65 // OnWindowDestroyed() is called |
| 66 class ServerWindowDestructionObserver : public ServerWindowObserver { |
| 67 public: |
| 68 ServerWindowDestructionObserver(ServerWindow* window, bool* destroyed) |
| 69 : window_(window), destroyed_(destroyed) { |
| 70 window_->AddObserver(this); |
| 71 } |
| 72 ~ServerWindowDestructionObserver() override { |
| 73 if (window_) |
| 74 window_->RemoveObserver(this); |
| 75 } |
| 76 |
| 77 // ServerWindowObserver: |
| 78 void OnWindowDestroyed(ServerWindow* window) override { |
| 79 *destroyed_ = true; |
| 80 window_->RemoveObserver(this); |
| 81 window_ = nullptr; |
| 82 } |
| 83 |
| 84 private: |
| 85 ServerWindow* window_; |
| 86 bool* destroyed_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(ServerWindowDestructionObserver); |
| 89 }; |
| 90 |
| 52 } // namespace | 91 } // namespace |
| 53 | 92 |
| 54 // ----------------------------------------------------------------------------- | 93 // ----------------------------------------------------------------------------- |
| 55 | 94 |
| 56 class DisplayTest : public testing::Test { | 95 class DisplayTest : public testing::Test { |
| 57 public: | 96 public: |
| 58 DisplayTest() {} | 97 DisplayTest() {} |
| 59 ~DisplayTest() override {} | 98 ~DisplayTest() override {} |
| 60 | 99 |
| 61 WindowServer* window_server() { return ws_test_helper_.window_server(); } | 100 WindowServer* window_server() { return ws_test_helper_.window_server(); } |
| 101 DisplayManager* display_manager() { |
| 102 return window_server()->display_manager(); |
| 103 } |
| 62 TestWindowServerDelegate* window_server_delegate() { | 104 TestWindowServerDelegate* window_server_delegate() { |
| 63 return ws_test_helper_.window_server_delegate(); | 105 return ws_test_helper_.window_server_delegate(); |
| 64 } | 106 } |
| 107 TestPlatformScreen& platform_screen() { return platform_screen_; } |
| 65 | 108 |
| 66 protected: | 109 protected: |
| 67 // testing::Test: | 110 // testing::Test: |
| 68 void SetUp() override { | 111 void SetUp() override { |
| 112 platform_screen_.Init(window_server()->display_manager()); |
| 69 window_server()->user_id_tracker()->AddUserId(kTestId1); | 113 window_server()->user_id_tracker()->AddUserId(kTestId1); |
| 70 window_server()->user_id_tracker()->AddUserId(kTestId2); | 114 window_server()->user_id_tracker()->AddUserId(kTestId2); |
| 71 } | 115 } |
| 72 | 116 |
| 73 private: | 117 private: |
| 74 WindowServerTestHelper ws_test_helper_; | 118 WindowServerTestHelper ws_test_helper_; |
| 119 TestPlatformScreen platform_screen_; |
| 120 |
| 75 DISALLOW_COPY_AND_ASSIGN(DisplayTest); | 121 DISALLOW_COPY_AND_ASSIGN(DisplayTest); |
| 76 }; | 122 }; |
| 77 | 123 |
| 78 TEST_F(DisplayTest, CallsCreateDefaultDisplays) { | 124 TEST_F(DisplayTest, CreateDisplay) { |
| 79 const int kNumHostsToCreate = 2; | 125 AddWindowManager(window_server(), kTestId1); |
| 80 window_server_delegate()->CreateDisplays(kNumHostsToCreate); | 126 const int64_t display_id = |
| 127 platform_screen().AddDisplay(MakeViewportMetrics(0, 0, 1024, 768, 1.0f)); |
| 81 | 128 |
| 82 DisplayManager* display_manager = window_server()->display_manager(); | 129 ASSERT_EQ(1u, display_manager()->displays().size()); |
| 130 Display* display = display_manager()->GetDisplayById(display_id); |
| 131 |
| 132 // Display should have root window with correct size. |
| 133 ASSERT_NE(nullptr, display->root_window()); |
| 134 EXPECT_EQ("0,0 1024x768", display->root_window()->bounds().ToString()); |
| 135 |
| 136 // Display should have a WM root window with the correct size too. |
| 137 EXPECT_EQ(1u, display->num_window_manger_states()); |
| 138 WindowManagerDisplayRoot* root1 = |
| 139 display->GetWindowManagerDisplayRootForUser(kTestId1); |
| 140 ASSERT_NE(nullptr, root1); |
| 141 ASSERT_NE(nullptr, root1->root()); |
| 142 EXPECT_EQ("0,0 1024x768", root1->root()->bounds().ToString()); |
| 143 } |
| 144 |
| 145 TEST_F(DisplayTest, CreateDisplayBeforeWM) { |
| 146 // Add one display, no WM exists yet. |
| 147 const int64_t display_id = |
| 148 platform_screen().AddDisplay(MakeViewportMetrics(0, 0, 1024, 768, 1.0f)); |
| 149 EXPECT_EQ(1u, display_manager()->displays().size()); |
| 150 |
| 151 Display* display = display_manager()->GetDisplayById(display_id); |
| 152 |
| 153 // Display should have root window with correct size. |
| 154 ASSERT_NE(nullptr, display->root_window()); |
| 155 EXPECT_EQ("0,0 1024x768", display->root_window()->bounds().ToString()); |
| 156 |
| 157 // There should be no WM state for display yet. |
| 158 EXPECT_EQ(0u, display->num_window_manger_states()); |
| 159 EXPECT_EQ(nullptr, display->GetWindowManagerDisplayRootForUser(kTestId1)); |
| 160 |
| 83 AddWindowManager(window_server(), kTestId1); | 161 AddWindowManager(window_server(), kTestId1); |
| 84 // The first register should trigger creation of the default | |
| 85 // Displays. There should be kNumHostsToCreate Displays. | |
| 86 EXPECT_EQ(static_cast<size_t>(kNumHostsToCreate), | |
| 87 display_manager->displays().size()); | |
| 88 | 162 |
| 89 // Each host should have a WindowManagerState for kTestId1. | 163 // After adding a WM display should have WM state and WM root for the display. |
| 90 for (Display* display : display_manager->displays()) { | 164 EXPECT_EQ(1u, display->num_window_manger_states()); |
| 91 EXPECT_EQ(1u, display->num_window_manger_states()); | 165 WindowManagerDisplayRoot* root1 = |
| 92 EXPECT_TRUE(GetWindowManagerStateForUser(display, kTestId1)); | 166 display->GetWindowManagerDisplayRootForUser(kTestId1); |
| 93 EXPECT_FALSE(GetWindowManagerStateForUser(display, kTestId2)); | 167 ASSERT_NE(nullptr, root1); |
| 94 } | 168 ASSERT_NE(nullptr, root1->root()); |
| 169 EXPECT_EQ("0,0 1024x768", root1->root()->bounds().ToString()); |
| 170 } |
| 95 | 171 |
| 96 // Add another registry, should trigger creation of another wm. | 172 TEST_F(DisplayTest, CreateDisplayWithTwoWindowManagers) { |
| 173 AddWindowManager(window_server(), kTestId1); |
| 174 const int64_t display_id = platform_screen().AddDisplay(); |
| 175 Display* display = display_manager()->GetDisplayById(display_id); |
| 176 |
| 177 // There should be only be one WM at this point. |
| 178 ASSERT_EQ(1u, display->num_window_manger_states()); |
| 179 EXPECT_NE(nullptr, display->GetWindowManagerDisplayRootForUser(kTestId1)); |
| 180 EXPECT_EQ(nullptr, display->GetWindowManagerDisplayRootForUser(kTestId2)); |
| 181 |
| 97 AddWindowManager(window_server(), kTestId2); | 182 AddWindowManager(window_server(), kTestId2); |
| 98 for (Display* display : display_manager->displays()) { | 183 |
| 99 ASSERT_EQ(2u, display->num_window_manger_states()); | 184 // There should now be two WMs. |
| 100 WindowManagerDisplayRoot* root1 = | 185 ASSERT_EQ(2u, display->num_window_manger_states()); |
| 101 display->GetWindowManagerDisplayRootForUser(kTestId1); | 186 WindowManagerDisplayRoot* root1 = |
| 102 ASSERT_TRUE(root1); | 187 display->GetWindowManagerDisplayRootForUser(kTestId1); |
| 103 WindowManagerDisplayRoot* root2 = | 188 ASSERT_NE(nullptr, root1); |
| 104 display->GetWindowManagerDisplayRootForUser(kTestId2); | 189 WindowManagerDisplayRoot* root2 = |
| 105 ASSERT_TRUE(root2); | 190 display->GetWindowManagerDisplayRootForUser(kTestId2); |
| 106 // Verify the two states have different roots. | 191 ASSERT_NE(nullptr, root2); |
| 107 EXPECT_NE(root1, root2); | 192 |
| 108 EXPECT_NE(root1->root(), root2->root()); | 193 // Verify the two WMs have different roots but with the same bounds. |
| 109 } | 194 EXPECT_NE(root1, root2); |
| 195 EXPECT_NE(root1->root(), root2->root()); |
| 196 EXPECT_EQ(root1->root()->bounds(), root2->root()->bounds()); |
| 197 } |
| 198 |
| 199 TEST_F(DisplayTest, CreateDisplayWithDeviceScaleFactor) { |
| 200 // The display bounds should be the pixel_size / device_scale_factor. |
| 201 const ViewportMetrics metrics = MakeViewportMetrics(0, 0, 1024, 768, 2.0f); |
| 202 EXPECT_EQ("0,0 512x384", metrics.bounds.ToString()); |
| 203 EXPECT_EQ("1024x768", metrics.pixel_size.ToString()); |
| 204 |
| 205 const int64_t display_id = platform_screen().AddDisplay(metrics); |
| 206 Display* display = display_manager()->GetDisplayById(display_id); |
| 207 |
| 208 // The root ServerWindow bounds should be in PP. |
| 209 EXPECT_EQ("0,0 1024x768", display->root_window()->bounds().ToString()); |
| 210 |
| 211 ViewportMetrics modified_metrics = metrics; |
| 212 modified_metrics.work_area.set_height(metrics.work_area.height() - 48); |
| 213 platform_screen().ModifyDisplay(display_id, modified_metrics); |
| 214 |
| 215 // The root ServerWindow should still be in PP after updating the work area. |
| 216 EXPECT_EQ("0,0 1024x768", display->root_window()->bounds().ToString()); |
| 110 } | 217 } |
| 111 | 218 |
| 112 TEST_F(DisplayTest, Destruction) { | 219 TEST_F(DisplayTest, Destruction) { |
| 113 window_server_delegate()->CreateDisplays(1); | |
| 114 | |
| 115 AddWindowManager(window_server(), kTestId1); | 220 AddWindowManager(window_server(), kTestId1); |
| 116 | 221 |
| 117 // Add another registry, should trigger creation of another wm. | 222 int64_t display_id = platform_screen().AddDisplay(); |
| 118 DisplayManager* display_manager = window_server()->display_manager(); | 223 |
| 224 // Add a second WM. |
| 119 AddWindowManager(window_server(), kTestId2); | 225 AddWindowManager(window_server(), kTestId2); |
| 120 ASSERT_EQ(1u, display_manager->displays().size()); | 226 ASSERT_EQ(1u, display_manager()->displays().size()); |
| 121 Display* display = *display_manager->displays().begin(); | 227 Display* display = display_manager()->GetDisplayById(display_id); |
| 122 ASSERT_EQ(2u, display->num_window_manger_states()); | 228 ASSERT_EQ(2u, display->num_window_manger_states()); |
| 123 // There should be two trees, one for each windowmanager. | 229 // There should be two trees, one for each windowmanager. |
| 124 EXPECT_EQ(2u, window_server()->num_trees()); | 230 EXPECT_EQ(2u, window_server()->num_trees()); |
| 125 | 231 |
| 126 { | 232 { |
| 127 WindowManagerState* state = GetWindowManagerStateForUser(display, kTestId1); | 233 WindowManagerState* state = GetWindowManagerStateForUser(display, kTestId1); |
| 128 // Destroy the tree associated with |state|. Should result in deleting | 234 // Destroy the tree associated with |state|. Should result in deleting |
| 129 // |state|. | 235 // |state|. |
| 130 window_server()->DestroyTree(state->window_tree()); | 236 window_server()->DestroyTree(state->window_tree()); |
| 131 ASSERT_EQ(1u, display->num_window_manger_states()); | 237 ASSERT_EQ(1u, display->num_window_manger_states()); |
| 132 EXPECT_FALSE(GetWindowManagerStateForUser(display, kTestId1)); | 238 EXPECT_FALSE(GetWindowManagerStateForUser(display, kTestId1)); |
| 133 EXPECT_EQ(1u, display_manager->displays().size()); | 239 EXPECT_EQ(1u, display_manager()->displays().size()); |
| 134 EXPECT_EQ(1u, window_server()->num_trees()); | 240 EXPECT_EQ(1u, window_server()->num_trees()); |
| 135 } | 241 } |
| 136 | 242 |
| 137 EXPECT_FALSE(window_server_delegate()->got_on_no_more_displays()); | 243 EXPECT_FALSE(window_server_delegate()->got_on_no_more_displays()); |
| 138 window_server()->display_manager()->DestroyDisplay(display); | 244 platform_screen().RemoveDisplay(display_id); |
| 139 // There is still one tree left. | 245 // There is still one tree left. |
| 140 EXPECT_EQ(1u, window_server()->num_trees()); | 246 EXPECT_EQ(1u, window_server()->num_trees()); |
| 141 EXPECT_TRUE(window_server_delegate()->got_on_no_more_displays()); | 247 EXPECT_TRUE(window_server_delegate()->got_on_no_more_displays()); |
| 142 } | 248 } |
| 143 | 249 |
| 144 TEST_F(DisplayTest, EventStateResetOnUserSwitch) { | 250 TEST_F(DisplayTest, EventStateResetOnUserSwitch) { |
| 145 window_server_delegate()->CreateDisplays(1); | 251 const int64_t display_id = platform_screen().AddDisplay(); |
| 146 | 252 |
| 147 AddWindowManager(window_server(), kTestId1); | 253 AddWindowManager(window_server(), kTestId1); |
| 148 AddWindowManager(window_server(), kTestId2); | 254 AddWindowManager(window_server(), kTestId2); |
| 149 | 255 |
| 150 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); | 256 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); |
| 151 | 257 |
| 152 DisplayManager* display_manager = window_server()->display_manager(); | 258 ASSERT_EQ(1u, display_manager()->displays().size()); |
| 153 ASSERT_EQ(1u, display_manager->displays().size()); | 259 Display* display = display_manager()->GetDisplayById(display_id); |
| 154 Display* display = *display_manager->displays().begin(); | |
| 155 WindowManagerState* active_wms = | 260 WindowManagerState* active_wms = |
| 156 display->GetActiveWindowManagerDisplayRoot()->window_manager_state(); | 261 display->GetActiveWindowManagerDisplayRoot()->window_manager_state(); |
| 157 ASSERT_TRUE(active_wms); | 262 ASSERT_TRUE(active_wms); |
| 158 EXPECT_EQ(kTestId1, active_wms->user_id()); | 263 EXPECT_EQ(kTestId1, active_wms->user_id()); |
| 159 | 264 |
| 160 static_cast<PlatformDisplayDelegate*>(display)->OnEvent(ui::PointerEvent( | 265 static_cast<PlatformDisplayDelegate*>(display)->OnEvent(ui::PointerEvent( |
| 161 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(20, 25), | 266 ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(20, 25), |
| 162 gfx::Point(20, 25), base::TimeTicks(), | 267 gfx::Point(20, 25), base::TimeTicks(), |
| 163 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON))); | 268 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON))); |
| 164 | 269 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 179 display->GetActiveWindowManagerDisplayRoot()->window_manager_state(); | 284 display->GetActiveWindowManagerDisplayRoot()->window_manager_state(); |
| 180 EXPECT_EQ(kTestId2, active_wms->user_id()); | 285 EXPECT_EQ(kTestId2, active_wms->user_id()); |
| 181 EXPECT_EQ(gfx::Point(20, 25), | 286 EXPECT_EQ(gfx::Point(20, 25), |
| 182 active_wms->event_dispatcher()->mouse_pointer_last_location()); | 287 active_wms->event_dispatcher()->mouse_pointer_last_location()); |
| 183 EXPECT_FALSE(EventDispatcherTestApi(active_wms->event_dispatcher()) | 288 EXPECT_FALSE(EventDispatcherTestApi(active_wms->event_dispatcher()) |
| 184 .AreAnyPointersDown()); | 289 .AreAnyPointersDown()); |
| 185 } | 290 } |
| 186 | 291 |
| 187 // Verifies capture fails when wm is inactive and succeeds when wm is active. | 292 // Verifies capture fails when wm is inactive and succeeds when wm is active. |
| 188 TEST_F(DisplayTest, SetCaptureFromWindowManager) { | 293 TEST_F(DisplayTest, SetCaptureFromWindowManager) { |
| 189 window_server_delegate()->CreateDisplays(1); | 294 const int64_t display_id = platform_screen().AddDisplay(); |
| 190 AddWindowManager(window_server(), kTestId1); | 295 AddWindowManager(window_server(), kTestId1); |
| 191 AddWindowManager(window_server(), kTestId2); | 296 AddWindowManager(window_server(), kTestId2); |
| 192 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); | 297 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); |
| 193 DisplayManager* display_manager = window_server()->display_manager(); | 298 ASSERT_EQ(1u, display_manager()->displays().size()); |
| 194 ASSERT_EQ(1u, display_manager->displays().size()); | 299 Display* display = display_manager()->GetDisplayById(display_id); |
| 195 Display* display = *display_manager->displays().begin(); | |
| 196 WindowManagerState* wms_for_id2 = | 300 WindowManagerState* wms_for_id2 = |
| 197 GetWindowManagerStateForUser(display, kTestId2); | 301 GetWindowManagerStateForUser(display, kTestId2); |
| 198 ASSERT_TRUE(wms_for_id2); | 302 ASSERT_TRUE(wms_for_id2); |
| 199 EXPECT_FALSE(wms_for_id2->IsActive()); | 303 EXPECT_FALSE(wms_for_id2->IsActive()); |
| 200 | 304 |
| 201 // Create a child of the root that we can set capture on. | 305 // Create a child of the root that we can set capture on. |
| 202 WindowTree* tree = wms_for_id2->window_tree(); | 306 WindowTree* tree = wms_for_id2->window_tree(); |
| 203 ClientWindowId child_window_id; | 307 ClientWindowId child_window_id; |
| 204 ASSERT_TRUE(NewWindowInTree(tree, &child_window_id)); | 308 ASSERT_TRUE(NewWindowInTree(tree, &child_window_id)); |
| 205 | 309 |
| 206 WindowTreeTestApi(tree).EnableCapture(); | 310 WindowTreeTestApi(tree).EnableCapture(); |
| 207 | 311 |
| 208 // SetCapture() should fail for user id2 as it is inactive. | 312 // SetCapture() should fail for user id2 as it is inactive. |
| 209 EXPECT_FALSE(tree->SetCapture(child_window_id)); | 313 EXPECT_FALSE(tree->SetCapture(child_window_id)); |
| 210 | 314 |
| 211 // Make the second user active and verify capture works. | 315 // Make the second user active and verify capture works. |
| 212 window_server()->user_id_tracker()->SetActiveUserId(kTestId2); | 316 window_server()->user_id_tracker()->SetActiveUserId(kTestId2); |
| 213 EXPECT_TRUE(wms_for_id2->IsActive()); | 317 EXPECT_TRUE(wms_for_id2->IsActive()); |
| 214 EXPECT_TRUE(tree->SetCapture(child_window_id)); | 318 EXPECT_TRUE(tree->SetCapture(child_window_id)); |
| 215 } | 319 } |
| 216 | 320 |
| 217 TEST_F(DisplayTest, FocusFailsForInactiveUser) { | 321 TEST_F(DisplayTest, FocusFailsForInactiveUser) { |
| 218 window_server_delegate()->CreateDisplays(1); | 322 const int64_t display_id = platform_screen().AddDisplay(); |
| 219 AddWindowManager(window_server(), kTestId1); | 323 AddWindowManager(window_server(), kTestId1); |
| 220 TestWindowTreeClient* window_tree_client1 = | 324 TestWindowTreeClient* window_tree_client1 = |
| 221 window_server_delegate()->last_client(); | 325 window_server_delegate()->last_client(); |
| 222 ASSERT_TRUE(window_tree_client1); | 326 ASSERT_TRUE(window_tree_client1); |
| 223 AddWindowManager(window_server(), kTestId2); | 327 AddWindowManager(window_server(), kTestId2); |
| 224 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); | 328 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); |
| 225 DisplayManager* display_manager = window_server()->display_manager(); | 329 ASSERT_EQ(1u, display_manager()->displays().size()); |
| 226 ASSERT_EQ(1u, display_manager->displays().size()); | 330 Display* display = display_manager()->GetDisplayById(display_id); |
| 227 Display* display = *display_manager->displays().begin(); | |
| 228 WindowManagerState* wms_for_id2 = | 331 WindowManagerState* wms_for_id2 = |
| 229 GetWindowManagerStateForUser(display, kTestId2); | 332 GetWindowManagerStateForUser(display, kTestId2); |
| 230 wms_for_id2->window_tree()->AddActivationParent( | 333 wms_for_id2->window_tree()->AddActivationParent( |
| 231 ClientWindowIdForFirstRoot(wms_for_id2->window_tree())); | 334 ClientWindowIdForFirstRoot(wms_for_id2->window_tree())); |
| 232 ASSERT_TRUE(wms_for_id2); | 335 ASSERT_TRUE(wms_for_id2); |
| 233 EXPECT_FALSE(wms_for_id2->IsActive()); | 336 EXPECT_FALSE(wms_for_id2->IsActive()); |
| 234 ClientWindowId child2_id; | 337 ClientWindowId child2_id; |
| 235 NewWindowInTree(wms_for_id2->window_tree(), &child2_id); | 338 NewWindowInTree(wms_for_id2->window_tree(), &child2_id); |
| 236 | 339 |
| 237 // Focus should fail for windows in inactive window managers. | 340 // Focus should fail for windows in inactive window managers. |
| 238 EXPECT_FALSE(wms_for_id2->window_tree()->SetFocus(child2_id)); | 341 EXPECT_FALSE(wms_for_id2->window_tree()->SetFocus(child2_id)); |
| 239 | 342 |
| 240 // Focus should succeed for the active window manager. | 343 // Focus should succeed for the active window manager. |
| 241 WindowManagerState* wms_for_id1 = | 344 WindowManagerState* wms_for_id1 = |
| 242 GetWindowManagerStateForUser(display, kTestId1); | 345 GetWindowManagerStateForUser(display, kTestId1); |
| 243 ASSERT_TRUE(wms_for_id1); | 346 ASSERT_TRUE(wms_for_id1); |
| 244 wms_for_id1->window_tree()->AddActivationParent( | 347 wms_for_id1->window_tree()->AddActivationParent( |
| 245 ClientWindowIdForFirstRoot(wms_for_id1->window_tree())); | 348 ClientWindowIdForFirstRoot(wms_for_id1->window_tree())); |
| 246 ClientWindowId child1_id; | 349 ClientWindowId child1_id; |
| 247 NewWindowInTree(wms_for_id1->window_tree(), &child1_id); | 350 NewWindowInTree(wms_for_id1->window_tree(), &child1_id); |
| 248 EXPECT_TRUE(wms_for_id1->IsActive()); | 351 EXPECT_TRUE(wms_for_id1->IsActive()); |
| 249 EXPECT_TRUE(wms_for_id1->window_tree()->SetFocus(child1_id)); | 352 EXPECT_TRUE(wms_for_id1->window_tree()->SetFocus(child1_id)); |
| 250 } | 353 } |
| 251 | 354 |
| 252 // Verifies a single tree is used for multiple displays. | 355 // Verifies a single tree is used for multiple displays. |
| 253 TEST_F(DisplayTest, MultipleDisplays) { | 356 TEST_F(DisplayTest, MultipleDisplays) { |
| 254 window_server_delegate()->CreateDisplays(2); | 357 platform_screen().AddDisplay(); |
| 358 platform_screen().AddDisplay(); |
| 255 AddWindowManager(window_server(), kTestId1); | 359 AddWindowManager(window_server(), kTestId1); |
| 256 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); | 360 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); |
| 257 ASSERT_EQ(1u, window_server_delegate()->bindings()->size()); | 361 ASSERT_EQ(1u, window_server_delegate()->bindings()->size()); |
| 258 TestWindowTreeBinding* window_tree_binding = | 362 TestWindowTreeBinding* window_tree_binding = |
| 259 (*window_server_delegate()->bindings())[0]; | 363 (*window_server_delegate()->bindings())[0]; |
| 260 WindowTree* tree = window_tree_binding->tree(); | 364 WindowTree* tree = window_tree_binding->tree(); |
| 261 ASSERT_EQ(2u, tree->roots().size()); | 365 ASSERT_EQ(2u, tree->roots().size()); |
| 262 std::set<const ServerWindow*> roots = tree->roots(); | 366 std::set<const ServerWindow*> roots = tree->roots(); |
| 263 auto it = roots.begin(); | 367 auto it = roots.begin(); |
| 264 ServerWindow* root1 = tree->GetWindow((*it)->id()); | 368 ServerWindow* root1 = tree->GetWindow((*it)->id()); |
| 265 ++it; | 369 ++it; |
| 266 ServerWindow* root2 = tree->GetWindow((*it)->id()); | 370 ServerWindow* root2 = tree->GetWindow((*it)->id()); |
| 267 ASSERT_NE(root1, root2); | 371 ASSERT_NE(root1, root2); |
| 268 Display* display1 = tree->GetDisplay(root1); | 372 Display* display1 = tree->GetDisplay(root1); |
| 269 WindowManagerState* display1_wms = | 373 WindowManagerState* display1_wms = |
| 270 display1->GetWindowManagerDisplayRootForUser(kTestId1) | 374 display1->GetWindowManagerDisplayRootForUser(kTestId1) |
| 271 ->window_manager_state(); | 375 ->window_manager_state(); |
| 272 Display* display2 = tree->GetDisplay(root2); | 376 Display* display2 = tree->GetDisplay(root2); |
| 273 WindowManagerState* display2_wms = | 377 WindowManagerState* display2_wms = |
| 274 display2->GetWindowManagerDisplayRootForUser(kTestId1) | 378 display2->GetWindowManagerDisplayRootForUser(kTestId1) |
| 275 ->window_manager_state(); | 379 ->window_manager_state(); |
| 276 EXPECT_EQ(display1_wms->window_tree(), display2_wms->window_tree()); | 380 EXPECT_EQ(display1_wms->window_tree(), display2_wms->window_tree()); |
| 277 } | 381 } |
| 278 | 382 |
| 279 namespace { | |
| 280 | |
| 281 // Returns the first non-primary display. | |
| 282 Display* GetSecondaryDisplay(DisplayManager* display_manager) { | |
| 283 for (Display* display : display_manager->displays()) { | |
| 284 if (!display->platform_display()->IsPrimaryDisplay()) | |
| 285 return display; | |
| 286 } | |
| 287 return nullptr; | |
| 288 } | |
| 289 | |
| 290 // Returns the root ServerWindow for the specified Display. | |
| 291 ServerWindow* GetRootOnDisplay(WindowTree* tree, Display* display) { | |
| 292 for (const ServerWindow* root : tree->roots()) { | |
| 293 if (tree->GetDisplay(root) == display) | |
| 294 return const_cast<ServerWindow*>(root); | |
| 295 } | |
| 296 return nullptr; | |
| 297 } | |
| 298 | |
| 299 // Tracks destruction of a ServerWindow, setting a bool* to true when | |
| 300 // OnWindowDestroyed() is called | |
| 301 class ServerWindowDestructionObserver : public ServerWindowObserver { | |
| 302 public: | |
| 303 ServerWindowDestructionObserver(ServerWindow* window, bool* destroyed) | |
| 304 : window_(window), destroyed_(destroyed) { | |
| 305 window_->AddObserver(this); | |
| 306 } | |
| 307 ~ServerWindowDestructionObserver() override { | |
| 308 if (window_) | |
| 309 window_->RemoveObserver(this); | |
| 310 } | |
| 311 | |
| 312 // ServerWindowObserver: | |
| 313 void OnWindowDestroyed(ServerWindow* window) override { | |
| 314 *destroyed_ = true; | |
| 315 window_->RemoveObserver(this); | |
| 316 window_ = nullptr; | |
| 317 } | |
| 318 | |
| 319 private: | |
| 320 ServerWindow* window_; | |
| 321 bool* destroyed_; | |
| 322 | |
| 323 DISALLOW_COPY_AND_ASSIGN(ServerWindowDestructionObserver); | |
| 324 }; | |
| 325 | |
| 326 } // namespace | |
| 327 | |
| 328 // Assertions around destroying a secondary display. | 383 // Assertions around destroying a secondary display. |
| 329 TEST_F(DisplayTest, DestroyingDisplayDoesntDelete) { | 384 TEST_F(DisplayTest, DestroyingDisplayDoesntDelete) { |
| 330 window_server_delegate()->CreateDisplays(2); | |
| 331 AddWindowManager(window_server(), kTestId1); | 385 AddWindowManager(window_server(), kTestId1); |
| 386 platform_screen().AddDisplay(); |
| 387 const int64_t secondary_display_id = platform_screen().AddDisplay(); |
| 332 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); | 388 window_server()->user_id_tracker()->SetActiveUserId(kTestId1); |
| 333 ASSERT_EQ(1u, window_server_delegate()->bindings()->size()); | 389 ASSERT_EQ(1u, window_server_delegate()->bindings()->size()); |
| 334 WindowTree* tree = (*window_server_delegate()->bindings())[0]->tree(); | 390 WindowTree* tree = (*window_server_delegate()->bindings())[0]->tree(); |
| 335 ASSERT_EQ(2u, tree->roots().size()); | 391 ASSERT_EQ(2u, tree->roots().size()); |
| 336 Display* secondary_display = | 392 Display* secondary_display = |
| 337 GetSecondaryDisplay(window_server()->display_manager()); | 393 display_manager()->GetDisplayById(secondary_display_id); |
| 338 ASSERT_TRUE(secondary_display); | 394 ASSERT_TRUE(secondary_display); |
| 339 bool secondary_root_destroyed = false; | 395 bool secondary_root_destroyed = false; |
| 340 ServerWindow* secondary_root = GetRootOnDisplay(tree, secondary_display); | 396 ServerWindow* secondary_root = GetRootOnDisplay(tree, secondary_display); |
| 341 ASSERT_TRUE(secondary_root); | 397 ASSERT_TRUE(secondary_root); |
| 342 ServerWindowDestructionObserver observer(secondary_root, | 398 ServerWindowDestructionObserver observer(secondary_root, |
| 343 &secondary_root_destroyed); | 399 &secondary_root_destroyed); |
| 344 ClientWindowId secondary_root_id = | 400 ClientWindowId secondary_root_id = |
| 345 ClientWindowIdForWindow(tree, secondary_root); | 401 ClientWindowIdForWindow(tree, secondary_root); |
| 346 const int64_t secondary_display_id = secondary_display->GetId(); | |
| 347 TestWindowTreeClient* tree_client = | 402 TestWindowTreeClient* tree_client = |
| 348 static_cast<TestWindowTreeClient*>(tree->client()); | 403 static_cast<TestWindowTreeClient*>(tree->client()); |
| 349 tree_client->tracker()->changes()->clear(); | 404 tree_client->tracker()->changes()->clear(); |
| 350 TestWindowManager* test_window_manager = | 405 TestWindowManager* test_window_manager = |
| 351 window_server_delegate()->last_binding()->window_manager(); | 406 window_server_delegate()->last_binding()->window_manager(); |
| 352 EXPECT_FALSE(test_window_manager->got_display_removed()); | 407 EXPECT_FALSE(test_window_manager->got_display_removed()); |
| 353 window_server()->display_manager()->DestroyDisplay(secondary_display); | 408 platform_screen().RemoveDisplay(secondary_display_id); |
| 354 | 409 |
| 355 // Destroying the display should result in the following: | 410 // Destroying the display should result in the following: |
| 356 // . The WindowManager should be told it was removed with the right id. | 411 // . The WindowManager should be told it was removed with the right id. |
| 357 EXPECT_TRUE(test_window_manager->got_display_removed()); | 412 EXPECT_TRUE(test_window_manager->got_display_removed()); |
| 358 EXPECT_EQ(secondary_display_id, test_window_manager->display_removed_id()); | 413 EXPECT_EQ(secondary_display_id, test_window_manager->display_removed_id()); |
| 359 EXPECT_FALSE(secondary_root_destroyed); | 414 EXPECT_FALSE(secondary_root_destroyed); |
| 360 // The window should still be valid on the server side. | 415 // The window should still be valid on the server side. |
| 361 ASSERT_TRUE(tree->GetWindowByClientId(secondary_root_id)); | 416 ASSERT_TRUE(tree->GetWindowByClientId(secondary_root_id)); |
| 362 // No changes. | 417 // No changes. |
| 363 ASSERT_EQ(0u, tree_client->tracker()->changes()->size()); | 418 ASSERT_EQ(0u, tree_client->tracker()->changes()->size()); |
| 364 | 419 |
| 365 // The window should be destroyed when the client says so. | 420 // The window should be destroyed when the client says so. |
| 366 ASSERT_TRUE(tree->DeleteWindow(secondary_root_id)); | 421 ASSERT_TRUE(tree->DeleteWindow(secondary_root_id)); |
| 367 EXPECT_TRUE(secondary_root_destroyed); | 422 EXPECT_TRUE(secondary_root_destroyed); |
| 368 } | 423 } |
| 369 | 424 |
| 370 } // namespace test | 425 } // namespace test |
| 371 } // namespace ws | 426 } // namespace ws |
| 372 } // namespace ui | 427 } // namespace ui |
| OLD | NEW |