cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / view_manager / view_tree_apptest.cc
blob287d3b2f729963d7ebe908e9f3d19c2dcfc9db5f
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 "base/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "components/view_manager/ids.h"
10 #include "components/view_manager/public/interfaces/view_tree.mojom.h"
11 #include "components/view_manager/public/interfaces/view_tree_host.mojom.h"
12 #include "components/view_manager/test_change_tracker.h"
13 #include "mojo/application/public/cpp/application_delegate.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/application/public/cpp/application_test_base.h"
17 using mojo::ApplicationConnection;
18 using mojo::ApplicationDelegate;
19 using mojo::Array;
20 using mojo::Callback;
21 using mojo::ConnectionSpecificId;
22 using mojo::ERROR_CODE_NONE;
23 using mojo::ErrorCode;
24 using mojo::EventPtr;
25 using mojo::Id;
26 using mojo::InterfaceRequest;
27 using mojo::ORDER_DIRECTION_ABOVE;
28 using mojo::ORDER_DIRECTION_BELOW;
29 using mojo::OrderDirection;
30 using mojo::RectPtr;
31 using mojo::ServiceProvider;
32 using mojo::ServiceProviderPtr;
33 using mojo::String;
34 using mojo::ViewDataPtr;
35 using mojo::ViewTree;
36 using mojo::ViewTreeClient;
37 using mojo::ViewportMetricsPtr;
39 namespace view_manager {
41 // Creates an id used for transport from the specified parameters.
42 Id BuildViewId(ConnectionSpecificId connection_id,
43 ConnectionSpecificId view_id) {
44 return (connection_id << 16) | view_id;
47 // Callback function from ViewTree functions. ----------------------------------
49 void BoolResultCallback(base::RunLoop* run_loop,
50 bool* result_cache,
51 bool result) {
52 *result_cache = result;
53 run_loop->Quit();
56 void ErrorCodeResultCallback(base::RunLoop* run_loop,
57 ErrorCode* result_cache,
58 ErrorCode result) {
59 *result_cache = result;
60 run_loop->Quit();
63 void ViewTreeResultCallback(base::RunLoop* run_loop,
64 std::vector<TestView>* views,
65 Array<ViewDataPtr> results) {
66 ViewDatasToTestViews(results, views);
67 run_loop->Quit();
70 // -----------------------------------------------------------------------------
72 bool EmbedUrl(mojo::ApplicationImpl* app,
73 ViewTree* vm,
74 const String& url,
75 Id root_id) {
76 bool result = false;
77 base::RunLoop run_loop;
79 mojo::URLRequestPtr request(mojo::URLRequest::New());
80 request->url = mojo::String::From(url);
81 scoped_ptr<ApplicationConnection> connection =
82 app->ConnectToApplication(request.Pass());
83 mojo::ViewTreeClientPtr client;
84 connection->ConnectToService(&client);
85 vm->Embed(root_id, client.Pass(),
86 base::Bind(&BoolResultCallback, &run_loop, &result));
88 run_loop.Run();
89 return result;
92 bool Embed(ViewTree* vm, Id root_id, mojo::ViewTreeClientPtr client) {
93 bool result = false;
94 base::RunLoop run_loop;
96 vm->Embed(root_id, client.Pass(),
97 base::Bind(&BoolResultCallback, &run_loop, &result));
99 run_loop.Run();
100 return result;
103 ErrorCode CreateViewWithErrorCode(ViewTree* vm, Id view_id) {
104 ErrorCode result = ERROR_CODE_NONE;
105 base::RunLoop run_loop;
106 vm->CreateView(view_id,
107 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
108 run_loop.Run();
109 return result;
112 bool AddView(ViewTree* vm, Id parent, Id child) {
113 bool result = false;
114 base::RunLoop run_loop;
115 vm->AddView(parent, child,
116 base::Bind(&BoolResultCallback, &run_loop, &result));
117 run_loop.Run();
118 return result;
121 bool RemoveViewFromParent(ViewTree* vm, Id view_id) {
122 bool result = false;
123 base::RunLoop run_loop;
124 vm->RemoveViewFromParent(view_id,
125 base::Bind(&BoolResultCallback, &run_loop, &result));
126 run_loop.Run();
127 return result;
130 bool ReorderView(ViewTree* vm,
131 Id view_id,
132 Id relative_view_id,
133 OrderDirection direction) {
134 bool result = false;
135 base::RunLoop run_loop;
136 vm->ReorderView(view_id, relative_view_id, direction,
137 base::Bind(&BoolResultCallback, &run_loop, &result));
138 run_loop.Run();
139 return result;
142 void GetViewTree(ViewTree* vm,
143 Id view_id,
144 std::vector<TestView>* views) {
145 base::RunLoop run_loop;
146 vm->GetViewTree(view_id,
147 base::Bind(&ViewTreeResultCallback, &run_loop, views));
148 run_loop.Run();
151 bool DeleteView(ViewTree* vm, Id view_id) {
152 base::RunLoop run_loop;
153 bool result = false;
154 vm->DeleteView(view_id, base::Bind(&BoolResultCallback, &run_loop, &result));
155 run_loop.Run();
156 return result;
159 bool SetViewBounds(ViewTree* vm,
160 Id view_id,
161 int x,
162 int y,
163 int w,
164 int h) {
165 base::RunLoop run_loop;
166 bool result = false;
167 RectPtr rect(mojo::Rect::New());
168 rect->x = x;
169 rect->y = y;
170 rect->width = w;
171 rect->height = h;
172 vm->SetViewBounds(view_id, rect.Pass(),
173 base::Bind(&BoolResultCallback, &run_loop, &result));
174 run_loop.Run();
175 return result;
178 bool SetViewVisibility(ViewTree* vm, Id view_id, bool visible) {
179 base::RunLoop run_loop;
180 bool result = false;
181 vm->SetViewVisibility(view_id, visible,
182 base::Bind(&BoolResultCallback, &run_loop, &result));
183 run_loop.Run();
184 return result;
187 bool SetViewProperty(ViewTree* vm,
188 Id view_id,
189 const std::string& name,
190 const std::vector<uint8_t>* data) {
191 base::RunLoop run_loop;
192 bool result = false;
193 Array<uint8_t> mojo_data;
194 if (data)
195 mojo_data = Array<uint8_t>::From(*data);
196 vm->SetViewProperty(view_id, name, mojo_data.Pass(),
197 base::Bind(&BoolResultCallback, &run_loop, &result));
198 run_loop.Run();
199 return result;
202 // Utility functions -----------------------------------------------------------
204 // Waits for all messages to be received by |vm|. This is done by attempting to
205 // create a bogus view. When we get the response we know all messages have been
206 // processed.
207 bool WaitForAllMessages(ViewTree* vm) {
208 ErrorCode result = ERROR_CODE_NONE;
209 base::RunLoop run_loop;
210 vm->CreateView(ViewIdToTransportId(InvalidViewId()),
211 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
212 run_loop.Run();
213 return result != ERROR_CODE_NONE;
216 const Id kNullParentId = 0;
217 std::string IdToString(Id id) {
218 return (id == kNullParentId)
219 ? "null"
220 : base::StringPrintf("%d,%d", mojo::HiWord(id), mojo::LoWord(id));
223 std::string ViewParentToString(Id view, Id parent) {
224 return base::StringPrintf("view=%s parent=%s", IdToString(view).c_str(),
225 IdToString(parent).c_str());
228 // -----------------------------------------------------------------------------
230 // A ViewTreeClient implementation that logs all changes to a tracker.
231 class ViewTreeClientImpl : public mojo::ViewTreeClient,
232 public TestChangeTracker::Delegate {
233 public:
234 explicit ViewTreeClientImpl(mojo::ApplicationImpl* app)
235 : binding_(this), app_(app), connection_id_(0), root_view_id_(0) {
236 tracker_.set_delegate(this);
239 void Bind(mojo::InterfaceRequest<mojo::ViewTreeClient> request) {
240 binding_.Bind(request.Pass());
243 mojo::ViewTree* tree() { return tree_.get(); }
244 TestChangeTracker* tracker() { return &tracker_; }
246 // Runs a nested MessageLoop until |count| changes (calls to
247 // ViewTreeClient functions) have been received.
248 void WaitForChangeCount(size_t count) {
249 if (count == tracker_.changes()->size())
250 return;
252 ASSERT_TRUE(wait_state_.get() == nullptr);
253 wait_state_.reset(new WaitState);
254 wait_state_->change_count = count;
255 wait_state_->run_loop.Run();
256 wait_state_.reset();
259 // Runs a nested MessageLoop until OnEmbed() has been encountered.
260 void WaitForOnEmbed() {
261 if (tree_)
262 return;
263 embed_run_loop_.reset(new base::RunLoop);
264 embed_run_loop_->Run();
265 embed_run_loop_.reset();
268 bool WaitForIncomingMethodCall() {
269 return binding_.WaitForIncomingMethodCall();
272 Id CreateView(ConnectionSpecificId view_id) {
273 ErrorCode result = ERROR_CODE_NONE;
274 base::RunLoop run_loop;
275 Id id = BuildViewId(connection_id_, view_id);
276 tree()->CreateView(
277 id, base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
278 run_loop.Run();
279 return result == ERROR_CODE_NONE ? id : 0;
282 void set_root_view(Id root_view_id) { root_view_id_ = root_view_id; }
284 private:
285 // Used when running a nested MessageLoop.
286 struct WaitState {
287 WaitState() : change_count(0) {}
289 // Number of changes waiting for.
290 size_t change_count;
291 base::RunLoop run_loop;
294 // TestChangeTracker::Delegate:
295 void OnChangeAdded() override {
296 if (wait_state_.get() &&
297 wait_state_->change_count == tracker_.changes()->size()) {
298 wait_state_->run_loop.Quit();
302 // ViewTreeClient:
303 void OnEmbed(ConnectionSpecificId connection_id,
304 ViewDataPtr root,
305 mojo::ViewTreePtr tree,
306 mojo::Id focused_view_id,
307 uint32_t access_policy) override {
308 // TODO(sky): add coverage of |focused_view_id|.
309 tree_ = tree.Pass();
310 connection_id_ = connection_id;
311 tracker()->OnEmbed(connection_id, root.Pass());
312 if (embed_run_loop_)
313 embed_run_loop_->Quit();
315 void OnEmbeddedAppDisconnected(Id view_id) override {
316 tracker()->OnEmbeddedAppDisconnected(view_id);
318 void OnUnembed() override { tracker()->OnUnembed(); }
319 void OnViewBoundsChanged(Id view_id,
320 RectPtr old_bounds,
321 RectPtr new_bounds) override {
322 // The bounds of the root may change during startup on Android at random
323 // times. As this doesn't matter, and shouldn't impact test exepctations,
324 // it is ignored.
325 if (view_id == root_view_id_)
326 return;
327 tracker()->OnViewBoundsChanged(view_id, old_bounds.Pass(),
328 new_bounds.Pass());
330 void OnViewViewportMetricsChanged(ViewportMetricsPtr old_metrics,
331 ViewportMetricsPtr new_metrics) override {
332 // Don't track the metrics as they are available at an indeterministic time
333 // on Android.
335 void OnViewHierarchyChanged(Id view,
336 Id new_parent,
337 Id old_parent,
338 Array<ViewDataPtr> views) override {
339 tracker()->OnViewHierarchyChanged(view, new_parent, old_parent,
340 views.Pass());
342 void OnViewReordered(Id view_id,
343 Id relative_view_id,
344 OrderDirection direction) override {
345 tracker()->OnViewReordered(view_id, relative_view_id, direction);
347 void OnViewDeleted(Id view) override { tracker()->OnViewDeleted(view); }
348 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
349 tracker()->OnViewVisibilityChanged(view, visible);
351 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
352 tracker()->OnViewDrawnStateChanged(view, drawn);
354 void OnViewInputEvent(Id view_id,
355 EventPtr event,
356 const Callback<void()>& callback) override {
357 tracker()->OnViewInputEvent(view_id, event.Pass());
358 callback.Run();
360 void OnViewSharedPropertyChanged(uint32_t view,
361 const String& name,
362 Array<uint8_t> new_data) override {
363 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
365 // TODO(sky): add testing coverage.
366 void OnViewFocused(uint32_t focused_view_id) override {}
368 TestChangeTracker tracker_;
370 mojo::ViewTreePtr tree_;
372 // If non-null we're waiting for OnEmbed() using this RunLoop.
373 scoped_ptr<base::RunLoop> embed_run_loop_;
375 // If non-null we're waiting for a certain number of change notifications to
376 // be encountered.
377 scoped_ptr<WaitState> wait_state_;
379 mojo::Binding<ViewTreeClient> binding_;
380 mojo::ApplicationImpl* app_;
381 Id connection_id_;
382 Id root_view_id_;
384 DISALLOW_COPY_AND_ASSIGN(ViewTreeClientImpl);
387 // -----------------------------------------------------------------------------
389 // InterfaceFactory for vending ViewTreeClientImpls.
390 class ViewTreeClientFactory : public mojo::InterfaceFactory<ViewTreeClient> {
391 public:
392 explicit ViewTreeClientFactory(mojo::ApplicationImpl* app) : app_(app) {}
393 ~ViewTreeClientFactory() override {}
395 // Runs a nested MessageLoop until a new instance has been created.
396 scoped_ptr<ViewTreeClientImpl> WaitForInstance() {
397 if (!client_impl_.get()) {
398 DCHECK(!run_loop_.get());
399 run_loop_.reset(new base::RunLoop);
400 run_loop_->Run();
401 run_loop_.reset();
403 return client_impl_.Pass();
406 private:
407 // InterfaceFactory<ViewTreeClient>:
408 void Create(ApplicationConnection* connection,
409 InterfaceRequest<ViewTreeClient> request) override {
410 client_impl_.reset(new ViewTreeClientImpl(app_));
411 client_impl_->Bind(request.Pass());
412 if (run_loop_.get())
413 run_loop_->Quit();
416 mojo::ApplicationImpl* app_;
417 scoped_ptr<ViewTreeClientImpl> client_impl_;
418 scoped_ptr<base::RunLoop> run_loop_;
420 DISALLOW_COPY_AND_ASSIGN(ViewTreeClientFactory);
423 class ViewTreeAppTest : public mojo::test::ApplicationTestBase,
424 public ApplicationDelegate {
425 public:
426 ViewTreeAppTest()
427 : connection_id_1_(0), connection_id_2_(0), root_view_id_(0) {}
428 ~ViewTreeAppTest() override {}
430 protected:
431 // Returns the changes from the various connections.
432 std::vector<Change>* changes1() { return vm_client1_->tracker()->changes(); }
433 std::vector<Change>* changes2() { return vm_client2_->tracker()->changes(); }
434 std::vector<Change>* changes3() { return vm_client3_->tracker()->changes(); }
436 // Various connections. |vm1()|, being the first connection, has special
437 // permissions (it's treated as the window manager).
438 ViewTree* vm1() { return vm_client1_->tree(); }
439 ViewTree* vm2() { return vm_client2_->tree(); }
440 ViewTree* vm3() { return vm_client3_->tree(); }
442 ViewTreeClientImpl* vm_client1() { return vm_client1_.get(); }
443 ViewTreeClientImpl* vm_client2() { return vm_client2_.get(); }
444 ViewTreeClientImpl* vm_client3() { return vm_client3_.get(); }
446 Id root_view_id() const { return root_view_id_; }
448 int connection_id_1() const { return connection_id_1_; }
449 int connection_id_2() const { return connection_id_2_; }
451 void EstablishSecondConnectionWithRoot(Id root_id) {
452 ASSERT_TRUE(vm_client2_.get() == nullptr);
453 vm_client2_ =
454 EstablishConnectionViaEmbed(vm1(), root_id, &connection_id_2_);
455 ASSERT_GT(connection_id_2_, 0);
456 ASSERT_TRUE(vm_client2_.get() != nullptr);
457 vm_client2_->set_root_view(root_view_id_);
460 void EstablishSecondConnection(bool create_initial_view) {
461 Id view_1_1 = 0;
462 if (create_initial_view) {
463 view_1_1 = vm_client1()->CreateView(1);
464 ASSERT_TRUE(view_1_1);
466 ASSERT_NO_FATAL_FAILURE(
467 EstablishSecondConnectionWithRoot(BuildViewId(connection_id_1(), 1)));
469 if (create_initial_view) {
470 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
471 ChangeViewDescription(*changes2()));
475 void EstablishThirdConnection(ViewTree* owner, Id root_id) {
476 ASSERT_TRUE(vm_client3_.get() == nullptr);
477 vm_client3_ = EstablishConnectionViaEmbed(owner, root_id, nullptr);
478 ASSERT_TRUE(vm_client3_.get() != nullptr);
479 vm_client3_->set_root_view(root_view_id_);
482 // Establishes a new connection by way of Embed() on the specified
483 // ViewTree.
484 scoped_ptr<ViewTreeClientImpl> EstablishConnectionViaEmbed(
485 ViewTree* owner,
486 Id root_id,
487 int* connection_id) {
488 if (!EmbedUrl(application_impl(), owner, application_impl()->url(),
489 root_id)) {
490 ADD_FAILURE() << "Embed() failed";
491 return nullptr;
493 scoped_ptr<ViewTreeClientImpl> client =
494 client_factory_->WaitForInstance();
495 if (!client.get()) {
496 ADD_FAILURE() << "WaitForInstance failed";
497 return nullptr;
499 client->WaitForOnEmbed();
501 EXPECT_EQ("OnEmbed",
502 SingleChangeToDescription(*client->tracker()->changes()));
503 if (connection_id)
504 *connection_id = (*client->tracker()->changes())[0].connection_id;
505 return client.Pass();
508 // ApplicationTestBase:
509 ApplicationDelegate* GetApplicationDelegate() override { return this; }
510 void SetUp() override {
511 ApplicationTestBase::SetUp();
512 client_factory_.reset(new ViewTreeClientFactory(application_impl()));
513 mojo::URLRequestPtr request(mojo::URLRequest::New());
514 request->url = mojo::String::From("mojo:view_manager");
516 mojo::ViewTreeHostFactoryPtr factory;
517 application_impl()->ConnectToService(request.Pass(), &factory);
519 mojo::ViewTreeClientPtr tree_client_ptr;
520 vm_client1_.reset(new ViewTreeClientImpl(application_impl()));
521 vm_client1_->Bind(GetProxy(&tree_client_ptr));
523 factory->CreateViewTreeHost(GetProxy(&host_),
524 mojo::ViewTreeHostClientPtr(),
525 tree_client_ptr.Pass());
527 // Next we should get an embed call on the "window manager" client.
528 vm_client1_->WaitForIncomingMethodCall();
530 ASSERT_EQ(1u, changes1()->size());
531 EXPECT_EQ(CHANGE_TYPE_EMBED, (*changes1())[0].type);
532 // All these tests assume 1 for the client id. The only real assertion here
533 // is the client id is not zero, but adding this as rest of code here
534 // assumes 1.
535 ASSERT_GT((*changes1())[0].connection_id, 0);
536 connection_id_1_ = (*changes1())[0].connection_id;
537 ASSERT_FALSE((*changes1())[0].views.empty());
538 root_view_id_ = (*changes1())[0].views[0].view_id;
539 vm_client1_->set_root_view(root_view_id_);
540 changes1()->clear();
543 // ApplicationDelegate implementation.
544 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
545 connection->AddService(client_factory_.get());
546 return true;
549 scoped_ptr<ViewTreeClientImpl> vm_client1_;
550 scoped_ptr<ViewTreeClientImpl> vm_client2_;
551 scoped_ptr<ViewTreeClientImpl> vm_client3_;
553 mojo::ViewTreeHostPtr host_;
555 private:
556 scoped_ptr<ViewTreeClientFactory> client_factory_;
557 int connection_id_1_;
558 int connection_id_2_;
559 Id root_view_id_;
561 MOJO_DISALLOW_COPY_AND_ASSIGN(ViewTreeAppTest);
564 // Verifies two clients/connections get different ids.
565 TEST_F(ViewTreeAppTest, TwoClientsGetDifferentConnectionIds) {
566 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
568 ASSERT_EQ(1u, changes2()->size());
569 ASSERT_NE(connection_id_1(), connection_id_2());
572 // Verifies when Embed() is invoked any child views are removed.
573 TEST_F(ViewTreeAppTest, ViewsRemovedWhenEmbedding) {
574 // Two views 1 and 2. 2 is parented to 1.
575 Id view_1_1 = vm_client1()->CreateView(1);
576 ASSERT_TRUE(view_1_1);
577 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
579 Id view_1_2 = vm_client1()->CreateView(2);
580 ASSERT_TRUE(view_1_2);
581 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
583 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
584 ASSERT_EQ(1u, changes2()->size());
585 ASSERT_EQ(1u, (*changes2())[0].views.size());
586 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
587 ChangeViewDescription(*changes2()));
589 // Embed() removed view 2.
591 std::vector<TestView> views;
592 GetViewTree(vm1(), view_1_2, &views);
593 EXPECT_EQ(ViewParentToString(view_1_2, kNullParentId),
594 SingleViewDescription(views));
597 // vm2 should not see view 2.
599 std::vector<TestView> views;
600 GetViewTree(vm2(), view_1_1, &views);
601 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId),
602 SingleViewDescription(views));
605 std::vector<TestView> views;
606 GetViewTree(vm2(), view_1_2, &views);
607 EXPECT_TRUE(views.empty());
610 // Views 3 and 4 in connection 2.
611 Id view_2_3 = vm_client2()->CreateView(3);
612 Id view_2_4 = vm_client2()->CreateView(4);
613 ASSERT_TRUE(view_2_3);
614 ASSERT_TRUE(view_2_4);
615 ASSERT_TRUE(AddView(vm2(), view_2_3, view_2_4));
617 // Connection 3 rooted at 2.
618 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_3));
620 // View 4 should no longer have a parent.
622 std::vector<TestView> views;
623 GetViewTree(vm2(), view_2_3, &views);
624 EXPECT_EQ(ViewParentToString(view_2_3, kNullParentId),
625 SingleViewDescription(views));
627 views.clear();
628 GetViewTree(vm2(), view_2_4, &views);
629 EXPECT_EQ(ViewParentToString(view_2_4, kNullParentId),
630 SingleViewDescription(views));
633 // And view 4 should not be visible to connection 3.
635 std::vector<TestView> views;
636 GetViewTree(vm3(), view_2_3, &views);
637 EXPECT_EQ(ViewParentToString(view_2_3, kNullParentId),
638 SingleViewDescription(views));
642 // Verifies once Embed() has been invoked the parent connection can't see any
643 // children.
644 TEST_F(ViewTreeAppTest, CantAccessChildrenOfEmbeddedView) {
645 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
647 Id view_1_1 = BuildViewId(connection_id_1(), 1);
648 Id view_2_2 = vm_client2()->CreateView(2);
649 ASSERT_TRUE(view_2_2);
650 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
652 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
654 Id view_3_3 = vm_client3()->CreateView(3);
655 ASSERT_TRUE(view_3_3);
656 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
658 // Even though 3 is a child of 2 connection 2 can't see 3 as it's from a
659 // different connection.
661 std::vector<TestView> views;
662 GetViewTree(vm2(), view_2_2, &views);
663 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1),
664 SingleViewDescription(views));
667 // Connection 2 shouldn't be able to get view 3 at all.
669 std::vector<TestView> views;
670 GetViewTree(vm2(), view_3_3, &views);
671 EXPECT_TRUE(views.empty());
674 // Connection 1 should be able to see it all (its the root).
676 std::vector<TestView> views;
677 GetViewTree(vm1(), view_1_1, &views);
678 ASSERT_EQ(3u, views.size());
679 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
680 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[1].ToString());
681 EXPECT_EQ(ViewParentToString(view_3_3, view_2_2), views[2].ToString());
685 // Verifies once Embed() has been invoked the parent can't mutate the children.
686 TEST_F(ViewTreeAppTest, CantModifyChildrenOfEmbeddedView) {
687 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
689 Id view_1_1 = BuildViewId(connection_id_1(), 1);
690 Id view_2_2 = vm_client2()->CreateView(2);
691 ASSERT_TRUE(view_2_2);
692 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
694 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
696 Id view_2_3 = vm_client2()->CreateView(3);
697 ASSERT_TRUE(view_2_3);
698 // Connection 2 shouldn't be able to add anything to the view anymore.
699 ASSERT_FALSE(AddView(vm2(), view_2_2, view_2_3));
701 // Create view 3 in connection 3 and add it to view 3.
702 Id view_3_3 = vm_client3()->CreateView(3);
703 ASSERT_TRUE(view_3_3);
704 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
706 // Connection 2 shouldn't be able to remove view 3.
707 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_3_3));
710 // Verifies client gets a valid id.
711 TEST_F(ViewTreeAppTest, CreateView) {
712 Id view_1_1 = vm_client1()->CreateView(1);
713 ASSERT_TRUE(view_1_1);
714 EXPECT_TRUE(changes1()->empty());
716 // Can't create a view with the same id.
717 ASSERT_EQ(mojo::ERROR_CODE_VALUE_IN_USE,
718 CreateViewWithErrorCode(vm1(), view_1_1));
719 EXPECT_TRUE(changes1()->empty());
721 // Can't create a view with a bogus connection id.
722 EXPECT_EQ(
723 mojo::ERROR_CODE_ILLEGAL_ARGUMENT,
724 CreateViewWithErrorCode(vm1(), BuildViewId(connection_id_1() + 1, 1)));
725 EXPECT_TRUE(changes1()->empty());
728 // Verifies AddView fails when view is already in position.
729 TEST_F(ViewTreeAppTest, AddViewWithNoChange) {
730 Id view_1_2 = vm_client1()->CreateView(2);
731 Id view_1_3 = vm_client1()->CreateView(3);
732 ASSERT_TRUE(view_1_2);
733 ASSERT_TRUE(view_1_3);
735 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
737 // Make 3 a child of 2.
738 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_3));
740 // Try again, this should fail.
741 EXPECT_FALSE(AddView(vm1(), view_1_2, view_1_3));
744 // Verifies AddView fails when view is already in position.
745 TEST_F(ViewTreeAppTest, AddAncestorFails) {
746 Id view_1_2 = vm_client1()->CreateView(2);
747 Id view_1_3 = vm_client1()->CreateView(3);
748 ASSERT_TRUE(view_1_2);
749 ASSERT_TRUE(view_1_3);
751 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
753 // Make 3 a child of 2.
754 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_3));
756 // Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3.
757 EXPECT_FALSE(AddView(vm1(), view_1_3, view_1_2));
760 // Verifies adding to root sends right notifications.
761 TEST_F(ViewTreeAppTest, AddToRoot) {
762 Id view_1_21 = vm_client1()->CreateView(21);
763 Id view_1_3 = vm_client1()->CreateView(3);
764 ASSERT_TRUE(view_1_21);
765 ASSERT_TRUE(view_1_3);
767 Id view_1_1 = BuildViewId(connection_id_1(), 1);
768 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
769 changes2()->clear();
771 // Make 3 a child of 21.
772 ASSERT_TRUE(AddView(vm1(), view_1_21, view_1_3));
774 // Make 21 a child of 1.
775 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_21));
777 // Connection 2 should not be told anything (because the view is from a
778 // different connection). Create a view to ensure we got a response from
779 // the server.
780 ASSERT_TRUE(vm_client2()->CreateView(100));
781 EXPECT_TRUE(changes2()->empty());
784 // Verifies HierarchyChanged is correctly sent for various adds/removes.
785 TEST_F(ViewTreeAppTest, ViewHierarchyChangedViews) {
786 // 1,2->1,11.
787 Id view_1_2 = vm_client1()->CreateView(2);
788 ASSERT_TRUE(view_1_2);
789 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
790 Id view_1_11 = vm_client1()->CreateView(11);
791 ASSERT_TRUE(view_1_11);
792 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_11, true));
793 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_11));
795 Id view_1_1 = BuildViewId(connection_id_1(), 1);
796 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
797 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
799 ASSERT_TRUE(WaitForAllMessages(vm2()));
800 changes2()->clear();
802 // 1,1->1,2->1,11
804 // Client 2 should not get anything (1,2 is from another connection).
805 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
806 ASSERT_TRUE(WaitForAllMessages(vm2()));
807 EXPECT_TRUE(changes2()->empty());
810 // 0,1->1,1->1,2->1,11.
812 // Client 2 is now connected to the root, so it should have gotten a drawn
813 // notification.
814 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
815 vm_client2_->WaitForChangeCount(1u);
816 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=true",
817 SingleChangeToDescription(*changes2()));
820 // 1,1->1,2->1,11.
822 // Client 2 is no longer connected to the root, should get drawn state
823 // changed.
824 changes2()->clear();
825 ASSERT_TRUE(RemoveViewFromParent(vm1(), view_1_1));
826 vm_client2_->WaitForChangeCount(1);
827 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=false",
828 SingleChangeToDescription(*changes2()));
831 // 1,1->1,2->1,11->1,111.
832 Id view_1_111 = vm_client1()->CreateView(111);
833 ASSERT_TRUE(view_1_111);
834 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_111, true));
836 changes2()->clear();
837 ASSERT_TRUE(AddView(vm1(), view_1_11, view_1_111));
838 ASSERT_TRUE(WaitForAllMessages(vm2()));
839 EXPECT_TRUE(changes2()->empty());
842 // 0,1->1,1->1,2->1,11->1,111
844 changes2()->clear();
845 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
846 vm_client2_->WaitForChangeCount(1);
847 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=true",
848 SingleChangeToDescription(*changes2()));
852 TEST_F(ViewTreeAppTest, ViewHierarchyChangedAddingKnownToUnknown) {
853 // Create the following structure: root -> 1 -> 11 and 2->21 (2 has no
854 // parent).
855 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
856 Id view_1_1 = BuildViewId(connection_id_1(), 1);
858 Id view_2_11 = vm_client2()->CreateView(11);
859 Id view_2_2 = vm_client2()->CreateView(2);
860 Id view_2_21 = vm_client2()->CreateView(21);
861 ASSERT_TRUE(view_2_11);
862 ASSERT_TRUE(view_2_2);
863 ASSERT_TRUE(view_2_21);
865 // Set up the hierarchy.
866 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
867 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_11));
868 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_21));
870 // Remove 11, should result in a hierarchy change for the root.
872 changes1()->clear();
873 ASSERT_TRUE(RemoveViewFromParent(vm2(), view_2_11));
875 vm_client1_->WaitForChangeCount(1);
876 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_11) +
877 " new_parent=null old_parent=" + IdToString(view_1_1),
878 SingleChangeToDescription(*changes1()));
881 // Add 2 to 1.
883 changes1()->clear();
884 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
885 vm_client1_->WaitForChangeCount(1);
886 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
887 IdToString(view_1_1) + " old_parent=null",
888 SingleChangeToDescription(*changes1()));
889 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "],[" +
890 ViewParentToString(view_2_21, view_2_2) + "]",
891 ChangeViewDescription(*changes1()));
895 TEST_F(ViewTreeAppTest, ReorderView) {
896 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
898 Id view_2_1 = vm_client2()->CreateView(1);
899 Id view_2_2 = vm_client2()->CreateView(2);
900 Id view_2_3 = vm_client2()->CreateView(3);
901 Id view_1_4 = vm_client1()->CreateView(4); // Peer to 1,1
902 Id view_1_5 = vm_client1()->CreateView(5); // Peer to 1,1
903 Id view_2_6 = vm_client2()->CreateView(6); // Child of 1,2.
904 Id view_2_7 = vm_client2()->CreateView(7); // Unparented.
905 Id view_2_8 = vm_client2()->CreateView(8); // Unparented.
906 ASSERT_TRUE(view_2_1);
907 ASSERT_TRUE(view_2_2);
908 ASSERT_TRUE(view_2_3);
909 ASSERT_TRUE(view_1_4);
910 ASSERT_TRUE(view_1_5);
911 ASSERT_TRUE(view_2_6);
912 ASSERT_TRUE(view_2_7);
913 ASSERT_TRUE(view_2_8);
915 ASSERT_TRUE(AddView(vm2(), view_2_1, view_2_2));
916 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_6));
917 ASSERT_TRUE(AddView(vm2(), view_2_1, view_2_3));
918 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_4));
919 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_5));
920 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_2_1));
923 changes1()->clear();
924 ASSERT_TRUE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_ABOVE));
926 vm_client1_->WaitForChangeCount(1);
927 EXPECT_EQ("Reordered view=" + IdToString(view_2_2) + " relative=" +
928 IdToString(view_2_3) + " direction=above",
929 SingleChangeToDescription(*changes1()));
933 changes1()->clear();
934 ASSERT_TRUE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_BELOW));
936 vm_client1_->WaitForChangeCount(1);
937 EXPECT_EQ("Reordered view=" + IdToString(view_2_2) + " relative=" +
938 IdToString(view_2_3) + " direction=below",
939 SingleChangeToDescription(*changes1()));
942 // view2 is already below view3.
943 EXPECT_FALSE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_BELOW));
945 // view4 & 5 are unknown to connection2_.
946 EXPECT_FALSE(ReorderView(vm2(), view_1_4, view_1_5, ORDER_DIRECTION_ABOVE));
948 // view6 & view3 have different parents.
949 EXPECT_FALSE(ReorderView(vm1(), view_2_3, view_2_6, ORDER_DIRECTION_ABOVE));
951 // Non-existent view-ids
952 EXPECT_FALSE(ReorderView(vm1(), BuildViewId(connection_id_1(), 27),
953 BuildViewId(connection_id_1(), 28),
954 ORDER_DIRECTION_ABOVE));
956 // view7 & view8 are un-parented.
957 EXPECT_FALSE(ReorderView(vm1(), view_2_7, view_2_8, ORDER_DIRECTION_ABOVE));
960 // Verifies DeleteView works.
961 TEST_F(ViewTreeAppTest, DeleteView) {
962 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
963 Id view_1_1 = BuildViewId(connection_id_1(), 1);
964 Id view_2_2 = vm_client2()->CreateView(2);
965 ASSERT_TRUE(view_2_2);
967 // Make 2 a child of 1.
969 changes1()->clear();
970 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
971 vm_client1_->WaitForChangeCount(1);
972 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
973 IdToString(view_1_1) + " old_parent=null",
974 SingleChangeToDescription(*changes1()));
977 // Delete 2.
979 changes1()->clear();
980 changes2()->clear();
981 ASSERT_TRUE(DeleteView(vm2(), view_2_2));
982 EXPECT_TRUE(changes2()->empty());
984 vm_client1_->WaitForChangeCount(1);
985 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_2),
986 SingleChangeToDescription(*changes1()));
990 // Verifies DeleteView isn't allowed from a separate connection.
991 TEST_F(ViewTreeAppTest, DeleteViewFromAnotherConnectionDisallowed) {
992 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
993 EXPECT_FALSE(DeleteView(vm2(), BuildViewId(connection_id_1(), 1)));
996 // Verifies if a view was deleted and then reused that other clients are
997 // properly notified.
998 TEST_F(ViewTreeAppTest, ReuseDeletedViewId) {
999 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1000 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1001 Id view_2_2 = vm_client2()->CreateView(2);
1002 ASSERT_TRUE(view_2_2);
1004 // Add 2 to 1.
1006 changes1()->clear();
1007 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1008 vm_client1_->WaitForChangeCount(1);
1009 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
1010 IdToString(view_1_1) + " old_parent=null",
1011 SingleChangeToDescription(*changes1()));
1012 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "]",
1013 ChangeViewDescription(*changes1()));
1016 // Delete 2.
1018 changes1()->clear();
1019 ASSERT_TRUE(DeleteView(vm2(), view_2_2));
1021 vm_client1_->WaitForChangeCount(1);
1022 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_2),
1023 SingleChangeToDescription(*changes1()));
1026 // Create 2 again, and add it back to 1. Should get the same notification.
1027 view_2_2 = vm_client2()->CreateView(2);
1028 ASSERT_TRUE(view_2_2);
1030 changes1()->clear();
1031 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1033 vm_client1_->WaitForChangeCount(1);
1034 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
1035 IdToString(view_1_1) + " old_parent=null",
1036 SingleChangeToDescription(*changes1()));
1037 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "]",
1038 ChangeViewDescription(*changes1()));
1042 // Assertions for GetViewTree.
1043 TEST_F(ViewTreeAppTest, GetViewTree) {
1044 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1045 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1047 // Create 11 in first connection and make it a child of 1.
1048 Id view_1_11 = vm_client1()->CreateView(11);
1049 ASSERT_TRUE(view_1_11);
1050 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1051 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_11));
1053 // Create two views in second connection, 2 and 3, both children of 1.
1054 Id view_2_2 = vm_client2()->CreateView(2);
1055 Id view_2_3 = vm_client2()->CreateView(3);
1056 ASSERT_TRUE(view_2_2);
1057 ASSERT_TRUE(view_2_3);
1058 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1059 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_3));
1061 // Verifies GetViewTree() on the root. The root connection sees all.
1063 std::vector<TestView> views;
1064 GetViewTree(vm1(), root_view_id(), &views);
1065 ASSERT_EQ(5u, views.size());
1066 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId),
1067 views[0].ToString());
1068 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()),
1069 views[1].ToString());
1070 EXPECT_EQ(ViewParentToString(view_1_11, view_1_1), views[2].ToString());
1071 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[3].ToString());
1072 EXPECT_EQ(ViewParentToString(view_2_3, view_1_1), views[4].ToString());
1075 // Verifies GetViewTree() on the view 1,1 from vm2(). vm2() sees 1,1 as 1,1
1076 // is vm2()'s root and vm2() sees all the views it created.
1078 std::vector<TestView> views;
1079 GetViewTree(vm2(), view_1_1, &views);
1080 ASSERT_EQ(3u, views.size());
1081 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1082 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[1].ToString());
1083 EXPECT_EQ(ViewParentToString(view_2_3, view_1_1), views[2].ToString());
1086 // Connection 2 shouldn't be able to get the root tree.
1088 std::vector<TestView> views;
1089 GetViewTree(vm2(), root_view_id(), &views);
1090 ASSERT_EQ(0u, views.size());
1094 TEST_F(ViewTreeAppTest, SetViewBounds) {
1095 Id view_1_1 = vm_client1()->CreateView(1);
1096 ASSERT_TRUE(view_1_1);
1097 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1099 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1101 changes2()->clear();
1102 ASSERT_TRUE(SetViewBounds(vm1(), view_1_1, 0, 0, 100, 100));
1104 vm_client2_->WaitForChangeCount(1);
1105 EXPECT_EQ("BoundsChanged view=" + IdToString(view_1_1) +
1106 " old_bounds=0,0 0x0 new_bounds=0,0 100x100",
1107 SingleChangeToDescription(*changes2()));
1109 // Should not be possible to change the bounds of a view created by another
1110 // connection.
1111 ASSERT_FALSE(SetViewBounds(vm2(), view_1_1, 0, 0, 0, 0));
1114 // Verify AddView fails when trying to manipulate views in other roots.
1115 TEST_F(ViewTreeAppTest, CantMoveViewsFromOtherRoot) {
1116 // Create 1 and 2 in the first connection.
1117 Id view_1_1 = vm_client1()->CreateView(1);
1118 Id view_1_2 = vm_client1()->CreateView(2);
1119 ASSERT_TRUE(view_1_1);
1120 ASSERT_TRUE(view_1_2);
1122 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1124 // Try to move 2 to be a child of 1 from connection 2. This should fail as 2
1125 // should not be able to access 1.
1126 ASSERT_FALSE(AddView(vm2(), view_1_1, view_1_2));
1128 // Try to reparent 1 to the root. A connection is not allowed to reparent its
1129 // roots.
1130 ASSERT_FALSE(AddView(vm2(), root_view_id(), view_1_1));
1133 // Verify RemoveViewFromParent fails for views that are descendants of the
1134 // roots.
1135 TEST_F(ViewTreeAppTest, CantRemoveViewsInOtherRoots) {
1136 // Create 1 and 2 in the first connection and parent both to the root.
1137 Id view_1_1 = vm_client1()->CreateView(1);
1138 Id view_1_2 = vm_client1()->CreateView(2);
1139 ASSERT_TRUE(view_1_1);
1140 ASSERT_TRUE(view_1_2);
1142 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1143 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_2));
1145 // Establish the second connection and give it the root 1.
1146 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1148 // Connection 2 should not be able to remove view 2 or 1 from its parent.
1149 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_1_2));
1150 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_1_1));
1152 // Create views 10 and 11 in 2.
1153 Id view_2_10 = vm_client2()->CreateView(10);
1154 Id view_2_11 = vm_client2()->CreateView(11);
1155 ASSERT_TRUE(view_2_10);
1156 ASSERT_TRUE(view_2_11);
1158 // Parent 11 to 10.
1159 ASSERT_TRUE(AddView(vm2(), view_2_10, view_2_11));
1160 // Remove 11 from 10.
1161 ASSERT_TRUE(RemoveViewFromParent(vm2(), view_2_11));
1163 // Verify nothing was actually removed.
1165 std::vector<TestView> views;
1166 GetViewTree(vm1(), root_view_id(), &views);
1167 ASSERT_EQ(3u, views.size());
1168 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId),
1169 views[0].ToString());
1170 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()),
1171 views[1].ToString());
1172 EXPECT_EQ(ViewParentToString(view_1_2, root_view_id()),
1173 views[2].ToString());
1177 // Verify GetViewTree fails for views that are not descendants of the roots.
1178 TEST_F(ViewTreeAppTest, CantGetViewTreeOfOtherRoots) {
1179 // Create 1 and 2 in the first connection and parent both to the root.
1180 Id view_1_1 = vm_client1()->CreateView(1);
1181 Id view_1_2 = vm_client1()->CreateView(2);
1182 ASSERT_TRUE(view_1_1);
1183 ASSERT_TRUE(view_1_2);
1185 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1186 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_2));
1188 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1190 std::vector<TestView> views;
1192 // Should get nothing for the root.
1193 GetViewTree(vm2(), root_view_id(), &views);
1194 ASSERT_TRUE(views.empty());
1196 // Should get nothing for view 2.
1197 GetViewTree(vm2(), view_1_2, &views);
1198 ASSERT_TRUE(views.empty());
1200 // Should get view 1 if asked for.
1201 GetViewTree(vm2(), view_1_1, &views);
1202 ASSERT_EQ(1u, views.size());
1203 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1206 TEST_F(ViewTreeAppTest, EmbedWithSameViewId) {
1207 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1208 changes2()->clear();
1210 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1211 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), view_1_1));
1213 // Connection2 should have been told of the unembed and delete.
1215 vm_client2_->WaitForChangeCount(2);
1216 EXPECT_EQ("OnUnembed", ChangesToDescription1(*changes2())[0]);
1217 EXPECT_EQ("ViewDeleted view=" + IdToString(view_1_1),
1218 ChangesToDescription1(*changes2())[1]);
1221 // Connection2 has no root. Verify it can't see view 1,1 anymore.
1223 std::vector<TestView> views;
1224 GetViewTree(vm2(), view_1_1, &views);
1225 EXPECT_TRUE(views.empty());
1229 TEST_F(ViewTreeAppTest, EmbedWithSameViewId2) {
1230 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1231 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1232 changes2()->clear();
1234 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), view_1_1));
1236 // Connection2 should have been told about the unembed and delete.
1237 vm_client2_->WaitForChangeCount(2);
1238 changes2()->clear();
1240 // Create a view in the third connection and parent it to the root.
1241 Id view_3_1 = vm_client3()->CreateView(1);
1242 ASSERT_TRUE(view_3_1);
1243 ASSERT_TRUE(AddView(vm3(), view_1_1, view_3_1));
1245 // Connection 1 should have been told about the add (it owns the view).
1247 vm_client1_->WaitForChangeCount(1);
1248 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_3_1) + " new_parent=" +
1249 IdToString(view_1_1) + " old_parent=null",
1250 SingleChangeToDescription(*changes1()));
1253 // Embed 1,1 again.
1255 changes3()->clear();
1257 // We should get a new connection for the new embedding.
1258 scoped_ptr<ViewTreeClientImpl> connection4(
1259 EstablishConnectionViaEmbed(vm1(), view_1_1, nullptr));
1260 ASSERT_TRUE(connection4.get());
1261 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
1262 ChangeViewDescription(*connection4->tracker()->changes()));
1264 // And 3 should get an unembed and delete.
1265 vm_client3_->WaitForChangeCount(2);
1266 EXPECT_EQ("OnUnembed", ChangesToDescription1(*changes3())[0]);
1267 EXPECT_EQ("ViewDeleted view=" + IdToString(view_1_1),
1268 ChangesToDescription1(*changes3())[1]);
1271 // vm3() has no root. Verify it can't see view 1,1 anymore.
1273 std::vector<TestView> views;
1274 GetViewTree(vm3(), view_1_1, &views);
1275 EXPECT_TRUE(views.empty());
1278 // Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as
1279 // vm3() can no longer see 1,1.
1281 std::vector<TestView> views;
1282 GetViewTree(vm1(), view_1_1, &views);
1283 ASSERT_EQ(1u, views.size());
1284 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1287 // Verify vm3() can still see the view it created 3,1.
1289 std::vector<TestView> views;
1290 GetViewTree(vm3(), view_3_1, &views);
1291 ASSERT_EQ(1u, views.size());
1292 EXPECT_EQ(ViewParentToString(view_3_1, kNullParentId), views[0].ToString());
1296 // Assertions for SetViewVisibility.
1297 TEST_F(ViewTreeAppTest, SetViewVisibility) {
1298 // Create 1 and 2 in the first connection and parent both to the root.
1299 Id view_1_1 = vm_client1()->CreateView(1);
1300 Id view_1_2 = vm_client1()->CreateView(2);
1301 ASSERT_TRUE(view_1_1);
1302 ASSERT_TRUE(view_1_2);
1304 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1306 std::vector<TestView> views;
1307 GetViewTree(vm1(), root_view_id(), &views);
1308 ASSERT_EQ(2u, views.size());
1309 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId) +
1310 " visible=true drawn=true",
1311 views[0].ToString2());
1312 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1313 " visible=false drawn=false",
1314 views[1].ToString2());
1317 // Show all the views.
1318 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1319 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
1321 std::vector<TestView> views;
1322 GetViewTree(vm1(), root_view_id(), &views);
1323 ASSERT_EQ(2u, views.size());
1324 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId) +
1325 " visible=true drawn=true",
1326 views[0].ToString2());
1327 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1328 " visible=true drawn=true",
1329 views[1].ToString2());
1332 // Hide 1.
1333 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, false));
1335 std::vector<TestView> views;
1336 GetViewTree(vm1(), view_1_1, &views);
1337 ASSERT_EQ(1u, views.size());
1338 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1339 " visible=false drawn=false",
1340 views[0].ToString2());
1343 // Attach 2 to 1.
1344 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
1346 std::vector<TestView> views;
1347 GetViewTree(vm1(), view_1_1, &views);
1348 ASSERT_EQ(2u, views.size());
1349 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1350 " visible=false drawn=false",
1351 views[0].ToString2());
1352 EXPECT_EQ(
1353 ViewParentToString(view_1_2, view_1_1) + " visible=true drawn=false",
1354 views[1].ToString2());
1357 // Show 1.
1358 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1360 std::vector<TestView> views;
1361 GetViewTree(vm1(), view_1_1, &views);
1362 ASSERT_EQ(2u, views.size());
1363 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1364 " visible=true drawn=true",
1365 views[0].ToString2());
1366 EXPECT_EQ(
1367 ViewParentToString(view_1_2, view_1_1) + " visible=true drawn=true",
1368 views[1].ToString2());
1372 // Assertions for SetViewVisibility sending notifications.
1373 TEST_F(ViewTreeAppTest, SetViewVisibilityNotifications) {
1374 // Create 1,1 and 1,2. 1,2 is made a child of 1,1 and 1,1 a child of the root.
1375 Id view_1_1 = vm_client1()->CreateView(1);
1376 ASSERT_TRUE(view_1_1);
1377 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1378 Id view_1_2 = vm_client1()->CreateView(2);
1379 ASSERT_TRUE(view_1_2);
1380 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
1381 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1382 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
1384 // Establish the second connection at 1,2.
1385 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnectionWithRoot(view_1_2));
1387 // Add 2,3 as a child of 1,2.
1388 Id view_2_3 = vm_client2()->CreateView(3);
1389 ASSERT_TRUE(view_2_3);
1390 ASSERT_TRUE(SetViewVisibility(vm2(), view_2_3, true));
1391 ASSERT_TRUE(AddView(vm2(), view_1_2, view_2_3));
1392 WaitForAllMessages(vm1());
1394 changes2()->clear();
1395 // Hide 1,2 from connection 1. Connection 2 should see this.
1396 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, false));
1398 vm_client2_->WaitForChangeCount(1);
1399 EXPECT_EQ(
1400 "VisibilityChanged view=" + IdToString(view_1_2) + " visible=false",
1401 SingleChangeToDescription(*changes2()));
1404 changes1()->clear();
1405 // Show 1,2 from connection 2, connection 1 should be notified.
1406 ASSERT_TRUE(SetViewVisibility(vm2(), view_1_2, true));
1408 vm_client1_->WaitForChangeCount(1);
1409 EXPECT_EQ(
1410 "VisibilityChanged view=" + IdToString(view_1_2) + " visible=true",
1411 SingleChangeToDescription(*changes1()));
1414 changes2()->clear();
1415 // Hide 1,1, connection 2 should be told the draw state changed.
1416 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, false));
1418 vm_client2_->WaitForChangeCount(1);
1419 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=false",
1420 SingleChangeToDescription(*changes2()));
1423 changes2()->clear();
1424 // Show 1,1 from connection 1. Connection 2 should see this.
1425 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1427 vm_client2_->WaitForChangeCount(1);
1428 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=true",
1429 SingleChangeToDescription(*changes2()));
1432 // Change visibility of 2,3, connection 1 should see this.
1433 changes1()->clear();
1434 ASSERT_TRUE(SetViewVisibility(vm2(), view_2_3, false));
1436 vm_client1_->WaitForChangeCount(1);
1437 EXPECT_EQ(
1438 "VisibilityChanged view=" + IdToString(view_2_3) + " visible=false",
1439 SingleChangeToDescription(*changes1()));
1442 changes2()->clear();
1443 // Remove 1,1 from the root, connection 2 should see drawn state changed.
1444 ASSERT_TRUE(RemoveViewFromParent(vm1(), view_1_1));
1446 vm_client2_->WaitForChangeCount(1);
1447 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=false",
1448 SingleChangeToDescription(*changes2()));
1451 changes2()->clear();
1452 // Add 1,1 back to the root, connection 2 should see drawn state changed.
1453 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1455 vm_client2_->WaitForChangeCount(1);
1456 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=true",
1457 SingleChangeToDescription(*changes2()));
1461 TEST_F(ViewTreeAppTest, SetViewProperty) {
1462 Id view_1_1 = vm_client1()->CreateView(1);
1463 ASSERT_TRUE(view_1_1);
1465 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1466 changes2()->clear();
1468 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1470 std::vector<TestView> views;
1471 GetViewTree(vm1(), root_view_id(), &views);
1472 ASSERT_EQ(2u, views.size());
1473 EXPECT_EQ(root_view_id(), views[0].view_id);
1474 EXPECT_EQ(view_1_1, views[1].view_id);
1475 ASSERT_EQ(0u, views[1].properties.size());
1478 // Set properties on 1.
1479 changes2()->clear();
1480 std::vector<uint8_t> one(1, '1');
1481 ASSERT_TRUE(SetViewProperty(vm1(), view_1_1, "one", &one));
1483 vm_client2_->WaitForChangeCount(1);
1484 EXPECT_EQ(
1485 "PropertyChanged view=" + IdToString(view_1_1) + " key=one value=1",
1486 SingleChangeToDescription(*changes2()));
1489 // Test that our properties exist in the view tree
1491 std::vector<TestView> views;
1492 GetViewTree(vm1(), view_1_1, &views);
1493 ASSERT_EQ(1u, views.size());
1494 ASSERT_EQ(1u, views[0].properties.size());
1495 EXPECT_EQ(one, views[0].properties["one"]);
1498 changes2()->clear();
1499 // Set back to null.
1500 ASSERT_TRUE(SetViewProperty(vm1(), view_1_1, "one", NULL));
1502 vm_client2_->WaitForChangeCount(1);
1503 EXPECT_EQ(
1504 "PropertyChanged view=" + IdToString(view_1_1) + " key=one value=NULL",
1505 SingleChangeToDescription(*changes2()));
1509 TEST_F(ViewTreeAppTest, OnEmbeddedAppDisconnected) {
1510 // Create connection 2 and 3.
1511 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1512 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1513 Id view_2_2 = vm_client2()->CreateView(2);
1514 ASSERT_TRUE(view_2_2);
1515 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1516 changes2()->clear();
1517 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
1519 // Close connection 3. Connection 2 (which had previously embedded 3) should
1520 // be notified of this.
1521 vm_client3_.reset();
1522 vm_client2_->WaitForChangeCount(1);
1523 EXPECT_EQ("OnEmbeddedAppDisconnected view=" + IdToString(view_2_2),
1524 SingleChangeToDescription(*changes2()));
1527 // Verifies when the parent of an Embed() is destroyed the embedded app gets
1528 // a ViewDeleted (and doesn't trigger a DCHECK).
1529 TEST_F(ViewTreeAppTest, OnParentOfEmbedDisconnects) {
1530 // Create connection 2 and 3.
1531 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1532 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1533 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1534 Id view_2_2 = vm_client2()->CreateView(2);
1535 Id view_2_3 = vm_client2()->CreateView(3);
1536 ASSERT_TRUE(view_2_2);
1537 ASSERT_TRUE(view_2_3);
1538 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1539 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_3));
1540 changes2()->clear();
1541 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_3));
1542 changes3()->clear();
1544 // Close connection 2. Connection 3 should get a delete (for its root).
1545 vm_client2_.reset();
1546 vm_client3_->WaitForChangeCount(1);
1547 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_3),
1548 SingleChangeToDescription(*changes3()));
1551 // Verifies ViewTreeImpl doesn't incorrectly erase from its internal
1552 // map when a view from another connection with the same view_id is removed.
1553 TEST_F(ViewTreeAppTest, DontCleanMapOnDestroy) {
1554 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1555 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1556 ASSERT_TRUE(vm_client2()->CreateView(1));
1557 changes1()->clear();
1558 vm_client2_.reset();
1559 vm_client1_->WaitForChangeCount(1);
1560 EXPECT_EQ("OnEmbeddedAppDisconnected view=" + IdToString(view_1_1),
1561 SingleChangeToDescription(*changes1()));
1562 std::vector<TestView> views;
1563 GetViewTree(vm1(), view_1_1, &views);
1564 EXPECT_FALSE(views.empty());
1567 // Verifies Embed() works when supplying a ViewTreeClient.
1568 TEST_F(ViewTreeAppTest, EmbedSupplyingViewTreeClient) {
1569 ASSERT_TRUE(vm_client1()->CreateView(1));
1571 ViewTreeClientImpl client2(application_impl());
1572 mojo::ViewTreeClientPtr client2_ptr;
1573 mojo::Binding<ViewTreeClient> client2_binding(&client2, &client2_ptr);
1574 ASSERT_TRUE(
1575 Embed(vm1(), BuildViewId(connection_id_1(), 1), client2_ptr.Pass()));
1576 client2.WaitForOnEmbed();
1577 EXPECT_EQ("OnEmbed",
1578 SingleChangeToDescription(*client2.tracker()->changes()));
1581 TEST_F(ViewTreeAppTest, EmbedFailsFromOtherConnection) {
1582 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1584 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1585 Id view_2_2 = vm_client2()->CreateView(2);
1586 ASSERT_TRUE(view_2_2);
1587 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1588 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
1590 Id view_3_3 = vm_client3()->CreateView(3);
1591 ASSERT_TRUE(view_3_3);
1592 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
1594 // 2 should not be able to embed in view_3_3 as view_3_3 was not created by
1595 // 2.
1596 EXPECT_FALSE(EmbedUrl(application_impl(), vm2(), application_impl()->url(),
1597 view_3_3));
1600 // Verifies Embed() from window manager on another connections view works.
1601 TEST_F(ViewTreeAppTest, EmbedFromOtherConnection) {
1602 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1604 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1605 Id view_2_2 = vm_client2()->CreateView(2);
1606 ASSERT_TRUE(view_2_2);
1607 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1609 changes2()->clear();
1611 // Establish a third connection in view_2_2.
1612 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), view_2_2));
1614 WaitForAllMessages(vm2());
1615 EXPECT_EQ(std::string(), SingleChangeToDescription(*changes2()));
1618 // TODO(sky): need to better track changes to initial connection. For example,
1619 // that SetBounsdViews/AddView and the like don't result in messages to the
1620 // originating connection.
1622 // TODO(sky): make sure coverage of what was
1623 // ViewManagerTest.SecondEmbedRoot_InitService and
1624 // ViewManagerTest.MultipleEmbedRootsBeforeWTHReady gets added to window manager
1625 // tests.
1627 } // namespace view_manager