OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "mojo/common/common_type_converters.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
10 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 10 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
11 #include "mojo/services/public/cpp/view_manager/types.h" | 11 #include "mojo/services/public/cpp/view_manager/types.h" |
12 #include "mojo/services/public/cpp/view_manager/util.h" | 12 #include "mojo/services/public/cpp/view_manager/util.h" |
13 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | 13 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" |
14 #include "mojo/services/public/interfaces/window_manager/window_manager.mojom.h" | 14 #include "mojo/services/public/interfaces/window_manager/window_manager.mojom.h" |
15 #include "mojo/services/public/interfaces/window_manager/window_manager_internal
.mojom.h" | 15 #include "mojo/services/public/interfaces/window_manager/window_manager_internal
.mojom.h" |
16 #include "mojo/services/view_manager/client_connection.h" | 16 #include "mojo/services/view_manager/client_connection.h" |
17 #include "mojo/services/view_manager/connection_manager.h" | 17 #include "mojo/services/view_manager/connection_manager.h" |
18 #include "mojo/services/view_manager/connection_manager_delegate.h" | 18 #include "mojo/services/view_manager/connection_manager_delegate.h" |
19 #include "mojo/services/view_manager/display_manager.h" | 19 #include "mojo/services/view_manager/display_manager.h" |
20 #include "mojo/services/view_manager/ids.h" | 20 #include "mojo/services/view_manager/ids.h" |
| 21 #include "mojo/services/view_manager/server_view.h" |
21 #include "mojo/services/view_manager/test_change_tracker.h" | 22 #include "mojo/services/view_manager/test_change_tracker.h" |
22 #include "mojo/services/view_manager/view_manager_service_impl.h" | 23 #include "mojo/services/view_manager/view_manager_service_impl.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
25 | 26 |
26 namespace mojo { | 27 namespace mojo { |
27 namespace service { | 28 namespace service { |
28 namespace { | 29 namespace { |
29 | 30 |
30 // ----------------------------------------------------------------------------- | 31 // ----------------------------------------------------------------------------- |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 TestViewManagerClient client_; | 107 TestViewManagerClient client_; |
107 | 108 |
108 DISALLOW_COPY_AND_ASSIGN(TestClientConnection); | 109 DISALLOW_COPY_AND_ASSIGN(TestClientConnection); |
109 }; | 110 }; |
110 | 111 |
111 // ----------------------------------------------------------------------------- | 112 // ----------------------------------------------------------------------------- |
112 | 113 |
113 // Empty implementation of ConnectionManagerDelegate. | 114 // Empty implementation of ConnectionManagerDelegate. |
114 class TestConnectionManagerDelegate : public ConnectionManagerDelegate { | 115 class TestConnectionManagerDelegate : public ConnectionManagerDelegate { |
115 public: | 116 public: |
116 TestConnectionManagerDelegate() : last_client_(nullptr) {} | 117 TestConnectionManagerDelegate() : last_connection_(nullptr) {} |
117 ~TestConnectionManagerDelegate() override {} | 118 ~TestConnectionManagerDelegate() override {} |
118 | 119 |
119 // TestViewManagerClient that was created by the last | 120 TestViewManagerClient* last_client() { |
120 // CreateClientConnectionForEmbedAtView() call. | 121 return last_connection_ ? last_connection_->client() : nullptr; |
121 TestViewManagerClient* last_client() { return last_client_; } | 122 } |
| 123 |
| 124 TestClientConnection* last_connection() { return last_connection_; } |
122 | 125 |
123 private: | 126 private: |
124 // ConnectionManagerDelegate: | 127 // ConnectionManagerDelegate: |
125 void OnLostConnectionToWindowManager() override {} | 128 void OnLostConnectionToWindowManager() override {} |
126 | 129 |
127 ClientConnection* CreateClientConnectionForEmbedAtView( | 130 ClientConnection* CreateClientConnectionForEmbedAtView( |
128 ConnectionManager* connection_manager, | 131 ConnectionManager* connection_manager, |
129 ConnectionSpecificId creator_id, | 132 ConnectionSpecificId creator_id, |
130 const std::string& creator_url, | 133 const std::string& creator_url, |
131 const std::string& url, | 134 const std::string& url, |
132 const ViewId& root_id) override { | 135 const ViewId& root_id) override { |
133 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( | 136 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( |
134 connection_manager, creator_id, creator_url, url, root_id)); | 137 connection_manager, creator_id, creator_url, url, root_id)); |
135 TestClientConnection* connection = new TestClientConnection(service.Pass()); | 138 last_connection_ = new TestClientConnection(service.Pass()); |
136 last_client_ = connection->client(); | 139 return last_connection_; |
137 return connection; | |
138 } | 140 } |
139 | 141 |
140 TestViewManagerClient* last_client_; | 142 TestClientConnection* last_connection_; |
141 | 143 |
142 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate); | 144 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate); |
143 }; | 145 }; |
144 | 146 |
145 // ----------------------------------------------------------------------------- | 147 // ----------------------------------------------------------------------------- |
146 | 148 |
147 // Empty implementation of DisplayManager. | 149 // Empty implementation of DisplayManager. |
148 class TestDisplayManager : public DisplayManager { | 150 class TestDisplayManager : public DisplayManager { |
149 public: | 151 public: |
150 TestDisplayManager() {} | 152 TestDisplayManager() {} |
(...skipping 28 matching lines...) Expand all Loading... |
179 | 181 |
180 } // namespace | 182 } // namespace |
181 | 183 |
182 // ----------------------------------------------------------------------------- | 184 // ----------------------------------------------------------------------------- |
183 | 185 |
184 class ViewManagerServiceTest : public testing::Test { | 186 class ViewManagerServiceTest : public testing::Test { |
185 public: | 187 public: |
186 ViewManagerServiceTest() : wm_client_(nullptr) {} | 188 ViewManagerServiceTest() : wm_client_(nullptr) {} |
187 ~ViewManagerServiceTest() override {} | 189 ~ViewManagerServiceTest() override {} |
188 | 190 |
| 191 // ViewManagerServiceImpl for the window manager. |
| 192 ViewManagerServiceImpl* wm_connection() { |
| 193 return connection_manager_->GetConnection(1); |
| 194 } |
| 195 |
| 196 TestViewManagerClient* last_view_manager_client() { |
| 197 return delegate_.last_client(); |
| 198 } |
| 199 |
| 200 TestClientConnection* last_client_connection() { |
| 201 return delegate_.last_connection(); |
| 202 } |
| 203 |
| 204 ConnectionManager* connection_manager() { return connection_manager_.get(); } |
| 205 |
| 206 TestViewManagerClient* wm_client() { return wm_client_; } |
| 207 |
189 protected: | 208 protected: |
190 // testing::Test: | 209 // testing::Test: |
191 void SetUp() override { | 210 void SetUp() override { |
192 connection_manager_.reset(new ConnectionManager( | 211 connection_manager_.reset(new ConnectionManager( |
193 &delegate_, scoped_ptr<DisplayManager>(new TestDisplayManager), | 212 &delegate_, scoped_ptr<DisplayManager>(new TestDisplayManager), |
194 &wm_internal_)); | 213 &wm_internal_)); |
195 | |
196 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( | 214 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( |
197 connection_manager_.get(), kInvalidConnectionId, std::string(), | 215 connection_manager_.get(), kInvalidConnectionId, std::string(), |
198 std::string("mojo:window_manager"), RootViewId())); | 216 std::string("mojo:window_manager"), RootViewId())); |
199 scoped_ptr<TestClientConnection> client_connection( | 217 scoped_ptr<TestClientConnection> client_connection( |
200 new TestClientConnection(service.Pass())); | 218 new TestClientConnection(service.Pass())); |
201 wm_client_ = client_connection->client(); | 219 wm_client_ = client_connection->client(); |
| 220 ASSERT_TRUE(wm_client_ != nullptr); |
202 connection_manager_->SetWindowManagerClientConnection( | 221 connection_manager_->SetWindowManagerClientConnection( |
203 client_connection.Pass()); | 222 client_connection.Pass()); |
204 } | 223 ASSERT_TRUE(wm_connection() != nullptr); |
205 | 224 ASSERT_TRUE(wm_connection()->root() != nullptr); |
206 // ViewManagerServiceImpl for the window manager. | 225 } |
207 ViewManagerServiceImpl* wm_connection() { | 226 |
208 return connection_manager_->GetConnection(1); | 227 private: |
209 } | |
210 | |
211 // TestViewManagerClient that is used for the WM connection. | 228 // TestViewManagerClient that is used for the WM connection. |
212 TestViewManagerClient* wm_client_; | 229 TestViewManagerClient* wm_client_; |
213 | 230 |
214 private: | |
215 TestWindowManagerInternal wm_internal_; | 231 TestWindowManagerInternal wm_internal_; |
216 TestConnectionManagerDelegate delegate_; | 232 TestConnectionManagerDelegate delegate_; |
217 scoped_ptr<ConnectionManager> connection_manager_; | 233 scoped_ptr<ConnectionManager> connection_manager_; |
| 234 base::MessageLoop message_loop_; |
218 | 235 |
219 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest); | 236 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest); |
220 }; | 237 }; |
221 | 238 |
222 TEST_F(ViewManagerServiceTest, Basic) { | 239 namespace { |
223 wm_connection()->CreateView(ViewId(wm_connection()->id(), 1)); | 240 |
224 ASSERT_EQ(1u, wm_client_->tracker()->changes()->size()); | 241 const ServerView* GetFirstCloned(const ServerView* view) { |
| 242 for (const ServerView* child : view->GetChildren()) { |
| 243 if (child->id() == ClonedViewId()) |
| 244 return child; |
| 245 } |
| 246 return nullptr; |
| 247 } |
| 248 |
| 249 // Provides common setup for animation tests. Creates the following views: |
| 250 // 0,1 (the root, provided by view manager) |
| 251 // 1,1 the second connection is embedded here (view owned by wm_connection()). |
| 252 // 2,1 bounds=1,2 11x22 |
| 253 // 2,2 bounds=2,3 6x7 |
| 254 // 2,3 bounds=3,4 6x7 |
| 255 // CloneAndAnimate() is invoked for 2,2. |
| 256 void SetUpAnimate1(ViewManagerServiceTest* test, ViewId* embed_view_id) { |
| 257 *embed_view_id = ViewId(test->wm_connection()->id(), 1); |
| 258 EXPECT_EQ(ERROR_CODE_NONE, test->wm_connection()->CreateView(*embed_view_id)); |
| 259 EXPECT_TRUE(test->wm_connection()->AddView(*(test->wm_connection()->root()), |
| 260 *embed_view_id)); |
| 261 EXPECT_TRUE(test->wm_connection()->Embed( |
| 262 std::string(), *embed_view_id, InterfaceRequest<ServiceProvider>())); |
| 263 ViewManagerServiceImpl* connection1 = |
| 264 test->connection_manager()->GetConnectionWithRoot(*embed_view_id); |
| 265 ASSERT_TRUE(connection1 != nullptr); |
| 266 ASSERT_NE(connection1, test->wm_connection()); |
| 267 |
| 268 const ViewId child1(connection1->id(), 1); |
| 269 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1)); |
| 270 const ViewId child2(connection1->id(), 2); |
| 271 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2)); |
| 272 const ViewId child3(connection1->id(), 3); |
| 273 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3)); |
| 274 |
| 275 ServerView* v1 = connection1->GetView(child1); |
| 276 v1->SetBounds(gfx::Rect(1, 2, 11, 22)); |
| 277 ServerView* v2 = connection1->GetView(child2); |
| 278 v2->SetBounds(gfx::Rect(2, 3, 6, 7)); |
| 279 ServerView* v3 = connection1->GetView(child3); |
| 280 v3->SetBounds(gfx::Rect(3, 4, 6, 7)); |
| 281 |
| 282 EXPECT_TRUE(connection1->AddView(*embed_view_id, child1)); |
| 283 EXPECT_TRUE(connection1->AddView(child1, child2)); |
| 284 EXPECT_TRUE(connection1->AddView(child2, child3)); |
| 285 |
| 286 TestViewManagerClient* connection1_client = test->last_view_manager_client(); |
| 287 connection1_client->tracker()->changes()->clear(); |
| 288 test->wm_client()->tracker()->changes()->clear(); |
| 289 EXPECT_TRUE(test->connection_manager()->CloneAndAnimate(child2)); |
| 290 EXPECT_TRUE(connection1_client->tracker()->changes()->empty()); |
| 291 EXPECT_TRUE(test->wm_client()->tracker()->changes()->empty()); |
| 292 |
| 293 // We cloned v2. The cloned view ends up as a sibling of it. |
| 294 const ServerView* cloned_view = GetFirstCloned(connection1->GetView(child1)); |
| 295 ASSERT_TRUE(cloned_view); |
| 296 // |cloned_view| should have one and only one cloned child (corresponds to |
| 297 // |child3|). |
| 298 ASSERT_EQ(1u, cloned_view->GetChildren().size()); |
| 299 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId()); |
| 300 |
| 301 // Cloned views should match the bounds of the view they were cloned from. |
| 302 EXPECT_EQ(v2->bounds(), cloned_view->bounds()); |
| 303 EXPECT_EQ(v3->bounds(), cloned_view->GetChildren()[0]->bounds()); |
| 304 |
| 305 // Cloned views are owned by the ConnectionManager and shouldn't be returned |
| 306 // from ViewManagerServiceImpl::GetView. |
| 307 EXPECT_TRUE(connection1->GetView(ClonedViewId()) == nullptr); |
| 308 EXPECT_TRUE(test->wm_connection()->GetView(ClonedViewId()) == nullptr); |
| 309 } |
| 310 |
| 311 } // namespace |
| 312 |
| 313 // Verifies ViewManagerService::GetViewTree() doesn't return cloned views. |
| 314 TEST_F(ViewManagerServiceTest, ConnectionsCantSeeClonedViews) { |
| 315 ViewId embed_view_id; |
| 316 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id)); |
| 317 |
| 318 ViewManagerServiceImpl* connection1 = |
| 319 connection_manager()->GetConnectionWithRoot(embed_view_id); |
| 320 |
| 321 const ViewId child1(connection1->id(), 1); |
| 322 const ViewId child2(connection1->id(), 2); |
| 323 const ViewId child3(connection1->id(), 3); |
| 324 |
| 325 // Verify the root doesn't see any cloned views. |
| 326 std::vector<const ServerView*> views( |
| 327 wm_connection()->GetViewTree(*wm_connection()->root())); |
| 328 ASSERT_EQ(5u, views.size()); |
| 329 ASSERT_TRUE(views[0]->id() == *wm_connection()->root()); |
| 330 ASSERT_TRUE(views[1]->id() == embed_view_id); |
| 331 ASSERT_TRUE(views[2]->id() == child1); |
| 332 ASSERT_TRUE(views[3]->id() == child2); |
| 333 ASSERT_TRUE(views[4]->id() == child3); |
| 334 |
| 335 // Verify connection1 doesn't see any cloned views. |
| 336 std::vector<const ServerView*> v1_views( |
| 337 connection1->GetViewTree(embed_view_id)); |
| 338 ASSERT_EQ(4u, v1_views.size()); |
| 339 ASSERT_TRUE(v1_views[0]->id() == embed_view_id); |
| 340 ASSERT_TRUE(v1_views[1]->id() == child1); |
| 341 ASSERT_TRUE(v1_views[2]->id() == child2); |
| 342 ASSERT_TRUE(v1_views[3]->id() == child3); |
| 343 } |
| 344 |
| 345 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnConnectionClose) { |
| 346 ViewId embed_view_id; |
| 347 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id)); |
| 348 |
| 349 // Destroy connection1, which should force the cloned view to become a child |
| 350 // of where it was embedded (the embedded view still exists). |
| 351 connection_manager()->OnConnectionError(last_client_connection()); |
| 352 |
| 353 ServerView* embed_view = wm_connection()->GetView(embed_view_id); |
| 354 ASSERT_TRUE(embed_view != nullptr); |
| 355 const ServerView* cloned_view = GetFirstCloned(embed_view); |
| 356 ASSERT_TRUE(cloned_view); |
| 357 ASSERT_EQ(1u, cloned_view->GetChildren().size()); |
| 358 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId()); |
| 359 |
| 360 // Because the cloned view changed parents its bounds should have changed. |
| 361 EXPECT_EQ(gfx::Rect(3, 5, 6, 7), cloned_view->bounds()); |
| 362 // The bounds of the cloned child should not have changed though. |
| 363 EXPECT_EQ(gfx::Rect(3, 4, 6, 7), cloned_view->GetChildren()[0]->bounds()); |
| 364 } |
| 365 |
| 366 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnHide) { |
| 367 ViewId embed_view_id; |
| 368 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id)); |
| 369 |
| 370 ViewManagerServiceImpl* connection1 = |
| 371 connection_manager()->GetConnectionWithRoot(embed_view_id); |
| 372 |
| 373 // Hide the parent of the cloned view, which should force the cloned view to |
| 374 // become a sibling of the parent. |
| 375 const ServerView* view_to_hide = |
| 376 connection1->GetView(ViewId(connection1->id(), 1)); |
| 377 ASSERT_TRUE(connection1->SetViewVisibility(view_to_hide->id(), false)); |
| 378 |
| 379 const ServerView* cloned_view = GetFirstCloned(view_to_hide->parent()); |
| 380 ASSERT_TRUE(cloned_view); |
| 381 ASSERT_EQ(1u, cloned_view->GetChildren().size()); |
| 382 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId()); |
| 383 EXPECT_EQ(2u, cloned_view->parent()->GetChildren().size()); |
| 384 EXPECT_TRUE(cloned_view->parent()->GetChildren()[1] == cloned_view); |
| 385 } |
| 386 |
| 387 // Clone and animate on a tree with more depth. Basically that of |
| 388 // SetUpAnimate1() but cloning 2,1. |
| 389 TEST_F(ViewManagerServiceTest, CloneAndAnimateLargerDepth) { |
| 390 const ViewId embed_view_id(wm_connection()->id(), 1); |
| 391 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id)); |
| 392 EXPECT_TRUE( |
| 393 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id)); |
| 394 EXPECT_TRUE(wm_connection()->Embed(std::string(), embed_view_id, |
| 395 InterfaceRequest<ServiceProvider>())); |
| 396 ViewManagerServiceImpl* connection1 = |
| 397 connection_manager()->GetConnectionWithRoot(embed_view_id); |
| 398 ASSERT_TRUE(connection1 != nullptr); |
| 399 ASSERT_NE(connection1, wm_connection()); |
| 400 |
| 401 const ViewId child1(connection1->id(), 1); |
| 402 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1)); |
| 403 const ViewId child2(connection1->id(), 2); |
| 404 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2)); |
| 405 const ViewId child3(connection1->id(), 3); |
| 406 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3)); |
| 407 |
| 408 ServerView* v1 = connection1->GetView(child1); |
| 409 |
| 410 EXPECT_TRUE(connection1->AddView(embed_view_id, child1)); |
| 411 EXPECT_TRUE(connection1->AddView(child1, child2)); |
| 412 EXPECT_TRUE(connection1->AddView(child2, child3)); |
| 413 |
| 414 TestViewManagerClient* connection1_client = last_view_manager_client(); |
| 415 connection1_client->tracker()->changes()->clear(); |
| 416 wm_client()->tracker()->changes()->clear(); |
| 417 EXPECT_TRUE(connection_manager()->CloneAndAnimate(child1)); |
| 418 EXPECT_TRUE(connection1_client->tracker()->changes()->empty()); |
| 419 EXPECT_TRUE(wm_client()->tracker()->changes()->empty()); |
| 420 |
| 421 // We cloned v1. The cloned view ends up as a sibling of it. |
| 422 const ServerView* cloned_view = GetFirstCloned(v1->parent()); |
| 423 ASSERT_TRUE(cloned_view); |
| 424 // |cloned_view| should have a child and its child should have a child. |
| 425 ASSERT_EQ(1u, cloned_view->GetChildren().size()); |
| 426 const ServerView* cloned_view_child = cloned_view->GetChildren()[0]; |
| 427 EXPECT_EQ(1u, cloned_view_child->GetChildren().size()); |
| 428 EXPECT_TRUE(cloned_view_child->id() == ClonedViewId()); |
225 } | 429 } |
226 | 430 |
227 } // namespace service | 431 } // namespace service |
228 } // namespace mojo | 432 } // namespace mojo |
OLD | NEW |