Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / mus / view_tree_unittest.cc
blob6326c3941750331d6ed5ee13e210f58795a9c23c
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/mus/client_connection.h"
10 #include "components/mus/connection_manager.h"
11 #include "components/mus/connection_manager_delegate.h"
12 #include "components/mus/display_manager.h"
13 #include "components/mus/display_manager_factory.h"
14 #include "components/mus/ids.h"
15 #include "components/mus/public/cpp/types.h"
16 #include "components/mus/public/cpp/util.h"
17 #include "components/mus/public/interfaces/view_tree.mojom.h"
18 #include "components/mus/server_view.h"
19 #include "components/mus/surfaces/surfaces_state.h"
20 #include "components/mus/test_change_tracker.h"
21 #include "components/mus/view_tree_host_connection.h"
22 #include "components/mus/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"
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 mus {
37 namespace {
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 {
44 public:
45 TestViewTreeClient() {}
46 ~TestViewTreeClient() override {}
48 TestChangeTracker* tracker() { return &tracker_; }
50 private:
51 // ViewTreeClient:
52 void OnEmbed(uint16_t connection_id,
53 ViewDataPtr root,
54 mojo::ViewTreePtr tree,
55 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(),
73 new_metrics.Pass());
75 void OnViewHierarchyChanged(uint32_t view,
76 uint32_t new_parent,
77 uint32_t old_parent,
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,
94 const String& name,
95 Array<uint8_t> new_data) override {
96 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
98 void OnViewInputEvent(uint32_t view,
99 mojo::EventPtr event,
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 {
116 public:
117 explicit TestClientConnection(scoped_ptr<ViewTreeImpl> service_impl)
118 : ClientConnection(service_impl.Pass(), &client_) {}
120 TestViewTreeClient* client() { return &client_; }
122 private:
123 ~TestClientConnection() override {}
125 TestViewTreeClient client_;
127 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
130 // -----------------------------------------------------------------------------
132 // Empty implementation of ConnectionManagerDelegate.
133 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
134 public:
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_; }
144 private:
145 // ConnectionManagerDelegate:
146 void OnNoMoreRootConnections() override {}
148 ClientConnection* CreateClientConnectionForEmbedAtView(
149 ConnectionManager* connection_manager,
150 mojo::InterfaceRequest<mojo::ViewTree> service_request,
151 ConnectionSpecificId creator_id,
152 mojo::URLRequestPtr request,
153 const ViewId& root_id,
154 uint32_t policy_bitmask) override {
155 scoped_ptr<ViewTreeImpl> service(new ViewTreeImpl(
156 connection_manager, creator_id, root_id, policy_bitmask));
157 last_connection_ = new TestClientConnection(service.Pass());
158 return last_connection_;
160 ClientConnection* CreateClientConnectionForEmbedAtView(
161 ConnectionManager* connection_manager,
162 mojo::InterfaceRequest<mojo::ViewTree> service_request,
163 ConnectionSpecificId creator_id,
164 const ViewId& root_id,
165 uint32_t policy_bitmask,
166 mojo::ViewTreeClientPtr client) override {
167 // Used by ConnectionManager::AddRoot.
168 scoped_ptr<ViewTreeImpl> service(new ViewTreeImpl(
169 connection_manager, creator_id, root_id, policy_bitmask));
170 last_connection_ = new TestClientConnection(service.Pass());
171 return last_connection_;
174 TestClientConnection* last_connection_;
176 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
179 // -----------------------------------------------------------------------------
181 class TestViewTreeHostConnection : public ViewTreeHostConnection {
182 public:
183 TestViewTreeHostConnection(scoped_ptr<ViewTreeHostImpl> host_impl,
184 ConnectionManager* manager)
185 : ViewTreeHostConnection(host_impl.Pass(), manager) {}
186 ~TestViewTreeHostConnection() override {}
188 private:
189 // ViewTreeHostDelegate:
190 void OnDisplayInitialized() override {
191 connection_manager()->AddHost(this);
192 set_view_tree(connection_manager()->EmbedAtView(
193 kInvalidConnectionId, view_tree_host()->root_view()->id(),
194 mojo::ViewTree::ACCESS_POLICY_EMBED_ROOT, mojo::ViewTreeClientPtr()));
196 DISALLOW_COPY_AND_ASSIGN(TestViewTreeHostConnection);
199 // -----------------------------------------------------------------------------
200 // Empty implementation of DisplayManager.
201 class TestDisplayManager : public DisplayManager {
202 public:
203 TestDisplayManager() {}
204 ~TestDisplayManager() override {}
206 // DisplayManager:
207 void Init(DisplayManagerDelegate* delegate) override {
208 // It is necessary to tell the delegate about the ViewportMetrics to make
209 // sure that the ViewTreeHostConnection is correctly initialized (and a
210 // root-view is created).
211 mojo::ViewportMetrics metrics;
212 metrics.size_in_pixels = mojo::Size::From(gfx::Size(400, 300));
213 metrics.device_pixel_ratio = 1.f;
214 delegate->OnViewportMetricsChanged(mojo::ViewportMetrics(), metrics);
216 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override {
218 void SetViewportSize(const gfx::Size& size) override {}
219 void SetTitle(const base::string16& title) override {}
220 const mojo::ViewportMetrics& GetViewportMetrics() override {
221 return display_metrices_;
223 void UpdateTextInputState(const ui::TextInputState& state) override {}
224 void SetImeVisibility(bool visible) override {}
226 private:
227 mojo::ViewportMetrics display_metrices_;
229 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager);
232 // Factory that dispenses TestDisplayManagers.
233 class TestDisplayManagerFactory : public DisplayManagerFactory {
234 public:
235 TestDisplayManagerFactory() {}
236 ~TestDisplayManagerFactory() {}
237 DisplayManager* CreateDisplayManager(
238 bool is_headless,
239 mojo::ApplicationImpl* app_impl,
240 const scoped_refptr<GpuState>& gpu_state,
241 const scoped_refptr<SurfacesState>& surfaces_state) override {
242 return new TestDisplayManager();
245 private:
246 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerFactory);
249 mojo::EventPtr CreatePointerDownEvent(int x, int y) {
250 mojo::EventPtr event(mojo::Event::New());
251 event->action = mojo::EVENT_TYPE_POINTER_DOWN;
252 event->pointer_data = mojo::PointerData::New();
253 event->pointer_data->pointer_id = 1u;
254 event->pointer_data->location->x = x;
255 event->pointer_data->location->y = y;
256 return event.Pass();
259 mojo::EventPtr CreatePointerUpEvent(int x, int y) {
260 mojo::EventPtr event(mojo::Event::New());
261 event->action = mojo::EVENT_TYPE_POINTER_UP;
262 event->pointer_data = mojo::PointerData::New();
263 event->pointer_data->pointer_id = 1u;
264 event->pointer_data->location->x = x;
265 event->pointer_data->location->y = y;
266 return event.Pass();
269 } // namespace
271 // -----------------------------------------------------------------------------
273 class ViewTreeTest : public testing::Test {
274 public:
275 ViewTreeTest() : wm_client_(nullptr) {}
276 ~ViewTreeTest() override {}
278 // ViewTreeImpl for the window manager.
279 ViewTreeImpl* wm_connection() {
280 return connection_manager_->GetConnection(1);
283 TestViewTreeClient* last_view_tree_client() {
284 return delegate_.last_client();
287 TestClientConnection* last_client_connection() {
288 return delegate_.last_connection();
291 ConnectionManager* connection_manager() { return connection_manager_.get(); }
293 TestViewTreeClient* wm_client() { return wm_client_; }
295 TestViewTreeHostConnection* host_connection() { return host_connection_; }
296 DisplayManagerDelegate* display_manager_delegate() {
297 return host_connection()->view_tree_host();
300 protected:
301 // testing::Test:
302 void SetUp() override {
303 DisplayManager::set_factory_for_testing(&display_manager_factory_);
304 // TODO(fsamuel): This is probably broken. We need a root.
305 connection_manager_.reset(
306 new ConnectionManager(&delegate_, scoped_refptr<SurfacesState>()));
307 ViewTreeHostImpl* host = new ViewTreeHostImpl(
308 mojo::ViewTreeHostClientPtr(), connection_manager_.get(),
309 true /* is_headless */, nullptr, scoped_refptr<GpuState>(),
310 scoped_refptr<SurfacesState>());
311 // TODO(fsamuel): This is way too magical. We need to find a better way to
312 // manage lifetime.
313 host_connection_ = new TestViewTreeHostConnection(
314 make_scoped_ptr(host), connection_manager_.get());
315 host->Init(host_connection_);
316 wm_client_ = delegate_.last_client();
319 private:
320 // TestViewTreeClient that is used for the WM connection.
321 TestViewTreeClient* wm_client_;
322 TestDisplayManagerFactory display_manager_factory_;
323 TestConnectionManagerDelegate delegate_;
324 TestViewTreeHostConnection* host_connection_;
325 scoped_ptr<ConnectionManager> connection_manager_;
326 base::MessageLoop message_loop_;
328 DISALLOW_COPY_AND_ASSIGN(ViewTreeTest);
331 // Verifies focus correctly changes on pointer events.
332 TEST_F(ViewTreeTest, FocusOnPointer) {
333 const ViewId embed_view_id(wm_connection()->id(), 1);
334 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
335 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
336 EXPECT_TRUE(
337 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
338 host_connection()->view_tree_host()->root_view()->SetBounds(
339 gfx::Rect(0, 0, 100, 100));
340 mojo::URLRequestPtr request(mojo::URLRequest::New());
341 wm_connection()->Embed(embed_view_id, request.Pass());
342 ViewTreeImpl* connection1 =
343 connection_manager()->GetConnectionWithRoot(embed_view_id);
344 ASSERT_TRUE(connection1 != nullptr);
345 ASSERT_NE(connection1, wm_connection());
347 connection_manager()
348 ->GetView(embed_view_id)
349 ->SetBounds(gfx::Rect(0, 0, 50, 50));
351 const ViewId child1(connection1->id(), 1);
352 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
353 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
354 ServerView* v1 = connection1->GetView(child1);
355 v1->SetVisible(true);
356 v1->SetBounds(gfx::Rect(20, 20, 20, 20));
358 TestViewTreeClient* connection1_client = last_view_tree_client();
359 connection1_client->tracker()->changes()->clear();
360 wm_client()->tracker()->changes()->clear();
362 display_manager_delegate()->OnEvent(ViewId(), CreatePointerDownEvent(21, 22));
363 // Focus should go to child1. This result in notifying both the window
364 // manager and client connection being notified.
365 EXPECT_EQ(v1, connection1->GetHost()->GetFocusedView());
366 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
367 EXPECT_EQ("Focused id=2,1",
368 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
369 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
370 EXPECT_EQ(
371 "Focused id=2,1",
372 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
374 display_manager_delegate()->OnEvent(ViewId(), CreatePointerUpEvent(21, 22));
375 wm_client()->tracker()->changes()->clear();
376 connection1_client->tracker()->changes()->clear();
378 // Press outside of the embedded view. Focus should go to the root. Notice
379 // the client1 doesn't see who has focus as the focused view (root) isn't
380 // visible to it.
381 display_manager_delegate()->OnEvent(ViewId(), CreatePointerDownEvent(61, 22));
382 EXPECT_EQ(host_connection()->view_tree_host()->root_view(),
383 host_connection()->view_tree_host()->GetFocusedView());
384 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
385 EXPECT_EQ("Focused id=0,2",
386 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
387 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
388 EXPECT_EQ(
389 "Focused id=null",
390 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
392 display_manager_delegate()->OnEvent(ViewId(), CreatePointerUpEvent(21, 22));
393 wm_client()->tracker()->changes()->clear();
394 connection1_client->tracker()->changes()->clear();
396 // Press in the same location. Should not get a focus change event (only input
397 // event).
398 display_manager_delegate()->OnEvent(ViewId(), CreatePointerDownEvent(61, 22));
399 EXPECT_EQ(host_connection()->view_tree_host()->root_view(),
400 host_connection()->view_tree_host()->GetFocusedView());
401 ASSERT_EQ(wm_client()->tracker()->changes()->size(), 1u);
402 EXPECT_EQ("InputEvent view=0,2 event_action=4",
403 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
404 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
407 TEST_F(ViewTreeTest, BasicInputEventTarget) {
408 const ViewId embed_view_id(wm_connection()->id(), 1);
409 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
410 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
411 EXPECT_TRUE(
412 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
413 host_connection()->view_tree_host()->root_view()->SetBounds(
414 gfx::Rect(0, 0, 100, 100));
415 mojo::URLRequestPtr request(mojo::URLRequest::New());
416 wm_connection()->Embed(embed_view_id, request.Pass());
417 ViewTreeImpl* connection1 =
418 connection_manager()->GetConnectionWithRoot(embed_view_id);
419 ASSERT_TRUE(connection1 != nullptr);
420 ASSERT_NE(connection1, wm_connection());
422 connection_manager()
423 ->GetView(embed_view_id)
424 ->SetBounds(gfx::Rect(0, 0, 50, 50));
426 const ViewId child1(connection1->id(), 1);
427 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
428 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
429 ServerView* v1 = connection1->GetView(child1);
430 v1->SetVisible(true);
431 v1->SetBounds(gfx::Rect(20, 20, 20, 20));
433 TestViewTreeClient* embed_connection = last_view_tree_client();
434 embed_connection->tracker()->changes()->clear();
435 wm_client()->tracker()->changes()->clear();
437 // Send an event to |v1|. |embed_connection| should get the event, not
438 // |wm_client|, since |v1| lives inside an embedded view.
439 display_manager_delegate()->OnEvent(ViewId(), CreatePointerDownEvent(21, 22));
440 ASSERT_EQ(1u, wm_client()->tracker()->changes()->size());
441 EXPECT_EQ("Focused id=2,1",
442 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
443 ASSERT_EQ(2u, embed_connection->tracker()->changes()->size());
444 EXPECT_EQ("Focused id=2,1",
445 ChangesToDescription1(*embed_connection->tracker()->changes())[0]);
446 EXPECT_EQ("InputEvent view=2,1 event_action=4",
447 ChangesToDescription1(*embed_connection->tracker()->changes())[1]);
450 } // namespace mus