Add version checking to webview library loads.
[chromium-blink-merge.git] / components / view_manager / view_manager_service_unittest.cc
blob1d6e1432149ee1beb8a949ee6c91de994339fca8
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/message_loop/message_loop.h"
9 #include "components/view_manager/client_connection.h"
10 #include "components/view_manager/connection_manager.h"
11 #include "components/view_manager/connection_manager_delegate.h"
12 #include "components/view_manager/display_manager.h"
13 #include "components/view_manager/display_manager_factory.h"
14 #include "components/view_manager/ids.h"
15 #include "components/view_manager/public/cpp/types.h"
16 #include "components/view_manager/public/cpp/util.h"
17 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
18 #include "components/view_manager/server_view.h"
19 #include "components/view_manager/surfaces/surfaces_state.h"
20 #include "components/view_manager/test_change_tracker.h"
21 #include "components/view_manager/view_manager_root_connection.h"
22 #include "components/view_manager/view_manager_service_impl.h"
23 #include "mojo/application/public/interfaces/service_provider.mojom.h"
24 #include "mojo/converters/geometry/geometry_type_converters.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gfx/geometry/rect.h"
28 using mojo::Array;
29 using mojo::ERROR_CODE_NONE;
30 using mojo::InterfaceRequest;
31 using mojo::ServiceProvider;
32 using mojo::ServiceProviderPtr;
33 using mojo::String;
34 using mojo::ViewDataPtr;
36 namespace view_manager {
37 namespace {
39 // -----------------------------------------------------------------------------
41 // ViewManagerClient implementation that logs all calls to a TestChangeTracker.
42 // TODO(sky): refactor so both this and ViewManagerServiceAppTest share code.
43 class TestViewManagerClient : public mojo::ViewManagerClient {
44 public:
45 TestViewManagerClient() {}
46 ~TestViewManagerClient() override {}
48 TestChangeTracker* tracker() { return &tracker_; }
50 private:
51 // ViewManagerClient:
52 void OnEmbed(uint16_t connection_id,
53 ViewDataPtr root,
54 mojo::ViewManagerServicePtr view_manager_service,
55 mojo::Id focused_view_id) override {
56 // TODO(sky): add test coverage of |focused_view_id|.
57 tracker_.OnEmbed(connection_id, root.Pass());
59 void OnEmbedForDescendant(
60 uint32_t view,
61 mojo::URLRequestPtr request,
62 const OnEmbedForDescendantCallback& callback) override {}
63 void OnEmbeddedAppDisconnected(uint32_t view) override {
64 tracker_.OnEmbeddedAppDisconnected(view);
66 void OnUnembed() override { tracker_.OnUnembed(); }
67 void OnViewBoundsChanged(uint32_t view,
68 mojo::RectPtr old_bounds,
69 mojo::RectPtr new_bounds) override {
70 tracker_.OnViewBoundsChanged(view, old_bounds.Pass(), new_bounds.Pass());
72 void OnViewViewportMetricsChanged(
73 mojo::ViewportMetricsPtr old_metrics,
74 mojo::ViewportMetricsPtr new_metrics) override {
75 tracker_.OnViewViewportMetricsChanged(old_metrics.Pass(),
76 new_metrics.Pass());
78 void OnViewHierarchyChanged(uint32_t view,
79 uint32_t new_parent,
80 uint32_t old_parent,
81 Array<ViewDataPtr> views) override {
82 tracker_.OnViewHierarchyChanged(view, new_parent, old_parent, views.Pass());
84 void OnViewReordered(uint32_t view_id,
85 uint32_t relative_view_id,
86 mojo::OrderDirection direction) override {
87 tracker_.OnViewReordered(view_id, relative_view_id, direction);
89 void OnViewDeleted(uint32_t view) override { tracker_.OnViewDeleted(view); }
90 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
91 tracker_.OnViewVisibilityChanged(view, visible);
93 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
94 tracker_.OnViewDrawnStateChanged(view, drawn);
96 void OnViewSharedPropertyChanged(uint32_t view,
97 const String& name,
98 Array<uint8_t> new_data) override {
99 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
101 void OnViewInputEvent(uint32_t view,
102 mojo::EventPtr event,
103 const mojo::Callback<void()>& callback) override {
104 tracker_.OnViewInputEvent(view, event.Pass());
106 void OnViewFocused(uint32_t focused_view_id) override {
107 tracker_.OnViewFocused(focused_view_id);
110 TestChangeTracker tracker_;
112 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClient);
115 // -----------------------------------------------------------------------------
117 // ClientConnection implementation that vends TestViewManagerClient.
118 class TestClientConnection : public ClientConnection {
119 public:
120 explicit TestClientConnection(scoped_ptr<ViewManagerServiceImpl> service_impl)
121 : ClientConnection(service_impl.Pass(), &client_) {}
123 TestViewManagerClient* client() { return &client_; }
125 private:
126 ~TestClientConnection() override {}
128 TestViewManagerClient client_;
130 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
133 // -----------------------------------------------------------------------------
135 // Empty implementation of ConnectionManagerDelegate.
136 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
137 public:
138 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
139 ~TestConnectionManagerDelegate() override {}
141 TestViewManagerClient* last_client() {
142 return last_connection_ ? last_connection_->client() : nullptr;
145 TestClientConnection* last_connection() { return last_connection_; }
147 private:
148 // ConnectionManagerDelegate:
149 void OnNoMoreRootConnections() override {}
151 ClientConnection* CreateClientConnectionForEmbedAtView(
152 ConnectionManager* connection_manager,
153 mojo::InterfaceRequest<mojo::ViewManagerService> service_request,
154 mojo::ConnectionSpecificId creator_id,
155 mojo::URLRequestPtr request,
156 const ViewId& root_id) override {
157 scoped_ptr<ViewManagerServiceImpl> service(
158 new ViewManagerServiceImpl(connection_manager, creator_id, root_id));
159 last_connection_ = new TestClientConnection(service.Pass());
160 return last_connection_;
162 ClientConnection* CreateClientConnectionForEmbedAtView(
163 ConnectionManager* connection_manager,
164 mojo::InterfaceRequest<mojo::ViewManagerService> service_request,
165 mojo::ConnectionSpecificId creator_id,
166 const ViewId& root_id,
167 mojo::ViewManagerClientPtr client) override {
168 // Used by ConnectionManager::AddRoot.
169 scoped_ptr<ViewManagerServiceImpl> service(
170 new ViewManagerServiceImpl(connection_manager, creator_id, root_id));
171 last_connection_ = new TestClientConnection(service.Pass());
172 return last_connection_;
175 TestClientConnection* last_connection_;
177 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
180 // -----------------------------------------------------------------------------
182 class TestViewManagerRootConnection : public ViewManagerRootConnection {
183 public:
184 TestViewManagerRootConnection(scoped_ptr<ViewManagerRootImpl> root,
185 ConnectionManager* manager)
186 : ViewManagerRootConnection(root.Pass(), manager) {}
187 ~TestViewManagerRootConnection() override {}
189 private:
190 // ViewManagerRootDelegate:
191 void OnDisplayInitialized() override {
192 connection_manager()->AddRoot(this);
193 set_view_manager_service(connection_manager()->EmbedAtView(
194 kInvalidConnectionId,
195 view_manager_root()->root_view()->id(),
196 mojo::ViewManagerClientPtr()));
198 DISALLOW_COPY_AND_ASSIGN(TestViewManagerRootConnection);
201 // -----------------------------------------------------------------------------
202 // Empty implementation of DisplayManager.
203 class TestDisplayManager : public DisplayManager {
204 public:
205 TestDisplayManager() {}
206 ~TestDisplayManager() override {}
208 // DisplayManager:
209 void Init(DisplayManagerDelegate* delegate) override {
210 // It is necessary to tell the delegate about the ViewportMetrics to make
211 // sure that the ViewManagerRootConnection is correctly initialized (and a
212 // root-view is created).
213 mojo::ViewportMetrics metrics;
214 metrics.size_in_pixels = mojo::Size::From(gfx::Size(400, 300));
215 metrics.device_pixel_ratio = 1.f;
216 delegate->OnViewportMetricsChanged(mojo::ViewportMetrics(), metrics);
218 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override {
220 void SetViewportSize(const gfx::Size& size) override {}
221 const mojo::ViewportMetrics& GetViewportMetrics() override {
222 return display_metrices_;
224 void UpdateTextInputState(const ui::TextInputState& state) override {}
225 void SetImeVisibility(bool visible) override {}
227 private:
228 mojo::ViewportMetrics display_metrices_;
230 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager);
233 // Factory that dispenses TestDisplayManagers.
234 class TestDisplayManagerFactory : public DisplayManagerFactory {
235 public:
236 TestDisplayManagerFactory() {}
237 ~TestDisplayManagerFactory() {}
238 DisplayManager* CreateDisplayManager(
239 bool is_headless,
240 mojo::ApplicationImpl* app_impl,
241 const scoped_refptr<gles2::GpuState>& gpu_state,
242 const scoped_refptr<surfaces::SurfacesState>& surfaces_state) override {
243 return new TestDisplayManager();
246 private:
247 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerFactory);
250 mojo::EventPtr CreatePointerDownEvent(int x, int y) {
251 mojo::EventPtr event(mojo::Event::New());
252 event->action = mojo::EVENT_TYPE_POINTER_DOWN;
253 event->pointer_data = mojo::PointerData::New();
254 event->pointer_data->pointer_id = 1u;
255 event->pointer_data->x = x;
256 event->pointer_data->y = y;
257 return event.Pass();
260 mojo::EventPtr CreatePointerUpEvent(int x, int y) {
261 mojo::EventPtr event(mojo::Event::New());
262 event->action = mojo::EVENT_TYPE_POINTER_UP;
263 event->pointer_data = mojo::PointerData::New();
264 event->pointer_data->pointer_id = 1u;
265 event->pointer_data->x = x;
266 event->pointer_data->y = y;
267 return event.Pass();
270 } // namespace
272 // -----------------------------------------------------------------------------
274 class ViewManagerServiceTest : public testing::Test {
275 public:
276 ViewManagerServiceTest() : wm_client_(nullptr) {}
277 ~ViewManagerServiceTest() override {}
279 // ViewManagerServiceImpl for the window manager.
280 ViewManagerServiceImpl* wm_connection() {
281 return connection_manager_->GetConnection(1);
284 TestViewManagerClient* last_view_manager_client() {
285 return delegate_.last_client();
288 TestClientConnection* last_client_connection() {
289 return delegate_.last_connection();
292 ConnectionManager* connection_manager() { return connection_manager_.get(); }
294 TestViewManagerClient* wm_client() { return wm_client_; }
296 TestViewManagerRootConnection* root_connection() { return root_connection_; }
298 protected:
299 // testing::Test:
300 void SetUp() override {
301 DisplayManager::set_factory_for_testing(&display_manager_factory_);
302 // TODO(fsamuel): This is probably broken. We need a root.
303 connection_manager_.reset(new ConnectionManager(&delegate_));
304 ViewManagerRootImpl* root = new ViewManagerRootImpl(
305 connection_manager_.get(), true /* is_headless */, nullptr,
306 scoped_refptr<gles2::GpuState>(),
307 scoped_refptr<surfaces::SurfacesState>());
308 // TODO(fsamuel): This is way too magical. We need to find a better way to
309 // manage lifetime.
310 root_connection_ = new TestViewManagerRootConnection(
311 make_scoped_ptr(root), connection_manager_.get());
312 root->Init(root_connection_);
313 wm_client_ = delegate_.last_client();
316 private:
317 // TestViewManagerClient that is used for the WM connection.
318 TestViewManagerClient* wm_client_;
319 TestDisplayManagerFactory display_manager_factory_;
320 TestConnectionManagerDelegate delegate_;
321 TestViewManagerRootConnection* root_connection_;
322 scoped_ptr<ConnectionManager> connection_manager_;
323 base::MessageLoop message_loop_;
325 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest);
328 namespace {
330 const ServerView* GetFirstCloned(const ServerView* view) {
331 for (const ServerView* child : view->GetChildren()) {
332 if (child->id() == ClonedViewId())
333 return child;
335 return nullptr;
338 // Provides common setup for animation tests. Creates the following views:
339 // 0,1 (the root, provided by view manager)
340 // 1,1 the second connection is embedded here (view owned by wm_connection()).
341 // 2,1 bounds=1,2 11x22
342 // 2,2 bounds=2,3 6x7
343 // 2,3 bounds=3,4 6x7
344 // CloneAndAnimate() is invoked for 2,2.
345 void SetUpAnimate1(ViewManagerServiceTest* test, ViewId* embed_view_id) {
346 *embed_view_id = ViewId(test->wm_connection()->id(), 1);
347 EXPECT_EQ(ERROR_CODE_NONE, test->wm_connection()->CreateView(*embed_view_id));
348 EXPECT_TRUE(test->wm_connection()->SetViewVisibility(*embed_view_id, true));
349 EXPECT_TRUE(test->wm_connection()->AddView(*(test->wm_connection()->root()),
350 *embed_view_id));
351 mojo::URLRequestPtr request(mojo::URLRequest::New());
352 test->wm_connection()->EmbedAllowingReembed(*embed_view_id, request.Pass(),
353 mojo::Callback<void(bool)>());
354 ViewManagerServiceImpl* connection1 =
355 test->connection_manager()->GetConnectionWithRoot(*embed_view_id);
356 ASSERT_TRUE(connection1 != nullptr);
357 ASSERT_NE(connection1, test->wm_connection());
359 const ViewId child1(connection1->id(), 1);
360 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
361 const ViewId child2(connection1->id(), 2);
362 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
363 const ViewId child3(connection1->id(), 3);
364 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
366 ServerView* v1 = connection1->GetView(child1);
367 v1->SetVisible(true);
368 v1->SetBounds(gfx::Rect(1, 2, 11, 22));
369 ServerView* v2 = connection1->GetView(child2);
370 v2->SetVisible(true);
371 v2->SetBounds(gfx::Rect(2, 3, 6, 7));
372 ServerView* v3 = connection1->GetView(child3);
373 v3->SetVisible(true);
374 v3->SetBounds(gfx::Rect(3, 4, 6, 7));
376 EXPECT_TRUE(connection1->AddView(*embed_view_id, child1));
377 EXPECT_TRUE(connection1->AddView(child1, child2));
378 EXPECT_TRUE(connection1->AddView(child2, child3));
380 TestViewManagerClient* connection1_client = test->last_view_manager_client();
381 connection1_client->tracker()->changes()->clear();
382 test->wm_client()->tracker()->changes()->clear();
383 EXPECT_TRUE(test->connection_manager()->CloneAndAnimate(child2));
384 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
385 EXPECT_TRUE(test->wm_client()->tracker()->changes()->empty());
387 // We cloned v2. The cloned view ends up as a sibling of it.
388 const ServerView* cloned_view = GetFirstCloned(connection1->GetView(child1));
389 ASSERT_TRUE(cloned_view);
390 // |cloned_view| should have one and only one cloned child (corresponds to
391 // |child3|).
392 ASSERT_EQ(1u, cloned_view->GetChildren().size());
393 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
395 // Cloned views should match the bounds of the view they were cloned from.
396 EXPECT_EQ(v2->bounds(), cloned_view->bounds());
397 EXPECT_EQ(v3->bounds(), cloned_view->GetChildren()[0]->bounds());
399 // Cloned views are owned by the ConnectionManager and shouldn't be returned
400 // from ViewManagerServiceImpl::GetView.
401 EXPECT_TRUE(connection1->GetView(ClonedViewId()) == nullptr);
402 EXPECT_TRUE(test->wm_connection()->GetView(ClonedViewId()) == nullptr);
405 } // namespace
407 // Verifies ViewManagerService::GetViewTree() doesn't return cloned views.
408 TEST_F(ViewManagerServiceTest, ConnectionsCantSeeClonedViews) {
409 ViewId embed_view_id;
410 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
412 ViewManagerServiceImpl* connection1 =
413 connection_manager()->GetConnectionWithRoot(embed_view_id);
415 const ViewId child1(connection1->id(), 1);
416 const ViewId child2(connection1->id(), 2);
417 const ViewId child3(connection1->id(), 3);
419 // Verify the root doesn't see any cloned views.
420 std::vector<const ServerView*> views(
421 wm_connection()->GetViewTree(*wm_connection()->root()));
422 ASSERT_EQ(5u, views.size());
423 ASSERT_TRUE(views[0]->id() == *wm_connection()->root());
424 ASSERT_TRUE(views[1]->id() == embed_view_id);
425 ASSERT_TRUE(views[2]->id() == child1);
426 ASSERT_TRUE(views[3]->id() == child2);
427 ASSERT_TRUE(views[4]->id() == child3);
429 // Verify connection1 doesn't see any cloned views.
430 std::vector<const ServerView*> v1_views(
431 connection1->GetViewTree(embed_view_id));
432 ASSERT_EQ(4u, v1_views.size());
433 ASSERT_TRUE(v1_views[0]->id() == embed_view_id);
434 ASSERT_TRUE(v1_views[1]->id() == child1);
435 ASSERT_TRUE(v1_views[2]->id() == child2);
436 ASSERT_TRUE(v1_views[3]->id() == child3);
439 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnConnectionClose) {
440 ViewId embed_view_id;
441 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
443 // Destroy connection1, which should force the cloned view to become a child
444 // of where it was embedded (the embedded view still exists).
445 connection_manager()->OnConnectionError(last_client_connection());
447 ServerView* embed_view = wm_connection()->GetView(embed_view_id);
448 ASSERT_TRUE(embed_view != nullptr);
449 const ServerView* cloned_view = GetFirstCloned(embed_view);
450 ASSERT_TRUE(cloned_view);
451 ASSERT_EQ(1u, cloned_view->GetChildren().size());
452 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
454 // Because the cloned view changed parents its bounds should have changed.
455 EXPECT_EQ(gfx::Rect(3, 5, 6, 7), cloned_view->bounds());
456 // The bounds of the cloned child should not have changed though.
457 EXPECT_EQ(gfx::Rect(3, 4, 6, 7), cloned_view->GetChildren()[0]->bounds());
460 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnHide) {
461 ViewId embed_view_id;
462 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
464 ViewManagerServiceImpl* connection1 =
465 connection_manager()->GetConnectionWithRoot(embed_view_id);
467 // Hide the parent of the cloned view, which should force the cloned view to
468 // become a sibling of the parent.
469 const ServerView* view_to_hide =
470 connection1->GetView(ViewId(connection1->id(), 1));
471 ASSERT_TRUE(connection1->SetViewVisibility(view_to_hide->id(), false));
473 const ServerView* cloned_view = GetFirstCloned(view_to_hide->parent());
474 ASSERT_TRUE(cloned_view);
475 ASSERT_EQ(1u, cloned_view->GetChildren().size());
476 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
477 EXPECT_EQ(2u, cloned_view->parent()->GetChildren().size());
478 EXPECT_TRUE(cloned_view->parent()->GetChildren()[1] == cloned_view);
481 // Clone and animate on a tree with more depth. Basically that of
482 // SetUpAnimate1() but cloning 2,1.
483 TEST_F(ViewManagerServiceTest, CloneAndAnimateLargerDepth) {
484 const ViewId embed_view_id(wm_connection()->id(), 1);
485 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
486 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
487 EXPECT_TRUE(
488 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
489 mojo::URLRequestPtr request(mojo::URLRequest::New());
490 wm_connection()->EmbedAllowingReembed(embed_view_id, request.Pass(),
491 mojo::Callback<void(bool)>());
492 ViewManagerServiceImpl* connection1 =
493 connection_manager()->GetConnectionWithRoot(embed_view_id);
494 ASSERT_TRUE(connection1 != nullptr);
495 ASSERT_NE(connection1, wm_connection());
497 const ViewId child1(connection1->id(), 1);
498 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
499 const ViewId child2(connection1->id(), 2);
500 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
501 const ViewId child3(connection1->id(), 3);
502 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
504 ServerView* v1 = connection1->GetView(child1);
505 v1->SetVisible(true);
506 connection1->GetView(child2)->SetVisible(true);
507 connection1->GetView(child3)->SetVisible(true);
509 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
510 EXPECT_TRUE(connection1->AddView(child1, child2));
511 EXPECT_TRUE(connection1->AddView(child2, child3));
513 TestViewManagerClient* connection1_client = last_view_manager_client();
514 connection1_client->tracker()->changes()->clear();
515 wm_client()->tracker()->changes()->clear();
516 EXPECT_TRUE(connection_manager()->CloneAndAnimate(child1));
517 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
518 EXPECT_TRUE(wm_client()->tracker()->changes()->empty());
520 // We cloned v1. The cloned view ends up as a sibling of it.
521 const ServerView* cloned_view = GetFirstCloned(v1->parent());
522 ASSERT_TRUE(cloned_view);
523 // |cloned_view| should have a child and its child should have a child.
524 ASSERT_EQ(1u, cloned_view->GetChildren().size());
525 const ServerView* cloned_view_child = cloned_view->GetChildren()[0];
526 EXPECT_EQ(1u, cloned_view_child->GetChildren().size());
527 EXPECT_TRUE(cloned_view_child->id() == ClonedViewId());
530 // Verifies focus correctly changes on pointer events.
531 TEST_F(ViewManagerServiceTest, FocusOnPointer) {
532 const ViewId embed_view_id(wm_connection()->id(), 1);
533 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
534 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
535 EXPECT_TRUE(
536 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
537 root_connection()->view_manager_root()->root_view()->
538 SetBounds(gfx::Rect(0, 0, 100, 100));
539 mojo::URLRequestPtr request(mojo::URLRequest::New());
540 wm_connection()->EmbedAllowingReembed(embed_view_id, request.Pass(),
541 mojo::Callback<void(bool)>());
542 ViewManagerServiceImpl* connection1 =
543 connection_manager()->GetConnectionWithRoot(embed_view_id);
544 ASSERT_TRUE(connection1 != nullptr);
545 ASSERT_NE(connection1, wm_connection());
547 connection_manager()
548 ->GetView(embed_view_id)
549 ->SetBounds(gfx::Rect(0, 0, 50, 50));
551 const ViewId child1(connection1->id(), 1);
552 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
553 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
554 ServerView* v1 = connection1->GetView(child1);
555 v1->SetVisible(true);
556 v1->SetBounds(gfx::Rect(20, 20, 20, 20));
558 TestViewManagerClient* connection1_client = last_view_manager_client();
559 connection1_client->tracker()->changes()->clear();
560 wm_client()->tracker()->changes()->clear();
562 connection_manager()->OnEvent(root_connection()->view_manager_root(),
563 CreatePointerDownEvent(21, 22));
564 // Focus should go to child1. This results in notifying both the window
565 // manager and client connection being notified.
566 EXPECT_EQ(v1, connection_manager()->GetFocusedView());
567 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
568 EXPECT_EQ("Focused id=2,1",
569 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
570 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
571 EXPECT_EQ(
572 "Focused id=2,1",
573 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
575 connection_manager()->OnEvent(root_connection()->view_manager_root(),
576 CreatePointerUpEvent(21, 22));
577 wm_client()->tracker()->changes()->clear();
578 connection1_client->tracker()->changes()->clear();
580 // Press outside of the embedded view. Focus should go to the root. Notice
581 // the client1 doesn't see who has focus as the focused view (root) isn't
582 // visible to it.
583 connection_manager()->OnEvent(root_connection()->view_manager_root(),
584 CreatePointerDownEvent(61, 22));
585 EXPECT_EQ(root_connection()->view_manager_root()->root_view(),
586 connection_manager()->GetFocusedView());
587 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
588 EXPECT_EQ("Focused id=0,2",
589 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
590 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
591 EXPECT_EQ(
592 "Focused id=null",
593 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
595 connection_manager()->OnEvent(root_connection()->view_manager_root(),
596 CreatePointerUpEvent(21, 22));
597 wm_client()->tracker()->changes()->clear();
598 connection1_client->tracker()->changes()->clear();
600 // Press in the same location. Should not get a focus change event (only input
601 // event).
602 connection_manager()->OnEvent(root_connection()->view_manager_root(),
603 CreatePointerDownEvent(61, 22));
604 EXPECT_EQ(root_connection()->view_manager_root()->root_view(),
605 connection_manager()->GetFocusedView());
606 ASSERT_EQ(wm_client()->tracker()->changes()->size(), 1u);
607 EXPECT_EQ("InputEvent view=0,2 event_action=4",
608 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
609 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
612 } // namespace view_manager