OLD | NEW |
| (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/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" | |
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 mus { | |
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 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 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_; | |
159 } | |
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_; | |
172 } | |
173 | |
174 TestClientConnection* last_connection_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate); | |
177 }; | |
178 | |
179 // ----------------------------------------------------------------------------- | |
180 | |
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 {} | |
187 | |
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())); | |
195 } | |
196 DISALLOW_COPY_AND_ASSIGN(TestViewTreeHostConnection); | |
197 }; | |
198 | |
199 // ----------------------------------------------------------------------------- | |
200 // Empty implementation of DisplayManager. | |
201 class TestDisplayManager : public DisplayManager { | |
202 public: | |
203 TestDisplayManager() {} | |
204 ~TestDisplayManager() override {} | |
205 | |
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); | |
215 } | |
216 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override { | |
217 } | |
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_; | |
222 } | |
223 void UpdateTextInputState(const ui::TextInputState& state) override {} | |
224 void SetImeVisibility(bool visible) override {} | |
225 | |
226 private: | |
227 mojo::ViewportMetrics display_metrices_; | |
228 | |
229 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager); | |
230 }; | |
231 | |
232 // Factory that dispenses TestDisplayManagers. | |
233 class TestDisplayManagerFactory : public DisplayManagerFactory { | |
234 public: | |
235 TestDisplayManagerFactory() {} | |
236 ~TestDisplayManagerFactory() {} | |
237 DisplayManager* CreateDisplayManager( | |
238 mojo::ApplicationImpl* app_impl, | |
239 const scoped_refptr<GpuState>& gpu_state, | |
240 const scoped_refptr<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->location = mojo::LocationData::New(); | |
254 event->pointer_data->location->x = x; | |
255 event->pointer_data->location->y = y; | |
256 return event.Pass(); | |
257 } | |
258 | |
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 = mojo::LocationData::New(); | |
265 event->pointer_data->location->x = x; | |
266 event->pointer_data->location->y = y; | |
267 return event.Pass(); | |
268 } | |
269 | |
270 } // namespace | |
271 | |
272 // ----------------------------------------------------------------------------- | |
273 | |
274 class ViewTreeTest : public testing::Test { | |
275 public: | |
276 ViewTreeTest() : wm_client_(nullptr) {} | |
277 ~ViewTreeTest() override {} | |
278 | |
279 // ViewTreeImpl for the window manager. | |
280 ViewTreeImpl* wm_connection() { | |
281 return connection_manager_->GetConnection(1); | |
282 } | |
283 | |
284 TestViewTreeClient* last_view_tree_client() { | |
285 return delegate_.last_client(); | |
286 } | |
287 | |
288 TestClientConnection* last_client_connection() { | |
289 return delegate_.last_connection(); | |
290 } | |
291 | |
292 ConnectionManager* connection_manager() { return connection_manager_.get(); } | |
293 | |
294 TestViewTreeClient* wm_client() { return wm_client_; } | |
295 | |
296 TestViewTreeHostConnection* host_connection() { return host_connection_; } | |
297 DisplayManagerDelegate* display_manager_delegate() { | |
298 return host_connection()->view_tree_host(); | |
299 } | |
300 | |
301 protected: | |
302 // testing::Test: | |
303 void SetUp() override { | |
304 DisplayManager::set_factory_for_testing(&display_manager_factory_); | |
305 // TODO(fsamuel): This is probably broken. We need a root. | |
306 connection_manager_.reset( | |
307 new ConnectionManager(&delegate_, scoped_refptr<SurfacesState>())); | |
308 ViewTreeHostImpl* host = new ViewTreeHostImpl( | |
309 mojo::ViewTreeHostClientPtr(), connection_manager_.get(), | |
310 nullptr, scoped_refptr<GpuState>(), | |
311 scoped_refptr<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()->SetBounds( | |
340 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 mus | |
OLD | NEW |