Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / components / view_manager / view_manager_service_apptest.cc
blobbb533c729b4503883b5ff58bd3c0ceecf8edbd6f
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 "components/view_manager/ids.h"
9 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
10 #include "components/view_manager/test_change_tracker.h"
11 #include "components/window_manager/public/interfaces/window_manager.mojom.h"
12 #include "components/window_manager/public/interfaces/window_manager_internal.mojom.h"
13 #include "mojo/application/application_test_base_chromium.h"
14 #include "third_party/mojo/src/mojo/public/cpp/application/application_delegate.h"
15 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.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 // The following functions call through to the supplied ViewManagerService. They
73 // block until call completes and return the result.
74 bool CreateView(ViewManagerService* vm, Id view_id) {
75 ErrorCode result = ERROR_CODE_NONE;
76 base::RunLoop run_loop;
77 vm->CreateView(view_id,
78 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
79 run_loop.Run();
80 return result == ERROR_CODE_NONE;
83 bool EmbedUrl(ViewManagerService* vm, const String& url, Id root_id) {
84 bool result = false;
85 base::RunLoop run_loop;
87 vm->EmbedUrl(url, root_id, nullptr, nullptr,
88 base::Bind(&BoolResultCallback, &run_loop, &result));
90 run_loop.Run();
91 return result;
94 bool Embed(ViewManagerService* vm,
95 Id root_id,
96 mojo::ViewManagerClientPtr client) {
97 bool result = false;
98 base::RunLoop run_loop;
100 vm->Embed(root_id, client.Pass(),
101 base::Bind(&BoolResultCallback, &run_loop, &result));
103 run_loop.Run();
104 return result;
107 ErrorCode CreateViewWithErrorCode(ViewManagerService* vm, Id view_id) {
108 ErrorCode result = ERROR_CODE_NONE;
109 base::RunLoop run_loop;
110 vm->CreateView(view_id,
111 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
112 run_loop.Run();
113 return result;
116 bool AddView(ViewManagerService* vm, Id parent, Id child) {
117 bool result = false;
118 base::RunLoop run_loop;
119 vm->AddView(parent, child,
120 base::Bind(&BoolResultCallback, &run_loop, &result));
121 run_loop.Run();
122 return result;
125 bool RemoveViewFromParent(ViewManagerService* vm, Id view_id) {
126 bool result = false;
127 base::RunLoop run_loop;
128 vm->RemoveViewFromParent(view_id,
129 base::Bind(&BoolResultCallback, &run_loop, &result));
130 run_loop.Run();
131 return result;
134 bool ReorderView(ViewManagerService* vm,
135 Id view_id,
136 Id relative_view_id,
137 OrderDirection direction) {
138 bool result = false;
139 base::RunLoop run_loop;
140 vm->ReorderView(view_id, relative_view_id, direction,
141 base::Bind(&BoolResultCallback, &run_loop, &result));
142 run_loop.Run();
143 return result;
146 void GetViewTree(ViewManagerService* vm,
147 Id view_id,
148 std::vector<TestView>* views) {
149 base::RunLoop run_loop;
150 vm->GetViewTree(view_id,
151 base::Bind(&ViewTreeResultCallback, &run_loop, views));
152 run_loop.Run();
155 bool DeleteView(ViewManagerService* vm, Id view_id) {
156 base::RunLoop run_loop;
157 bool result = false;
158 vm->DeleteView(view_id, base::Bind(&BoolResultCallback, &run_loop, &result));
159 run_loop.Run();
160 return result;
163 bool SetViewBounds(ViewManagerService* vm,
164 Id view_id,
165 int x,
166 int y,
167 int w,
168 int h) {
169 base::RunLoop run_loop;
170 bool result = false;
171 RectPtr rect(mojo::Rect::New());
172 rect->x = x;
173 rect->y = y;
174 rect->width = w;
175 rect->height = h;
176 vm->SetViewBounds(view_id, rect.Pass(),
177 base::Bind(&BoolResultCallback, &run_loop, &result));
178 run_loop.Run();
179 return result;
182 bool SetViewVisibility(ViewManagerService* vm, Id view_id, bool visible) {
183 base::RunLoop run_loop;
184 bool result = false;
185 vm->SetViewVisibility(view_id, visible,
186 base::Bind(&BoolResultCallback, &run_loop, &result));
187 run_loop.Run();
188 return result;
191 bool SetViewProperty(ViewManagerService* vm,
192 Id view_id,
193 const std::string& name,
194 const std::vector<uint8_t>* data) {
195 base::RunLoop run_loop;
196 bool result = false;
197 Array<uint8_t> mojo_data;
198 if (data)
199 mojo_data = Array<uint8_t>::From(*data);
200 vm->SetViewProperty(view_id, name, mojo_data.Pass(),
201 base::Bind(&BoolResultCallback, &run_loop, &result));
202 run_loop.Run();
203 return result;
206 // Utility functions -----------------------------------------------------------
208 // Waits for all messages to be received by |vm|. This is done by attempting to
209 // create a bogus view. When we get the response we know all messages have been
210 // processed.
211 bool WaitForAllMessages(ViewManagerService* vm) {
212 ErrorCode result = ERROR_CODE_NONE;
213 base::RunLoop run_loop;
214 vm->CreateView(ViewIdToTransportId(InvalidViewId()),
215 base::Bind(&ErrorCodeResultCallback, &run_loop, &result));
216 run_loop.Run();
217 return result != ERROR_CODE_NONE;
220 bool HasClonedView(const std::vector<TestView>& views) {
221 for (size_t i = 0; i < views.size(); ++i)
222 if (views[i].view_id == ViewIdToTransportId(ClonedViewId()))
223 return true;
224 return false;
227 // -----------------------------------------------------------------------------
229 // A ViewManagerClient implementation that logs all changes to a tracker.
230 class ViewManagerClientImpl : public mojo::ViewManagerClient,
231 public TestChangeTracker::Delegate {
232 public:
233 ViewManagerClientImpl() : binding_(this) { tracker_.set_delegate(this); }
235 void Bind(mojo::InterfaceRequest<mojo::ViewManagerClient> request) {
236 binding_.Bind(request.Pass());
239 mojo::ViewManagerService* service() { return service_.get(); }
240 TestChangeTracker* tracker() { return &tracker_; }
242 // Runs a nested MessageLoop until |count| changes (calls to
243 // ViewManagerClient functions) have been received.
244 void WaitForChangeCount(size_t count) {
245 if (count == tracker_.changes()->size())
246 return;
248 ASSERT_TRUE(wait_state_.get() == nullptr);
249 wait_state_.reset(new WaitState);
250 wait_state_->change_count = count;
251 wait_state_->run_loop.Run();
252 wait_state_.reset();
255 // Runs a nested MessageLoop until OnEmbed() has been encountered.
256 void WaitForOnEmbed() {
257 if (service_)
258 return;
259 embed_run_loop_.reset(new base::RunLoop);
260 embed_run_loop_->Run();
261 embed_run_loop_.reset();
264 bool WaitForIncomingMethodCall() {
265 return binding_.WaitForIncomingMethodCall();
268 private:
269 // Used when running a nested MessageLoop.
270 struct WaitState {
271 WaitState() : change_count(0) {}
273 // Number of changes waiting for.
274 size_t change_count;
275 base::RunLoop run_loop;
278 // TestChangeTracker::Delegate:
279 void OnChangeAdded() override {
280 if (wait_state_.get() &&
281 wait_state_->change_count == tracker_.changes()->size()) {
282 wait_state_->run_loop.Quit();
286 // ViewManagerClient:
287 void OnEmbed(ConnectionSpecificId connection_id,
288 const String& creator_url,
289 ViewDataPtr root,
290 mojo::ViewManagerServicePtr view_manager_service,
291 InterfaceRequest<ServiceProvider> services,
292 ServiceProviderPtr exposed_services,
293 mojo::Id focused_view_id) override {
294 // TODO(sky): add coverage of |focused_view_id|.
295 service_ = view_manager_service.Pass();
296 tracker()->OnEmbed(connection_id, creator_url, root.Pass());
297 if (embed_run_loop_)
298 embed_run_loop_->Quit();
300 void OnEmbeddedAppDisconnected(Id view_id) override {
301 tracker()->OnEmbeddedAppDisconnected(view_id);
303 void OnViewBoundsChanged(Id view_id,
304 RectPtr old_bounds,
305 RectPtr new_bounds) override {
306 tracker()->OnViewBoundsChanged(view_id, old_bounds.Pass(),
307 new_bounds.Pass());
309 void OnViewViewportMetricsChanged(ViewportMetricsPtr old_metrics,
310 ViewportMetricsPtr new_metrics) override {
311 tracker()->OnViewViewportMetricsChanged(old_metrics.Pass(),
312 new_metrics.Pass());
314 void OnViewHierarchyChanged(Id view,
315 Id new_parent,
316 Id old_parent,
317 Array<ViewDataPtr> views) override {
318 tracker()->OnViewHierarchyChanged(view, new_parent, old_parent,
319 views.Pass());
321 void OnViewReordered(Id view_id,
322 Id relative_view_id,
323 OrderDirection direction) override {
324 tracker()->OnViewReordered(view_id, relative_view_id, direction);
326 void OnViewDeleted(Id view) override { tracker()->OnViewDeleted(view); }
327 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
328 tracker()->OnViewVisibilityChanged(view, visible);
330 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
331 tracker()->OnViewDrawnStateChanged(view, drawn);
333 void OnViewInputEvent(Id view_id,
334 EventPtr event,
335 const Callback<void()>& callback) override {
336 tracker()->OnViewInputEvent(view_id, event.Pass());
337 callback.Run();
339 void OnViewSharedPropertyChanged(uint32_t view,
340 const String& name,
341 Array<uint8_t> new_data) override {
342 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
344 // TODO(sky): add testing coverage.
345 void OnViewFocused(uint32_t focused_view_id) override {}
347 TestChangeTracker tracker_;
349 mojo::ViewManagerServicePtr service_;
351 // If non-null we're waiting for OnEmbed() using this RunLoop.
352 scoped_ptr<base::RunLoop> embed_run_loop_;
354 // If non-null we're waiting for a certain number of change notifications to
355 // be encountered.
356 scoped_ptr<WaitState> wait_state_;
358 mojo::Binding<ViewManagerClient> binding_;
359 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
362 // -----------------------------------------------------------------------------
364 // InterfaceFactory for vending ViewManagerClientImpls.
365 class ViewManagerClientFactory
366 : public mojo::InterfaceFactory<ViewManagerClient> {
367 public:
368 ViewManagerClientFactory() {}
369 ~ViewManagerClientFactory() override {}
371 // Runs a nested MessageLoop until a new instance has been created.
372 scoped_ptr<ViewManagerClientImpl> WaitForInstance() {
373 if (!client_impl_.get()) {
374 DCHECK(!run_loop_.get());
375 run_loop_.reset(new base::RunLoop);
376 run_loop_->Run();
377 run_loop_.reset();
379 return client_impl_.Pass();
382 private:
383 // InterfaceFactory<ViewManagerClient>:
384 void Create(ApplicationConnection* connection,
385 InterfaceRequest<ViewManagerClient> request) override {
386 client_impl_.reset(new ViewManagerClientImpl);
387 client_impl_->Bind(request.Pass());
388 if (run_loop_.get())
389 run_loop_->Quit();
392 scoped_ptr<ViewManagerClientImpl> client_impl_;
393 scoped_ptr<base::RunLoop> run_loop_;
395 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientFactory);
398 class ViewManagerServiceAppTest
399 : public mojo::test::ApplicationTestBase,
400 public ApplicationDelegate,
401 public mojo::InterfaceFactory<mojo::WindowManagerInternal>,
402 public mojo::WindowManagerInternal {
403 public:
404 ViewManagerServiceAppTest() : wm_internal_binding_(this) {}
405 ~ViewManagerServiceAppTest() override {}
407 protected:
408 // Returns the changes from the various connections.
409 std::vector<Change>* changes1() { return vm_client1_.tracker()->changes(); }
410 std::vector<Change>* changes2() { return vm_client2_->tracker()->changes(); }
411 std::vector<Change>* changes3() { return vm_client3_->tracker()->changes(); }
413 // Various connections. |vm1()|, being the first connection, has special
414 // permissions (it's treated as the window manager).
415 ViewManagerService* vm1() { return vm1_.get(); }
416 ViewManagerService* vm2() { return vm_client2_->service(); }
417 ViewManagerService* vm3() { return vm_client3_->service(); }
419 void EstablishSecondConnectionWithRoot(Id root_id) {
420 ASSERT_TRUE(vm_client2_.get() == nullptr);
421 vm_client2_ = EstablishConnectionViaEmbed(vm1(), root_id);
422 ASSERT_TRUE(vm_client2_.get() != nullptr);
425 void EstablishSecondConnection(bool create_initial_view) {
426 if (create_initial_view)
427 ASSERT_TRUE(CreateView(vm1_.get(), BuildViewId(1, 1)));
428 ASSERT_NO_FATAL_FAILURE(
429 EstablishSecondConnectionWithRoot(BuildViewId(1, 1)));
431 if (create_initial_view)
432 EXPECT_EQ("[view=1,1 parent=null]", ChangeViewDescription(*changes2()));
435 void EstablishThirdConnection(ViewManagerService* owner, Id root_id) {
436 ASSERT_TRUE(vm_client3_.get() == nullptr);
437 vm_client3_ = EstablishConnectionViaEmbed(owner, root_id);
438 ASSERT_TRUE(vm_client3_.get() != nullptr);
441 // Establishes a new connection by way of Embed() on the specified
442 // ViewManagerService.
443 scoped_ptr<ViewManagerClientImpl> EstablishConnectionViaEmbed(
444 ViewManagerService* owner,
445 Id root_id) {
446 if (!EmbedUrl(owner, application_impl()->url(), root_id)) {
447 ADD_FAILURE() << "Embed() failed";
448 return nullptr;
450 scoped_ptr<ViewManagerClientImpl> client =
451 client_factory_.WaitForInstance();
452 if (!client.get()) {
453 ADD_FAILURE() << "WaitForInstance failed";
454 return nullptr;
456 client->WaitForOnEmbed();
458 const std::string expected_creator =
459 owner == vm1() ? "mojo:window_manager" : application_impl()->url();
460 EXPECT_EQ("OnEmbed creator=" + expected_creator,
461 SingleChangeToDescription(*client->tracker()->changes()));
462 return client.Pass();
465 // ApplicationTestBase:
466 ApplicationDelegate* GetApplicationDelegate() override { return this; }
467 void SetUp() override {
468 ApplicationTestBase::SetUp();
469 ApplicationConnection* vm_connection =
470 application_impl()->ConnectToApplication("mojo:view_manager");
471 vm_connection->AddService(this);
472 vm_connection->ConnectToService(&vm1_);
473 vm_connection->ConnectToService(&wm_internal_client_);
474 // Spin a run loop until the view manager service sends us the
475 // ViewManagerClient pipe to use for the "window manager" connection.
476 view_manager_setup_run_loop_.reset(new base::RunLoop);
477 view_manager_setup_run_loop_->Run();
478 view_manager_setup_run_loop_ = nullptr;
479 // Next we should get an embed call on the "window manager" client.
480 vm_client1_.WaitForIncomingMethodCall();
481 ASSERT_EQ(1u, changes1()->size());
482 EXPECT_EQ(CHANGE_TYPE_EMBED, (*changes1())[0].type);
483 // All these tests assume 1 for the client id. The only real assertion here
484 // is the client id is not zero, but adding this as rest of code here
485 // assumes 1.
486 ASSERT_EQ(1, (*changes1())[0].connection_id);
487 changes1()->clear();
490 // ApplicationDelegate implementation.
491 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
492 connection->AddService(&client_factory_);
493 return true;
496 // mojo::InterfaceFactory<mojo::WindowManagerInternal> implementation.
497 void Create(
498 ApplicationConnection* connection,
499 mojo::InterfaceRequest<mojo::WindowManagerInternal> request) override {
500 DCHECK(!wm_internal_binding_.is_bound());
501 wm_internal_binding_.Bind(request.Pass());
504 // mojo::WindowManagerInternal implementation.
505 void SetViewManagerClient(
506 mojo::ScopedMessagePipeHandle view_manager_client_request) override {
507 auto typed_request = mojo::MakeRequest<mojo::ViewManagerClient>(
508 view_manager_client_request.Pass());
509 vm_client1_.Bind(typed_request.Pass());
510 view_manager_setup_run_loop_->Quit();
512 void OnAccelerator(mojo::EventPtr event) override {}
514 mojo::Binding<mojo::WindowManagerInternal> wm_internal_binding_;
515 mojo::WindowManagerInternalClientPtr wm_internal_client_;
516 ViewManagerClientImpl vm_client1_;
517 scoped_ptr<ViewManagerClientImpl> vm_client2_;
518 scoped_ptr<ViewManagerClientImpl> vm_client3_;
520 private:
521 mojo::ViewManagerServicePtr vm1_;
522 ViewManagerClientFactory client_factory_;
523 scoped_ptr<base::RunLoop> view_manager_setup_run_loop_;
525 MOJO_DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceAppTest);
528 // Verifies two clients/connections get different ids.
529 TEST_F(ViewManagerServiceAppTest, TwoClientsGetDifferentConnectionIds) {
530 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
532 // It isn't strictly necessary that the second connection gets 2, but these
533 // tests are written assuming that is the case. The key thing is the
534 // connection ids of |connection_| and |connection2_| differ.
535 ASSERT_EQ(1u, changes2()->size());
536 ASSERT_EQ(2, (*changes2())[0].connection_id);
539 // Verifies when Embed() is invoked any child views are removed.
540 TEST_F(ViewManagerServiceAppTest, ViewsRemovedWhenEmbedding) {
541 // Two views 1 and 2. 2 is parented to 1.
542 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
543 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
544 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 2)));
546 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
547 EXPECT_EQ("[view=1,1 parent=null]", ChangeViewDescription(*changes2()));
549 // Embed() removed view 2.
551 std::vector<TestView> views;
552 GetViewTree(vm1(), BuildViewId(1, 2), &views);
553 EXPECT_EQ("view=1,2 parent=null", SingleViewDescription(views));
556 // vm2 should not see view 2.
558 std::vector<TestView> views;
559 GetViewTree(vm2(), BuildViewId(1, 1), &views);
560 EXPECT_EQ("view=1,1 parent=null", SingleViewDescription(views));
563 std::vector<TestView> views;
564 GetViewTree(vm2(), BuildViewId(1, 2), &views);
565 EXPECT_TRUE(views.empty());
568 // Views 3 and 4 in connection 2.
569 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
570 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 4)));
571 ASSERT_TRUE(AddView(vm2(), BuildViewId(2, 3), BuildViewId(2, 4)));
573 // Connection 3 rooted at 2.
574 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), BuildViewId(2, 3)));
576 // View 4 should no longer have a parent.
578 std::vector<TestView> views;
579 GetViewTree(vm2(), BuildViewId(2, 3), &views);
580 EXPECT_EQ("view=2,3 parent=null", SingleViewDescription(views));
582 views.clear();
583 GetViewTree(vm2(), BuildViewId(2, 4), &views);
584 EXPECT_EQ("view=2,4 parent=null", SingleViewDescription(views));
587 // And view 4 should not be visible to connection 3.
589 std::vector<TestView> views;
590 GetViewTree(vm3(), BuildViewId(2, 3), &views);
591 EXPECT_EQ("view=2,3 parent=null", SingleViewDescription(views));
595 // Verifies once Embed() has been invoked the parent connection can't see any
596 // children.
597 TEST_F(ViewManagerServiceAppTest, CantAccessChildrenOfEmbeddedView) {
598 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
600 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
601 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
603 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), BuildViewId(2, 2)));
605 ASSERT_TRUE(CreateView(vm3(), BuildViewId(3, 3)));
606 ASSERT_TRUE(AddView(vm3(), BuildViewId(2, 2), BuildViewId(3, 3)));
608 // Even though 3 is a child of 2 connection 2 can't see 3 as it's from a
609 // different connection.
611 std::vector<TestView> views;
612 GetViewTree(vm2(), BuildViewId(2, 2), &views);
613 EXPECT_EQ("view=2,2 parent=1,1", SingleViewDescription(views));
616 // Connection 2 shouldn't be able to get view 3 at all.
618 std::vector<TestView> views;
619 GetViewTree(vm2(), BuildViewId(3, 3), &views);
620 EXPECT_TRUE(views.empty());
623 // Connection 1 should be able to see it all (its the root).
625 std::vector<TestView> views;
626 GetViewTree(vm1(), BuildViewId(1, 1), &views);
627 ASSERT_EQ(3u, views.size());
628 EXPECT_EQ("view=1,1 parent=null", views[0].ToString());
629 EXPECT_EQ("view=2,2 parent=1,1", views[1].ToString());
630 EXPECT_EQ("view=3,3 parent=2,2", views[2].ToString());
634 // Verifies once Embed() has been invoked the parent can't mutate the children.
635 TEST_F(ViewManagerServiceAppTest, CantModifyChildrenOfEmbeddedView) {
636 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
638 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
639 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
641 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), BuildViewId(2, 2)));
643 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
644 // Connection 2 shouldn't be able to add anything to the view anymore.
645 ASSERT_FALSE(AddView(vm2(), BuildViewId(2, 2), BuildViewId(2, 3)));
647 // Create view 3 in connection 3 and add it to view 3.
648 ASSERT_TRUE(CreateView(vm3(), BuildViewId(3, 3)));
649 ASSERT_TRUE(AddView(vm3(), BuildViewId(2, 2), BuildViewId(3, 3)));
651 // Connection 2 shouldn't be able to remove view 3.
652 ASSERT_FALSE(RemoveViewFromParent(vm2(), BuildViewId(3, 3)));
655 // Verifies client gets a valid id.
656 TEST_F(ViewManagerServiceAppTest, CreateView) {
657 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
658 EXPECT_TRUE(changes1()->empty());
660 // Can't create a view with the same id.
661 ASSERT_EQ(mojo::ERROR_CODE_VALUE_IN_USE,
662 CreateViewWithErrorCode(vm1(), BuildViewId(1, 1)));
663 EXPECT_TRUE(changes1()->empty());
665 // Can't create a view with a bogus connection id.
666 EXPECT_EQ(mojo::ERROR_CODE_ILLEGAL_ARGUMENT,
667 CreateViewWithErrorCode(vm1(), BuildViewId(2, 1)));
668 EXPECT_TRUE(changes1()->empty());
671 // Verifies AddView fails when view is already in position.
672 TEST_F(ViewManagerServiceAppTest, AddViewWithNoChange) {
673 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
674 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 3)));
676 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
678 // Make 3 a child of 2.
679 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 2), BuildViewId(1, 3)));
681 // Try again, this should fail.
682 EXPECT_FALSE(AddView(vm1(), BuildViewId(1, 2), BuildViewId(1, 3)));
685 // Verifies AddView fails when view is already in position.
686 TEST_F(ViewManagerServiceAppTest, AddAncestorFails) {
687 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
688 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 3)));
690 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
692 // Make 3 a child of 2.
693 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 2), BuildViewId(1, 3)));
695 // Try to make 2 a child of 3, this should fail since 2 is an ancestor of 3.
696 EXPECT_FALSE(AddView(vm1(), BuildViewId(1, 3), BuildViewId(1, 2)));
699 // Verifies adding to root sends right notifications.
700 TEST_F(ViewManagerServiceAppTest, AddToRoot) {
701 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 21)));
702 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 3)));
704 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
705 changes2()->clear();
707 // Make 3 a child of 21.
708 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 21), BuildViewId(1, 3)));
710 // Make 21 a child of 1.
711 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 21)));
713 // Connection 2 should not be told anything (because the view is from a
714 // different connection). Create a view to ensure we got a response from
715 // the server.
716 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 100)));
717 EXPECT_TRUE(changes2()->empty());
720 // Verifies HierarchyChanged is correctly sent for various adds/removes.
721 TEST_F(ViewManagerServiceAppTest, ViewHierarchyChangedViews) {
722 // 1,2->1,11.
723 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
724 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 2), true));
725 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 11)));
726 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 11), true));
727 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 2), BuildViewId(1, 11)));
729 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
730 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), true));
732 ASSERT_TRUE(WaitForAllMessages(vm2()));
733 changes2()->clear();
735 // 1,1->1,2->1,11
737 // Client 2 should not get anything (1,2 is from another connection).
738 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 2)));
739 ASSERT_TRUE(WaitForAllMessages(vm2()));
740 EXPECT_TRUE(changes2()->empty());
743 // 0,1->1,1->1,2->1,11.
745 // Client 2 is now connected to the root, so it should have gotten a drawn
746 // notification.
747 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
748 vm_client2_->WaitForChangeCount(1u);
749 EXPECT_EQ("DrawnStateChanged view=1,1 drawn=true",
750 SingleChangeToDescription(*changes2()));
753 // 1,1->1,2->1,11.
755 // Client 2 is no longer connected to the root, should get drawn state
756 // changed.
757 changes2()->clear();
758 ASSERT_TRUE(RemoveViewFromParent(vm1(), BuildViewId(1, 1)));
759 vm_client2_->WaitForChangeCount(1);
760 EXPECT_EQ("DrawnStateChanged view=1,1 drawn=false",
761 SingleChangeToDescription(*changes2()));
764 // 1,1->1,2->1,11->1,111.
765 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 111)));
766 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 111), true));
768 changes2()->clear();
769 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 11), BuildViewId(1, 111)));
770 ASSERT_TRUE(WaitForAllMessages(vm2()));
771 EXPECT_TRUE(changes2()->empty());
774 // 0,1->1,1->1,2->1,11->1,111
776 changes2()->clear();
777 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
778 vm_client2_->WaitForChangeCount(1);
779 EXPECT_EQ("DrawnStateChanged view=1,1 drawn=true",
780 SingleChangeToDescription(*changes2()));
784 TEST_F(ViewManagerServiceAppTest, ViewHierarchyChangedAddingKnownToUnknown) {
785 // Create the following structure: root -> 1 -> 11 and 2->21 (2 has no
786 // parent).
787 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
789 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 11)));
790 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
791 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 21)));
793 // Set up the hierarchy.
794 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
795 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 11)));
796 ASSERT_TRUE(AddView(vm2(), BuildViewId(2, 2), BuildViewId(2, 21)));
798 // Remove 11, should result in a hierarchy change for the root.
800 changes1()->clear();
801 ASSERT_TRUE(RemoveViewFromParent(vm2(), BuildViewId(2, 11)));
803 vm_client1_.WaitForChangeCount(1);
804 EXPECT_EQ("HierarchyChanged view=2,11 new_parent=null old_parent=1,1",
805 SingleChangeToDescription(*changes1()));
808 // Add 2 to 1.
810 changes1()->clear();
811 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
813 vm_client1_.WaitForChangeCount(1);
814 EXPECT_EQ("HierarchyChanged view=2,2 new_parent=1,1 old_parent=null",
815 SingleChangeToDescription(*changes1()));
816 EXPECT_EQ(
817 "[view=2,2 parent=1,1],"
818 "[view=2,21 parent=2,2]",
819 ChangeViewDescription(*changes1()));
823 TEST_F(ViewManagerServiceAppTest, ReorderView) {
824 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
826 Id view1_id = BuildViewId(2, 1);
827 Id view2_id = BuildViewId(2, 2);
828 Id view3_id = BuildViewId(2, 3);
829 Id view4_id = BuildViewId(1, 4); // Peer to 1,1
830 Id view5_id = BuildViewId(1, 5); // Peer to 1,1
831 Id view6_id = BuildViewId(2, 6); // Child of 1,2.
832 Id view7_id = BuildViewId(2, 7); // Unparented.
833 Id view8_id = BuildViewId(2, 8); // Unparented.
834 ASSERT_TRUE(CreateView(vm2(), view1_id));
835 ASSERT_TRUE(CreateView(vm2(), view2_id));
836 ASSERT_TRUE(CreateView(vm2(), view3_id));
837 ASSERT_TRUE(CreateView(vm1(), view4_id));
838 ASSERT_TRUE(CreateView(vm1(), view5_id));
839 ASSERT_TRUE(CreateView(vm2(), view6_id));
840 ASSERT_TRUE(CreateView(vm2(), view7_id));
841 ASSERT_TRUE(CreateView(vm2(), view8_id));
842 ASSERT_TRUE(AddView(vm2(), view1_id, view2_id));
843 ASSERT_TRUE(AddView(vm2(), view2_id, view6_id));
844 ASSERT_TRUE(AddView(vm2(), view1_id, view3_id));
845 ASSERT_TRUE(AddView(vm1(), ViewIdToTransportId(RootViewId()), view4_id));
846 ASSERT_TRUE(AddView(vm1(), ViewIdToTransportId(RootViewId()), view5_id));
847 ASSERT_TRUE(AddView(vm1(), ViewIdToTransportId(RootViewId()), view1_id));
850 changes1()->clear();
851 ASSERT_TRUE(ReorderView(vm2(), view2_id, view3_id, ORDER_DIRECTION_ABOVE));
853 vm_client1_.WaitForChangeCount(1);
854 EXPECT_EQ("Reordered view=2,2 relative=2,3 direction=above",
855 SingleChangeToDescription(*changes1()));
859 changes1()->clear();
860 ASSERT_TRUE(ReorderView(vm2(), view2_id, view3_id, ORDER_DIRECTION_BELOW));
862 vm_client1_.WaitForChangeCount(1);
863 EXPECT_EQ("Reordered view=2,2 relative=2,3 direction=below",
864 SingleChangeToDescription(*changes1()));
867 // view2 is already below view3.
868 EXPECT_FALSE(ReorderView(vm2(), view2_id, view3_id, ORDER_DIRECTION_BELOW));
870 // view4 & 5 are unknown to connection2_.
871 EXPECT_FALSE(ReorderView(vm2(), view4_id, view5_id, ORDER_DIRECTION_ABOVE));
873 // view6 & view3 have different parents.
874 EXPECT_FALSE(ReorderView(vm1(), view3_id, view6_id, ORDER_DIRECTION_ABOVE));
876 // Non-existent view-ids
877 EXPECT_FALSE(ReorderView(vm1(), BuildViewId(1, 27), BuildViewId(1, 28),
878 ORDER_DIRECTION_ABOVE));
880 // view7 & view8 are un-parented.
881 EXPECT_FALSE(ReorderView(vm1(), view7_id, view8_id, ORDER_DIRECTION_ABOVE));
884 // Verifies DeleteView works.
885 TEST_F(ViewManagerServiceAppTest, DeleteView) {
886 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
887 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
889 // Make 2 a child of 1.
891 changes1()->clear();
892 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
893 vm_client1_.WaitForChangeCount(1);
894 EXPECT_EQ("HierarchyChanged view=2,2 new_parent=1,1 old_parent=null",
895 SingleChangeToDescription(*changes1()));
898 // Delete 2.
900 changes1()->clear();
901 changes2()->clear();
902 ASSERT_TRUE(DeleteView(vm2(), BuildViewId(2, 2)));
903 EXPECT_TRUE(changes2()->empty());
905 vm_client1_.WaitForChangeCount(1);
906 EXPECT_EQ("ViewDeleted view=2,2", SingleChangeToDescription(*changes1()));
910 // Verifies DeleteView isn't allowed from a separate connection.
911 TEST_F(ViewManagerServiceAppTest, DeleteViewFromAnotherConnectionDisallowed) {
912 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
913 EXPECT_FALSE(DeleteView(vm2(), BuildViewId(1, 1)));
916 // Verifies if a view was deleted and then reused that other clients are
917 // properly notified.
918 TEST_F(ViewManagerServiceAppTest, ReuseDeletedViewId) {
919 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
920 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
922 // Add 2 to 1.
924 changes1()->clear();
925 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
927 vm_client1_.WaitForChangeCount(1);
928 EXPECT_EQ("HierarchyChanged view=2,2 new_parent=1,1 old_parent=null",
929 SingleChangeToDescription(*changes1()));
930 EXPECT_EQ("[view=2,2 parent=1,1]", ChangeViewDescription(*changes1()));
933 // Delete 2.
935 changes1()->clear();
936 ASSERT_TRUE(DeleteView(vm2(), BuildViewId(2, 2)));
938 vm_client1_.WaitForChangeCount(1);
939 EXPECT_EQ("ViewDeleted view=2,2", SingleChangeToDescription(*changes1()));
942 // Create 2 again, and add it back to 1. Should get the same notification.
943 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
945 changes1()->clear();
946 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
948 vm_client1_.WaitForChangeCount(1);
949 EXPECT_EQ("HierarchyChanged view=2,2 new_parent=1,1 old_parent=null",
950 SingleChangeToDescription(*changes1()));
951 EXPECT_EQ("[view=2,2 parent=1,1]", ChangeViewDescription(*changes1()));
955 // Assertions for GetViewTree.
956 TEST_F(ViewManagerServiceAppTest, GetViewTree) {
957 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
959 // Create 11 in first connection and make it a child of 1.
960 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 11)));
961 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
962 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 11)));
964 // Create two views in second connection, 2 and 3, both children of 1.
965 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
966 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
967 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
968 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 3)));
970 // Verifies GetViewTree() on the root. The root connection sees all.
972 std::vector<TestView> views;
973 GetViewTree(vm1(), BuildViewId(0, 1), &views);
974 ASSERT_EQ(5u, views.size());
975 EXPECT_EQ("view=0,1 parent=null", views[0].ToString());
976 EXPECT_EQ("view=1,1 parent=0,1", views[1].ToString());
977 EXPECT_EQ("view=1,11 parent=1,1", views[2].ToString());
978 EXPECT_EQ("view=2,2 parent=1,1", views[3].ToString());
979 EXPECT_EQ("view=2,3 parent=1,1", views[4].ToString());
982 // Verifies GetViewTree() on the view 1,1 from vm2(). vm2() sees 1,1 as 1,1
983 // is vm2()'s root and vm2() sees all the views it created.
985 std::vector<TestView> views;
986 GetViewTree(vm2(), BuildViewId(1, 1), &views);
987 ASSERT_EQ(3u, views.size());
988 EXPECT_EQ("view=1,1 parent=null", views[0].ToString());
989 EXPECT_EQ("view=2,2 parent=1,1", views[1].ToString());
990 EXPECT_EQ("view=2,3 parent=1,1", views[2].ToString());
993 // Connection 2 shouldn't be able to get the root tree.
995 std::vector<TestView> views;
996 GetViewTree(vm2(), BuildViewId(0, 1), &views);
997 ASSERT_EQ(0u, views.size());
1001 TEST_F(ViewManagerServiceAppTest, SetViewBounds) {
1002 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1003 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1005 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1007 changes2()->clear();
1008 ASSERT_TRUE(SetViewBounds(vm1(), BuildViewId(1, 1), 0, 0, 100, 100));
1010 vm_client2_->WaitForChangeCount(1);
1011 EXPECT_EQ("BoundsChanged view=1,1 old_bounds=0,0 0x0 new_bounds=0,0 100x100",
1012 SingleChangeToDescription(*changes2()));
1014 // Should not be possible to change the bounds of a view created by another
1015 // connection.
1016 ASSERT_FALSE(SetViewBounds(vm2(), BuildViewId(1, 1), 0, 0, 0, 0));
1019 // Verify AddView fails when trying to manipulate views in other roots.
1020 TEST_F(ViewManagerServiceAppTest, CantMoveViewsFromOtherRoot) {
1021 // Create 1 and 2 in the first connection.
1022 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1023 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
1025 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1027 // Try to move 2 to be a child of 1 from connection 2. This should fail as 2
1028 // should not be able to access 1.
1029 ASSERT_FALSE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(1, 2)));
1031 // Try to reparent 1 to the root. A connection is not allowed to reparent its
1032 // roots.
1033 ASSERT_FALSE(AddView(vm2(), BuildViewId(0, 1), BuildViewId(1, 1)));
1036 // Verify RemoveViewFromParent fails for views that are descendants of the
1037 // roots.
1038 TEST_F(ViewManagerServiceAppTest, CantRemoveViewsInOtherRoots) {
1039 // Create 1 and 2 in the first connection and parent both to the root.
1040 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1041 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
1043 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1044 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 2)));
1046 // Establish the second connection and give it the root 1.
1047 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1049 // Connection 2 should not be able to remove view 2 or 1 from its parent.
1050 ASSERT_FALSE(RemoveViewFromParent(vm2(), BuildViewId(1, 2)));
1051 ASSERT_FALSE(RemoveViewFromParent(vm2(), BuildViewId(1, 1)));
1053 // Create views 10 and 11 in 2.
1054 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 10)));
1055 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 11)));
1057 // Parent 11 to 10.
1058 ASSERT_TRUE(AddView(vm2(), BuildViewId(2, 10), BuildViewId(2, 11)));
1059 // Remove 11 from 10.
1060 ASSERT_TRUE(RemoveViewFromParent(vm2(), BuildViewId(2, 11)));
1062 // Verify nothing was actually removed.
1064 std::vector<TestView> views;
1065 GetViewTree(vm1(), BuildViewId(0, 1), &views);
1066 ASSERT_EQ(3u, views.size());
1067 EXPECT_EQ("view=0,1 parent=null", views[0].ToString());
1068 EXPECT_EQ("view=1,1 parent=0,1", views[1].ToString());
1069 EXPECT_EQ("view=1,2 parent=0,1", views[2].ToString());
1073 // Verify GetViewTree fails for views that are not descendants of the roots.
1074 TEST_F(ViewManagerServiceAppTest, CantGetViewTreeOfOtherRoots) {
1075 // Create 1 and 2 in the first connection and parent both to the root.
1076 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1077 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
1079 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1080 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 2)));
1082 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1084 std::vector<TestView> views;
1086 // Should get nothing for the root.
1087 GetViewTree(vm2(), BuildViewId(0, 1), &views);
1088 ASSERT_TRUE(views.empty());
1090 // Should get nothing for view 2.
1091 GetViewTree(vm2(), BuildViewId(1, 2), &views);
1092 ASSERT_TRUE(views.empty());
1094 // Should get view 1 if asked for.
1095 GetViewTree(vm2(), BuildViewId(1, 1), &views);
1096 ASSERT_EQ(1u, views.size());
1097 EXPECT_EQ("view=1,1 parent=null", views[0].ToString());
1100 TEST_F(ViewManagerServiceAppTest, EmbedWithSameViewId) {
1101 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1102 changes2()->clear();
1104 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), BuildViewId(1, 1)));
1106 // Connection2 should have been told the view was deleted.
1108 vm_client2_->WaitForChangeCount(1);
1109 EXPECT_EQ("ViewDeleted view=1,1", SingleChangeToDescription(*changes2()));
1112 // Connection2 has no root. Verify it can't see view 1,1 anymore.
1114 std::vector<TestView> views;
1115 GetViewTree(vm2(), BuildViewId(1, 1), &views);
1116 EXPECT_TRUE(views.empty());
1120 TEST_F(ViewManagerServiceAppTest, EmbedWithSameViewId2) {
1121 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1122 changes2()->clear();
1124 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm1(), BuildViewId(1, 1)));
1126 // Connection2 should have been told the view was deleted.
1127 vm_client2_->WaitForChangeCount(1);
1128 changes2()->clear();
1130 // Create a view in the third connection and parent it to the root.
1131 ASSERT_TRUE(CreateView(vm3(), BuildViewId(3, 1)));
1132 ASSERT_TRUE(AddView(vm3(), BuildViewId(1, 1), BuildViewId(3, 1)));
1134 // Connection 1 should have been told about the add (it owns the view).
1136 vm_client1_.WaitForChangeCount(1);
1137 EXPECT_EQ("HierarchyChanged view=3,1 new_parent=1,1 old_parent=null",
1138 SingleChangeToDescription(*changes1()));
1141 // Embed 1,1 again.
1143 changes3()->clear();
1145 // We should get a new connection for the new embedding.
1146 scoped_ptr<ViewManagerClientImpl> connection4(
1147 EstablishConnectionViaEmbed(vm1(), BuildViewId(1, 1)));
1148 ASSERT_TRUE(connection4.get());
1149 EXPECT_EQ("[view=1,1 parent=null]",
1150 ChangeViewDescription(*connection4->tracker()->changes()));
1152 // And 3 should get a delete.
1153 vm_client3_->WaitForChangeCount(1);
1154 EXPECT_EQ("ViewDeleted view=1,1", SingleChangeToDescription(*changes3()));
1157 // vm3() has no root. Verify it can't see view 1,1 anymore.
1159 std::vector<TestView> views;
1160 GetViewTree(vm3(), BuildViewId(1, 1), &views);
1161 EXPECT_TRUE(views.empty());
1164 // Verify 3,1 is no longer parented to 1,1. We have to do this from 1,1 as
1165 // vm3() can no longer see 1,1.
1167 std::vector<TestView> views;
1168 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1169 ASSERT_EQ(1u, views.size());
1170 EXPECT_EQ("view=1,1 parent=null", views[0].ToString());
1173 // Verify vm3() can still see the view it created 3,1.
1175 std::vector<TestView> views;
1176 GetViewTree(vm3(), BuildViewId(3, 1), &views);
1177 ASSERT_EQ(1u, views.size());
1178 EXPECT_EQ("view=3,1 parent=null", views[0].ToString());
1182 // Assertions for SetViewVisibility.
1183 TEST_F(ViewManagerServiceAppTest, SetViewVisibility) {
1184 // Create 1 and 2 in the first connection and parent both to the root.
1185 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1186 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
1188 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1190 std::vector<TestView> views;
1191 GetViewTree(vm1(), BuildViewId(0, 1), &views);
1192 ASSERT_EQ(2u, views.size());
1193 EXPECT_EQ("view=0,1 parent=null visible=true drawn=true",
1194 views[0].ToString2());
1195 EXPECT_EQ("view=1,1 parent=0,1 visible=false drawn=false",
1196 views[1].ToString2());
1199 // Show all the views.
1200 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), true));
1201 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 2), true));
1203 std::vector<TestView> views;
1204 GetViewTree(vm1(), BuildViewId(0, 1), &views);
1205 ASSERT_EQ(2u, views.size());
1206 EXPECT_EQ("view=0,1 parent=null visible=true drawn=true",
1207 views[0].ToString2());
1208 EXPECT_EQ("view=1,1 parent=0,1 visible=true drawn=true",
1209 views[1].ToString2());
1212 // Hide 1.
1213 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), false));
1215 std::vector<TestView> views;
1216 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1217 ASSERT_EQ(1u, views.size());
1218 EXPECT_EQ("view=1,1 parent=0,1 visible=false drawn=false",
1219 views[0].ToString2());
1222 // Attach 2 to 1.
1223 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 2)));
1225 std::vector<TestView> views;
1226 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1227 ASSERT_EQ(2u, views.size());
1228 EXPECT_EQ("view=1,1 parent=0,1 visible=false drawn=false",
1229 views[0].ToString2());
1230 EXPECT_EQ("view=1,2 parent=1,1 visible=true drawn=false",
1231 views[1].ToString2());
1234 // Show 1.
1235 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), true));
1237 std::vector<TestView> views;
1238 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1239 ASSERT_EQ(2u, views.size());
1240 EXPECT_EQ("view=1,1 parent=0,1 visible=true drawn=true",
1241 views[0].ToString2());
1242 EXPECT_EQ("view=1,2 parent=1,1 visible=true drawn=true",
1243 views[1].ToString2());
1247 // Assertions for SetViewVisibility sending notifications.
1248 TEST_F(ViewManagerServiceAppTest, SetViewVisibilityNotifications) {
1249 // Create 1,1 and 1,2. 1,2 is made a child of 1,1 and 1,1 a child of the root.
1250 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1251 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), true));
1252 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 2)));
1253 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 2), true));
1254 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1255 ASSERT_TRUE(AddView(vm1(), BuildViewId(1, 1), BuildViewId(1, 2)));
1257 // Establish the second connection at 1,2.
1258 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnectionWithRoot(BuildViewId(1, 2)));
1260 // Add 2,3 as a child of 1,2.
1261 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
1262 ASSERT_TRUE(SetViewVisibility(vm2(), BuildViewId(2, 3), true));
1263 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 2), BuildViewId(2, 3)));
1264 WaitForAllMessages(vm1());
1266 changes2()->clear();
1267 // Hide 1,2 from connection 1. Connection 2 should see this.
1268 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 2), false));
1270 vm_client2_->WaitForChangeCount(1);
1271 EXPECT_EQ("VisibilityChanged view=1,2 visible=false",
1272 SingleChangeToDescription(*changes2()));
1275 changes1()->clear();
1276 // Show 1,2 from connection 2, connection 1 should be notified.
1277 ASSERT_TRUE(SetViewVisibility(vm2(), BuildViewId(1, 2), true));
1279 vm_client1_.WaitForChangeCount(1);
1280 EXPECT_EQ("VisibilityChanged view=1,2 visible=true",
1281 SingleChangeToDescription(*changes1()));
1284 changes2()->clear();
1285 // Hide 1,1, connection 2 should be told the draw state changed.
1286 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), false));
1288 vm_client2_->WaitForChangeCount(1);
1289 EXPECT_EQ("DrawnStateChanged view=1,2 drawn=false",
1290 SingleChangeToDescription(*changes2()));
1293 changes2()->clear();
1294 // Show 1,1 from connection 1. Connection 2 should see this.
1295 ASSERT_TRUE(SetViewVisibility(vm1(), BuildViewId(1, 1), true));
1297 vm_client2_->WaitForChangeCount(1);
1298 EXPECT_EQ("DrawnStateChanged view=1,2 drawn=true",
1299 SingleChangeToDescription(*changes2()));
1302 // Change visibility of 2,3, connection 1 should see this.
1303 changes1()->clear();
1304 ASSERT_TRUE(SetViewVisibility(vm2(), BuildViewId(2, 3), false));
1306 vm_client1_.WaitForChangeCount(1);
1307 EXPECT_EQ("VisibilityChanged view=2,3 visible=false",
1308 SingleChangeToDescription(*changes1()));
1311 changes2()->clear();
1312 // Remove 1,1 from the root, connection 2 should see drawn state changed.
1313 ASSERT_TRUE(RemoveViewFromParent(vm1(), BuildViewId(1, 1)));
1315 vm_client2_->WaitForChangeCount(1);
1316 EXPECT_EQ("DrawnStateChanged view=1,2 drawn=false",
1317 SingleChangeToDescription(*changes2()));
1320 changes2()->clear();
1321 // Add 1,1 back to the root, connection 2 should see drawn state changed.
1322 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1324 vm_client2_->WaitForChangeCount(1);
1325 EXPECT_EQ("DrawnStateChanged view=1,2 drawn=true",
1326 SingleChangeToDescription(*changes2()));
1330 TEST_F(ViewManagerServiceAppTest, SetViewProperty) {
1331 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1333 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1334 changes2()->clear();
1336 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1338 std::vector<TestView> views;
1339 GetViewTree(vm1(), BuildViewId(0, 1), &views);
1340 ASSERT_EQ(2u, views.size());
1341 EXPECT_EQ(BuildViewId(0, 1), views[0].view_id);
1342 EXPECT_EQ(BuildViewId(1, 1), views[1].view_id);
1343 ASSERT_EQ(0u, views[1].properties.size());
1346 // Set properties on 1.
1347 changes2()->clear();
1348 std::vector<uint8_t> one(1, '1');
1349 ASSERT_TRUE(SetViewProperty(vm1(), BuildViewId(1, 1), "one", &one));
1351 vm_client2_->WaitForChangeCount(1);
1352 EXPECT_EQ("PropertyChanged view=1,1 key=one value=1",
1353 SingleChangeToDescription(*changes2()));
1356 // Test that our properties exist in the view tree
1358 std::vector<TestView> views;
1359 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1360 ASSERT_EQ(1u, views.size());
1361 ASSERT_EQ(1u, views[0].properties.size());
1362 EXPECT_EQ(one, views[0].properties["one"]);
1365 changes2()->clear();
1366 // Set back to null.
1367 ASSERT_TRUE(SetViewProperty(vm1(), BuildViewId(1, 1), "one", NULL));
1369 vm_client2_->WaitForChangeCount(1);
1370 EXPECT_EQ("PropertyChanged view=1,1 key=one value=NULL",
1371 SingleChangeToDescription(*changes2()));
1375 TEST_F(ViewManagerServiceAppTest, OnEmbeddedAppDisconnected) {
1376 // Create connection 2 and 3.
1377 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1378 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
1379 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
1380 changes2()->clear();
1381 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), BuildViewId(2, 2)));
1383 // Close connection 3. Connection 2 (which had previously embedded 3) should
1384 // be notified of this.
1385 vm_client3_.reset();
1386 vm_client2_->WaitForChangeCount(1);
1387 EXPECT_EQ("OnEmbeddedAppDisconnected view=2,2",
1388 SingleChangeToDescription(*changes2()));
1391 // Verifies when the parent of an Embed() is destroyed the embedded app gets
1392 // a ViewDeleted (and doesn't trigger a DCHECK).
1393 TEST_F(ViewManagerServiceAppTest, OnParentOfEmbedDisconnects) {
1394 // Create connection 2 and 3.
1395 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1396 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1397 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
1398 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
1399 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
1400 ASSERT_TRUE(AddView(vm2(), BuildViewId(2, 2), BuildViewId(2, 3)));
1401 changes2()->clear();
1402 ASSERT_NO_FATAL_FAILURE(EstablishThirdConnection(vm2(), BuildViewId(2, 3)));
1403 changes3()->clear();
1405 // Close connection 2. Connection 3 should get a delete (for its root).
1406 vm_client2_.reset();
1407 vm_client3_->WaitForChangeCount(1);
1408 EXPECT_EQ("ViewDeleted view=2,3", SingleChangeToDescription(*changes3()));
1411 // Verifies ViewManagerServiceImpl doesn't incorrectly erase from its internal
1412 // map when a view from another connection with the same view_id is removed.
1413 TEST_F(ViewManagerServiceAppTest, DontCleanMapOnDestroy) {
1414 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1415 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 1)));
1416 changes1()->clear();
1417 vm_client2_.reset();
1418 vm_client1_.WaitForChangeCount(1);
1419 EXPECT_EQ("OnEmbeddedAppDisconnected view=1,1",
1420 SingleChangeToDescription(*changes1()));
1421 std::vector<TestView> views;
1422 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1423 EXPECT_FALSE(views.empty());
1426 TEST_F(ViewManagerServiceAppTest, CloneAndAnimate) {
1427 // Create connection 2 and 3.
1428 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1429 ASSERT_TRUE(AddView(vm1(), BuildViewId(0, 1), BuildViewId(1, 1)));
1430 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 2)));
1431 ASSERT_TRUE(CreateView(vm2(), BuildViewId(2, 3)));
1432 ASSERT_TRUE(AddView(vm2(), BuildViewId(1, 1), BuildViewId(2, 2)));
1433 ASSERT_TRUE(AddView(vm2(), BuildViewId(2, 2), BuildViewId(2, 3)));
1434 changes2()->clear();
1436 ASSERT_TRUE(WaitForAllMessages(vm1()));
1437 changes1()->clear();
1439 wm_internal_client_->CloneAndAnimate(BuildViewId(2, 3));
1440 ASSERT_TRUE(WaitForAllMessages(vm1()));
1442 ASSERT_TRUE(WaitForAllMessages(vm1()));
1443 ASSERT_TRUE(WaitForAllMessages(vm2()));
1445 // No messages should have been received.
1446 EXPECT_TRUE(changes1()->empty());
1447 EXPECT_TRUE(changes2()->empty());
1449 // No one should be able to see the cloned tree.
1450 std::vector<TestView> views;
1451 GetViewTree(vm1(), BuildViewId(1, 1), &views);
1452 EXPECT_FALSE(HasClonedView(views));
1453 views.clear();
1455 GetViewTree(vm2(), BuildViewId(1, 1), &views);
1456 EXPECT_FALSE(HasClonedView(views));
1459 // Verifies Embed() works when supplying a ViewManagerClient.
1460 TEST_F(ViewManagerServiceAppTest, EmbedSupplyingViewManagerClient) {
1461 ASSERT_TRUE(CreateView(vm1(), BuildViewId(1, 1)));
1463 ViewManagerClientImpl client2;
1464 mojo::ViewManagerClientPtr client2_ptr;
1465 mojo::Binding<ViewManagerClient> client2_binding(&client2, &client2_ptr);
1466 ASSERT_TRUE(Embed(vm1(), BuildViewId(1, 1), client2_ptr.Pass()));
1467 client2.WaitForOnEmbed();
1468 EXPECT_EQ("OnEmbed creator=mojo:window_manager",
1469 SingleChangeToDescription(*client2.tracker()->changes()));
1472 // TODO(sky): need to better track changes to initial connection. For example,
1473 // that SetBounsdViews/AddView and the like don't result in messages to the
1474 // originating connection.
1476 // TODO(sky): make sure coverage of what was
1477 // ViewManagerTest.SecondEmbedRoot_InitService and
1478 // ViewManagerTest.MultipleEmbedRootsBeforeWTHReady gets added to window manager
1479 // tests.
1481 } // namespace view_manager