Update V8 to version 4.6.52.
[chromium-blink-merge.git] / components / view_manager / view_manager_service_apptest.cc
blobe18c23da2abd96fafb198fc3d3d38118ccc66570
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_manager.mojom.h"
11 #include "components/view_manager/public/interfaces/view_manager_root.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::ViewManagerClient;
36 using mojo::ViewManagerService;
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 ViewManagerService 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 ViewManagerService* 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 ApplicationConnection* connection =
82 app->ConnectToApplication(request.Pass());
83 mojo::ViewManagerClientPtr 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 EmbedAllowingReembed(mojo::ApplicationImpl* app,
93 ViewManagerService* vm,
94 const String& url,
95 Id root_id) {
96 bool result = false;
97 base::RunLoop run_loop;
99 mojo::URLRequestPtr request(mojo::URLRequest::New());
100 request->url = mojo::String::From(url);
101 vm->EmbedAllowingReembed(
102 root_id, request.Pass(),
103 base::Bind(&BoolResultCallback, &run_loop, &result));
105 run_loop.Run();
106 return result;
109 bool Embed(ViewManagerService* vm,
110 Id root_id,
111 mojo::ViewManagerClientPtr client) {
112 bool result = false;
113 base::RunLoop run_loop;
115 vm->Embed(root_id, client.Pass(),
116 base::Bind(&BoolResultCallback, &run_loop, &result));
118 run_loop.Run();
119 return result;
122 ErrorCode CreateViewWithErrorCode(ViewManagerService* vm, Id view_id) {
123 ErrorCode result = ERROR_CODE_NONE;
124 base::RunLoop run_loop;
125 vm->CreateView(view_id,
126 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
127 run_loop.Run();
128 return result;
131 bool AddView(ViewManagerService* vm, Id parent, Id child) {
132 bool result = false;
133 base::RunLoop run_loop;
134 vm->AddView(parent, child,
135 base::Bind(&BoolResultCallback, &run_loop, &result));
136 run_loop.Run();
137 return result;
140 bool RemoveViewFromParent(ViewManagerService* vm, Id view_id) {
141 bool result = false;
142 base::RunLoop run_loop;
143 vm->RemoveViewFromParent(view_id,
144 base::Bind(&BoolResultCallback, &run_loop, &result));
145 run_loop.Run();
146 return result;
149 bool ReorderView(ViewManagerService* vm,
150 Id view_id,
151 Id relative_view_id,
152 OrderDirection direction) {
153 bool result = false;
154 base::RunLoop run_loop;
155 vm->ReorderView(view_id, relative_view_id, direction,
156 base::Bind(&BoolResultCallback, &run_loop, &result));
157 run_loop.Run();
158 return result;
161 void GetViewTree(ViewManagerService* vm,
162 Id view_id,
163 std::vector<TestView>* views) {
164 base::RunLoop run_loop;
165 vm->GetViewTree(view_id,
166 base::Bind(&ViewTreeResultCallback, &run_loop, views));
167 run_loop.Run();
170 bool DeleteView(ViewManagerService* vm, Id view_id) {
171 base::RunLoop run_loop;
172 bool result = false;
173 vm->DeleteView(view_id, base::Bind(&BoolResultCallback, &run_loop, &result));
174 run_loop.Run();
175 return result;
178 bool SetViewBounds(ViewManagerService* vm,
179 Id view_id,
180 int x,
181 int y,
182 int w,
183 int h) {
184 base::RunLoop run_loop;
185 bool result = false;
186 RectPtr rect(mojo::Rect::New());
187 rect->x = x;
188 rect->y = y;
189 rect->width = w;
190 rect->height = h;
191 vm->SetViewBounds(view_id, rect.Pass(),
192 base::Bind(&BoolResultCallback, &run_loop, &result));
193 run_loop.Run();
194 return result;
197 bool SetViewVisibility(ViewManagerService* vm, Id view_id, bool visible) {
198 base::RunLoop run_loop;
199 bool result = false;
200 vm->SetViewVisibility(view_id, visible,
201 base::Bind(&BoolResultCallback, &run_loop, &result));
202 run_loop.Run();
203 return result;
206 bool SetViewProperty(ViewManagerService* vm,
207 Id view_id,
208 const std::string& name,
209 const std::vector<uint8_t>* data) {
210 base::RunLoop run_loop;
211 bool result = false;
212 Array<uint8_t> mojo_data;
213 if (data)
214 mojo_data = Array<uint8_t>::From(*data);
215 vm->SetViewProperty(view_id, name, mojo_data.Pass(),
216 base::Bind(&BoolResultCallback, &run_loop, &result));
217 run_loop.Run();
218 return result;
221 // Utility functions -----------------------------------------------------------
223 // Waits for all messages to be received by |vm|. This is done by attempting to
224 // create a bogus view. When we get the response we know all messages have been
225 // processed.
226 bool WaitForAllMessages(ViewManagerService* vm) {
227 ErrorCode result = ERROR_CODE_NONE;
228 base::RunLoop run_loop;
229 vm->CreateView(ViewIdToTransportId(InvalidViewId()),
230 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
231 run_loop.Run();
232 return result != ERROR_CODE_NONE;
235 bool HasClonedView(const std::vector<TestView>& views) {
236 for (size_t i = 0; i < views.size(); ++i)
237 if (views[i].view_id == ViewIdToTransportId(ClonedViewId()))
238 return true;
239 return false;
242 const Id kNullParentId = 0;
243 std::string IdToString(Id id) {
244 return (id == kNullParentId)
245 ? "null"
246 : base::StringPrintf("%d,%d", mojo::HiWord(id), mojo::LoWord(id));
249 std::string ViewParentToString(Id view, Id parent) {
250 return base::StringPrintf("view=%s parent=%s", IdToString(view).c_str(),
251 IdToString(parent).c_str());
254 // -----------------------------------------------------------------------------
256 // A ViewManagerClient implementation that logs all changes to a tracker.
257 class ViewManagerClientImpl : public mojo::ViewManagerClient,
258 public TestChangeTracker::Delegate {
259 public:
260 explicit ViewManagerClientImpl(mojo::ApplicationImpl* app)
261 : binding_(this), app_(app), connection_id_(0), root_view_id_(0) {
262 tracker_.set_delegate(this);
265 void Bind(mojo::InterfaceRequest<mojo::ViewManagerClient> request) {
266 binding_.Bind(request.Pass());
269 mojo::ViewManagerService* service() { return service_.get(); }
270 TestChangeTracker* tracker() { return &tracker_; }
272 // Runs a nested MessageLoop until |count| changes (calls to
273 // ViewManagerClient functions) have been received.
274 void WaitForChangeCount(size_t count) {
275 if (count == tracker_.changes()->size())
276 return;
278 ASSERT_TRUE(wait_state_.get() == nullptr);
279 wait_state_.reset(new WaitState);
280 wait_state_->change_count = count;
281 wait_state_->run_loop.Run();
282 wait_state_.reset();
285 // Runs a nested MessageLoop until OnEmbed() has been encountered.
286 void WaitForOnEmbed() {
287 if (service_)
288 return;
289 embed_run_loop_.reset(new base::RunLoop);
290 embed_run_loop_->Run();
291 embed_run_loop_.reset();
294 bool WaitForIncomingMethodCall() {
295 return binding_.WaitForIncomingMethodCall();
298 Id CreateView(ConnectionSpecificId view_id) {
299 ErrorCode result = ERROR_CODE_NONE;
300 base::RunLoop run_loop;
301 Id id = BuildViewId(connection_id_, view_id);
302 service()->CreateView(
303 id, base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
304 run_loop.Run();
305 return result == ERROR_CODE_NONE ? id : 0;
308 void set_root_view(Id root_view_id) { root_view_id_ = root_view_id; }
310 private:
311 // Used when running a nested MessageLoop.
312 struct WaitState {
313 WaitState() : change_count(0) {}
315 // Number of changes waiting for.
316 size_t change_count;
317 base::RunLoop run_loop;
320 // TestChangeTracker::Delegate:
321 void OnChangeAdded() override {
322 if (wait_state_.get() &&
323 wait_state_->change_count == tracker_.changes()->size()) {
324 wait_state_->run_loop.Quit();
328 // ViewManagerClient:
329 void OnEmbed(ConnectionSpecificId connection_id,
330 ViewDataPtr root,
331 mojo::ViewManagerServicePtr view_manager_service,
332 mojo::Id focused_view_id) override {
333 // TODO(sky): add coverage of |focused_view_id|.
334 service_ = view_manager_service.Pass();
335 connection_id_ = connection_id;
336 tracker()->OnEmbed(connection_id, root.Pass());
337 if (embed_run_loop_)
338 embed_run_loop_->Quit();
340 void OnEmbedForDescendant(
341 uint32_t view,
342 mojo::URLRequestPtr request,
343 const OnEmbedForDescendantCallback& callback) override {
344 tracker()->OnEmbedForDescendant(view);
345 mojo::ViewManagerClientPtr client;
346 ApplicationConnection* connection =
347 app_->ConnectToApplication(request.Pass());
348 connection->ConnectToService(&client);
349 callback.Run(client.Pass());
351 void OnEmbeddedAppDisconnected(Id view_id) override {
352 tracker()->OnEmbeddedAppDisconnected(view_id);
354 void OnUnembed() override { tracker()->OnUnembed(); }
355 void OnViewBoundsChanged(Id view_id,
356 RectPtr old_bounds,
357 RectPtr new_bounds) override {
358 // The bounds of the root may change during startup on Android at random
359 // times. As this doesn't matter, and shouldn't impact test exepctations,
360 // it is ignored.
361 if (view_id == root_view_id_)
362 return;
363 tracker()->OnViewBoundsChanged(view_id, old_bounds.Pass(),
364 new_bounds.Pass());
366 void OnViewViewportMetricsChanged(ViewportMetricsPtr old_metrics,
367 ViewportMetricsPtr new_metrics) override {
368 // Don't track the metrics as they are available at an indeterministic time
369 // on Android.
371 void OnViewHierarchyChanged(Id view,
372 Id new_parent,
373 Id old_parent,
374 Array<ViewDataPtr> views) override {
375 tracker()->OnViewHierarchyChanged(view, new_parent, old_parent,
376 views.Pass());
378 void OnViewReordered(Id view_id,
379 Id relative_view_id,
380 OrderDirection direction) override {
381 tracker()->OnViewReordered(view_id, relative_view_id, direction);
383 void OnViewDeleted(Id view) override { tracker()->OnViewDeleted(view); }
384 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
385 tracker()->OnViewVisibilityChanged(view, visible);
387 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
388 tracker()->OnViewDrawnStateChanged(view, drawn);
390 void OnViewInputEvent(Id view_id,
391 EventPtr event,
392 const Callback<void()>& callback) override {
393 tracker()->OnViewInputEvent(view_id, event.Pass());
394 callback.Run();
396 void OnViewSharedPropertyChanged(uint32_t view,
397 const String& name,
398 Array<uint8_t> new_data) override {
399 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
401 // TODO(sky): add testing coverage.
402 void OnViewFocused(uint32_t focused_view_id) override {}
404 TestChangeTracker tracker_;
406 mojo::ViewManagerServicePtr service_;
408 // If non-null we're waiting for OnEmbed() using this RunLoop.
409 scoped_ptr<base::RunLoop> embed_run_loop_;
411 // If non-null we're waiting for a certain number of change notifications to
412 // be encountered.
413 scoped_ptr<WaitState> wait_state_;
415 mojo::Binding<ViewManagerClient> binding_;
416 mojo::ApplicationImpl* app_;
417 Id connection_id_;
418 Id root_view_id_;
420 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
423 // -----------------------------------------------------------------------------
425 // InterfaceFactory for vending ViewManagerClientImpls.
426 class ViewManagerClientFactory
427 : public mojo::InterfaceFactory<ViewManagerClient> {
428 public:
429 explicit ViewManagerClientFactory(mojo::ApplicationImpl* app) : app_(app) {}
430 ~ViewManagerClientFactory() override {}
432 // Runs a nested MessageLoop until a new instance has been created.
433 scoped_ptr<ViewManagerClientImpl> WaitForInstance() {
434 if (!client_impl_.get()) {
435 DCHECK(!run_loop_.get());
436 run_loop_.reset(new base::RunLoop);
437 run_loop_->Run();
438 run_loop_.reset();
440 return client_impl_.Pass();
443 private:
444 // InterfaceFactory<ViewManagerClient>:
445 void Create(ApplicationConnection* connection,
446 InterfaceRequest<ViewManagerClient> request) override {
447 client_impl_.reset(new ViewManagerClientImpl(app_));
448 client_impl_->Bind(request.Pass());
449 if (run_loop_.get())
450 run_loop_->Quit();
453 mojo::ApplicationImpl* app_;
454 scoped_ptr<ViewManagerClientImpl> client_impl_;
455 scoped_ptr<base::RunLoop> run_loop_;
457 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientFactory);
460 class ViewManagerServiceAppTest : public mojo::test::ApplicationTestBase,
461 public ApplicationDelegate {
462 public:
463 ViewManagerServiceAppTest()
464 : connection_id_1_(0), connection_id_2_(0), root_view_id_(0) {}
465 ~ViewManagerServiceAppTest() override {}
467 protected:
468 enum class EmbedType {
469 ALLOW_REEMBED,
470 NO_REEMBED,
473 // Returns the changes from the various connections.
474 std::vector<Change>* changes1() { return vm_client1_->tracker()->changes(); }
475 std::vector<Change>* changes2() { return vm_client2_->tracker()->changes(); }
476 std::vector<Change>* changes3() { return vm_client3_->tracker()->changes(); }
478 // Various connections. |vm1()|, being the first connection, has special
479 // permissions (it's treated as the window manager).
480 ViewManagerService* vm1() { return vm_client1_->service(); }
481 ViewManagerService* vm2() { return vm_client2_->service(); }
482 ViewManagerService* vm3() { return vm_client3_->service(); }
484 ViewManagerClientImpl* vm_client1() { return vm_client1_.get(); }
485 ViewManagerClientImpl* vm_client2() { return vm_client2_.get(); }
486 ViewManagerClientImpl* vm_client3() { return vm_client3_.get(); }
488 Id root_view_id() const { return root_view_id_; }
490 int connection_id_1() const { return connection_id_1_; }
491 int connection_id_2() const { return connection_id_2_; }
493 void EstablishSecondConnectionWithRoot(Id root_id) {
494 ASSERT_TRUE(vm_client2_.get() == nullptr);
495 vm_client2_ = EstablishConnectionViaEmbed(
496 vm1(), root_id, EmbedType::NO_REEMBED, &connection_id_2_);
497 ASSERT_GT(connection_id_2_, 0);
498 ASSERT_TRUE(vm_client2_.get() != nullptr);
499 vm_client2_->set_root_view(root_view_id_);
502 void EstablishSecondConnection(bool create_initial_view) {
503 Id view_1_1 = 0;
504 if (create_initial_view) {
505 view_1_1 = vm_client1()->CreateView(1);
506 ASSERT_TRUE(view_1_1);
508 ASSERT_NO_FATAL_FAILURE(
509 EstablishSecondConnectionWithRoot(BuildViewId(connection_id_1(), 1)));
511 if (create_initial_view) {
512 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
513 ChangeViewDescription(*changes2()));
517 void EstablishThirdConnection(ViewManagerService* owner, Id root_id) {
518 ASSERT_TRUE(vm_client3_.get() == nullptr);
519 vm_client3_ = EstablishConnectionViaEmbed(owner, root_id,
520 EmbedType::NO_REEMBED, nullptr);
521 ASSERT_TRUE(vm_client3_.get() != nullptr);
522 vm_client3_->set_root_view(root_view_id_);
525 // Establishes a new connection by way of Embed() on the specified
526 // ViewManagerService.
527 scoped_ptr<ViewManagerClientImpl> EstablishConnectionViaEmbed(
528 ViewManagerService* owner,
529 Id root_id,
530 EmbedType embed_type,
531 int* connection_id) {
532 if (embed_type == EmbedType::NO_REEMBED &&
533 !EmbedUrl(application_impl(), owner, application_impl()->url(),
534 root_id)) {
535 ADD_FAILURE() << "Embed() failed";
536 return nullptr;
537 } else if (embed_type == EmbedType::ALLOW_REEMBED &&
538 !EmbedAllowingReembed(application_impl(), owner,
539 application_impl()->url(), root_id)) {
540 ADD_FAILURE() << "Embed() failed";
541 return nullptr;
543 scoped_ptr<ViewManagerClientImpl> client =
544 client_factory_->WaitForInstance();
545 if (!client.get()) {
546 ADD_FAILURE() << "WaitForInstance failed";
547 return nullptr;
549 client->WaitForOnEmbed();
551 EXPECT_EQ("OnEmbed",
552 SingleChangeToDescription(*client->tracker()->changes()));
553 if (connection_id)
554 *connection_id = (*client->tracker()->changes())[0].connection_id;
555 return client.Pass();
558 // ApplicationTestBase:
559 ApplicationDelegate* GetApplicationDelegate() override { return this; }
560 void SetUp() override {
561 ApplicationTestBase::SetUp();
562 client_factory_.reset(new ViewManagerClientFactory(application_impl()));
563 mojo::URLRequestPtr request(mojo::URLRequest::New());
564 request->url = mojo::String::From("mojo:view_manager");
565 ApplicationConnection* vm_connection =
566 application_impl()->ConnectToApplication(request.Pass());
567 vm_connection->AddService(client_factory_.get());
568 vm_connection->ConnectToService(&view_manager_root_);
569 vm_client1_ = client_factory_->WaitForInstance();
570 ASSERT_TRUE(vm_client1_);
571 // Next we should get an embed call on the "window manager" client.
572 vm_client1_->WaitForIncomingMethodCall();
573 ASSERT_EQ(1u, changes1()->size());
574 EXPECT_EQ(CHANGE_TYPE_EMBED, (*changes1())[0].type);
575 // All these tests assume 1 for the client id. The only real assertion here
576 // is the client id is not zero, but adding this as rest of code here
577 // assumes 1.
578 ASSERT_GT((*changes1())[0].connection_id, 0);
579 connection_id_1_ = (*changes1())[0].connection_id;
580 ASSERT_FALSE((*changes1())[0].views.empty());
581 root_view_id_ = (*changes1())[0].views[0].view_id;
582 vm_client1_->set_root_view(root_view_id_);
583 changes1()->clear();
586 // ApplicationDelegate implementation.
587 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
588 connection->AddService(client_factory_.get());
589 return true;
592 scoped_ptr<ViewManagerClientImpl> vm_client1_;
593 scoped_ptr<ViewManagerClientImpl> vm_client2_;
594 scoped_ptr<ViewManagerClientImpl> vm_client3_;
596 mojo::ViewManagerRootPtr view_manager_root_;
598 private:
599 scoped_ptr<ViewManagerClientFactory> client_factory_;
600 int connection_id_1_;
601 int connection_id_2_;
602 Id root_view_id_;
604 MOJO_DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceAppTest);
607 // Verifies two clients/connections get different ids.
608 TEST_F(ViewManagerServiceAppTest, TwoClientsGetDifferentConnectionIds) {
609 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
611 ASSERT_EQ(1u, changes2()->size());
612 ASSERT_NE(connection_id_1(), connection_id_2());
615 // Verifies when Embed() is invoked any child views are removed.
616 TEST_F(ViewManagerServiceAppTest, ViewsRemovedWhenEmbedding) {
617 // Two views 1 and 2. 2 is parented to 1.
618 Id view_1_1 = vm_client1()->CreateView(1);
619 ASSERT_TRUE(view_1_1);
620 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
622 Id view_1_2 = vm_client1()->CreateView(2);
623 ASSERT_TRUE(view_1_2);
624 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
626 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
627 ASSERT_EQ(1u, changes2()->size());
628 ASSERT_EQ(1u, (*changes2())[0].views.size());
629 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
630 ChangeViewDescription(*changes2()));
632 // Embed() removed view 2.
634 std::vector<TestView> views;
635 GetViewTree(vm1(), view_1_2, &views);
636 EXPECT_EQ(ViewParentToString(view_1_2, kNullParentId),
637 SingleViewDescription(views));
640 // vm2 should not see view 2.
642 std::vector<TestView> views;
643 GetViewTree(vm2(), view_1_1, &views);
644 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId),
645 SingleViewDescription(views));
648 std::vector<TestView> views;
649 GetViewTree(vm2(), view_1_2, &views);
650 EXPECT_TRUE(views.empty());
653 // Views 3 and 4 in connection 2.
654 Id view_2_3 = vm_client2()->CreateView(3);
655 Id view_2_4 = vm_client2()->CreateView(4);
656 ASSERT_TRUE(view_2_3);
657 ASSERT_TRUE(view_2_4);
658 ASSERT_TRUE(AddView(vm2(), view_2_3, view_2_4));
660 // Connection 3 rooted at 2.
661 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_3));
663 // View 4 should no longer have a parent.
665 std::vector<TestView> views;
666 GetViewTree(vm2(), view_2_3, &views);
667 EXPECT_EQ(ViewParentToString(view_2_3, kNullParentId),
668 SingleViewDescription(views));
670 views.clear();
671 GetViewTree(vm2(), view_2_4, &views);
672 EXPECT_EQ(ViewParentToString(view_2_4, kNullParentId),
673 SingleViewDescription(views));
676 // And view 4 should not be visible to connection 3.
678 std::vector<TestView> views;
679 GetViewTree(vm3(), view_2_3, &views);
680 EXPECT_EQ(ViewParentToString(view_2_3, kNullParentId),
681 SingleViewDescription(views));
685 // Verifies once Embed() has been invoked the parent connection can't see any
686 // children.
687 TEST_F(ViewManagerServiceAppTest, CantAccessChildrenOfEmbeddedView) {
688 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
690 Id view_1_1 = BuildViewId(connection_id_1(), 1);
691 Id view_2_2 = vm_client2()->CreateView(2);
692 ASSERT_TRUE(view_2_2);
693 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
695 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
697 Id view_3_3 = vm_client3()->CreateView(3);
698 ASSERT_TRUE(view_3_3);
699 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
701 // Even though 3 is a child of 2 connection 2 can't see 3 as it's from a
702 // different connection.
704 std::vector<TestView> views;
705 GetViewTree(vm2(), view_2_2, &views);
706 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1),
707 SingleViewDescription(views));
710 // Connection 2 shouldn't be able to get view 3 at all.
712 std::vector<TestView> views;
713 GetViewTree(vm2(), view_3_3, &views);
714 EXPECT_TRUE(views.empty());
717 // Connection 1 should be able to see it all (its the root).
719 std::vector<TestView> views;
720 GetViewTree(vm1(), view_1_1, &views);
721 ASSERT_EQ(3u, views.size());
722 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
723 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[1].ToString());
724 EXPECT_EQ(ViewParentToString(view_3_3, view_2_2), views[2].ToString());
728 // Verifies once Embed() has been invoked the parent can't mutate the children.
729 TEST_F(ViewManagerServiceAppTest, CantModifyChildrenOfEmbeddedView) {
730 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
732 Id view_1_1 = BuildViewId(connection_id_1(), 1);
733 Id view_2_2 = vm_client2()->CreateView(2);
734 ASSERT_TRUE(view_2_2);
735 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
737 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
739 Id view_2_3 = vm_client2()->CreateView(3);
740 ASSERT_TRUE(view_2_3);
741 // Connection 2 shouldn't be able to add anything to the view anymore.
742 ASSERT_FALSE(AddView(vm2(), view_2_2, view_2_3));
744 // Create view 3 in connection 3 and add it to view 3.
745 Id view_3_3 = vm_client3()->CreateView(3);
746 ASSERT_TRUE(view_3_3);
747 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
749 // Connection 2 shouldn't be able to remove view 3.
750 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_3_3));
753 // Verifies client gets a valid id.
754 TEST_F(ViewManagerServiceAppTest, CreateView) {
755 Id view_1_1 = vm_client1()->CreateView(1);
756 ASSERT_TRUE(view_1_1);
757 EXPECT_TRUE(changes1()->empty());
759 // Can't create a view with the same id.
760 ASSERT_EQ(mojo::ERROR_CODE_VALUE_IN_USE,
761 CreateViewWithErrorCode(vm1(), view_1_1));
762 EXPECT_TRUE(changes1()->empty());
764 // Can't create a view with a bogus connection id.
765 EXPECT_EQ(
766 mojo::ERROR_CODE_ILLEGAL_ARGUMENT,
767 CreateViewWithErrorCode(vm1(), BuildViewId(connection_id_1() + 1, 1)));
768 EXPECT_TRUE(changes1()->empty());
771 // Verifies AddView fails when view is already in position.
772 TEST_F(ViewManagerServiceAppTest, AddViewWithNoChange) {
773 Id view_1_2 = vm_client1()->CreateView(2);
774 Id view_1_3 = vm_client1()->CreateView(3);
775 ASSERT_TRUE(view_1_2);
776 ASSERT_TRUE(view_1_3);
778 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
780 // Make 3 a child of 2.
781 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_3));
783 // Try again, this should fail.
784 EXPECT_FALSE(AddView(vm1(), view_1_2, view_1_3));
787 // Verifies AddView fails when view is already in position.
788 TEST_F(ViewManagerServiceAppTest, AddAncestorFails) {
789 Id view_1_2 = vm_client1()->CreateView(2);
790 Id view_1_3 = vm_client1()->CreateView(3);
791 ASSERT_TRUE(view_1_2);
792 ASSERT_TRUE(view_1_3);
794 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
796 // Make 3 a child of 2.
797 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_3));
799 // Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3.
800 EXPECT_FALSE(AddView(vm1(), view_1_3, view_1_2));
803 // Verifies adding to root sends right notifications.
804 TEST_F(ViewManagerServiceAppTest, AddToRoot) {
805 Id view_1_21 = vm_client1()->CreateView(21);
806 Id view_1_3 = vm_client1()->CreateView(3);
807 ASSERT_TRUE(view_1_21);
808 ASSERT_TRUE(view_1_3);
810 Id view_1_1 = BuildViewId(connection_id_1(), 1);
811 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
812 changes2()->clear();
814 // Make 3 a child of 21.
815 ASSERT_TRUE(AddView(vm1(), view_1_21, view_1_3));
817 // Make 21 a child of 1.
818 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_21));
820 // Connection 2 should not be told anything (because the view is from a
821 // different connection). Create a view to ensure we got a response from
822 // the server.
823 ASSERT_TRUE(vm_client2()->CreateView(100));
824 EXPECT_TRUE(changes2()->empty());
827 // Verifies HierarchyChanged is correctly sent for various adds/removes.
828 TEST_F(ViewManagerServiceAppTest, ViewHierarchyChangedViews) {
829 // 1,2->1,11.
830 Id view_1_2 = vm_client1()->CreateView(2);
831 ASSERT_TRUE(view_1_2);
832 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
833 Id view_1_11 = vm_client1()->CreateView(11);
834 ASSERT_TRUE(view_1_11);
835 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_11, true));
836 ASSERT_TRUE(AddView(vm1(), view_1_2, view_1_11));
838 Id view_1_1 = BuildViewId(connection_id_1(), 1);
839 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
840 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
842 ASSERT_TRUE(WaitForAllMessages(vm2()));
843 changes2()->clear();
845 // 1,1->1,2->1,11
847 // Client 2 should not get anything (1,2 is from another connection).
848 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
849 ASSERT_TRUE(WaitForAllMessages(vm2()));
850 EXPECT_TRUE(changes2()->empty());
853 // 0,1->1,1->1,2->1,11.
855 // Client 2 is now connected to the root, so it should have gotten a drawn
856 // notification.
857 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
858 vm_client2_->WaitForChangeCount(1u);
859 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=true",
860 SingleChangeToDescription(*changes2()));
863 // 1,1->1,2->1,11.
865 // Client 2 is no longer connected to the root, should get drawn state
866 // changed.
867 changes2()->clear();
868 ASSERT_TRUE(RemoveViewFromParent(vm1(), view_1_1));
869 vm_client2_->WaitForChangeCount(1);
870 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=false",
871 SingleChangeToDescription(*changes2()));
874 // 1,1->1,2->1,11->1,111.
875 Id view_1_111 = vm_client1()->CreateView(111);
876 ASSERT_TRUE(view_1_111);
877 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_111, true));
879 changes2()->clear();
880 ASSERT_TRUE(AddView(vm1(), view_1_11, view_1_111));
881 ASSERT_TRUE(WaitForAllMessages(vm2()));
882 EXPECT_TRUE(changes2()->empty());
885 // 0,1->1,1->1,2->1,11->1,111
887 changes2()->clear();
888 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
889 vm_client2_->WaitForChangeCount(1);
890 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_1) + " drawn=true",
891 SingleChangeToDescription(*changes2()));
895 TEST_F(ViewManagerServiceAppTest, ViewHierarchyChangedAddingKnownToUnknown) {
896 // Create the following structure: root -> 1 -> 11 and 2->21 (2 has no
897 // parent).
898 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
899 Id view_1_1 = BuildViewId(connection_id_1(), 1);
901 Id view_2_11 = vm_client2()->CreateView(11);
902 Id view_2_2 = vm_client2()->CreateView(2);
903 Id view_2_21 = vm_client2()->CreateView(21);
904 ASSERT_TRUE(view_2_11);
905 ASSERT_TRUE(view_2_2);
906 ASSERT_TRUE(view_2_21);
908 // Set up the hierarchy.
909 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
910 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_11));
911 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_21));
913 // Remove 11, should result in a hierarchy change for the root.
915 changes1()->clear();
916 ASSERT_TRUE(RemoveViewFromParent(vm2(), view_2_11));
918 vm_client1_->WaitForChangeCount(1);
919 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_11) +
920 " new_parent=null old_parent=" + IdToString(view_1_1),
921 SingleChangeToDescription(*changes1()));
924 // Add 2 to 1.
926 changes1()->clear();
927 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
928 vm_client1_->WaitForChangeCount(1);
929 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
930 IdToString(view_1_1) + " old_parent=null",
931 SingleChangeToDescription(*changes1()));
932 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "],[" +
933 ViewParentToString(view_2_21, view_2_2) + "]",
934 ChangeViewDescription(*changes1()));
938 TEST_F(ViewManagerServiceAppTest, ReorderView) {
939 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
941 Id view_2_1 = vm_client2()->CreateView(1);
942 Id view_2_2 = vm_client2()->CreateView(2);
943 Id view_2_3 = vm_client2()->CreateView(3);
944 Id view_1_4 = vm_client1()->CreateView(4); // Peer to 1,1
945 Id view_1_5 = vm_client1()->CreateView(5); // Peer to 1,1
946 Id view_2_6 = vm_client2()->CreateView(6); // Child of 1,2.
947 Id view_2_7 = vm_client2()->CreateView(7); // Unparented.
948 Id view_2_8 = vm_client2()->CreateView(8); // Unparented.
949 ASSERT_TRUE(view_2_1);
950 ASSERT_TRUE(view_2_2);
951 ASSERT_TRUE(view_2_3);
952 ASSERT_TRUE(view_1_4);
953 ASSERT_TRUE(view_1_5);
954 ASSERT_TRUE(view_2_6);
955 ASSERT_TRUE(view_2_7);
956 ASSERT_TRUE(view_2_8);
958 ASSERT_TRUE(AddView(vm2(), view_2_1, view_2_2));
959 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_6));
960 ASSERT_TRUE(AddView(vm2(), view_2_1, view_2_3));
961 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_4));
962 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_5));
963 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_2_1));
966 changes1()->clear();
967 ASSERT_TRUE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_ABOVE));
969 vm_client1_->WaitForChangeCount(1);
970 EXPECT_EQ("Reordered view=" + IdToString(view_2_2) + " relative=" +
971 IdToString(view_2_3) + " direction=above",
972 SingleChangeToDescription(*changes1()));
976 changes1()->clear();
977 ASSERT_TRUE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_BELOW));
979 vm_client1_->WaitForChangeCount(1);
980 EXPECT_EQ("Reordered view=" + IdToString(view_2_2) + " relative=" +
981 IdToString(view_2_3) + " direction=below",
982 SingleChangeToDescription(*changes1()));
985 // view2 is already below view3.
986 EXPECT_FALSE(ReorderView(vm2(), view_2_2, view_2_3, ORDER_DIRECTION_BELOW));
988 // view4 & 5 are unknown to connection2_.
989 EXPECT_FALSE(ReorderView(vm2(), view_1_4, view_1_5, ORDER_DIRECTION_ABOVE));
991 // view6 & view3 have different parents.
992 EXPECT_FALSE(ReorderView(vm1(), view_2_3, view_2_6, ORDER_DIRECTION_ABOVE));
994 // Non-existent view-ids
995 EXPECT_FALSE(ReorderView(vm1(), BuildViewId(connection_id_1(), 27),
996 BuildViewId(connection_id_1(), 28),
997 ORDER_DIRECTION_ABOVE));
999 // view7 & view8 are un-parented.
1000 EXPECT_FALSE(ReorderView(vm1(), view_2_7, view_2_8, ORDER_DIRECTION_ABOVE));
1003 // Verifies DeleteView works.
1004 TEST_F(ViewManagerServiceAppTest, DeleteView) {
1005 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1006 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1007 Id view_2_2 = vm_client2()->CreateView(2);
1008 ASSERT_TRUE(view_2_2);
1010 // Make 2 a child of 1.
1012 changes1()->clear();
1013 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1014 vm_client1_->WaitForChangeCount(1);
1015 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
1016 IdToString(view_1_1) + " old_parent=null",
1017 SingleChangeToDescription(*changes1()));
1020 // Delete 2.
1022 changes1()->clear();
1023 changes2()->clear();
1024 ASSERT_TRUE(DeleteView(vm2(), view_2_2));
1025 EXPECT_TRUE(changes2()->empty());
1027 vm_client1_->WaitForChangeCount(1);
1028 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_2),
1029 SingleChangeToDescription(*changes1()));
1033 // Verifies DeleteView isn't allowed from a separate connection.
1034 TEST_F(ViewManagerServiceAppTest, DeleteViewFromAnotherConnectionDisallowed) {
1035 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1036 EXPECT_FALSE(DeleteView(vm2(), BuildViewId(connection_id_1(), 1)));
1039 // Verifies if a view was deleted and then reused that other clients are
1040 // properly notified.
1041 TEST_F(ViewManagerServiceAppTest, ReuseDeletedViewId) {
1042 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1043 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1044 Id view_2_2 = vm_client2()->CreateView(2);
1045 ASSERT_TRUE(view_2_2);
1047 // Add 2 to 1.
1049 changes1()->clear();
1050 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1051 vm_client1_->WaitForChangeCount(1);
1052 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
1053 IdToString(view_1_1) + " old_parent=null",
1054 SingleChangeToDescription(*changes1()));
1055 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "]",
1056 ChangeViewDescription(*changes1()));
1059 // Delete 2.
1061 changes1()->clear();
1062 ASSERT_TRUE(DeleteView(vm2(), view_2_2));
1064 vm_client1_->WaitForChangeCount(1);
1065 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_2),
1066 SingleChangeToDescription(*changes1()));
1069 // Create 2 again, and add it back to 1. Should get the same notification.
1070 view_2_2 = vm_client2()->CreateView(2);
1071 ASSERT_TRUE(view_2_2);
1073 changes1()->clear();
1074 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1076 vm_client1_->WaitForChangeCount(1);
1077 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_2_2) + " new_parent=" +
1078 IdToString(view_1_1) + " old_parent=null",
1079 SingleChangeToDescription(*changes1()));
1080 EXPECT_EQ("[" + ViewParentToString(view_2_2, view_1_1) + "]",
1081 ChangeViewDescription(*changes1()));
1085 // Assertions for GetViewTree.
1086 TEST_F(ViewManagerServiceAppTest, GetViewTree) {
1087 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1088 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1090 // Create 11 in first connection and make it a child of 1.
1091 Id view_1_11 = vm_client1()->CreateView(11);
1092 ASSERT_TRUE(view_1_11);
1093 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1094 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_11));
1096 // Create two views in second connection, 2 and 3, both children of 1.
1097 Id view_2_2 = vm_client2()->CreateView(2);
1098 Id view_2_3 = vm_client2()->CreateView(3);
1099 ASSERT_TRUE(view_2_2);
1100 ASSERT_TRUE(view_2_3);
1101 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1102 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_3));
1104 // Verifies GetViewTree() on the root. The root connection sees all.
1106 std::vector<TestView> views;
1107 GetViewTree(vm1(), root_view_id(), &views);
1108 ASSERT_EQ(5u, views.size());
1109 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId),
1110 views[0].ToString());
1111 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()),
1112 views[1].ToString());
1113 EXPECT_EQ(ViewParentToString(view_1_11, view_1_1), views[2].ToString());
1114 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[3].ToString());
1115 EXPECT_EQ(ViewParentToString(view_2_3, view_1_1), views[4].ToString());
1118 // Verifies GetViewTree() on the view 1,1 from vm2(). vm2() sees 1,1 as 1,1
1119 // is vm2()'s root and vm2() sees all the views it created.
1121 std::vector<TestView> views;
1122 GetViewTree(vm2(), view_1_1, &views);
1123 ASSERT_EQ(3u, views.size());
1124 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1125 EXPECT_EQ(ViewParentToString(view_2_2, view_1_1), views[1].ToString());
1126 EXPECT_EQ(ViewParentToString(view_2_3, view_1_1), views[2].ToString());
1129 // Connection 2 shouldn't be able to get the root tree.
1131 std::vector<TestView> views;
1132 GetViewTree(vm2(), root_view_id(), &views);
1133 ASSERT_EQ(0u, views.size());
1137 TEST_F(ViewManagerServiceAppTest, SetViewBounds) {
1138 Id view_1_1 = vm_client1()->CreateView(1);
1139 ASSERT_TRUE(view_1_1);
1140 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1142 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1144 changes2()->clear();
1145 ASSERT_TRUE(SetViewBounds(vm1(), view_1_1, 0, 0, 100, 100));
1147 vm_client2_->WaitForChangeCount(1);
1148 EXPECT_EQ("BoundsChanged view=" + IdToString(view_1_1) +
1149 " old_bounds=0,0 0x0 new_bounds=0,0 100x100",
1150 SingleChangeToDescription(*changes2()));
1152 // Should not be possible to change the bounds of a view created by another
1153 // connection.
1154 ASSERT_FALSE(SetViewBounds(vm2(), view_1_1, 0, 0, 0, 0));
1157 // Verify AddView fails when trying to manipulate views in other roots.
1158 TEST_F(ViewManagerServiceAppTest, CantMoveViewsFromOtherRoot) {
1159 // Create 1 and 2 in the first connection.
1160 Id view_1_1 = vm_client1()->CreateView(1);
1161 Id view_1_2 = vm_client1()->CreateView(2);
1162 ASSERT_TRUE(view_1_1);
1163 ASSERT_TRUE(view_1_2);
1165 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1167 // Try to move 2 to be a child of 1 from connection 2. This should fail as 2
1168 // should not be able to access 1.
1169 ASSERT_FALSE(AddView(vm2(), view_1_1, view_1_2));
1171 // Try to reparent 1 to the root. A connection is not allowed to reparent its
1172 // roots.
1173 ASSERT_FALSE(AddView(vm2(), root_view_id(), view_1_1));
1176 // Verify RemoveViewFromParent fails for views that are descendants of the
1177 // roots.
1178 TEST_F(ViewManagerServiceAppTest, CantRemoveViewsInOtherRoots) {
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 // Establish the second connection and give it the root 1.
1189 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1191 // Connection 2 should not be able to remove view 2 or 1 from its parent.
1192 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_1_2));
1193 ASSERT_FALSE(RemoveViewFromParent(vm2(), view_1_1));
1195 // Create views 10 and 11 in 2.
1196 Id view_2_10 = vm_client2()->CreateView(10);
1197 Id view_2_11 = vm_client2()->CreateView(11);
1198 ASSERT_TRUE(view_2_10);
1199 ASSERT_TRUE(view_2_11);
1201 // Parent 11 to 10.
1202 ASSERT_TRUE(AddView(vm2(), view_2_10, view_2_11));
1203 // Remove 11 from 10.
1204 ASSERT_TRUE(RemoveViewFromParent(vm2(), view_2_11));
1206 // Verify nothing was actually removed.
1208 std::vector<TestView> views;
1209 GetViewTree(vm1(), root_view_id(), &views);
1210 ASSERT_EQ(3u, views.size());
1211 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId),
1212 views[0].ToString());
1213 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()),
1214 views[1].ToString());
1215 EXPECT_EQ(ViewParentToString(view_1_2, root_view_id()),
1216 views[2].ToString());
1220 // Verify GetViewTree fails for views that are not descendants of the roots.
1221 TEST_F(ViewManagerServiceAppTest, CantGetViewTreeOfOtherRoots) {
1222 // Create 1 and 2 in the first connection and parent both to the root.
1223 Id view_1_1 = vm_client1()->CreateView(1);
1224 Id view_1_2 = vm_client1()->CreateView(2);
1225 ASSERT_TRUE(view_1_1);
1226 ASSERT_TRUE(view_1_2);
1228 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1229 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_2));
1231 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1233 std::vector<TestView> views;
1235 // Should get nothing for the root.
1236 GetViewTree(vm2(), root_view_id(), &views);
1237 ASSERT_TRUE(views.empty());
1239 // Should get nothing for view 2.
1240 GetViewTree(vm2(), view_1_2, &views);
1241 ASSERT_TRUE(views.empty());
1243 // Should get view 1 if asked for.
1244 GetViewTree(vm2(), view_1_1, &views);
1245 ASSERT_EQ(1u, views.size());
1246 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1249 TEST_F(ViewManagerServiceAppTest, EmbedWithSameViewId) {
1250 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1251 changes2()->clear();
1253 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1254 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), view_1_1));
1256 // Connection2 should have been told of the unembed and delete.
1258 vm_client2_->WaitForChangeCount(2);
1259 EXPECT_EQ("OnUnembed", ChangesToDescription1(*changes2())[0]);
1260 EXPECT_EQ("ViewDeleted view=" + IdToString(view_1_1),
1261 ChangesToDescription1(*changes2())[1]);
1264 // Connection2 has no root. Verify it can't see view 1,1 anymore.
1266 std::vector<TestView> views;
1267 GetViewTree(vm2(), view_1_1, &views);
1268 EXPECT_TRUE(views.empty());
1272 TEST_F(ViewManagerServiceAppTest, EmbedWithSameViewId2) {
1273 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1274 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1275 changes2()->clear();
1277 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), view_1_1));
1279 // Connection2 should have been told about the unembed and delete.
1280 vm_client2_->WaitForChangeCount(2);
1281 changes2()->clear();
1283 // Create a view in the third connection and parent it to the root.
1284 Id view_3_1 = vm_client3()->CreateView(1);
1285 ASSERT_TRUE(view_3_1);
1286 ASSERT_TRUE(AddView(vm3(), view_1_1, view_3_1));
1288 // Connection 1 should have been told about the add (it owns the view).
1290 vm_client1_->WaitForChangeCount(1);
1291 EXPECT_EQ("HierarchyChanged view=" + IdToString(view_3_1) + " new_parent=" +
1292 IdToString(view_1_1) + " old_parent=null",
1293 SingleChangeToDescription(*changes1()));
1296 // Embed 1,1 again.
1298 changes3()->clear();
1300 // We should get a new connection for the new embedding.
1301 scoped_ptr<ViewManagerClientImpl> connection4(EstablishConnectionViaEmbed(
1302 vm1(), view_1_1, EmbedType::NO_REEMBED, nullptr));
1303 ASSERT_TRUE(connection4.get());
1304 EXPECT_EQ("[" + ViewParentToString(view_1_1, kNullParentId) + "]",
1305 ChangeViewDescription(*connection4->tracker()->changes()));
1307 // And 3 should get an unembed and delete.
1308 vm_client3_->WaitForChangeCount(2);
1309 EXPECT_EQ("OnUnembed", ChangesToDescription1(*changes3())[0]);
1310 EXPECT_EQ("ViewDeleted view=" + IdToString(view_1_1),
1311 ChangesToDescription1(*changes3())[1]);
1314 // vm3() has no root. Verify it can't see view 1,1 anymore.
1316 std::vector<TestView> views;
1317 GetViewTree(vm3(), view_1_1, &views);
1318 EXPECT_TRUE(views.empty());
1321 // Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as
1322 // vm3() can no longer see 1,1.
1324 std::vector<TestView> views;
1325 GetViewTree(vm1(), view_1_1, &views);
1326 ASSERT_EQ(1u, views.size());
1327 EXPECT_EQ(ViewParentToString(view_1_1, kNullParentId), views[0].ToString());
1330 // Verify vm3() can still see the view it created 3,1.
1332 std::vector<TestView> views;
1333 GetViewTree(vm3(), view_3_1, &views);
1334 ASSERT_EQ(1u, views.size());
1335 EXPECT_EQ(ViewParentToString(view_3_1, kNullParentId), views[0].ToString());
1339 // Assertions for SetViewVisibility.
1340 TEST_F(ViewManagerServiceAppTest, SetViewVisibility) {
1341 // Create 1 and 2 in the first connection and parent both to the root.
1342 Id view_1_1 = vm_client1()->CreateView(1);
1343 Id view_1_2 = vm_client1()->CreateView(2);
1344 ASSERT_TRUE(view_1_1);
1345 ASSERT_TRUE(view_1_2);
1347 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1349 std::vector<TestView> views;
1350 GetViewTree(vm1(), root_view_id(), &views);
1351 ASSERT_EQ(2u, views.size());
1352 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId) +
1353 " visible=true drawn=true",
1354 views[0].ToString2());
1355 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1356 " visible=false drawn=false",
1357 views[1].ToString2());
1360 // Show all the views.
1361 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1362 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
1364 std::vector<TestView> views;
1365 GetViewTree(vm1(), root_view_id(), &views);
1366 ASSERT_EQ(2u, views.size());
1367 EXPECT_EQ(ViewParentToString(root_view_id(), kNullParentId) +
1368 " visible=true drawn=true",
1369 views[0].ToString2());
1370 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1371 " visible=true drawn=true",
1372 views[1].ToString2());
1375 // Hide 1.
1376 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, false));
1378 std::vector<TestView> views;
1379 GetViewTree(vm1(), view_1_1, &views);
1380 ASSERT_EQ(1u, views.size());
1381 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1382 " visible=false drawn=false",
1383 views[0].ToString2());
1386 // Attach 2 to 1.
1387 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
1389 std::vector<TestView> views;
1390 GetViewTree(vm1(), view_1_1, &views);
1391 ASSERT_EQ(2u, views.size());
1392 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1393 " visible=false drawn=false",
1394 views[0].ToString2());
1395 EXPECT_EQ(
1396 ViewParentToString(view_1_2, view_1_1) + " visible=true drawn=false",
1397 views[1].ToString2());
1400 // Show 1.
1401 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1403 std::vector<TestView> views;
1404 GetViewTree(vm1(), view_1_1, &views);
1405 ASSERT_EQ(2u, views.size());
1406 EXPECT_EQ(ViewParentToString(view_1_1, root_view_id()) +
1407 " visible=true drawn=true",
1408 views[0].ToString2());
1409 EXPECT_EQ(
1410 ViewParentToString(view_1_2, view_1_1) + " visible=true drawn=true",
1411 views[1].ToString2());
1415 // Assertions for SetViewVisibility sending notifications.
1416 TEST_F(ViewManagerServiceAppTest, SetViewVisibilityNotifications) {
1417 // Create 1,1 and 1,2. 1,2 is made a child of 1,1 and 1,1 a child of the root.
1418 Id view_1_1 = vm_client1()->CreateView(1);
1419 ASSERT_TRUE(view_1_1);
1420 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1421 Id view_1_2 = vm_client1()->CreateView(2);
1422 ASSERT_TRUE(view_1_2);
1423 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, true));
1424 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1425 ASSERT_TRUE(AddView(vm1(), view_1_1, view_1_2));
1427 // Establish the second connection at 1,2.
1428 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnectionWithRoot(view_1_2));
1430 // Add 2,3 as a child of 1,2.
1431 Id view_2_3 = vm_client2()->CreateView(3);
1432 ASSERT_TRUE(view_2_3);
1433 ASSERT_TRUE(SetViewVisibility(vm2(), view_2_3, true));
1434 ASSERT_TRUE(AddView(vm2(), view_1_2, view_2_3));
1435 WaitForAllMessages(vm1());
1437 changes2()->clear();
1438 // Hide 1,2 from connection 1. Connection 2 should see this.
1439 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_2, false));
1441 vm_client2_->WaitForChangeCount(1);
1442 EXPECT_EQ(
1443 "VisibilityChanged view=" + IdToString(view_1_2) + " visible=false",
1444 SingleChangeToDescription(*changes2()));
1447 changes1()->clear();
1448 // Show 1,2 from connection 2, connection 1 should be notified.
1449 ASSERT_TRUE(SetViewVisibility(vm2(), view_1_2, true));
1451 vm_client1_->WaitForChangeCount(1);
1452 EXPECT_EQ(
1453 "VisibilityChanged view=" + IdToString(view_1_2) + " visible=true",
1454 SingleChangeToDescription(*changes1()));
1457 changes2()->clear();
1458 // Hide 1,1, connection 2 should be told the draw state changed.
1459 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, false));
1461 vm_client2_->WaitForChangeCount(1);
1462 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=false",
1463 SingleChangeToDescription(*changes2()));
1466 changes2()->clear();
1467 // Show 1,1 from connection 1. Connection 2 should see this.
1468 ASSERT_TRUE(SetViewVisibility(vm1(), view_1_1, true));
1470 vm_client2_->WaitForChangeCount(1);
1471 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=true",
1472 SingleChangeToDescription(*changes2()));
1475 // Change visibility of 2,3, connection 1 should see this.
1476 changes1()->clear();
1477 ASSERT_TRUE(SetViewVisibility(vm2(), view_2_3, false));
1479 vm_client1_->WaitForChangeCount(1);
1480 EXPECT_EQ(
1481 "VisibilityChanged view=" + IdToString(view_2_3) + " visible=false",
1482 SingleChangeToDescription(*changes1()));
1485 changes2()->clear();
1486 // Remove 1,1 from the root, connection 2 should see drawn state changed.
1487 ASSERT_TRUE(RemoveViewFromParent(vm1(), view_1_1));
1489 vm_client2_->WaitForChangeCount(1);
1490 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=false",
1491 SingleChangeToDescription(*changes2()));
1494 changes2()->clear();
1495 // Add 1,1 back to the root, connection 2 should see drawn state changed.
1496 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1498 vm_client2_->WaitForChangeCount(1);
1499 EXPECT_EQ("DrawnStateChanged view=" + IdToString(view_1_2) + " drawn=true",
1500 SingleChangeToDescription(*changes2()));
1504 TEST_F(ViewManagerServiceAppTest, SetViewProperty) {
1505 Id view_1_1 = vm_client1()->CreateView(1);
1506 ASSERT_TRUE(view_1_1);
1508 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1509 changes2()->clear();
1511 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1513 std::vector<TestView> views;
1514 GetViewTree(vm1(), root_view_id(), &views);
1515 ASSERT_EQ(2u, views.size());
1516 EXPECT_EQ(root_view_id(), views[0].view_id);
1517 EXPECT_EQ(view_1_1, views[1].view_id);
1518 ASSERT_EQ(0u, views[1].properties.size());
1521 // Set properties on 1.
1522 changes2()->clear();
1523 std::vector<uint8_t> one(1, '1');
1524 ASSERT_TRUE(SetViewProperty(vm1(), view_1_1, "one", &one));
1526 vm_client2_->WaitForChangeCount(1);
1527 EXPECT_EQ(
1528 "PropertyChanged view=" + IdToString(view_1_1) + " key=one value=1",
1529 SingleChangeToDescription(*changes2()));
1532 // Test that our properties exist in the view tree
1534 std::vector<TestView> views;
1535 GetViewTree(vm1(), view_1_1, &views);
1536 ASSERT_EQ(1u, views.size());
1537 ASSERT_EQ(1u, views[0].properties.size());
1538 EXPECT_EQ(one, views[0].properties["one"]);
1541 changes2()->clear();
1542 // Set back to null.
1543 ASSERT_TRUE(SetViewProperty(vm1(), view_1_1, "one", NULL));
1545 vm_client2_->WaitForChangeCount(1);
1546 EXPECT_EQ(
1547 "PropertyChanged view=" + IdToString(view_1_1) + " key=one value=NULL",
1548 SingleChangeToDescription(*changes2()));
1552 TEST_F(ViewManagerServiceAppTest, OnEmbeddedAppDisconnected) {
1553 // Create connection 2 and 3.
1554 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1555 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1556 Id view_2_2 = vm_client2()->CreateView(2);
1557 ASSERT_TRUE(view_2_2);
1558 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1559 changes2()->clear();
1560 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
1562 // Close connection 3. Connection 2 (which had previously embedded 3) should
1563 // be notified of this.
1564 vm_client3_.reset();
1565 vm_client2_->WaitForChangeCount(1);
1566 EXPECT_EQ("OnEmbeddedAppDisconnected view=" + IdToString(view_2_2),
1567 SingleChangeToDescription(*changes2()));
1570 // Verifies when the parent of an Embed() is destroyed the embedded app gets
1571 // a ViewDeleted (and doesn't trigger a DCHECK).
1572 TEST_F(ViewManagerServiceAppTest, OnParentOfEmbedDisconnects) {
1573 // Create connection 2 and 3.
1574 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1575 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1576 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1577 Id view_2_2 = vm_client2()->CreateView(2);
1578 Id view_2_3 = vm_client2()->CreateView(3);
1579 ASSERT_TRUE(view_2_2);
1580 ASSERT_TRUE(view_2_3);
1581 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1582 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_3));
1583 changes2()->clear();
1584 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_3));
1585 changes3()->clear();
1587 // Close connection 2. Connection 3 should get a delete (for its root).
1588 vm_client2_.reset();
1589 vm_client3_->WaitForChangeCount(1);
1590 EXPECT_EQ("ViewDeleted view=" + IdToString(view_2_3),
1591 SingleChangeToDescription(*changes3()));
1594 // Verifies ViewManagerServiceImpl doesn't incorrectly erase from its internal
1595 // map when a view from another connection with the same view_id is removed.
1596 TEST_F(ViewManagerServiceAppTest, DontCleanMapOnDestroy) {
1597 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1598 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1599 ASSERT_TRUE(vm_client2()->CreateView(1));
1600 changes1()->clear();
1601 vm_client2_.reset();
1602 vm_client1_->WaitForChangeCount(1);
1603 EXPECT_EQ("OnEmbeddedAppDisconnected view=" + IdToString(view_1_1),
1604 SingleChangeToDescription(*changes1()));
1605 std::vector<TestView> views;
1606 GetViewTree(vm1(), view_1_1, &views);
1607 EXPECT_FALSE(views.empty());
1610 TEST_F(ViewManagerServiceAppTest, CloneAndAnimate) {
1611 // Create connection 2 and 3.
1612 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1613 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1614 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1615 Id view_2_2 = vm_client2()->CreateView(2);
1616 Id view_2_3 = vm_client2()->CreateView(3);
1617 ASSERT_TRUE(view_2_2);
1618 ASSERT_TRUE(view_2_3);
1619 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1620 ASSERT_TRUE(AddView(vm2(), view_2_2, view_2_3));
1621 changes2()->clear();
1623 ASSERT_TRUE(WaitForAllMessages(vm1()));
1624 changes1()->clear();
1626 view_manager_root_->CloneAndAnimate(view_2_3);
1627 ASSERT_TRUE(WaitForAllMessages(vm1()));
1629 ASSERT_TRUE(WaitForAllMessages(vm1()));
1630 ASSERT_TRUE(WaitForAllMessages(vm2()));
1632 // No messages should have been received.
1633 EXPECT_TRUE(changes1()->empty());
1634 EXPECT_TRUE(changes2()->empty());
1636 // No one should be able to see the cloned tree.
1637 std::vector<TestView> views;
1638 GetViewTree(vm1(), view_1_1, &views);
1639 EXPECT_FALSE(HasClonedView(views));
1640 views.clear();
1642 GetViewTree(vm2(), view_1_1, &views);
1643 EXPECT_FALSE(HasClonedView(views));
1646 // Verifies Embed() works when supplying a ViewManagerClient.
1647 TEST_F(ViewManagerServiceAppTest, EmbedSupplyingViewManagerClient) {
1648 ASSERT_TRUE(vm_client1()->CreateView(1));
1650 ViewManagerClientImpl client2(application_impl());
1651 mojo::ViewManagerClientPtr client2_ptr;
1652 mojo::Binding<ViewManagerClient> client2_binding(&client2, &client2_ptr);
1653 ASSERT_TRUE(
1654 Embed(vm1(), BuildViewId(connection_id_1(), 1), client2_ptr.Pass()));
1655 client2.WaitForOnEmbed();
1656 EXPECT_EQ("OnEmbed",
1657 SingleChangeToDescription(*client2.tracker()->changes()));
1660 TEST_F(ViewManagerServiceAppTest, OnWillEmbed) {
1661 // Create connections 2 and 3, marking 2 as an embed root.
1662 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1663 Id view_1_1 = BuildViewId(connection_id_1(), 1);
1664 ASSERT_TRUE(AddView(vm1(), root_view_id(), view_1_1));
1665 Id view_2_2 = vm_client2()->CreateView(2);
1666 ASSERT_TRUE(view_2_2);
1667 ASSERT_TRUE(AddView(vm2(), view_1_1, view_2_2));
1668 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), view_2_2));
1669 Id view_3_3 = vm_client3()->CreateView(3);
1670 ASSERT_TRUE(view_3_3);
1671 ASSERT_TRUE(AddView(vm3(), view_2_2, view_3_3));
1672 vm2()->SetEmbedRoot();
1673 // Make sure the viewmanager processed the SetEmbedRoot() call.
1674 ASSERT_TRUE(WaitForAllMessages(vm2()));
1675 changes2()->clear();
1677 // Embed 4 into 3, connection 2 should get the OnWillEmbed.
1678 scoped_ptr<ViewManagerClientImpl> connection4(EstablishConnectionViaEmbed(
1679 vm3(), view_3_3, EmbedType::ALLOW_REEMBED, nullptr));
1680 ASSERT_TRUE(connection4.get());
1681 EXPECT_EQ("OnEmbedForDescendant view=" + IdToString(view_3_3),
1682 SingleChangeToDescription(*changes2()));
1684 // Mark 3 as an embed root.
1685 vm3()->SetEmbedRoot();
1686 // Make sure the viewmanager processed the SetEmbedRoot() call.
1687 ASSERT_TRUE(WaitForAllMessages(vm3()));
1688 changes2()->clear();
1689 changes3()->clear();
1691 // Embed 5 into 4. Only 3 should get the will embed.
1692 Id view_4_4 = connection4->CreateView(4);
1693 ASSERT_TRUE(view_4_4);
1694 ASSERT_TRUE(AddView(connection4->service(), view_3_3, view_4_4));
1695 scoped_ptr<ViewManagerClientImpl> connection5(EstablishConnectionViaEmbed(
1696 connection4->service(), view_4_4, EmbedType::ALLOW_REEMBED, nullptr));
1697 ASSERT_TRUE(connection5.get());
1698 EXPECT_EQ("OnEmbedForDescendant view=" + IdToString(view_4_4),
1699 SingleChangeToDescription(*changes3()));
1700 ASSERT_TRUE(changes2()->empty());
1703 // TODO(sky): need to better track changes to initial connection. For example,
1704 // that SetBounsdViews/AddView and the like don't result in messages to the
1705 // originating connection.
1707 // TODO(sky): make sure coverage of what was
1708 // ViewManagerTest.SecondEmbedRoot_InitService and
1709 // ViewManagerTest.MultipleEmbedRootsBeforeWTHReady gets added to window manager
1710 // tests.
1712 } // namespace view_manager