Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: components/view_manager/view_tree_unittest.cc

Issue 1344573002: Mandoline: Rename components/view_manager to components/mus (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include <string>
6 #include <vector>
7
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"
27
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;
35
36 namespace view_manager {
37 namespace {
38
39 // -----------------------------------------------------------------------------
40
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 {}
47
48 TestChangeTracker* tracker() { return &tracker_; }
49
50 private:
51 // ViewTreeClient:
52 void OnEmbed(uint16_t connection_id,
53 ViewDataPtr root,
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());
59 }
60 void OnEmbeddedAppDisconnected(uint32_t view) override {
61 tracker_.OnEmbeddedAppDisconnected(view);
62 }
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());
68 }
69 void OnViewViewportMetricsChanged(
70 mojo::ViewportMetricsPtr old_metrics,
71 mojo::ViewportMetricsPtr new_metrics) override {
72 tracker_.OnViewViewportMetricsChanged(old_metrics.Pass(),
73 new_metrics.Pass());
74 }
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());
80 }
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);
85 }
86 void OnViewDeleted(uint32_t view) override { tracker_.OnViewDeleted(view); }
87 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
88 tracker_.OnViewVisibilityChanged(view, visible);
89 }
90 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
91 tracker_.OnViewDrawnStateChanged(view, drawn);
92 }
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());
97 }
98 void OnViewInputEvent(uint32_t view,
99 mojo::EventPtr event,
100 const mojo::Callback<void()>& callback) override {
101 tracker_.OnViewInputEvent(view, event.Pass());
102 }
103 void OnViewFocused(uint32_t focused_view_id) override {
104 tracker_.OnViewFocused(focused_view_id);
105 }
106
107 TestChangeTracker tracker_;
108
109 DISALLOW_COPY_AND_ASSIGN(TestViewTreeClient);
110 };
111
112 // -----------------------------------------------------------------------------
113
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_) {}
119
120 TestViewTreeClient* client() { return &client_; }
121
122 private:
123 ~TestClientConnection() override {}
124
125 TestViewTreeClient client_;
126
127 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
128 };
129
130 // -----------------------------------------------------------------------------
131
132 // Empty implementation of ConnectionManagerDelegate.
133 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
134 public:
135 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
136 ~TestConnectionManagerDelegate() override {}
137
138 TestViewTreeClient* last_client() {
139 return last_connection_ ? last_connection_->client() : nullptr;
140 }
141
142 TestClientConnection* last_connection() { return last_connection_; }
143
144 private:
145 // ConnectionManagerDelegate:
146 void OnNoMoreRootConnections() override {}
147
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_;
158 }
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_;
170 }
171
172 TestClientConnection* last_connection_;
173
174 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
175 };
176
177 // -----------------------------------------------------------------------------
178
179 class TestViewTreeHostConnection : public ViewTreeHostConnection {
180 public:
181 TestViewTreeHostConnection(scoped_ptr<ViewTreeHostImpl> host_impl,
182 ConnectionManager* manager)
183 : ViewTreeHostConnection(host_impl.Pass(), manager) {}
184 ~TestViewTreeHostConnection() override {}
185
186 private:
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()));
194 }
195 DISALLOW_COPY_AND_ASSIGN(TestViewTreeHostConnection);
196 };
197
198 // -----------------------------------------------------------------------------
199 // Empty implementation of DisplayManager.
200 class TestDisplayManager : public DisplayManager {
201 public:
202 TestDisplayManager() {}
203 ~TestDisplayManager() override {}
204
205 // DisplayManager:
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);
214 }
215 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override {
216 }
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_;
221 }
222 void UpdateTextInputState(const ui::TextInputState& state) override {}
223 void SetImeVisibility(bool visible) override {}
224
225 private:
226 mojo::ViewportMetrics display_metrices_;
227
228 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager);
229 };
230
231 // Factory that dispenses TestDisplayManagers.
232 class TestDisplayManagerFactory : public DisplayManagerFactory {
233 public:
234 TestDisplayManagerFactory() {}
235 ~TestDisplayManagerFactory() {}
236 DisplayManager* CreateDisplayManager(
237 bool is_headless,
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();
242 }
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerFactory);
246 };
247
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;
255 return event.Pass();
256 }
257
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;
265 return event.Pass();
266 }
267
268 } // namespace
269
270 // -----------------------------------------------------------------------------
271
272 class ViewTreeTest : public testing::Test {
273 public:
274 ViewTreeTest() : wm_client_(nullptr) {}
275 ~ViewTreeTest() override {}
276
277 // ViewTreeImpl for the window manager.
278 ViewTreeImpl* wm_connection() {
279 return connection_manager_->GetConnection(1);
280 }
281
282 TestViewTreeClient* last_view_tree_client() {
283 return delegate_.last_client();
284 }
285
286 TestClientConnection* last_client_connection() {
287 return delegate_.last_connection();
288 }
289
290 ConnectionManager* connection_manager() { return connection_manager_.get(); }
291
292 TestViewTreeClient* wm_client() { return wm_client_; }
293
294 TestViewTreeHostConnection* host_connection() { return host_connection_; }
295 DisplayManagerDelegate* display_manager_delegate() {
296 return host_connection()->view_tree_host();
297 }
298
299 protected:
300 // testing::Test:
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
313 // manage lifetime.
314 host_connection_ = new TestViewTreeHostConnection(
315 make_scoped_ptr(host), connection_manager_.get());
316 host->Init(host_connection_);
317 wm_client_ = delegate_.last_client();
318 }
319
320 private:
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_;
328
329 DISALLOW_COPY_AND_ASSIGN(ViewTreeTest);
330 };
331
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));
337 EXPECT_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());
347
348 connection_manager()
349 ->GetView(embed_view_id)
350 ->SetBounds(gfx::Rect(0, 0, 50, 50));
351
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));
358
359 TestViewTreeClient* connection1_client = last_view_tree_client();
360 connection1_client->tracker()->changes()->clear();
361 wm_client()->tracker()->changes()->clear();
362
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);
371 EXPECT_EQ(
372 "Focused id=2,1",
373 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
374
375 display_manager_delegate()->OnEvent(CreatePointerUpEvent(21, 22));
376 wm_client()->tracker()->changes()->clear();
377 connection1_client->tracker()->changes()->clear();
378
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
381 // visible to it.
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);
389 EXPECT_EQ(
390 "Focused id=null",
391 ChangesToDescription1(*connection1_client->tracker()->changes())[0]);
392
393 display_manager_delegate()->OnEvent(CreatePointerUpEvent(21, 22));
394 wm_client()->tracker()->changes()->clear();
395 connection1_client->tracker()->changes()->clear();
396
397 // Press in the same location. Should not get a focus change event (only input
398 // event).
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());
406 }
407
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));
412 EXPECT_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());
422
423 connection_manager()
424 ->GetView(embed_view_id)
425 ->SetBounds(gfx::Rect(0, 0, 50, 50));
426
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));
433
434 TestViewTreeClient* embed_connection = last_view_tree_client();
435 embed_connection->tracker()->changes()->clear();
436 wm_client()->tracker()->changes()->clear();
437
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]);
449 }
450
451 } // namespace view_manager
OLDNEW
« no previous file with comments | « components/view_manager/view_tree_impl.cc ('k') | components/view_manager/window_manager_access_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698