| 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 "mojo/services/view_manager/view_manager_connection.h" | 5 #include "mojo/services/view_manager/view_manager_connection.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 void BooleanCallback(bool* result_cache, bool result) { | 46 void BooleanCallback(bool* result_cache, bool result) { |
| 47 *result_cache = result; | 47 *result_cache = result; |
| 48 current_run_loop->Quit(); | 48 current_run_loop->Quit(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Creates an id used for transport from the specified parameters. | 51 // Creates an id used for transport from the specified parameters. |
| 52 uint32_t CreateNodeId(uint16_t connection_id, uint16_t node_id) { | 52 uint32_t CreateNodeId(uint16_t connection_id, uint16_t node_id) { |
| 53 return NodeIdToTransportId(NodeId(connection_id, node_id)); | 53 return NodeIdToTransportId(NodeId(connection_id, node_id)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Creates an id used for transport from the specified parameters. |
| 57 uint32_t CreateViewId(uint16_t connection_id, uint16_t view_id) { |
| 58 return ViewIdToTransportId(ViewId(connection_id, view_id)); |
| 59 } |
| 60 |
| 56 // Creates a node with the specified id. Returns true on success. Blocks until | 61 // Creates a node with the specified id. Returns true on success. Blocks until |
| 57 // we get back result from server. | 62 // we get back result from server. |
| 58 bool CreateNode(ViewManager* view_manager, uint16_t id) { | 63 bool CreateNode(ViewManager* view_manager, uint16_t id) { |
| 59 bool result = false; | 64 bool result = false; |
| 60 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 65 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
| 61 DoRunLoop(); | 66 DoRunLoop(); |
| 62 return result; | 67 return result; |
| 63 } | 68 } |
| 64 | 69 |
| 70 // TODO(sky): make a macro for these functions, they are all the same. |
| 71 |
| 72 // Deletes a node, blocking until done. |
| 73 bool DeleteNode(ViewManager* view_manager, |
| 74 uint32_t node_id, |
| 75 int32_t change_id) { |
| 76 bool result = false; |
| 77 view_manager->DeleteNode(node_id, change_id, |
| 78 base::Bind(&BooleanCallback, &result)); |
| 79 DoRunLoop(); |
| 80 return result; |
| 81 } |
| 82 |
| 65 // Adds a node, blocking until done. | 83 // Adds a node, blocking until done. |
| 66 bool AddNode(ViewManager* view_manager, | 84 bool AddNode(ViewManager* view_manager, |
| 67 uint32_t parent, | 85 uint32_t parent, |
| 68 uint32_t child, | 86 uint32_t child, |
| 69 int32_t change_id) { | 87 int32_t change_id) { |
| 70 bool result = false; | 88 bool result = false; |
| 71 view_manager->AddNode(parent, child, change_id, | 89 view_manager->AddNode(parent, child, change_id, |
| 72 base::Bind(&BooleanCallback, &result)); | 90 base::Bind(&BooleanCallback, &result)); |
| 73 DoRunLoop(); | 91 DoRunLoop(); |
| 74 return result; | 92 return result; |
| 75 } | 93 } |
| 76 | 94 |
| 77 // Removes a node, blocking until done. | 95 // Removes a node, blocking until done. |
| 78 bool RemoveNodeFromParent(ViewManager* view_manager, | 96 bool RemoveNodeFromParent(ViewManager* view_manager, |
| 79 uint32_t node_id, | 97 uint32_t node_id, |
| 80 int32_t change_id) { | 98 int32_t change_id) { |
| 81 bool result = false; | 99 bool result = false; |
| 82 view_manager->RemoveNodeFromParent(node_id, change_id, | 100 view_manager->RemoveNodeFromParent(node_id, change_id, |
| 83 base::Bind(&BooleanCallback, &result)); | 101 base::Bind(&BooleanCallback, &result)); |
| 84 DoRunLoop(); | 102 DoRunLoop(); |
| 85 return result; | 103 return result; |
| 86 } | 104 } |
| 87 | 105 |
| 106 // Creates a view with the specified id. Returns true on success. Blocks until |
| 107 // we get back result from server. |
| 108 bool CreateView(ViewManager* view_manager, uint16_t id) { |
| 109 bool result = false; |
| 110 view_manager->CreateView(id, base::Bind(&BooleanCallback, &result)); |
| 111 DoRunLoop(); |
| 112 return result; |
| 113 } |
| 114 |
| 115 // Sets a view on the specified node. Returns true on success. Blocks until we |
| 116 // get back result from server. |
| 117 bool SetView(ViewManager* view_manager, |
| 118 uint32_t node_id, |
| 119 uint32_t view_id, |
| 120 int32_t change_id) { |
| 121 bool result = false; |
| 122 view_manager->SetView(node_id, view_id, change_id, |
| 123 base::Bind(&BooleanCallback, &result)); |
| 124 DoRunLoop(); |
| 125 return result; |
| 126 } |
| 127 |
| 88 } // namespace | 128 } // namespace |
| 89 | 129 |
| 90 typedef std::vector<std::string> Changes; | 130 typedef std::vector<std::string> Changes; |
| 91 | 131 |
| 92 class ViewManagerClientImpl : public ViewManagerClient { | 132 class ViewManagerClientImpl : public ViewManagerClient { |
| 93 public: | 133 public: |
| 94 ViewManagerClientImpl() : id_(0), quit_count_(0) {} | 134 ViewManagerClientImpl() : id_(0), quit_count_(0) {} |
| 95 | 135 |
| 96 void set_quit_count(int count) { quit_count_ = count; } | 136 void set_quit_count(int count) { quit_count_ = count; } |
| 97 | 137 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 112 virtual void OnNodeHierarchyChanged(uint32_t node, | 152 virtual void OnNodeHierarchyChanged(uint32_t node, |
| 113 uint32_t new_parent, | 153 uint32_t new_parent, |
| 114 uint32_t old_parent, | 154 uint32_t old_parent, |
| 115 int32_t change_id) OVERRIDE { | 155 int32_t change_id) OVERRIDE { |
| 116 changes_.push_back( | 156 changes_.push_back( |
| 117 base::StringPrintf( | 157 base::StringPrintf( |
| 118 "change_id=%d node=%s new_parent=%s old_parent=%s", | 158 "change_id=%d node=%s new_parent=%s old_parent=%s", |
| 119 change_id, NodeIdToString(node).c_str(), | 159 change_id, NodeIdToString(node).c_str(), |
| 120 NodeIdToString(new_parent).c_str(), | 160 NodeIdToString(new_parent).c_str(), |
| 121 NodeIdToString(old_parent).c_str())); | 161 NodeIdToString(old_parent).c_str())); |
| 122 if (quit_count_ > 0) { | 162 QuitIfNecessary(); |
| 123 if (--quit_count_ == 0) | 163 } |
| 124 current_run_loop->Quit(); | 164 virtual void OnNodeViewReplaced(uint32_t node, |
| 125 } | 165 uint32_t new_view_id, |
| 166 uint32_t old_view_id, |
| 167 int32_t change_id) OVERRIDE { |
| 168 changes_.push_back( |
| 169 base::StringPrintf( |
| 170 "change_id=%d node=%s new_view=%s old_view=%s", |
| 171 change_id, NodeIdToString(node).c_str(), |
| 172 NodeIdToString(new_view_id).c_str(), |
| 173 NodeIdToString(old_view_id).c_str())); |
| 174 QuitIfNecessary(); |
| 175 } |
| 176 |
| 177 void QuitIfNecessary() { |
| 178 if (quit_count_ > 0 && --quit_count_ == 0) |
| 179 current_run_loop->Quit(); |
| 126 } | 180 } |
| 127 | 181 |
| 128 uint16_t id_; | 182 uint16_t id_; |
| 129 | 183 |
| 130 // Used to determine when/if to quit the run loop. | 184 // Used to determine when/if to quit the run loop. |
| 131 int quit_count_; | 185 int quit_count_; |
| 132 | 186 |
| 133 Changes changes_; | 187 Changes changes_; |
| 134 | 188 |
| 135 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); | 189 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 connection2_.reset(new ViewManagerConnection); | 209 connection2_.reset(new ViewManagerConnection); |
| 156 InterfacePipe<ViewManagerClient, ViewManager> pipe; | 210 InterfacePipe<ViewManagerClient, ViewManager> pipe; |
| 157 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); | 211 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); |
| 158 connection2_->Initialize( | 212 connection2_->Initialize( |
| 159 &service_factory_, | 213 &service_factory_, |
| 160 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); | 214 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); |
| 161 // Wait for the id. | 215 // Wait for the id. |
| 162 DoRunLoop(); | 216 DoRunLoop(); |
| 163 } | 217 } |
| 164 | 218 |
| 219 void DestroySecondConnection() { |
| 220 connection2_.reset(); |
| 221 view_manager2_.reset(); |
| 222 } |
| 223 |
| 165 Environment env_; | 224 Environment env_; |
| 166 base::MessageLoop loop_; | 225 base::MessageLoop loop_; |
| 167 RootNodeManager root_node_manager_; | 226 RootNodeManager root_node_manager_; |
| 168 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; | 227 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; |
| 169 ViewManagerConnection connection_; | 228 ViewManagerConnection connection_; |
| 170 ViewManagerClientImpl client_; | 229 ViewManagerClientImpl client_; |
| 171 RemotePtr<ViewManager> view_manager_; | 230 RemotePtr<ViewManager> view_manager_; |
| 172 | 231 |
| 173 ViewManagerClientImpl client2_; | 232 ViewManagerClientImpl client2_; |
| 174 RemotePtr<ViewManager> view_manager2_; | 233 RemotePtr<ViewManager> view_manager2_; |
| 175 scoped_ptr<ViewManagerConnection> connection2_; | 234 scoped_ptr<ViewManagerConnection> connection2_; |
| 176 | 235 |
| 177 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); | 236 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); |
| 178 }; | 237 }; |
| 179 | 238 |
| 180 // Verifies client gets a valid id. | 239 // Verifies client gets a valid id. |
| 181 TEST_F(ViewManagerConnectionTest, ValidId) { | 240 TEST_F(ViewManagerConnectionTest, ValidId) { |
| 182 EXPECT_NE(0, client_.id()); | 241 // All these tests assume 1 for the client id. The only real assertion here is |
| 242 // the client id is not zero, but adding this as rest of code here assumes 1. |
| 243 EXPECT_EQ(1, client_.id()); |
| 183 } | 244 } |
| 184 | 245 |
| 185 // Verifies two clients/connections get different ids. | 246 // Verifies two clients/connections get different ids. |
| 186 TEST_F(ViewManagerConnectionTest, TwoClientsGetDifferentConnectionIds) { | 247 TEST_F(ViewManagerConnectionTest, TwoClientsGetDifferentConnectionIds) { |
| 187 EstablishSecondConnection(); | 248 EstablishSecondConnection(); |
| 188 EXPECT_NE(0, client2_.id()); | 249 EXPECT_NE(0, client2_.id()); |
| 189 EXPECT_NE(client_.id(), client2_.id()); | 250 EXPECT_NE(client_.id(), client2_.id()); |
| 190 } | 251 } |
| 191 | 252 |
| 192 // Verifies client gets a valid id. | 253 // Verifies client gets a valid id. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 ASSERT_EQ(1u, changes.size()); | 326 ASSERT_EQ(1u, changes.size()); |
| 266 EXPECT_EQ("change_id=0 node=1,2 new_parent=1,1 old_parent=null", | 327 EXPECT_EQ("change_id=0 node=1,2 new_parent=1,1 old_parent=null", |
| 267 changes[0]); | 328 changes[0]); |
| 268 } | 329 } |
| 269 } | 330 } |
| 270 | 331 |
| 271 // Verifies adding to root sends right notifications. | 332 // Verifies adding to root sends right notifications. |
| 272 TEST_F(ViewManagerConnectionTest, AddToRoot) { | 333 TEST_F(ViewManagerConnectionTest, AddToRoot) { |
| 273 ASSERT_TRUE(CreateNode(view_manager_.get(), 21)); | 334 ASSERT_TRUE(CreateNode(view_manager_.get(), 21)); |
| 274 ASSERT_TRUE(CreateNode(view_manager_.get(), 3)); | 335 ASSERT_TRUE(CreateNode(view_manager_.get(), 3)); |
| 275 | |
| 276 EXPECT_TRUE(client_.GetAndClearChanges().empty()); | 336 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
| 277 | 337 |
| 278 // Make 3 a child of 21. | 338 // Make 3 a child of 21. |
| 279 { | 339 { |
| 280 AllocationScope scope; | 340 AllocationScope scope; |
| 281 ASSERT_TRUE(AddNode(view_manager_.get(), | 341 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 282 CreateNodeId(client_.id(), 21), | 342 CreateNodeId(client_.id(), 21), |
| 283 CreateNodeId(client_.id(), 3), | 343 CreateNodeId(client_.id(), 3), |
| 284 11)); | 344 11)); |
| 285 Changes changes(client_.GetAndClearChanges()); | 345 Changes changes(client_.GetAndClearChanges()); |
| 286 ASSERT_EQ(1u, changes.size()); | 346 ASSERT_EQ(1u, changes.size()); |
| 287 EXPECT_EQ("change_id=11 node=1,3 new_parent=1,21 old_parent=null", | 347 EXPECT_EQ("change_id=11 node=1,3 new_parent=1,21 old_parent=null", |
| 288 changes[0]); | 348 changes[0]); |
| 289 } | 349 } |
| 290 | 350 |
| 291 // Make 21 a child of the root. | 351 // Make 21 a child of the root. |
| 292 { | 352 { |
| 293 AllocationScope scope; | 353 AllocationScope scope; |
| 294 ASSERT_TRUE(AddNode(view_manager_.get(), | 354 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 295 CreateNodeId(0, 1), | 355 CreateNodeId(0, 1), |
| 296 CreateNodeId(client_.id(), 21), | 356 CreateNodeId(client_.id(), 21), |
| 297 44)); | 357 44)); |
| 298 Changes changes(client_.GetAndClearChanges()); | 358 Changes changes(client_.GetAndClearChanges()); |
| 299 ASSERT_EQ(1u, changes.size()); | 359 ASSERT_EQ(1u, changes.size()); |
| 300 EXPECT_EQ("change_id=44 node=1,21 new_parent=0,1 old_parent=null", | 360 EXPECT_EQ("change_id=44 node=1,21 new_parent=0,1 old_parent=null", |
| 301 changes[0]); | 361 changes[0]); |
| 302 } | 362 } |
| 303 } | 363 } |
| 304 | 364 |
| 365 // Verifies DeleteNode works. |
| 366 TEST_F(ViewManagerConnectionTest, DeleteNode) { |
| 367 ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
| 368 ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
| 369 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
| 370 |
| 371 // Make 2 a child of 1. |
| 372 { |
| 373 AllocationScope scope; |
| 374 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 375 CreateNodeId(client_.id(), 1), |
| 376 CreateNodeId(client_.id(), 2), |
| 377 11)); |
| 378 Changes changes(client_.GetAndClearChanges()); |
| 379 ASSERT_EQ(1u, changes.size()); |
| 380 EXPECT_EQ("change_id=11 node=1,2 new_parent=1,1 old_parent=null", |
| 381 changes[0]); |
| 382 } |
| 383 |
| 384 // Add 1 to the root |
| 385 { |
| 386 AllocationScope scope; |
| 387 ASSERT_TRUE(AddNode(view_manager_.get(), |
| 388 CreateNodeId(0, 1), |
| 389 CreateNodeId(client_.id(), 1), |
| 390 101)); |
| 391 Changes changes(client_.GetAndClearChanges()); |
| 392 ASSERT_EQ(1u, changes.size()); |
| 393 EXPECT_EQ("change_id=101 node=1,1 new_parent=0,1 old_parent=null", |
| 394 changes[0]); |
| 395 } |
| 396 |
| 397 // Delete 1. |
| 398 { |
| 399 AllocationScope scope; |
| 400 ASSERT_TRUE(DeleteNode(view_manager_.get(), |
| 401 CreateNodeId(client_.id(), 1), |
| 402 121)); |
| 403 Changes changes(client_.GetAndClearChanges()); |
| 404 ASSERT_EQ(2u, changes.size()); |
| 405 EXPECT_EQ("change_id=121 node=1,1 new_parent=null old_parent=0,1", |
| 406 changes[0]); |
| 407 EXPECT_EQ("change_id=121 node=1,2 new_parent=null old_parent=1,1", |
| 408 changes[1]); |
| 409 } |
| 410 } |
| 411 |
| 412 // Assertions around setting a view. |
| 413 TEST_F(ViewManagerConnectionTest, SetView) { |
| 414 ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
| 415 ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
| 416 ASSERT_TRUE(CreateView(view_manager_.get(), 11)); |
| 417 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
| 418 |
| 419 // Set view 11 on node 1. |
| 420 { |
| 421 ASSERT_TRUE(SetView(view_manager_.get(), |
| 422 CreateNodeId(client_.id(), 1), |
| 423 CreateViewId(client_.id(), 11), |
| 424 21)); |
| 425 Changes changes(client_.GetAndClearChanges()); |
| 426 ASSERT_EQ(1u, changes.size()); |
| 427 EXPECT_EQ("change_id=21 node=1,1 new_view=1,11 old_view=null", |
| 428 changes[0]); |
| 429 } |
| 430 |
| 431 // Set view 11 on node 2. |
| 432 { |
| 433 ASSERT_TRUE(SetView(view_manager_.get(), |
| 434 CreateNodeId(client_.id(), 2), |
| 435 CreateViewId(client_.id(), 11), |
| 436 22)); |
| 437 Changes changes(client_.GetAndClearChanges()); |
| 438 ASSERT_EQ(2u, changes.size()); |
| 439 EXPECT_EQ("change_id=22 node=1,1 new_view=null old_view=1,11", |
| 440 changes[0]); |
| 441 EXPECT_EQ("change_id=22 node=1,2 new_view=1,11 old_view=null", |
| 442 changes[1]); |
| 443 } |
| 444 } |
| 445 |
| 446 // Verifies deleting a node with a view sends correct notifications. |
| 447 TEST_F(ViewManagerConnectionTest, DeleteNodeWithView) { |
| 448 ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
| 449 ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
| 450 ASSERT_TRUE(CreateView(view_manager_.get(), 11)); |
| 451 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
| 452 |
| 453 // Set view 11 on node 1. |
| 454 ASSERT_TRUE(SetView(view_manager_.get(), |
| 455 CreateNodeId(client_.id(), 1), |
| 456 CreateViewId(client_.id(), 11), |
| 457 21)); |
| 458 client_.GetAndClearChanges(); |
| 459 |
| 460 // Delete node 1. |
| 461 { |
| 462 ASSERT_TRUE(DeleteNode(view_manager_.get(), |
| 463 CreateNodeId(client_.id(), 1), |
| 464 121)); |
| 465 Changes changes(client_.GetAndClearChanges()); |
| 466 ASSERT_EQ(1u, changes.size()); |
| 467 EXPECT_EQ("change_id=121 node=1,1 new_view=null old_view=1,11", |
| 468 changes[0]); |
| 469 } |
| 470 |
| 471 // Set view 11 on node 2. |
| 472 { |
| 473 ASSERT_TRUE(SetView(view_manager_.get(), |
| 474 CreateNodeId(client_.id(), 2), |
| 475 CreateViewId(client_.id(), 11), |
| 476 22)); |
| 477 Changes changes(client_.GetAndClearChanges()); |
| 478 ASSERT_EQ(1u, changes.size()); |
| 479 EXPECT_EQ("change_id=22 node=1,2 new_view=1,11 old_view=null", changes[0]); |
| 480 } |
| 481 } |
| 482 |
| 483 // Sets view from one connection on another. |
| 484 TEST_F(ViewManagerConnectionTest, SetViewFromSecondConnection) { |
| 485 EstablishSecondConnection(); |
| 486 |
| 487 // Create two nodes in first connection. |
| 488 ASSERT_TRUE(CreateNode(view_manager_.get(), 1)); |
| 489 ASSERT_TRUE(CreateNode(view_manager_.get(), 2)); |
| 490 |
| 491 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
| 492 EXPECT_TRUE(client2_.GetAndClearChanges().empty()); |
| 493 |
| 494 // Create a view in the second connection. |
| 495 ASSERT_TRUE(CreateView(view_manager2_.get(), 51)); |
| 496 |
| 497 // Attach view to node 1 in the first connection. |
| 498 { |
| 499 ASSERT_TRUE(SetView(view_manager2_.get(), |
| 500 CreateNodeId(client_.id(), 1), |
| 501 CreateViewId(client2_.id(), 51), |
| 502 22)); |
| 503 Changes changes(client_.GetAndClearChanges()); |
| 504 ASSERT_EQ(1u, changes.size()); |
| 505 EXPECT_EQ("change_id=0 node=1,1 new_view=2,51 old_view=null", changes[0]); |
| 506 |
| 507 changes = client2_.GetAndClearChanges(); |
| 508 ASSERT_EQ(1u, changes.size()); |
| 509 EXPECT_EQ("change_id=22 node=1,1 new_view=2,51 old_view=null", changes[0]); |
| 510 } |
| 511 |
| 512 // Shutdown the second connection and verify view is removed. |
| 513 { |
| 514 DestroySecondConnection(); |
| 515 client_.set_quit_count(1); |
| 516 DoRunLoop(); |
| 517 |
| 518 Changes changes(client_.GetAndClearChanges()); |
| 519 ASSERT_EQ(1u, changes.size()); |
| 520 EXPECT_EQ("change_id=0 node=1,1 new_view=null old_view=2,51", changes[0]); |
| 521 } |
| 522 } |
| 523 |
| 305 } // namespace view_manager | 524 } // namespace view_manager |
| 306 } // namespace services | 525 } // namespace services |
| 307 } // namespace mojo | 526 } // namespace mojo |
| OLD | NEW |