[Drive] Handle error cases earlier in FakeDriveService
[chromium-blink-merge.git] / mojo / services / view_manager / view_manager_unittest.cc
blob5ea3b71899fcb5709543993c51f242f3e538ee08
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
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/at_exit.h"
9 #include "base/auto_reset.h"
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "mojo/application_manager/application_manager.h"
17 #include "mojo/common/common_type_converters.h"
18 #include "mojo/public/cpp/application/application_connection.h"
19 #include "mojo/public/cpp/application/application_delegate.h"
20 #include "mojo/public/cpp/application/application_impl.h"
21 #include "mojo/public/cpp/application/connect.h"
22 #include "mojo/public/cpp/bindings/lib/router.h"
23 #include "mojo/public/interfaces/application/service_provider.mojom.h"
24 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
25 #include "mojo/services/public/cpp/view_manager/types.h"
26 #include "mojo/services/public/cpp/view_manager/util.h"
27 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
28 #include "mojo/services/view_manager/ids.h"
29 #include "mojo/services/view_manager/test_change_tracker.h"
30 #include "mojo/shell/shell_test_helper.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "ui/gfx/geometry/rect.h"
34 namespace mojo {
35 namespace service {
37 namespace {
39 const char kTestServiceURL[] = "mojo:test_url";
40 const char kTestServiceURL2[] = "mojo:test_url2";
42 // ViewManagerProxy is a proxy to an ViewManagerService. It handles invoking
43 // ViewManagerService functions on the right thread in a synchronous manner
44 // (each ViewManagerService cover function blocks until the response from the
45 // ViewManagerService is returned). In addition it tracks the set of
46 // ViewManagerClient messages received by way of a vector of Changes. Use
47 // DoRunLoopUntilChangesCount() to wait for a certain number of messages to be
48 // received.
49 class ViewManagerProxy : public TestChangeTracker::Delegate {
50 public:
51 explicit ViewManagerProxy(TestChangeTracker* tracker)
52 : tracker_(tracker),
53 main_loop_(NULL),
54 view_manager_(NULL),
55 quit_count_(0),
56 router_(NULL) {
57 SetInstance(this);
60 virtual ~ViewManagerProxy() {
63 // Runs a message loop until the single instance has been created.
64 static ViewManagerProxy* WaitForInstance() {
65 if (!instance_)
66 RunMainLoop();
67 ViewManagerProxy* instance = instance_;
68 instance_ = NULL;
69 return instance;
72 ViewManagerService* view_manager() { return view_manager_; }
74 // Runs the main loop until |count| changes have been received.
75 std::vector<Change> DoRunLoopUntilChangesCount(size_t count) {
76 DCHECK_EQ(0u, quit_count_);
77 if (tracker_->changes()->size() >= count) {
78 CopyChangesFromTracker();
79 return changes_;
81 quit_count_ = count - tracker_->changes()->size();
82 // Run the current message loop. When |count| Changes have been received,
83 // we'll quit.
84 RunMainLoop();
85 return changes_;
88 const std::vector<Change>& changes() const { return changes_; }
90 // Destroys the connection, blocking until done.
91 void Destroy() {
92 router_->CloseMessagePipe();
95 void ClearChanges() {
96 changes_.clear();
97 tracker_->changes()->clear();
100 void CopyChangesFromTracker() {
101 std::vector<Change> changes;
102 tracker_->changes()->swap(changes);
103 changes_.swap(changes);
106 // The following functions are cover methods for ViewManagerService. They
107 // block until the result is received.
108 bool CreateNode(Id node_id) {
109 changes_.clear();
110 ErrorCode result = ERROR_CODE_NONE;
111 view_manager_->CreateView(
112 node_id,
113 base::Bind(&ViewManagerProxy::GotResultWithErrorCode,
114 base::Unretained(this), &result));
115 RunMainLoop();
116 return result == ERROR_CODE_NONE;
118 ErrorCode CreateNodeWithErrorCode(Id node_id) {
119 changes_.clear();
120 ErrorCode result = ERROR_CODE_NONE;
121 view_manager_->CreateView(
122 node_id,
123 base::Bind(&ViewManagerProxy::GotResultWithErrorCode,
124 base::Unretained(this), &result));
125 RunMainLoop();
126 return result;
128 bool AddNode(Id parent, Id child) {
129 changes_.clear();
130 bool result = false;
131 view_manager_->AddView(parent, child,
132 base::Bind(&ViewManagerProxy::GotResult,
133 base::Unretained(this), &result));
134 RunMainLoop();
135 return result;
137 bool RemoveNodeFromParent(Id node_id) {
138 changes_.clear();
139 bool result = false;
140 view_manager_->RemoveViewFromParent(node_id,
141 base::Bind(&ViewManagerProxy::GotResult,
142 base::Unretained(this), &result));
143 RunMainLoop();
144 return result;
146 bool ReorderNode(Id node_id,
147 Id relative_node_id,
148 OrderDirection direction) {
149 changes_.clear();
150 bool result = false;
151 view_manager_->ReorderView(node_id, relative_node_id, direction,
152 base::Bind(&ViewManagerProxy::GotResult,
153 base::Unretained(this), &result));
154 RunMainLoop();
155 return result;
157 void GetNodeTree(Id node_id, std::vector<TestNode>* nodes) {
158 changes_.clear();
159 view_manager_->GetViewTree(node_id,
160 base::Bind(&ViewManagerProxy::GotNodeTree,
161 base::Unretained(this), nodes));
162 RunMainLoop();
164 bool Embed(const Id node_id, const char* url) {
165 changes_.clear();
166 base::AutoReset<bool> auto_reset(&in_embed_, true);
167 bool result = false;
168 ServiceProviderPtr services;
169 view_manager_->Embed(url, node_id, services.Pass(),
170 base::Bind(&ViewManagerProxy::GotResult,
171 base::Unretained(this), &result));
172 RunMainLoop();
173 return result;
175 bool DeleteNode(Id node_id) {
176 changes_.clear();
177 bool result = false;
178 view_manager_->DeleteView(node_id,
179 base::Bind(&ViewManagerProxy::GotResult,
180 base::Unretained(this), &result));
181 RunMainLoop();
182 return result;
184 bool SetNodeBounds(Id node_id, const gfx::Rect& bounds) {
185 changes_.clear();
186 bool result = false;
187 view_manager_->SetViewBounds(node_id, Rect::From(bounds),
188 base::Bind(&ViewManagerProxy::GotResult,
189 base::Unretained(this), &result));
190 RunMainLoop();
191 return result;
194 private:
195 friend class TestViewManagerClientConnection;
197 void set_router(mojo::internal::Router* router) { router_ = router; }
199 void set_view_manager(ViewManagerService* view_manager) {
200 view_manager_ = view_manager;
203 static void RunMainLoop() {
204 DCHECK(!main_run_loop_);
205 main_run_loop_ = new base::RunLoop;
206 main_run_loop_->Run();
207 delete main_run_loop_;
208 main_run_loop_ = NULL;
211 void QuitCountReached() {
212 CopyChangesFromTracker();
213 main_run_loop_->Quit();
216 static void SetInstance(ViewManagerProxy* instance) {
217 DCHECK(!instance_);
218 instance_ = instance;
219 // Embed() runs its own run loop that is quit when the result is
220 // received. Embed() also results in a new instance. If we quit here while
221 // waiting for a Embed() we would prematurely return before we got the
222 // result from Embed().
223 if (!in_embed_ && main_run_loop_)
224 main_run_loop_->Quit();
227 // Callbacks from the various ViewManagerService functions.
228 void GotResult(bool* result_cache, bool result) {
229 *result_cache = result;
230 DCHECK(main_run_loop_);
231 main_run_loop_->Quit();
234 void GotResultWithErrorCode(ErrorCode* error_code_cache,
235 ErrorCode error_code) {
236 *error_code_cache = error_code;
237 DCHECK(main_run_loop_);
238 main_run_loop_->Quit();
241 void GotNodeTree(std::vector<TestNode>* nodes, Array<ViewDataPtr> results) {
242 ViewDatasToTestNodes(results, nodes);
243 DCHECK(main_run_loop_);
244 main_run_loop_->Quit();
247 // TestChangeTracker::Delegate:
248 virtual void OnChangeAdded() OVERRIDE {
249 if (quit_count_ > 0 && --quit_count_ == 0)
250 QuitCountReached();
253 static ViewManagerProxy* instance_;
254 static base::RunLoop* main_run_loop_;
255 static bool in_embed_;
257 TestChangeTracker* tracker_;
259 // MessageLoop of the test.
260 base::MessageLoop* main_loop_;
262 ViewManagerService* view_manager_;
264 // Number of changes we're waiting on until we quit the current loop.
265 size_t quit_count_;
267 std::vector<Change> changes_;
269 mojo::internal::Router* router_;
271 DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy);
274 // static
275 ViewManagerProxy* ViewManagerProxy::instance_ = NULL;
277 // static
278 base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL;
280 // static
281 bool ViewManagerProxy::in_embed_ = false;
283 class TestViewManagerClientConnection
284 : public InterfaceImpl<ViewManagerClient> {
285 public:
286 TestViewManagerClientConnection() : connection_(&tracker_) {
287 tracker_.set_delegate(&connection_);
290 // InterfaceImpl:
291 virtual void OnConnectionEstablished() OVERRIDE {
292 connection_.set_router(internal_state()->router());
293 connection_.set_view_manager(client());
296 // ViewManagerClient:
297 virtual void OnEmbed(
298 ConnectionSpecificId connection_id,
299 const String& creator_url,
300 ViewDataPtr root,
301 InterfaceRequest<ServiceProvider> services) OVERRIDE {
302 tracker_.OnEmbed(connection_id, creator_url, root.Pass());
304 virtual void OnViewBoundsChanged(Id node_id,
305 RectPtr old_bounds,
306 RectPtr new_bounds) OVERRIDE {
307 tracker_.OnNodeBoundsChanged(node_id, old_bounds.Pass(), new_bounds.Pass());
309 virtual void OnViewHierarchyChanged(Id node,
310 Id new_parent,
311 Id old_parent,
312 Array<ViewDataPtr> nodes) OVERRIDE {
313 tracker_.OnNodeHierarchyChanged(node, new_parent, old_parent, nodes.Pass());
315 virtual void OnViewReordered(Id node_id,
316 Id relative_node_id,
317 OrderDirection direction) OVERRIDE {
318 tracker_.OnNodeReordered(node_id, relative_node_id, direction);
320 virtual void OnViewDeleted(Id node) OVERRIDE {
321 tracker_.OnNodeDeleted(node);
323 virtual void OnViewInputEvent(Id node_id,
324 EventPtr event,
325 const Callback<void()>& callback) OVERRIDE {
326 tracker_.OnNodeInputEvent(node_id, event.Pass());
328 virtual void Embed(
329 const String& url,
330 InterfaceRequest<ServiceProvider> service_provider) OVERRIDE {
331 tracker_.DelegateEmbed(url);
333 virtual void DispatchOnViewInputEvent(mojo::EventPtr event) OVERRIDE {
336 private:
337 TestChangeTracker tracker_;
338 ViewManagerProxy connection_;
340 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection);
343 // Used with ViewManagerService::Embed(). Creates a
344 // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy.
345 class EmbedApplicationLoader : public ApplicationLoader,
346 ApplicationDelegate,
347 public InterfaceFactory<ViewManagerClient> {
348 public:
349 EmbedApplicationLoader() {}
350 virtual ~EmbedApplicationLoader() {}
352 // ApplicationLoader implementation:
353 virtual void Load(ApplicationManager* manager,
354 const GURL& url,
355 scoped_refptr<LoadCallbacks> callbacks) OVERRIDE {
356 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
357 if (!shell_handle.is_valid())
358 return;
359 scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this,
360 shell_handle.Pass()));
361 apps_.push_back(app.release());
363 virtual void OnServiceError(ApplicationManager* manager,
364 const GURL& url) OVERRIDE {}
366 // ApplicationDelegate implementation:
367 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
368 OVERRIDE {
369 connection->AddService(this);
370 return true;
373 // InterfaceFactory<ViewManagerClient> implementation:
374 virtual void Create(ApplicationConnection* connection,
375 InterfaceRequest<ViewManagerClient> request) OVERRIDE {
376 BindToRequest(new TestViewManagerClientConnection, &request);
379 private:
380 ScopedVector<ApplicationImpl> apps_;
382 DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader);
385 // Creates an id used for transport from the specified parameters.
386 Id BuildNodeId(ConnectionSpecificId connection_id,
387 ConnectionSpecificId node_id) {
388 return (connection_id << 16) | node_id;
391 // Callback from Embed(). |result| is the result of the
392 // Embed() call and |run_loop| the nested RunLoop.
393 void EmbedCallback(bool* result_cache, base::RunLoop* run_loop, bool result) {
394 *result_cache = result;
395 run_loop->Quit();
398 // Embed from an application that does not yet have a view manager connection.
399 // Blocks until result is determined.
400 bool InitEmbed(ViewManagerInitService* view_manager_init,
401 const std::string& url,
402 size_t number_of_calls) {
403 bool result = false;
404 base::RunLoop run_loop;
405 for (size_t i = 0; i < number_of_calls; ++i) {
406 ServiceProviderPtr sp;
407 view_manager_init->Embed(url, sp.Pass(),
408 base::Bind(&EmbedCallback, &result, &run_loop));
410 run_loop.Run();
411 return result;
414 } // namespace
416 typedef std::vector<std::string> Changes;
418 class ViewManagerTest : public testing::Test {
419 public:
420 ViewManagerTest()
421 : connection_(NULL),
422 connection2_(NULL),
423 connection3_(NULL) {}
425 virtual void SetUp() OVERRIDE {
426 test_helper_.Init();
428 test_helper_.SetLoaderForURL(
429 scoped_ptr<ApplicationLoader>(new EmbedApplicationLoader()),
430 GURL(kTestServiceURL));
432 test_helper_.SetLoaderForURL(
433 scoped_ptr<ApplicationLoader>(new EmbedApplicationLoader()),
434 GURL(kTestServiceURL2));
436 test_helper_.application_manager()->ConnectToService(
437 GURL("mojo:mojo_view_manager"), &view_manager_init_);
438 ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 1));
440 connection_ = ViewManagerProxy::WaitForInstance();
441 ASSERT_TRUE(connection_ != NULL);
442 connection_->DoRunLoopUntilChangesCount(1);
445 virtual void TearDown() OVERRIDE {
446 if (connection3_)
447 connection3_->Destroy();
448 if (connection2_)
449 connection2_->Destroy();
450 if (connection_)
451 connection_->Destroy();
454 protected:
455 void EstablishSecondConnectionWithRoot(Id root_id) {
456 ASSERT_TRUE(connection_->Embed(root_id, kTestServiceURL));
457 connection2_ = ViewManagerProxy::WaitForInstance();
458 ASSERT_TRUE(connection2_ != NULL);
459 connection2_->DoRunLoopUntilChangesCount(1);
460 ASSERT_EQ(1u, connection2_->changes().size());
463 // Creates a second connection to the viewmanager.
464 void EstablishSecondConnection(bool create_initial_node) {
465 if (create_initial_node)
466 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
467 ASSERT_NO_FATAL_FAILURE(
468 EstablishSecondConnectionWithRoot(BuildNodeId(1, 1)));
469 const std::vector<Change>& changes(connection2_->changes());
470 ASSERT_EQ(1u, changes.size());
471 EXPECT_EQ("OnEmbed creator=mojo:test_url",
472 ChangesToDescription1(changes)[0]);
473 if (create_initial_node) {
474 EXPECT_EQ("[node=1,1 parent=null]",
475 ChangeNodeDescription(changes));
479 void EstablishThirdConnection(ViewManagerProxy* owner, Id root_id) {
480 ASSERT_TRUE(connection3_ == NULL);
481 ASSERT_TRUE(owner->Embed(root_id, kTestServiceURL2));
482 connection3_ = ViewManagerProxy::WaitForInstance();
483 ASSERT_TRUE(connection3_ != NULL);
484 connection3_->DoRunLoopUntilChangesCount(1);
485 ASSERT_EQ(1u, connection3_->changes().size());
486 EXPECT_EQ("OnEmbed creator=mojo:test_url",
487 ChangesToDescription1(connection3_->changes())[0]);
490 void DestroySecondConnection() {
491 connection2_->Destroy();
492 connection2_ = NULL;
495 base::ShadowingAtExitManager at_exit_;
496 shell::ShellTestHelper test_helper_;
498 ViewManagerInitServicePtr view_manager_init_;
500 // NOTE: this connection is the root. As such, it has special permissions.
501 ViewManagerProxy* connection_;
502 ViewManagerProxy* connection2_;
503 ViewManagerProxy* connection3_;
505 DISALLOW_COPY_AND_ASSIGN(ViewManagerTest);
508 TEST_F(ViewManagerTest, SecondEmbedRoot_InitService) {
509 ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 1));
510 connection_->DoRunLoopUntilChangesCount(1);
511 EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url);
514 TEST_F(ViewManagerTest, SecondEmbedRoot_Service) {
515 ASSERT_TRUE(connection_->Embed(BuildNodeId(0, 0), kTestServiceURL));
516 connection_->DoRunLoopUntilChangesCount(1);
517 EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url);
520 TEST_F(ViewManagerTest, MultipleEmbedRootsBeforeWTHReady) {
521 ASSERT_TRUE(InitEmbed(view_manager_init_.get(), kTestServiceURL, 2));
522 connection_->DoRunLoopUntilChangesCount(2);
523 EXPECT_EQ(kTestServiceURL, connection_->changes()[0].embed_url);
524 EXPECT_EQ(kTestServiceURL, connection_->changes()[1].embed_url);
527 // Verifies client gets a valid id.
528 #if defined(OS_LINUX)
529 // http://crbug.com/396492
530 #define MAYBE_ValidId DISABLED_ValidId
531 #else
532 #define MAYBE_ValidId ValidId
533 #endif
534 TEST_F(ViewManagerTest, MAYBE_ValidId) {
535 // TODO(beng): this should really have the URL of the application that
536 // connected to ViewManagerInit.
537 EXPECT_EQ("OnEmbed creator=",
538 ChangesToDescription1(connection_->changes())[0]);
540 // All these tests assume 1 for the client id. The only real assertion here is
541 // the client id is not zero, but adding this as rest of code here assumes 1.
542 EXPECT_EQ(1, connection_->changes()[0].connection_id);
545 // Verifies two clients/connections get different ids.
546 TEST_F(ViewManagerTest, TwoClientsGetDifferentConnectionIds) {
547 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
548 EXPECT_EQ("OnEmbed creator=mojo:test_url",
549 ChangesToDescription1(connection2_->changes())[0]);
551 // It isn't strictly necessary that the second connection gets 2, but these
552 // tests are written assuming that is the case. The key thing is the
553 // connection ids of |connection_| and |connection2_| differ.
554 EXPECT_EQ(2, connection2_->changes()[0].connection_id);
557 // Verifies when Embed() is invoked any child nodes are removed.
558 TEST_F(ViewManagerTest, NodesRemovedWhenEmbedding) {
559 // Two nodes 1 and 2. 2 is parented to 1.
560 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
561 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
562 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2)));
564 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
565 EXPECT_EQ("[node=1,1 parent=null]",
566 ChangeNodeDescription(connection2_->changes()));
568 // Embed() removed node 2.
570 std::vector<TestNode> nodes;
571 connection_->GetNodeTree(BuildNodeId(1, 2), &nodes);
572 ASSERT_EQ(1u, nodes.size());
573 EXPECT_EQ("node=1,2 parent=null", nodes[0].ToString());
576 // |connection2_| should not see node 2.
578 std::vector<TestNode> nodes;
579 connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes);
580 ASSERT_EQ(1u, nodes.size());
581 EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString());
584 std::vector<TestNode> nodes;
585 connection2_->GetNodeTree(BuildNodeId(1, 2), &nodes);
586 EXPECT_TRUE(nodes.empty());
589 // Nodes 3 and 4 in connection 2.
590 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3)));
591 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 4)));
592 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 3), BuildNodeId(2, 4)));
594 // Connection 3 rooted at 2.
595 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_,
596 BuildNodeId(2, 3)));
598 // Node 4 should no longer have a parent.
600 std::vector<TestNode> nodes;
601 connection2_->GetNodeTree(BuildNodeId(2, 3), &nodes);
602 ASSERT_EQ(1u, nodes.size());
603 EXPECT_EQ("node=2,3 parent=null", nodes[0].ToString());
605 nodes.clear();
606 connection2_->GetNodeTree(BuildNodeId(2, 4), &nodes);
607 ASSERT_EQ(1u, nodes.size());
608 EXPECT_EQ("node=2,4 parent=null", nodes[0].ToString());
611 // And node 4 should not be visible to connection 3.
613 std::vector<TestNode> nodes;
614 connection3_->GetNodeTree(BuildNodeId(2, 3), &nodes);
615 ASSERT_EQ(1u, nodes.size());
616 EXPECT_EQ("node=2,3 parent=null", nodes[0].ToString());
620 // Verifies once Embed() has been invoked the parent connection can't see any
621 // children.
622 TEST_F(ViewManagerTest, CantAccessChildrenOfEmbeddedNode) {
623 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
625 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
626 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
628 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_,
629 BuildNodeId(2, 2)));
631 ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 3)));
632 ASSERT_TRUE(connection3_->AddNode(BuildNodeId(2, 2), BuildNodeId(3, 3)));
634 // Even though 3 is a child of 2 connection 2 can't see 3 as it's from a
635 // different connection.
637 std::vector<TestNode> nodes;
638 connection2_->GetNodeTree(BuildNodeId(2, 2), &nodes);
639 ASSERT_EQ(1u, nodes.size());
640 EXPECT_EQ("node=2,2 parent=1,1", nodes[0].ToString());
644 std::vector<TestNode> nodes;
645 connection2_->GetNodeTree(BuildNodeId(3, 3), &nodes);
646 EXPECT_TRUE(nodes.empty());
649 // Connection 2 shouldn't be able to get node 3 at all.
651 std::vector<TestNode> nodes;
652 connection2_->GetNodeTree(BuildNodeId(3, 3), &nodes);
653 EXPECT_TRUE(nodes.empty());
656 // Connection 1 should be able to see it all (its the root).
658 std::vector<TestNode> nodes;
659 connection_->GetNodeTree(BuildNodeId(1, 1), &nodes);
660 ASSERT_EQ(3u, nodes.size());
661 EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString());
662 EXPECT_EQ("node=2,2 parent=1,1", nodes[1].ToString());
663 EXPECT_EQ("node=3,3 parent=2,2", nodes[2].ToString());
667 // Verifies once Embed() has been invoked the parent can't mutate the children.
668 TEST_F(ViewManagerTest, CantModifyChildrenOfEmbeddedNode) {
669 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
671 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
672 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
674 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection2_,
675 BuildNodeId(2, 2)));
677 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3)));
678 // Connection 2 shouldn't be able to add anything to the node anymore.
679 ASSERT_FALSE(connection2_->AddNode(BuildNodeId(2, 2), BuildNodeId(2, 3)));
681 // Create node 3 in connection 3 and add it to node 3.
682 ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 3)));
683 ASSERT_TRUE(connection3_->AddNode(BuildNodeId(2, 2), BuildNodeId(3, 3)));
685 // Connection 2 shouldn't be able to remove node 3.
686 ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(3, 3)));
689 // Verifies client gets a valid id.
690 TEST_F(ViewManagerTest, CreateNode) {
691 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
692 EXPECT_TRUE(connection_->changes().empty());
694 // Can't create a node with the same id.
695 ASSERT_EQ(ERROR_CODE_VALUE_IN_USE,
696 connection_->CreateNodeWithErrorCode(BuildNodeId(1, 1)));
697 EXPECT_TRUE(connection_->changes().empty());
699 // Can't create a node with a bogus connection id.
700 EXPECT_EQ(ERROR_CODE_ILLEGAL_ARGUMENT,
701 connection_->CreateNodeWithErrorCode(BuildNodeId(2, 1)));
702 EXPECT_TRUE(connection_->changes().empty());
705 // Verifies AddNode fails when node is already in position.
706 TEST_F(ViewManagerTest, AddNodeWithNoChange) {
707 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
708 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3)));
710 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
712 // Make 3 a child of 2.
713 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3)));
715 // Try again, this should fail.
716 EXPECT_FALSE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3)));
719 // Verifies AddNode fails when node is already in position.
720 TEST_F(ViewManagerTest, AddAncestorFails) {
721 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
722 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3)));
724 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
726 // Make 3 a child of 2.
727 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 3)));
729 // Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3.
730 EXPECT_FALSE(connection_->AddNode(BuildNodeId(1, 3), BuildNodeId(1, 2)));
733 // Verifies adding to root sends right notifications.
734 TEST_F(ViewManagerTest, AddToRoot) {
735 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 21)));
736 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 3)));
738 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
740 // Make 3 a child of 21.
741 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 21), BuildNodeId(1, 3)));
743 // Make 21 a child of 1.
744 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 21)));
746 // Connection 2 should not be told anything (because the node is from a
747 // different connection). Create a node to ensure we got a response from
748 // the server.
749 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 100)));
750 connection2_->CopyChangesFromTracker();
751 EXPECT_TRUE(connection2_->changes().empty());
754 // Verifies HierarchyChanged is correctly sent for various adds/removes.
755 TEST_F(ViewManagerTest, NodeHierarchyChangedNodes) {
756 // 1,2->1,11.
757 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
758 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 11)));
759 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 2), BuildNodeId(1, 11)));
761 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
763 // 1,1->1,2->1,11
765 // Client 2 should not get anything (1,2 is from another connection).
766 connection2_->ClearChanges();
767 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2)));
768 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 100)));
769 connection2_->CopyChangesFromTracker();
770 EXPECT_TRUE(connection2_->changes().empty());
773 // 0,1->1,1->1,2->1,11.
775 // Again, client 2 should not get anything.
776 connection2_->ClearChanges();
777 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
778 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 101)));
779 connection2_->CopyChangesFromTracker();
780 EXPECT_TRUE(connection2_->changes().empty());
783 // 1,1->1,2->1,11.
785 connection2_->ClearChanges();
786 ASSERT_TRUE(connection_->RemoveNodeFromParent(BuildNodeId(1, 1)));
787 EXPECT_TRUE(connection_->changes().empty());
788 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 102)));
789 connection2_->CopyChangesFromTracker();
790 EXPECT_TRUE(connection2_->changes().empty());
793 // 1,1->1,2->1,11->1,111.
794 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 111)));
796 connection2_->ClearChanges();
797 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 11), BuildNodeId(1, 111)));
798 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 103)));
799 connection2_->CopyChangesFromTracker();
800 EXPECT_TRUE(connection2_->changes().empty());
803 // 0,1->1,1->1,2->1,11->1,111
805 connection2_->ClearChanges();
806 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
807 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 104)));
808 connection2_->CopyChangesFromTracker();
809 EXPECT_TRUE(connection2_->changes().empty());
813 TEST_F(ViewManagerTest, NodeHierarchyChangedAddingKnownToUnknown) {
814 // Create the following structure: root -> 1 -> 11 and 2->21 (2 has no
815 // parent).
816 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
818 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 11)));
819 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
820 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 21)));
822 // Set up the hierarchy.
823 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
824 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 11)));
825 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 2), BuildNodeId(2, 21)));
827 // Remove 11, should result in a hierarchy change for the root.
829 connection_->ClearChanges();
830 ASSERT_TRUE(connection2_->RemoveNodeFromParent(BuildNodeId(2, 11)));
832 connection_->DoRunLoopUntilChangesCount(1);
833 const Changes changes(ChangesToDescription1(connection_->changes()));
834 ASSERT_EQ(1u, changes.size());
835 EXPECT_EQ("HierarchyChanged node=2,11 new_parent=null old_parent=1,1",
836 changes[0]);
839 // Add 2 to 1.
841 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
843 connection_->DoRunLoopUntilChangesCount(1);
844 const Changes changes(ChangesToDescription1(connection_->changes()));
845 ASSERT_EQ(1u, changes.size());
846 EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null",
847 changes[0]);
848 EXPECT_EQ("[node=2,2 parent=1,1],"
849 "[node=2,21 parent=2,2]",
850 ChangeNodeDescription(connection_->changes()));
854 TEST_F(ViewManagerTest, ReorderNode) {
855 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
857 Id node1_id = BuildNodeId(2, 1);
858 Id node2_id = BuildNodeId(2, 2);
859 Id node3_id = BuildNodeId(2, 3);
860 Id node4_id = BuildNodeId(1, 4); // Peer to 1,1
861 Id node5_id = BuildNodeId(1, 5); // Peer to 1,1
862 Id node6_id = BuildNodeId(2, 6); // Child of 1,2.
863 Id node7_id = BuildNodeId(2, 7); // Unparented.
864 Id node8_id = BuildNodeId(2, 8); // Unparented.
865 ASSERT_TRUE(connection2_->CreateNode(node1_id));
866 ASSERT_TRUE(connection2_->CreateNode(node2_id));
867 ASSERT_TRUE(connection2_->CreateNode(node3_id));
868 ASSERT_TRUE(connection_->CreateNode(node4_id));
869 ASSERT_TRUE(connection_->CreateNode(node5_id));
870 ASSERT_TRUE(connection2_->CreateNode(node6_id));
871 ASSERT_TRUE(connection2_->CreateNode(node7_id));
872 ASSERT_TRUE(connection2_->CreateNode(node8_id));
873 ASSERT_TRUE(connection2_->AddNode(node1_id, node2_id));
874 ASSERT_TRUE(connection2_->AddNode(node2_id, node6_id));
875 ASSERT_TRUE(connection2_->AddNode(node1_id, node3_id));
876 ASSERT_TRUE(connection_->AddNode(
877 NodeIdToTransportId(RootNodeId()), node4_id));
878 ASSERT_TRUE(connection_->AddNode(
879 NodeIdToTransportId(RootNodeId()), node5_id));
881 ASSERT_TRUE(connection_->AddNode(
882 NodeIdToTransportId(RootNodeId()), node1_id));
885 ASSERT_TRUE(
886 connection2_->ReorderNode(node2_id, node3_id, ORDER_DIRECTION_ABOVE));
888 connection_->DoRunLoopUntilChangesCount(1);
889 const Changes changes(ChangesToDescription1(connection_->changes()));
890 ASSERT_EQ(1u, changes.size());
891 EXPECT_EQ("Reordered node=2,2 relative=2,3 direction=above",
892 changes[0]);
896 ASSERT_TRUE(connection2_->ReorderNode(
897 node2_id, node3_id, ORDER_DIRECTION_BELOW));
899 connection_->DoRunLoopUntilChangesCount(1);
900 const Changes changes(ChangesToDescription1(connection_->changes()));
901 ASSERT_EQ(1u, changes.size());
902 EXPECT_EQ("Reordered node=2,2 relative=2,3 direction=below",
903 changes[0]);
906 // node2 is already below node3.
907 EXPECT_FALSE(
908 connection2_->ReorderNode(node2_id, node3_id, ORDER_DIRECTION_BELOW));
910 // node4 & 5 are unknown to connection2_.
911 EXPECT_FALSE(connection2_->ReorderNode(
912 node4_id, node5_id, ORDER_DIRECTION_ABOVE));
914 // node6 & node3 have different parents.
915 EXPECT_FALSE(
916 connection_->ReorderNode(node3_id, node6_id, ORDER_DIRECTION_ABOVE));
918 // Non-existent node-ids
919 EXPECT_FALSE(connection_->ReorderNode(
920 BuildNodeId(1, 27), BuildNodeId(1, 28), ORDER_DIRECTION_ABOVE));
922 // node7 & node8 are un-parented.
923 EXPECT_FALSE(
924 connection_->ReorderNode(node7_id, node8_id, ORDER_DIRECTION_ABOVE));
927 // Verifies DeleteNode works.
928 TEST_F(ViewManagerTest, DeleteNode) {
929 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
930 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
932 // Make 2 a child of 1.
934 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
935 connection_->DoRunLoopUntilChangesCount(1);
936 const Changes changes(ChangesToDescription1(connection_->changes()));
937 ASSERT_EQ(1u, changes.size());
938 EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null",
939 changes[0]);
942 // Delete 2.
944 ASSERT_TRUE(connection2_->DeleteNode(BuildNodeId(2, 2)));
945 EXPECT_TRUE(connection2_->changes().empty());
947 connection_->DoRunLoopUntilChangesCount(1);
948 const Changes changes(ChangesToDescription1(connection_->changes()));
949 ASSERT_EQ(1u, changes.size());
950 EXPECT_EQ("NodeDeleted node=2,2", changes[0]);
954 // Verifies DeleteNode isn't allowed from a separate connection.
955 TEST_F(ViewManagerTest, DeleteNodeFromAnotherConnectionDisallowed) {
956 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
957 EXPECT_FALSE(connection2_->DeleteNode(BuildNodeId(1, 1)));
960 // Verifies if a node was deleted and then reused that other clients are
961 // properly notified.
962 TEST_F(ViewManagerTest, ReuseDeletedNodeId) {
963 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
964 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
966 // Add 2 to 1.
968 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
970 connection_->DoRunLoopUntilChangesCount(1);
971 const Changes changes(ChangesToDescription1(connection_->changes()));
972 EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null",
973 changes[0]);
974 EXPECT_EQ("[node=2,2 parent=1,1]",
975 ChangeNodeDescription(connection_->changes()));
978 // Delete 2.
980 ASSERT_TRUE(connection2_->DeleteNode(BuildNodeId(2, 2)));
982 connection_->DoRunLoopUntilChangesCount(1);
983 const Changes changes(ChangesToDescription1(connection_->changes()));
984 ASSERT_EQ(1u, changes.size());
985 EXPECT_EQ("NodeDeleted node=2,2", changes[0]);
988 // Create 2 again, and add it back to 1. Should get the same notification.
989 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
991 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
993 connection_->DoRunLoopUntilChangesCount(1);
994 const Changes changes(ChangesToDescription1(connection_->changes()));
995 EXPECT_EQ("HierarchyChanged node=2,2 new_parent=1,1 old_parent=null",
996 changes[0]);
997 EXPECT_EQ("[node=2,2 parent=1,1]",
998 ChangeNodeDescription(connection_->changes()));
1002 // Assertions for GetNodeTree.
1003 TEST_F(ViewManagerTest, GetNodeTree) {
1004 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1006 // Create 11 in first connection and make it a child of 1.
1007 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 11)));
1008 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
1009 ASSERT_TRUE(connection_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 11)));
1011 // Create two nodes in second connection, 2 and 3, both children of 1.
1012 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 2)));
1013 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 3)));
1014 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 2)));
1015 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(2, 3)));
1017 // Verifies GetNodeTree() on the root. The root connection sees all.
1019 std::vector<TestNode> nodes;
1020 connection_->GetNodeTree(BuildNodeId(0, 1), &nodes);
1021 ASSERT_EQ(5u, nodes.size());
1022 EXPECT_EQ("node=0,1 parent=null", nodes[0].ToString());
1023 EXPECT_EQ("node=1,1 parent=0,1", nodes[1].ToString());
1024 EXPECT_EQ("node=1,11 parent=1,1", nodes[2].ToString());
1025 EXPECT_EQ("node=2,2 parent=1,1", nodes[3].ToString());
1026 EXPECT_EQ("node=2,3 parent=1,1", nodes[4].ToString());
1029 // Verifies GetNodeTree() on the node 1,1. This does not include any children
1030 // as they are not from this connection.
1032 std::vector<TestNode> nodes;
1033 connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes);
1034 ASSERT_EQ(1u, nodes.size());
1035 EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString());
1038 // Connection 2 shouldn't be able to get the root tree.
1040 std::vector<TestNode> nodes;
1041 connection2_->GetNodeTree(BuildNodeId(0, 1), &nodes);
1042 ASSERT_EQ(0u, nodes.size());
1046 TEST_F(ViewManagerTest, SetNodeBounds) {
1047 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1048 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
1050 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1052 ASSERT_TRUE(connection_->SetNodeBounds(BuildNodeId(1, 1),
1053 gfx::Rect(0, 0, 100, 100)));
1055 connection2_->DoRunLoopUntilChangesCount(1);
1056 const Changes changes(ChangesToDescription1(connection2_->changes()));
1057 ASSERT_EQ(1u, changes.size());
1058 EXPECT_EQ("BoundsChanged node=1,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100",
1059 changes[0]);
1061 // Should not be possible to change the bounds of a node created by another
1062 // connection.
1063 ASSERT_FALSE(connection2_->SetNodeBounds(BuildNodeId(1, 1),
1064 gfx::Rect(0, 0, 0, 0)));
1067 // Verify AddNode fails when trying to manipulate nodes in other roots.
1068 TEST_F(ViewManagerTest, CantMoveNodesFromOtherRoot) {
1069 // Create 1 and 2 in the first connection.
1070 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1071 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
1073 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1075 // Try to move 2 to be a child of 1 from connection 2. This should fail as 2
1076 // should not be able to access 1.
1077 ASSERT_FALSE(connection2_->AddNode(BuildNodeId(1, 1), BuildNodeId(1, 2)));
1079 // Try to reparent 1 to the root. A connection is not allowed to reparent its
1080 // roots.
1081 ASSERT_FALSE(connection2_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
1084 // Verify RemoveNodeFromParent fails for nodes that are descendants of the
1085 // roots.
1086 TEST_F(ViewManagerTest, CantRemoveNodesInOtherRoots) {
1087 // Create 1 and 2 in the first connection and parent both to the root.
1088 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1089 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
1091 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
1092 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 2)));
1094 // Establish the second connection and give it the root 1.
1095 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1097 // Connection 2 should not be able to remove node 2 or 1 from its parent.
1098 ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(1, 2)));
1099 ASSERT_FALSE(connection2_->RemoveNodeFromParent(BuildNodeId(1, 1)));
1101 // Create nodes 10 and 11 in 2.
1102 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 10)));
1103 ASSERT_TRUE(connection2_->CreateNode(BuildNodeId(2, 11)));
1105 // Parent 11 to 10.
1106 ASSERT_TRUE(connection2_->AddNode(BuildNodeId(2, 10), BuildNodeId(2, 11)));
1107 // Remove 11 from 10.
1108 ASSERT_TRUE(connection2_->RemoveNodeFromParent( BuildNodeId(2, 11)));
1110 // Verify nothing was actually removed.
1112 std::vector<TestNode> nodes;
1113 connection_->GetNodeTree(BuildNodeId(0, 1), &nodes);
1114 ASSERT_EQ(3u, nodes.size());
1115 EXPECT_EQ("node=0,1 parent=null", nodes[0].ToString());
1116 EXPECT_EQ("node=1,1 parent=0,1", nodes[1].ToString());
1117 EXPECT_EQ("node=1,2 parent=0,1", nodes[2].ToString());
1121 // Verify GetNodeTree fails for nodes that are not descendants of the roots.
1122 TEST_F(ViewManagerTest, CantGetNodeTreeOfOtherRoots) {
1123 // Create 1 and 2 in the first connection and parent both to the root.
1124 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1125 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
1127 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 1)));
1128 ASSERT_TRUE(connection_->AddNode(BuildNodeId(0, 1), BuildNodeId(1, 2)));
1130 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1132 std::vector<TestNode> nodes;
1134 // Should get nothing for the root.
1135 connection2_->GetNodeTree(BuildNodeId(0, 1), &nodes);
1136 ASSERT_TRUE(nodes.empty());
1138 // Should get nothing for node 2.
1139 connection2_->GetNodeTree(BuildNodeId(1, 2), &nodes);
1140 ASSERT_TRUE(nodes.empty());
1142 // Should get node 1 if asked for.
1143 connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes);
1144 ASSERT_EQ(1u, nodes.size());
1145 EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString());
1148 TEST_F(ViewManagerTest, ConnectTwice) {
1149 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1150 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 2)));
1152 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1154 // Try to connect again to 1,1, this should fail as already connected to that
1155 // root.
1156 ASSERT_FALSE(connection_->Embed(BuildNodeId(1, 1), kTestServiceURL));
1158 // Connecting to 1,2 should succeed and end up in connection2.
1160 ASSERT_TRUE(connection_->Embed(BuildNodeId(1, 2), kTestServiceURL));
1161 connection2_->DoRunLoopUntilChangesCount(1);
1162 const Changes changes(ChangesToDescription1(connection2_->changes()));
1163 ASSERT_EQ(1u, changes.size());
1164 EXPECT_EQ("OnEmbed creator=mojo:test_url", changes[0]);
1165 EXPECT_EQ("[node=1,2 parent=null]",
1166 ChangeNodeDescription(connection2_->changes()));
1170 TEST_F(ViewManagerTest, OnNodeInput) {
1171 ASSERT_TRUE(connection_->CreateNode(BuildNodeId(1, 1)));
1172 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1174 // Dispatch an event to the node and verify its received.
1176 EventPtr event(Event::New());
1177 event->action = static_cast<EventType>(1);
1178 connection_->view_manager()->DispatchOnViewInputEvent(
1179 BuildNodeId(1, 1),
1180 event.Pass());
1181 connection2_->DoRunLoopUntilChangesCount(1);
1182 const Changes changes(ChangesToDescription1(connection2_->changes()));
1183 ASSERT_EQ(1u, changes.size());
1184 EXPECT_EQ("InputEvent node=1,1 event_action=1", changes[0]);
1188 TEST_F(ViewManagerTest, EmbedWithSameNodeId) {
1189 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1191 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection_,
1192 BuildNodeId(1, 1)));
1194 // Connection2 should have been told the node was deleted.
1196 connection2_->DoRunLoopUntilChangesCount(1);
1197 const Changes changes(ChangesToDescription1(connection2_->changes()));
1198 ASSERT_EQ(1u, changes.size());
1199 EXPECT_EQ("NodeDeleted node=1,1", changes[0]);
1202 // Connection2 has no root. Verify it can't see node 1,1 anymore.
1204 std::vector<TestNode> nodes;
1205 connection2_->GetNodeTree(BuildNodeId(1, 1), &nodes);
1206 EXPECT_TRUE(nodes.empty());
1210 TEST_F(ViewManagerTest, EmbedWithSameNodeId2) {
1211 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1213 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(connection_,
1214 BuildNodeId(1, 1)));
1216 // Connection2 should have been told the node was deleted.
1217 connection2_->DoRunLoopUntilChangesCount(1);
1218 connection2_->ClearChanges();
1220 // Create a node in the third connection and parent it to the root.
1221 ASSERT_TRUE(connection3_->CreateNode(BuildNodeId(3, 1)));
1222 ASSERT_TRUE(connection3_->AddNode(BuildNodeId(1, 1), BuildNodeId(3, 1)));
1224 // Connection 1 should have been told about the add (it owns the node).
1226 connection_->DoRunLoopUntilChangesCount(1);
1227 const Changes changes(ChangesToDescription1(connection_->changes()));
1228 ASSERT_EQ(1u, changes.size());
1229 EXPECT_EQ("HierarchyChanged node=3,1 new_parent=1,1 old_parent=null",
1230 changes[0]);
1233 // Embed 1,1 back in connection 2.
1235 // 2 should be told about the new embed.
1236 ASSERT_TRUE(connection_->Embed(BuildNodeId(1, 1), kTestServiceURL));
1237 connection2_->DoRunLoopUntilChangesCount(1);
1238 const std::vector<Change>& changes(connection2_->changes());
1239 ASSERT_EQ(1u, changes.size());
1240 EXPECT_EQ("OnEmbed creator=mojo:test_url",
1241 ChangesToDescription1(changes)[0]);
1242 EXPECT_EQ("[node=1,1 parent=null]",
1243 ChangeNodeDescription(changes));
1245 // And 3 should get a delete.
1246 connection3_->DoRunLoopUntilChangesCount(1);
1247 ASSERT_EQ(1u, connection3_->changes().size());
1248 EXPECT_EQ("NodeDeleted node=1,1",
1249 ChangesToDescription1(connection3_->changes())[0]);
1252 // Connection3_ has no root. Verify it can't see node 1,1 anymore.
1254 std::vector<TestNode> nodes;
1255 connection3_->GetNodeTree(BuildNodeId(1, 1), &nodes);
1256 EXPECT_TRUE(nodes.empty());
1259 // Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as
1260 // connection3_ can no longer see 1,1.
1262 std::vector<TestNode> nodes;
1263 connection_->GetNodeTree(BuildNodeId(1, 1), &nodes);
1264 ASSERT_EQ(1u, nodes.size());
1265 EXPECT_EQ("node=1,1 parent=null", nodes[0].ToString());
1268 // Verify connection3_ can still see the node it created 3,1.
1270 std::vector<TestNode> nodes;
1271 connection3_->GetNodeTree(BuildNodeId(3, 1), &nodes);
1272 ASSERT_EQ(1u, nodes.size());
1273 EXPECT_EQ("node=3,1 parent=null", nodes[0].ToString());
1277 // TODO(sky): add coverage of test that destroys connections and ensures other
1278 // connections get deletion notification.
1280 // TODO(sky): need to better track changes to initial connection. For example,
1281 // that SetBounsdNodes/AddNode and the like don't result in messages to the
1282 // originating connection.
1284 } // namespace service
1285 } // namespace mojo