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 | |
61 // Creates a node with the specified id. Returns true on success. Blocks until | 56 // Creates a node with the specified id. Returns true on success. Blocks until |
62 // we get back result from server. | 57 // we get back result from server. |
63 bool CreateNode(ViewManager* view_manager, uint16_t id) { | 58 bool CreateNode(ViewManager* view_manager, uint16_t id) { |
64 bool result = false; | 59 bool result = false; |
65 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); | 60 view_manager->CreateNode(id, base::Bind(&BooleanCallback, &result)); |
66 DoRunLoop(); | 61 DoRunLoop(); |
67 return result; | 62 return result; |
68 } | 63 } |
69 | 64 |
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 | |
83 // Adds a node, blocking until done. | 65 // Adds a node, blocking until done. |
84 bool AddNode(ViewManager* view_manager, | 66 bool AddNode(ViewManager* view_manager, |
85 uint32_t parent, | 67 uint32_t parent, |
86 uint32_t child, | 68 uint32_t child, |
87 int32_t change_id) { | 69 int32_t change_id) { |
88 bool result = false; | 70 bool result = false; |
89 view_manager->AddNode(parent, child, change_id, | 71 view_manager->AddNode(parent, child, change_id, |
90 base::Bind(&BooleanCallback, &result)); | 72 base::Bind(&BooleanCallback, &result)); |
91 DoRunLoop(); | 73 DoRunLoop(); |
92 return result; | 74 return result; |
93 } | 75 } |
94 | 76 |
95 // Removes a node, blocking until done. | 77 // Removes a node, blocking until done. |
96 bool RemoveNodeFromParent(ViewManager* view_manager, | 78 bool RemoveNodeFromParent(ViewManager* view_manager, |
97 uint32_t node_id, | 79 uint32_t node_id, |
98 int32_t change_id) { | 80 int32_t change_id) { |
99 bool result = false; | 81 bool result = false; |
100 view_manager->RemoveNodeFromParent(node_id, change_id, | 82 view_manager->RemoveNodeFromParent(node_id, change_id, |
101 base::Bind(&BooleanCallback, &result)); | 83 base::Bind(&BooleanCallback, &result)); |
102 DoRunLoop(); | 84 DoRunLoop(); |
103 return result; | 85 return result; |
104 } | 86 } |
105 | 87 |
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 | |
128 } // namespace | 88 } // namespace |
129 | 89 |
130 typedef std::vector<std::string> Changes; | 90 typedef std::vector<std::string> Changes; |
131 | 91 |
132 class ViewManagerClientImpl : public ViewManagerClient { | 92 class ViewManagerClientImpl : public ViewManagerClient { |
133 public: | 93 public: |
134 ViewManagerClientImpl() : id_(0), quit_count_(0) {} | 94 ViewManagerClientImpl() : id_(0), quit_count_(0) {} |
135 | 95 |
136 void set_quit_count(int count) { quit_count_ = count; } | 96 void set_quit_count(int count) { quit_count_ = count; } |
137 | 97 |
(...skipping 14 matching lines...) Expand all Loading... |
152 virtual void OnNodeHierarchyChanged(uint32_t node, | 112 virtual void OnNodeHierarchyChanged(uint32_t node, |
153 uint32_t new_parent, | 113 uint32_t new_parent, |
154 uint32_t old_parent, | 114 uint32_t old_parent, |
155 int32_t change_id) OVERRIDE { | 115 int32_t change_id) OVERRIDE { |
156 changes_.push_back( | 116 changes_.push_back( |
157 base::StringPrintf( | 117 base::StringPrintf( |
158 "change_id=%d node=%s new_parent=%s old_parent=%s", | 118 "change_id=%d node=%s new_parent=%s old_parent=%s", |
159 change_id, NodeIdToString(node).c_str(), | 119 change_id, NodeIdToString(node).c_str(), |
160 NodeIdToString(new_parent).c_str(), | 120 NodeIdToString(new_parent).c_str(), |
161 NodeIdToString(old_parent).c_str())); | 121 NodeIdToString(old_parent).c_str())); |
162 QuitIfNecessary(); | 122 if (quit_count_ > 0) { |
163 } | 123 if (--quit_count_ == 0) |
164 virtual void OnNodeViewReplaced(uint32_t node, | 124 current_run_loop->Quit(); |
165 uint32_t new_view_id, | 125 } |
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(); | |
180 } | 126 } |
181 | 127 |
182 uint16_t id_; | 128 uint16_t id_; |
183 | 129 |
184 // Used to determine when/if to quit the run loop. | 130 // Used to determine when/if to quit the run loop. |
185 int quit_count_; | 131 int quit_count_; |
186 | 132 |
187 Changes changes_; | 133 Changes changes_; |
188 | 134 |
189 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); | 135 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); |
(...skipping 19 matching lines...) Expand all Loading... |
209 connection2_.reset(new ViewManagerConnection); | 155 connection2_.reset(new ViewManagerConnection); |
210 InterfacePipe<ViewManagerClient, ViewManager> pipe; | 156 InterfacePipe<ViewManagerClient, ViewManager> pipe; |
211 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); | 157 view_manager2_.reset(pipe.handle_to_peer.Pass(), &client2_); |
212 connection2_->Initialize( | 158 connection2_->Initialize( |
213 &service_factory_, | 159 &service_factory_, |
214 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); | 160 ScopedMessagePipeHandle::From(pipe.handle_to_self.Pass())); |
215 // Wait for the id. | 161 // Wait for the id. |
216 DoRunLoop(); | 162 DoRunLoop(); |
217 } | 163 } |
218 | 164 |
219 void DestroySecondConnection() { | |
220 connection2_.reset(); | |
221 view_manager2_.reset(); | |
222 } | |
223 | |
224 Environment env_; | 165 Environment env_; |
225 base::MessageLoop loop_; | 166 base::MessageLoop loop_; |
226 RootNodeManager root_node_manager_; | 167 RootNodeManager root_node_manager_; |
227 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; | 168 ServiceConnector<ViewManagerConnection, RootNodeManager> service_factory_; |
228 ViewManagerConnection connection_; | 169 ViewManagerConnection connection_; |
229 ViewManagerClientImpl client_; | 170 ViewManagerClientImpl client_; |
230 RemotePtr<ViewManager> view_manager_; | 171 RemotePtr<ViewManager> view_manager_; |
231 | 172 |
232 ViewManagerClientImpl client2_; | 173 ViewManagerClientImpl client2_; |
233 RemotePtr<ViewManager> view_manager2_; | 174 RemotePtr<ViewManager> view_manager2_; |
234 scoped_ptr<ViewManagerConnection> connection2_; | 175 scoped_ptr<ViewManagerConnection> connection2_; |
235 | 176 |
236 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); | 177 DISALLOW_COPY_AND_ASSIGN(ViewManagerConnectionTest); |
237 }; | 178 }; |
238 | 179 |
239 // Verifies client gets a valid id. | 180 // Verifies client gets a valid id. |
240 TEST_F(ViewManagerConnectionTest, ValidId) { | 181 TEST_F(ViewManagerConnectionTest, ValidId) { |
241 // All these tests assume 1 for the client id. The only real assertion here is | 182 EXPECT_NE(0, client_.id()); |
242 // the client id is not zero, but adding this as rest of code here assumes 1. | |
243 EXPECT_EQ(1, client_.id()); | |
244 } | 183 } |
245 | 184 |
246 // Verifies two clients/connections get different ids. | 185 // Verifies two clients/connections get different ids. |
247 TEST_F(ViewManagerConnectionTest, TwoClientsGetDifferentConnectionIds) { | 186 TEST_F(ViewManagerConnectionTest, TwoClientsGetDifferentConnectionIds) { |
248 EstablishSecondConnection(); | 187 EstablishSecondConnection(); |
249 EXPECT_NE(0, client2_.id()); | 188 EXPECT_NE(0, client2_.id()); |
250 EXPECT_NE(client_.id(), client2_.id()); | 189 EXPECT_NE(client_.id(), client2_.id()); |
251 } | 190 } |
252 | 191 |
253 // Verifies client gets a valid id. | 192 // Verifies client gets a valid id. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 ASSERT_EQ(1u, changes.size()); | 265 ASSERT_EQ(1u, changes.size()); |
327 EXPECT_EQ("change_id=0 node=1,2 new_parent=1,1 old_parent=null", | 266 EXPECT_EQ("change_id=0 node=1,2 new_parent=1,1 old_parent=null", |
328 changes[0]); | 267 changes[0]); |
329 } | 268 } |
330 } | 269 } |
331 | 270 |
332 // Verifies adding to root sends right notifications. | 271 // Verifies adding to root sends right notifications. |
333 TEST_F(ViewManagerConnectionTest, AddToRoot) { | 272 TEST_F(ViewManagerConnectionTest, AddToRoot) { |
334 ASSERT_TRUE(CreateNode(view_manager_.get(), 21)); | 273 ASSERT_TRUE(CreateNode(view_manager_.get(), 21)); |
335 ASSERT_TRUE(CreateNode(view_manager_.get(), 3)); | 274 ASSERT_TRUE(CreateNode(view_manager_.get(), 3)); |
| 275 |
336 EXPECT_TRUE(client_.GetAndClearChanges().empty()); | 276 EXPECT_TRUE(client_.GetAndClearChanges().empty()); |
337 | 277 |
338 // Make 3 a child of 21. | 278 // Make 3 a child of 21. |
339 { | 279 { |
340 AllocationScope scope; | 280 AllocationScope scope; |
341 ASSERT_TRUE(AddNode(view_manager_.get(), | 281 ASSERT_TRUE(AddNode(view_manager_.get(), |
342 CreateNodeId(client_.id(), 21), | 282 CreateNodeId(client_.id(), 21), |
343 CreateNodeId(client_.id(), 3), | 283 CreateNodeId(client_.id(), 3), |
344 11)); | 284 11)); |
345 Changes changes(client_.GetAndClearChanges()); | 285 Changes changes(client_.GetAndClearChanges()); |
346 ASSERT_EQ(1u, changes.size()); | 286 ASSERT_EQ(1u, changes.size()); |
347 EXPECT_EQ("change_id=11 node=1,3 new_parent=1,21 old_parent=null", | 287 EXPECT_EQ("change_id=11 node=1,3 new_parent=1,21 old_parent=null", |
348 changes[0]); | 288 changes[0]); |
349 } | 289 } |
350 | 290 |
351 // Make 21 a child of the root. | 291 // Make 21 a child of the root. |
352 { | 292 { |
353 AllocationScope scope; | 293 AllocationScope scope; |
354 ASSERT_TRUE(AddNode(view_manager_.get(), | 294 ASSERT_TRUE(AddNode(view_manager_.get(), |
355 CreateNodeId(0, 1), | 295 CreateNodeId(0, 1), |
356 CreateNodeId(client_.id(), 21), | 296 CreateNodeId(client_.id(), 21), |
357 44)); | 297 44)); |
358 Changes changes(client_.GetAndClearChanges()); | 298 Changes changes(client_.GetAndClearChanges()); |
359 ASSERT_EQ(1u, changes.size()); | 299 ASSERT_EQ(1u, changes.size()); |
360 EXPECT_EQ("change_id=44 node=1,21 new_parent=0,1 old_parent=null", | 300 EXPECT_EQ("change_id=44 node=1,21 new_parent=0,1 old_parent=null", |
361 changes[0]); | 301 changes[0]); |
362 } | 302 } |
363 } | 303 } |
364 | 304 |
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 | |
524 } // namespace view_manager | 305 } // namespace view_manager |
525 } // namespace services | 306 } // namespace services |
526 } // namespace mojo | 307 } // namespace mojo |
OLD | NEW |