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.
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_tree.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_tree_host_connection.h"
22 #include "components/view_manager/view_tree_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"
29 using mojo::ERROR_CODE_NONE
;
30 using mojo::InterfaceRequest
;
31 using mojo::ServiceProvider
;
32 using mojo::ServiceProviderPtr
;
34 using mojo::ViewDataPtr
;
36 namespace view_manager
{
39 // -----------------------------------------------------------------------------
41 // ViewTreeClient implementation that logs all calls to a TestChangeTracker.
42 // TODO(sky): refactor so both this and ViewTreeAppTest share code.
43 class TestViewTreeClient
: public mojo::ViewTreeClient
{
45 TestViewTreeClient() {}
46 ~TestViewTreeClient() override
{}
48 TestChangeTracker
* tracker() { return &tracker_
; }
52 void OnEmbed(uint16_t connection_id
,
54 mojo::ViewTreePtr tree
,
55 mojo::Id focused_view_id
,
56 uint32_t access_policy
) override
{
57 // TODO(sky): add test coverage of |focused_view_id|.
58 tracker_
.OnEmbed(connection_id
, root
.Pass());
60 void OnEmbeddedAppDisconnected(uint32_t view
) override
{
61 tracker_
.OnEmbeddedAppDisconnected(view
);
63 void OnUnembed() override
{ tracker_
.OnUnembed(); }
64 void OnViewBoundsChanged(uint32_t view
,
65 mojo::RectPtr old_bounds
,
66 mojo::RectPtr new_bounds
) override
{
67 tracker_
.OnViewBoundsChanged(view
, old_bounds
.Pass(), new_bounds
.Pass());
69 void OnViewViewportMetricsChanged(
70 mojo::ViewportMetricsPtr old_metrics
,
71 mojo::ViewportMetricsPtr new_metrics
) override
{
72 tracker_
.OnViewViewportMetricsChanged(old_metrics
.Pass(),
75 void OnViewHierarchyChanged(uint32_t view
,
78 Array
<ViewDataPtr
> views
) override
{
79 tracker_
.OnViewHierarchyChanged(view
, new_parent
, old_parent
, views
.Pass());
81 void OnViewReordered(uint32_t view_id
,
82 uint32_t relative_view_id
,
83 mojo::OrderDirection direction
) override
{
84 tracker_
.OnViewReordered(view_id
, relative_view_id
, direction
);
86 void OnViewDeleted(uint32_t view
) override
{ tracker_
.OnViewDeleted(view
); }
87 void OnViewVisibilityChanged(uint32_t view
, bool visible
) override
{
88 tracker_
.OnViewVisibilityChanged(view
, visible
);
90 void OnViewDrawnStateChanged(uint32_t view
, bool drawn
) override
{
91 tracker_
.OnViewDrawnStateChanged(view
, drawn
);
93 void OnViewSharedPropertyChanged(uint32_t view
,
95 Array
<uint8_t> new_data
) override
{
96 tracker_
.OnViewSharedPropertyChanged(view
, name
, new_data
.Pass());
98 void OnViewInputEvent(uint32_t view
,
100 const mojo::Callback
<void()>& callback
) override
{
101 tracker_
.OnViewInputEvent(view
, event
.Pass());
103 void OnViewFocused(uint32_t focused_view_id
) override
{
104 tracker_
.OnViewFocused(focused_view_id
);
107 TestChangeTracker tracker_
;
109 DISALLOW_COPY_AND_ASSIGN(TestViewTreeClient
);
112 // -----------------------------------------------------------------------------
114 // ClientConnection implementation that vends TestViewTreeClient.
115 class TestClientConnection
: public ClientConnection
{
117 explicit TestClientConnection(scoped_ptr
<ViewTreeImpl
> service_impl
)
118 : ClientConnection(service_impl
.Pass(), &client_
) {}
120 TestViewTreeClient
* client() { return &client_
; }
123 ~TestClientConnection() override
{}
125 TestViewTreeClient client_
;
127 DISALLOW_COPY_AND_ASSIGN(TestClientConnection
);
130 // -----------------------------------------------------------------------------
132 // Empty implementation of ConnectionManagerDelegate.
133 class TestConnectionManagerDelegate
: public ConnectionManagerDelegate
{
135 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
136 ~TestConnectionManagerDelegate() override
{}
138 TestViewTreeClient
* last_client() {
139 return last_connection_
? last_connection_
->client() : nullptr;
142 TestClientConnection
* last_connection() { return last_connection_
; }
145 // ConnectionManagerDelegate:
146 void OnNoMoreRootConnections() override
{}
148 ClientConnection
* CreateClientConnectionForEmbedAtView(
149 ConnectionManager
* connection_manager
,
150 mojo::InterfaceRequest
<mojo::ViewTree
> service_request
,
151 mojo::ConnectionSpecificId creator_id
,
152 mojo::URLRequestPtr request
,
153 const ViewId
& root_id
) override
{
154 scoped_ptr
<ViewTreeImpl
> service(
155 new ViewTreeImpl(connection_manager
, creator_id
, root_id
));
156 last_connection_
= new TestClientConnection(service
.Pass());
157 return last_connection_
;
159 ClientConnection
* CreateClientConnectionForEmbedAtView(
160 ConnectionManager
* connection_manager
,
161 mojo::InterfaceRequest
<mojo::ViewTree
> service_request
,
162 mojo::ConnectionSpecificId creator_id
,
163 const ViewId
& root_id
,
164 mojo::ViewTreeClientPtr client
) override
{
165 // Used by ConnectionManager::AddRoot.
166 scoped_ptr
<ViewTreeImpl
> service(
167 new ViewTreeImpl(connection_manager
, creator_id
, root_id
));
168 last_connection_
= new TestClientConnection(service
.Pass());
169 return last_connection_
;
172 TestClientConnection
* last_connection_
;
174 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate
);
177 // -----------------------------------------------------------------------------
179 class TestViewTreeHostConnection
: public ViewTreeHostConnection
{
181 TestViewTreeHostConnection(scoped_ptr
<ViewTreeHostImpl
> host_impl
,
182 ConnectionManager
* manager
)
183 : ViewTreeHostConnection(host_impl
.Pass(), manager
) {}
184 ~TestViewTreeHostConnection() override
{}
187 // ViewTreeHostDelegate:
188 void OnDisplayInitialized() override
{
189 connection_manager()->AddHost(this);
190 set_view_tree(connection_manager()->EmbedAtView(
191 kInvalidConnectionId
,
192 view_tree_host()->root_view()->id(),
193 mojo::ViewTreeClientPtr()));
195 DISALLOW_COPY_AND_ASSIGN(TestViewTreeHostConnection
);
198 // -----------------------------------------------------------------------------
199 // Empty implementation of DisplayManager.
200 class TestDisplayManager
: public DisplayManager
{
202 TestDisplayManager() {}
203 ~TestDisplayManager() override
{}
206 void Init(DisplayManagerDelegate
* delegate
) override
{
207 // It is necessary to tell the delegate about the ViewportMetrics to make
208 // sure that the ViewTreeHostConnection is correctly initialized (and a
209 // root-view is created).
210 mojo::ViewportMetrics metrics
;
211 metrics
.size_in_pixels
= mojo::Size::From(gfx::Size(400, 300));
212 metrics
.device_pixel_ratio
= 1.f
;
213 delegate
->OnViewportMetricsChanged(mojo::ViewportMetrics(), metrics
);
215 void SchedulePaint(const ServerView
* view
, const gfx::Rect
& bounds
) override
{
217 void SetViewportSize(const gfx::Size
& size
) override
{}
218 void SetTitle(const base::string16
& title
) override
{}
219 const mojo::ViewportMetrics
& GetViewportMetrics() override
{
220 return display_metrices_
;
222 void UpdateTextInputState(const ui::TextInputState
& state
) override
{}
223 void SetImeVisibility(bool visible
) override
{}
226 mojo::ViewportMetrics display_metrices_
;
228 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager
);
231 // Factory that dispenses TestDisplayManagers.
232 class TestDisplayManagerFactory
: public DisplayManagerFactory
{
234 TestDisplayManagerFactory() {}
235 ~TestDisplayManagerFactory() {}
236 DisplayManager
* CreateDisplayManager(
238 mojo::ApplicationImpl
* app_impl
,
239 const scoped_refptr
<gles2::GpuState
>& gpu_state
,
240 const scoped_refptr
<surfaces::SurfacesState
>& surfaces_state
) override
{
241 return new TestDisplayManager();
245 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerFactory
);
248 mojo::EventPtr
CreatePointerDownEvent(int x
, int y
) {
249 mojo::EventPtr
event(mojo::Event::New());
250 event
->action
= mojo::EVENT_TYPE_POINTER_DOWN
;
251 event
->pointer_data
= mojo::PointerData::New();
252 event
->pointer_data
->pointer_id
= 1u;
253 event
->pointer_data
->x
= x
;
254 event
->pointer_data
->y
= y
;
258 mojo::EventPtr
CreatePointerUpEvent(int x
, int y
) {
259 mojo::EventPtr
event(mojo::Event::New());
260 event
->action
= mojo::EVENT_TYPE_POINTER_UP
;
261 event
->pointer_data
= mojo::PointerData::New();
262 event
->pointer_data
->pointer_id
= 1u;
263 event
->pointer_data
->x
= x
;
264 event
->pointer_data
->y
= y
;
270 // -----------------------------------------------------------------------------
272 class ViewTreeTest
: public testing::Test
{
274 ViewTreeTest() : wm_client_(nullptr) {}
275 ~ViewTreeTest() override
{}
277 // ViewTreeImpl for the window manager.
278 ViewTreeImpl
* wm_connection() {
279 return connection_manager_
->GetConnection(1);
282 TestViewTreeClient
* last_view_tree_client() {
283 return delegate_
.last_client();
286 TestClientConnection
* last_client_connection() {
287 return delegate_
.last_connection();
290 ConnectionManager
* connection_manager() { return connection_manager_
.get(); }
292 TestViewTreeClient
* wm_client() { return wm_client_
; }
294 TestViewTreeHostConnection
* host_connection() { return host_connection_
; }
295 DisplayManagerDelegate
* display_manager_delegate() {
296 return host_connection()->view_tree_host();
301 void SetUp() override
{
302 DisplayManager::set_factory_for_testing(&display_manager_factory_
);
303 // TODO(fsamuel): This is probably broken. We need a root.
304 connection_manager_
.reset(
305 new ConnectionManager(&delegate_
,
306 scoped_refptr
<surfaces::SurfacesState
>()));
307 ViewTreeHostImpl
* host
= new ViewTreeHostImpl(
308 mojo::ViewTreeHostClientPtr(),
309 connection_manager_
.get(), true /* is_headless */, nullptr,
310 scoped_refptr
<gles2::GpuState
>(),
311 scoped_refptr
<surfaces::SurfacesState
>());
312 // TODO(fsamuel): This is way too magical. We need to find a better way to
314 host_connection_
= new TestViewTreeHostConnection(
315 make_scoped_ptr(host
), connection_manager_
.get());
316 host
->Init(host_connection_
);
317 wm_client_
= delegate_
.last_client();
321 // TestViewTreeClient that is used for the WM connection.
322 TestViewTreeClient
* wm_client_
;
323 TestDisplayManagerFactory display_manager_factory_
;
324 TestConnectionManagerDelegate delegate_
;
325 TestViewTreeHostConnection
* host_connection_
;
326 scoped_ptr
<ConnectionManager
> connection_manager_
;
327 base::MessageLoop message_loop_
;
329 DISALLOW_COPY_AND_ASSIGN(ViewTreeTest
);
332 // Verifies focus correctly changes on pointer events.
333 TEST_F(ViewTreeTest
, FocusOnPointer
) {
334 const ViewId
embed_view_id(wm_connection()->id(), 1);
335 EXPECT_EQ(ERROR_CODE_NONE
, wm_connection()->CreateView(embed_view_id
));
336 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id
, true));
338 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id
));
339 host_connection()->view_tree_host()->root_view()->
340 SetBounds(gfx::Rect(0, 0, 100, 100));
341 mojo::URLRequestPtr
request(mojo::URLRequest::New());
342 wm_connection()->Embed(embed_view_id
, request
.Pass());
343 ViewTreeImpl
* connection1
=
344 connection_manager()->GetConnectionWithRoot(embed_view_id
);
345 ASSERT_TRUE(connection1
!= nullptr);
346 ASSERT_NE(connection1
, wm_connection());
349 ->GetView(embed_view_id
)
350 ->SetBounds(gfx::Rect(0, 0, 50, 50));
352 const ViewId
child1(connection1
->id(), 1);
353 EXPECT_EQ(ERROR_CODE_NONE
, connection1
->CreateView(child1
));
354 EXPECT_TRUE(connection1
->AddView(embed_view_id
, child1
));
355 ServerView
* v1
= connection1
->GetView(child1
);
356 v1
->SetVisible(true);
357 v1
->SetBounds(gfx::Rect(20, 20, 20, 20));
359 TestViewTreeClient
* connection1_client
= last_view_tree_client();
360 connection1_client
->tracker()->changes()->clear();
361 wm_client()->tracker()->changes()->clear();
363 display_manager_delegate()->OnEvent(CreatePointerDownEvent(21, 22));
364 // Focus should go to child1. This result in notifying both the window
365 // manager and client connection being notified.
366 EXPECT_EQ(v1
, connection1
->GetHost()->GetFocusedView());
367 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
368 EXPECT_EQ("Focused id=2,1",
369 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
370 ASSERT_GE(connection1_client
->tracker()->changes()->size(), 1u);
373 ChangesToDescription1(*connection1_client
->tracker()->changes())[0]);
375 display_manager_delegate()->OnEvent(CreatePointerUpEvent(21, 22));
376 wm_client()->tracker()->changes()->clear();
377 connection1_client
->tracker()->changes()->clear();
379 // Press outside of the embedded view. Focus should go to the root. Notice
380 // the client1 doesn't see who has focus as the focused view (root) isn't
382 display_manager_delegate()->OnEvent(CreatePointerDownEvent(61, 22));
383 EXPECT_EQ(host_connection()->view_tree_host()->root_view(),
384 host_connection()->view_tree_host()->GetFocusedView());
385 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
386 EXPECT_EQ("Focused id=0,2",
387 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
388 ASSERT_GE(connection1_client
->tracker()->changes()->size(), 1u);
391 ChangesToDescription1(*connection1_client
->tracker()->changes())[0]);
393 display_manager_delegate()->OnEvent(CreatePointerUpEvent(21, 22));
394 wm_client()->tracker()->changes()->clear();
395 connection1_client
->tracker()->changes()->clear();
397 // Press in the same location. Should not get a focus change event (only input
399 display_manager_delegate()->OnEvent(CreatePointerDownEvent(61, 22));
400 EXPECT_EQ(host_connection()->view_tree_host()->root_view(),
401 host_connection()->view_tree_host()->GetFocusedView());
402 ASSERT_EQ(wm_client()->tracker()->changes()->size(), 1u);
403 EXPECT_EQ("InputEvent view=0,2 event_action=4",
404 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
405 EXPECT_TRUE(connection1_client
->tracker()->changes()->empty());
408 TEST_F(ViewTreeTest
, BasicInputEventTarget
) {
409 const ViewId
embed_view_id(wm_connection()->id(), 1);
410 EXPECT_EQ(ERROR_CODE_NONE
, wm_connection()->CreateView(embed_view_id
));
411 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id
, true));
413 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id
));
414 host_connection()->view_tree_host()->root_view()->SetBounds(
415 gfx::Rect(0, 0, 100, 100));
416 mojo::URLRequestPtr
request(mojo::URLRequest::New());
417 wm_connection()->Embed(embed_view_id
, request
.Pass());
418 ViewTreeImpl
* connection1
=
419 connection_manager()->GetConnectionWithRoot(embed_view_id
);
420 ASSERT_TRUE(connection1
!= nullptr);
421 ASSERT_NE(connection1
, wm_connection());
424 ->GetView(embed_view_id
)
425 ->SetBounds(gfx::Rect(0, 0, 50, 50));
427 const ViewId
child1(connection1
->id(), 1);
428 EXPECT_EQ(ERROR_CODE_NONE
, connection1
->CreateView(child1
));
429 EXPECT_TRUE(connection1
->AddView(embed_view_id
, child1
));
430 ServerView
* v1
= connection1
->GetView(child1
);
431 v1
->SetVisible(true);
432 v1
->SetBounds(gfx::Rect(20, 20, 20, 20));
434 TestViewTreeClient
* embed_connection
= last_view_tree_client();
435 embed_connection
->tracker()->changes()->clear();
436 wm_client()->tracker()->changes()->clear();
438 // Send an event to |v1|. |embed_connection| should get the event, not
439 // |wm_client|, since |v1| lives inside an embedded view.
440 display_manager_delegate()->OnEvent(CreatePointerDownEvent(21, 22));
441 ASSERT_EQ(1u, wm_client()->tracker()->changes()->size());
442 EXPECT_EQ("Focused id=2,1",
443 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
444 ASSERT_EQ(2u, embed_connection
->tracker()->changes()->size());
445 EXPECT_EQ("Focused id=2,1",
446 ChangesToDescription1(*embed_connection
->tracker()->changes())[0]);
447 EXPECT_EQ("InputEvent view=2,1 event_action=4",
448 ChangesToDescription1(*embed_connection
->tracker()->changes())[1]);
451 } // namespace view_manager