Infobar material design refresh: bg color
[chromium-blink-merge.git] / components / view_manager / view_tree_unittest.cc
blobbdb67c5a2b7838b7e06fe23809cd30dde1320589
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_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"
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 // 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 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 OnEmbeddedAppDisconnected(uint32_t view) override {
60 tracker_.OnEmbeddedAppDisconnected(view);
62 void OnUnembed() override { tracker_.OnUnembed(); }
63 void OnViewBoundsChanged(uint32_t view,
64 mojo::RectPtr old_bounds,
65 mojo::RectPtr new_bounds) override {
66 tracker_.OnViewBoundsChanged(view, old_bounds.Pass(), new_bounds.Pass());
68 void OnViewViewportMetricsChanged(
69 mojo::ViewportMetricsPtr old_metrics,
70 mojo::ViewportMetricsPtr new_metrics) override {
71 tracker_.OnViewViewportMetricsChanged(old_metrics.Pass(),
72 new_metrics.Pass());
74 void OnViewHierarchyChanged(uint32_t view,
75 uint32_t new_parent,
76 uint32_t old_parent,
77 Array<ViewDataPtr> views) override {
78 tracker_.OnViewHierarchyChanged(view, new_parent, old_parent, views.Pass());
80 void OnViewReordered(uint32_t view_id,
81 uint32_t relative_view_id,
82 mojo::OrderDirection direction) override {
83 tracker_.OnViewReordered(view_id, relative_view_id, direction);
85 void OnViewDeleted(uint32_t view) override { tracker_.OnViewDeleted(view); }
86 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
87 tracker_.OnViewVisibilityChanged(view, visible);
89 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
90 tracker_.OnViewDrawnStateChanged(view, drawn);
92 void OnViewSharedPropertyChanged(uint32_t view,
93 const String& name,
94 Array<uint8_t> new_data) override {
95 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
97 void OnViewInputEvent(uint32_t view,
98 mojo::EventPtr event,
99 const mojo::Callback<void()>& callback) override {
100 tracker_.OnViewInputEvent(view, event.Pass());
102 void OnViewFocused(uint32_t focused_view_id) override {
103 tracker_.OnViewFocused(focused_view_id);
106 TestChangeTracker tracker_;
108 DISALLOW_COPY_AND_ASSIGN(TestViewTreeClient);
111 // -----------------------------------------------------------------------------
113 // ClientConnection implementation that vends TestViewTreeClient.
114 class TestClientConnection : public ClientConnection {
115 public:
116 explicit TestClientConnection(scoped_ptr<ViewTreeImpl> service_impl)
117 : ClientConnection(service_impl.Pass(), &client_) {}
119 TestViewTreeClient* client() { return &client_; }
121 private:
122 ~TestClientConnection() override {}
124 TestViewTreeClient client_;
126 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
129 // -----------------------------------------------------------------------------
131 // Empty implementation of ConnectionManagerDelegate.
132 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
133 public:
134 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
135 ~TestConnectionManagerDelegate() override {}
137 TestViewTreeClient* last_client() {
138 return last_connection_ ? last_connection_->client() : nullptr;
141 TestClientConnection* last_connection() { return last_connection_; }
143 private:
144 // ConnectionManagerDelegate:
145 void OnNoMoreRootConnections() override {}
147 ClientConnection* CreateClientConnectionForEmbedAtView(
148 ConnectionManager* connection_manager,
149 mojo::InterfaceRequest<mojo::ViewTree> service_request,
150 mojo::ConnectionSpecificId creator_id,
151 mojo::URLRequestPtr request,
152 const ViewId& root_id) override {
153 scoped_ptr<ViewTreeImpl> service(
154 new ViewTreeImpl(connection_manager, creator_id, root_id));
155 last_connection_ = new TestClientConnection(service.Pass());
156 return last_connection_;
158 ClientConnection* CreateClientConnectionForEmbedAtView(
159 ConnectionManager* connection_manager,
160 mojo::InterfaceRequest<mojo::ViewTree> service_request,
161 mojo::ConnectionSpecificId creator_id,
162 const ViewId& root_id,
163 mojo::ViewTreeClientPtr client) override {
164 // Used by ConnectionManager::AddRoot.
165 scoped_ptr<ViewTreeImpl> service(
166 new ViewTreeImpl(connection_manager, creator_id, root_id));
167 last_connection_ = new TestClientConnection(service.Pass());
168 return last_connection_;
171 TestClientConnection* last_connection_;
173 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
176 // -----------------------------------------------------------------------------
178 class TestViewTreeHostConnection : public ViewTreeHostConnection {
179 public:
180 TestViewTreeHostConnection(scoped_ptr<ViewTreeHostImpl> host_impl,
181 ConnectionManager* manager)
182 : ViewTreeHostConnection(host_impl.Pass(), manager) {}
183 ~TestViewTreeHostConnection() override {}
185 private:
186 // ViewTreeHostDelegate:
187 void OnDisplayInitialized() override {
188 connection_manager()->AddHost(this);
189 set_view_tree(connection_manager()->EmbedAtView(
190 kInvalidConnectionId,
191 view_tree_host()->root_view()->id(),
192 mojo::ViewTreeClientPtr()));
194 DISALLOW_COPY_AND_ASSIGN(TestViewTreeHostConnection);
197 // -----------------------------------------------------------------------------
198 // Empty implementation of DisplayManager.
199 class TestDisplayManager : public DisplayManager {
200 public:
201 TestDisplayManager() {}
202 ~TestDisplayManager() override {}
204 // DisplayManager:
205 void Init(DisplayManagerDelegate* delegate) override {
206 // It is necessary to tell the delegate about the ViewportMetrics to make
207 // sure that the ViewTreeHostConnection is correctly initialized (and a
208 // root-view is created).
209 mojo::ViewportMetrics metrics;
210 metrics.size_in_pixels = mojo::Size::From(gfx::Size(400, 300));
211 metrics.device_pixel_ratio = 1.f;
212 delegate->OnViewportMetricsChanged(mojo::ViewportMetrics(), metrics);
214 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override {
216 void SetViewportSize(const gfx::Size& size) override {}
217 const mojo::ViewportMetrics& GetViewportMetrics() override {
218 return display_metrices_;
220 void UpdateTextInputState(const ui::TextInputState& state) override {}
221 void SetImeVisibility(bool visible) override {}
223 private:
224 mojo::ViewportMetrics display_metrices_;
226 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager);
229 // Factory that dispenses TestDisplayManagers.
230 class TestDisplayManagerFactory : public DisplayManagerFactory {
231 public:
232 TestDisplayManagerFactory() {}
233 ~TestDisplayManagerFactory() {}
234 DisplayManager* CreateDisplayManager(
235 bool is_headless,
236 mojo::ApplicationImpl* app_impl,
237 const scoped_refptr<gles2::GpuState>& gpu_state,
238 const scoped_refptr<surfaces::SurfacesState>& surfaces_state) override {
239 return new TestDisplayManager();
242 private:
243 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerFactory);
246 mojo::EventPtr CreatePointerDownEvent(int x, int y) {
247 mojo::EventPtr event(mojo::Event::New());
248 event->action = mojo::EVENT_TYPE_POINTER_DOWN;
249 event->pointer_data = mojo::PointerData::New();
250 event->pointer_data->pointer_id = 1u;
251 event->pointer_data->x = x;
252 event->pointer_data->y = y;
253 return event.Pass();
256 mojo::EventPtr CreatePointerUpEvent(int x, int y) {
257 mojo::EventPtr event(mojo::Event::New());
258 event->action = mojo::EVENT_TYPE_POINTER_UP;
259 event->pointer_data = mojo::PointerData::New();
260 event->pointer_data->pointer_id = 1u;
261 event->pointer_data->x = x;
262 event->pointer_data->y = y;
263 return event.Pass();
266 } // namespace
268 // -----------------------------------------------------------------------------
270 class ViewTreeTest : public testing::Test {
271 public:
272 ViewTreeTest() : wm_client_(nullptr) {}
273 ~ViewTreeTest() override {}
275 // ViewTreeImpl for the window manager.
276 ViewTreeImpl* wm_connection() {
277 return connection_manager_->GetConnection(1);
280 TestViewTreeClient* last_view_tree_client() {
281 return delegate_.last_client();
284 TestClientConnection* last_client_connection() {
285 return delegate_.last_connection();
288 ConnectionManager* connection_manager() { return connection_manager_.get(); }
290 TestViewTreeClient* wm_client() { return wm_client_; }
292 TestViewTreeHostConnection* host_connection() { return host_connection_; }
294 protected:
295 // testing::Test:
296 void SetUp() override {
297 DisplayManager::set_factory_for_testing(&display_manager_factory_);
298 // TODO(fsamuel): This is probably broken. We need a root.
299 connection_manager_.reset(
300 new ConnectionManager(&delegate_,
301 scoped_refptr<surfaces::SurfacesState>()));
302 ViewTreeHostImpl* host = new ViewTreeHostImpl(
303 mojo::ViewTreeHostClientPtr(),
304 connection_manager_.get(), true /* is_headless */, nullptr,
305 scoped_refptr<gles2::GpuState>(),
306 scoped_refptr<surfaces::SurfacesState>());
307 // TODO(fsamuel): This is way too magical. We need to find a better way to
308 // manage lifetime.
309 host_connection_ = new TestViewTreeHostConnection(
310 make_scoped_ptr(host), connection_manager_.get());
311 host->Init(host_connection_);
312 wm_client_ = delegate_.last_client();
315 private:
316 // TestViewTreeClient that is used for the WM connection.
317 TestViewTreeClient* wm_client_;
318 TestDisplayManagerFactory display_manager_factory_;
319 TestConnectionManagerDelegate delegate_;
320 TestViewTreeHostConnection* host_connection_;
321 scoped_ptr<ConnectionManager> connection_manager_;
322 base::MessageLoop message_loop_;
324 DISALLOW_COPY_AND_ASSIGN(ViewTreeTest);
327 // Verifies focus correctly changes on pointer events.
328 TEST_F(ViewTreeTest, FocusOnPointer) {
329 const ViewId embed_view_id(wm_connection()->id(), 1);
330 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
331 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
332 EXPECT_TRUE(
333 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
334 host_connection()->view_tree_host()->root_view()->
335 SetBounds(gfx::Rect(0, 0, 100, 100));
336 mojo::URLRequestPtr request(mojo::URLRequest::New());
337 wm_connection()->Embed(embed_view_id, request.Pass());
338 ViewTreeImpl* connection1 =
339 connection_manager()->GetConnectionWithRoot(embed_view_id);
340 ASSERT_TRUE(connection1 != nullptr);
341 ASSERT_NE(connection1, wm_connection());
343 connection_manager()
344 ->GetView(embed_view_id)
345 ->SetBounds(gfx::Rect(0, 0, 50, 50));
347 const ViewId child1(connection1->id(), 1);
348 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
349 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
350 ServerView* v1 = connection1->GetView(child1);
351 v1->SetVisible(true);
352 v1->SetBounds(gfx::Rect(20, 20, 20, 20));
354 TestViewTreeClient* connection1_client = last_view_tree_client();
355 connection1_client->tracker()->changes()->clear();
356 wm_client()->tracker()->changes()->clear();
358 connection_manager()->OnEvent(host_connection()->view_tree_host(),
359 CreatePointerDownEvent(21, 22));
360 // Focus should go to child1. This result in notifying both the window
361 // manager and client connection being notified.
362 EXPECT_EQ(v1, connection1->GetHost()->GetFocusedView());
363 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
364 EXPECT_EQ("Focused id=2,1",
365 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
366 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
367 EXPECT_EQ(
368 "Focused id=2,1",
369 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
371 connection_manager()->OnEvent(host_connection()->view_tree_host(),
372 CreatePointerUpEvent(21, 22));
373 wm_client()->tracker()->changes()->clear();
374 connection1_client->tracker()->changes()->clear();
376 // Press outside of the embedded view. Focus should go to the root. Notice
377 // the client1 doesn't see who has focus as the focused view (root) isn't
378 // visible to it.
379 connection_manager()->OnEvent(host_connection()->view_tree_host(),
380 CreatePointerDownEvent(61, 22));
381 EXPECT_EQ(host_connection()->view_tree_host()->root_view(),
382 host_connection()->view_tree_host()->GetFocusedView());
383 ASSERT_GE(wm_client()->tracker()->changes()->size(), 1u);
384 EXPECT_EQ("Focused id=0,2",
385 ChangesToDescription1(*wm_client()->tracker()->changes())[0]);
386 ASSERT_GE(connection1_client->tracker()->changes()->size(), 1u);
387 EXPECT_EQ(
388 "Focused id=null",
389 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
391 connection_manager()->OnEvent(host_connection()->view_tree_host(),
392 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 connection_manager()->OnEvent(host_connection()->view_tree_host(),
399 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 } // namespace view_manager