| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> |
| 6 | 6 |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "ash/public/interfaces/user_window_controller.mojom.h" | |
| 11 #include "base/bind.h" | 7 #include "base/bind.h" |
| 12 #include "base/macros.h" | 8 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | |
| 14 #include "services/shell/public/cpp/service_test.h" | 9 #include "services/shell/public/cpp/service_test.h" |
| 15 #include "services/ui/public/cpp/window.h" | 10 #include "services/ui/public/cpp/window.h" |
| 16 #include "services/ui/public/cpp/window_tree_client.h" | 11 #include "services/ui/public/cpp/window_tree_client.h" |
| 17 #include "services/ui/public/cpp/window_tree_client_delegate.h" | 12 #include "services/ui/public/cpp/window_tree_client_delegate.h" |
| 18 #include "services/ui/public/interfaces/window_tree.mojom.h" | 13 #include "services/ui/public/interfaces/window_tree.mojom.h" |
| 19 | 14 |
| 20 namespace ash { | 15 namespace ash { |
| 21 namespace mus { | 16 namespace mus { |
| 22 | 17 |
| 23 class WindowTreeClientDelegate : public ui::WindowTreeClientDelegate { | 18 class WindowTreeClientDelegate : public ui::WindowTreeClientDelegate { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 41 ~WindowManagerTest() override {} | 36 ~WindowManagerTest() override {} |
| 42 | 37 |
| 43 private: | 38 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(WindowManagerTest); | 39 DISALLOW_COPY_AND_ASSIGN(WindowManagerTest); |
| 45 }; | 40 }; |
| 46 | 41 |
| 47 void OnEmbed(bool success) { | 42 void OnEmbed(bool success) { |
| 48 ASSERT_TRUE(success); | 43 ASSERT_TRUE(success); |
| 49 } | 44 } |
| 50 | 45 |
| 51 class TestUserWindowObserver : public mojom::UserWindowObserver { | |
| 52 public: | |
| 53 explicit TestUserWindowObserver(shell::Connector* connector) | |
| 54 : binding_(this), window_count_(0u), expected_window_count_(0u) { | |
| 55 connector->ConnectToInterface("mojo:ash", &user_window_controller_); | |
| 56 user_window_controller_->AddUserWindowObserver( | |
| 57 binding_.CreateInterfacePtrAndBind()); | |
| 58 } | |
| 59 | |
| 60 ~TestUserWindowObserver() override {} | |
| 61 | |
| 62 bool WaitUntilWindowCountReaches(size_t expected) { | |
| 63 DCHECK(quit_callback_.is_null()); | |
| 64 if (window_count_ != expected) { | |
| 65 base::RunLoop loop; | |
| 66 quit_callback_ = loop.QuitClosure(); | |
| 67 expected_window_count_ = expected; | |
| 68 loop.Run(); | |
| 69 quit_callback_ = base::Closure(); | |
| 70 } | |
| 71 return window_count_ == expected; | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 void QuitIfNecessary() { | |
| 76 if (window_count_ == expected_window_count_ && !quit_callback_.is_null()) | |
| 77 quit_callback_.Run(); | |
| 78 } | |
| 79 | |
| 80 // mojom::UserWindowObserver: | |
| 81 void OnUserWindowObserverAdded( | |
| 82 mojo::Array<mojom::UserWindowPtr> user_windows) override { | |
| 83 window_count_ = user_windows.size(); | |
| 84 QuitIfNecessary(); | |
| 85 } | |
| 86 | |
| 87 void OnUserWindowAdded(mojom::UserWindowPtr user_window) override { | |
| 88 ++window_count_; | |
| 89 QuitIfNecessary(); | |
| 90 } | |
| 91 | |
| 92 void OnUserWindowRemoved(uint32_t window_id) override { | |
| 93 ASSERT_TRUE(window_count_); | |
| 94 --window_count_; | |
| 95 QuitIfNecessary(); | |
| 96 } | |
| 97 | |
| 98 void OnUserWindowTitleChanged(uint32_t window_id, | |
| 99 const mojo::String& window_title) override {} | |
| 100 void OnUserWindowFocusChanged(uint32_t window_id, bool has_focus) override {} | |
| 101 void OnUserWindowAppIconChanged(uint32_t window_id, | |
| 102 mojo::Array<uint8_t> app_icon) override {} | |
| 103 | |
| 104 mojom::UserWindowControllerPtr user_window_controller_; | |
| 105 mojo::Binding<mojom::UserWindowObserver> binding_; | |
| 106 | |
| 107 size_t window_count_; | |
| 108 size_t expected_window_count_; | |
| 109 base::Closure quit_callback_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(TestUserWindowObserver); | |
| 112 }; | |
| 113 | |
| 114 TEST_F(WindowManagerTest, OpenWindow) { | 46 TEST_F(WindowManagerTest, OpenWindow) { |
| 115 WindowTreeClientDelegate window_tree_delegate; | 47 WindowTreeClientDelegate window_tree_delegate; |
| 116 | 48 |
| 117 connector()->Connect("mojo:ash"); | 49 connector()->Connect("mojo:ash"); |
| 118 | 50 |
| 119 // Connect to mus and create a new top level window. The request goes to | 51 // Connect to mus and create a new top level window. The request goes to |
| 120 // |ash|, but is async. | 52 // |ash|, but is async. |
| 121 std::unique_ptr<ui::WindowTreeClient> client( | 53 std::unique_ptr<ui::WindowTreeClient> client( |
| 122 new ui::WindowTreeClient(&window_tree_delegate, nullptr, nullptr)); | 54 new ui::WindowTreeClient(&window_tree_delegate, nullptr, nullptr)); |
| 123 client->ConnectViaWindowTreeFactory(connector()); | 55 client->ConnectViaWindowTreeFactory(connector()); |
| 124 ui::Window* top_level_window = client->NewTopLevelWindow(nullptr); | 56 ui::Window* top_level_window = client->NewTopLevelWindow(nullptr); |
| 125 ASSERT_TRUE(top_level_window); | 57 ASSERT_TRUE(top_level_window); |
| 126 ui::Window* child_window = client->NewWindow(); | 58 ui::Window* child_window = client->NewWindow(); |
| 127 ASSERT_TRUE(child_window); | 59 ASSERT_TRUE(child_window); |
| 128 top_level_window->AddChild(child_window); | 60 top_level_window->AddChild(child_window); |
| 129 | 61 |
| 130 // Create another WindowTreeClient by way of embedding in | 62 // Create another WindowTreeClient by way of embedding in |
| 131 // |child_window|. This blocks until it succeeds. | 63 // |child_window|. This blocks until it succeeds. |
| 132 ui::mojom::WindowTreeClientPtr tree_client; | 64 ui::mojom::WindowTreeClientPtr tree_client; |
| 133 auto tree_client_request = GetProxy(&tree_client); | 65 auto tree_client_request = GetProxy(&tree_client); |
| 134 child_window->Embed(std::move(tree_client), base::Bind(&OnEmbed)); | 66 child_window->Embed(std::move(tree_client), base::Bind(&OnEmbed)); |
| 135 std::unique_ptr<ui::WindowTreeClient> child_client(new ui::WindowTreeClient( | 67 std::unique_ptr<ui::WindowTreeClient> child_client(new ui::WindowTreeClient( |
| 136 &window_tree_delegate, nullptr, std::move(tree_client_request))); | 68 &window_tree_delegate, nullptr, std::move(tree_client_request))); |
| 137 child_client->WaitForEmbed(); | 69 child_client->WaitForEmbed(); |
| 138 ASSERT_TRUE(!child_client->GetRoots().empty()); | 70 ASSERT_TRUE(!child_client->GetRoots().empty()); |
| 139 } | 71 } |
| 140 | 72 |
| 141 TEST_F(WindowManagerTest, OpenWindowAndClose) { | |
| 142 connector()->Connect("mojo:ash"); | |
| 143 | |
| 144 TestUserWindowObserver observer(connector()); | |
| 145 | |
| 146 // Connect to mus and create a new top level window. | |
| 147 WindowTreeClientDelegate window_tree_delegate; | |
| 148 std::unique_ptr<ui::WindowTreeClient> client( | |
| 149 new ui::WindowTreeClient(&window_tree_delegate, nullptr, nullptr)); | |
| 150 client->ConnectViaWindowTreeFactory(connector()); | |
| 151 ui::Window* top_level_window = client->NewTopLevelWindow(nullptr); | |
| 152 ASSERT_TRUE(top_level_window); | |
| 153 | |
| 154 observer.WaitUntilWindowCountReaches(1u); | |
| 155 client.reset(); | |
| 156 observer.WaitUntilWindowCountReaches(0u); | |
| 157 } | |
| 158 | |
| 159 } // namespace mus | 73 } // namespace mus |
| 160 } // namespace ash | 74 } // namespace ash |
| OLD | NEW |