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

Side by Side Diff: mojo/services/view_manager/view_manager_service_unittest.cc

Issue 774473003: Move view_manager service implementation to //services (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years 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 "mojo/converters/geometry/geometry_type_converters.h"
10 #include "mojo/public/interfaces/application/service_provider.mojom.h"
11 #include "mojo/services/public/cpp/view_manager/types.h"
12 #include "mojo/services/public/cpp/view_manager/util.h"
13 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
14 #include "mojo/services/public/interfaces/window_manager/window_manager.mojom.h"
15 #include "mojo/services/public/interfaces/window_manager/window_manager_internal .mojom.h"
16 #include "mojo/services/view_manager/client_connection.h"
17 #include "mojo/services/view_manager/connection_manager.h"
18 #include "mojo/services/view_manager/connection_manager_delegate.h"
19 #include "mojo/services/view_manager/display_manager.h"
20 #include "mojo/services/view_manager/ids.h"
21 #include "mojo/services/view_manager/server_view.h"
22 #include "mojo/services/view_manager/test_change_tracker.h"
23 #include "mojo/services/view_manager/view_manager_service_impl.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/gfx/geometry/rect.h"
26
27 namespace mojo {
28 namespace service {
29 namespace {
30
31 // -----------------------------------------------------------------------------
32
33 // ViewManagerClient implementation that logs all calls to a TestChangeTracker.
34 // TODO(sky): refactor so both this and ViewManagerServiceAppTest share code.
35 class TestViewManagerClient : public ViewManagerClient {
36 public:
37 TestViewManagerClient() {}
38 ~TestViewManagerClient() override {}
39
40 TestChangeTracker* tracker() { return &tracker_; }
41
42 private:
43 // ViewManagerClient:
44 void OnEmbed(uint16_t connection_id,
45 const String& embedder_url,
46 ViewDataPtr root,
47 InterfaceRequest<ServiceProvider> parent_service_provider,
48 ScopedMessagePipeHandle window_manager_pipe) override {
49 tracker_.OnEmbed(connection_id, embedder_url, root.Pass());
50 }
51 void OnEmbeddedAppDisconnected(uint32_t view) override {
52 tracker_.OnEmbeddedAppDisconnected(view);
53 }
54 void OnViewBoundsChanged(uint32_t view,
55 RectPtr old_bounds,
56 RectPtr new_bounds) override {
57 tracker_.OnViewBoundsChanged(view, old_bounds.Pass(), new_bounds.Pass());
58 }
59 void OnViewHierarchyChanged(uint32_t view,
60 uint32_t new_parent,
61 uint32_t old_parent,
62 Array<ViewDataPtr> views) override {
63 tracker_.OnViewHierarchyChanged(view, new_parent, old_parent, views.Pass());
64 }
65 void OnViewReordered(uint32_t view_id,
66 uint32_t relative_view_id,
67 OrderDirection direction) override {
68 tracker_.OnViewReordered(view_id, relative_view_id, direction);
69 }
70 void OnViewDeleted(uint32_t view) override { tracker_.OnViewDeleted(view); }
71 void OnViewVisibilityChanged(uint32_t view, bool visible) override {
72 tracker_.OnViewVisibilityChanged(view, visible);
73 }
74 void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
75 tracker_.OnViewDrawnStateChanged(view, drawn);
76 }
77 void OnViewSharedPropertyChanged(uint32_t view,
78 const String& name,
79 Array<uint8_t> new_data) override {
80 tracker_.OnViewSharedPropertyChanged(view, name, new_data.Pass());
81 }
82 void OnViewInputEvent(uint32_t view,
83 EventPtr event,
84 const Callback<void()>& callback) override {
85 tracker_.OnViewInputEvent(view, event.Pass());
86 }
87
88 TestChangeTracker tracker_;
89
90 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClient);
91 };
92
93 // -----------------------------------------------------------------------------
94
95 // ClientConnection implementation that vends TestViewManagerClient.
96 class TestClientConnection : public ClientConnection {
97 public:
98 explicit TestClientConnection(scoped_ptr<ViewManagerServiceImpl> service_impl)
99 : ClientConnection(service_impl.Pass()) {
100 set_client(&client_);
101 }
102 ~TestClientConnection() override {}
103
104 TestViewManagerClient* client() { return &client_; }
105
106 private:
107 TestViewManagerClient client_;
108
109 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
110 };
111
112 // -----------------------------------------------------------------------------
113
114 // Empty implementation of ConnectionManagerDelegate.
115 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
116 public:
117 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
118 ~TestConnectionManagerDelegate() override {}
119
120 TestViewManagerClient* last_client() {
121 return last_connection_ ? last_connection_->client() : nullptr;
122 }
123
124 TestClientConnection* last_connection() { return last_connection_; }
125
126 private:
127 // ConnectionManagerDelegate:
128 void OnLostConnectionToWindowManager() override {}
129
130 ClientConnection* CreateClientConnectionForEmbedAtView(
131 ConnectionManager* connection_manager,
132 ConnectionSpecificId creator_id,
133 const std::string& creator_url,
134 const std::string& url,
135 const ViewId& root_id) override {
136 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl(
137 connection_manager, creator_id, creator_url, url, root_id));
138 last_connection_ = new TestClientConnection(service.Pass());
139 return last_connection_;
140 }
141
142 TestClientConnection* last_connection_;
143
144 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
145 };
146
147 // -----------------------------------------------------------------------------
148
149 // Empty implementation of DisplayManager.
150 class TestDisplayManager : public DisplayManager {
151 public:
152 TestDisplayManager() {}
153 ~TestDisplayManager() override {}
154
155 // DisplayManager:
156 void Init(ConnectionManager* connection_manager) override {}
157 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds) override {
158 }
159 void SetViewportSize(const gfx::Size& size) override {}
160
161 private:
162 DISALLOW_COPY_AND_ASSIGN(TestDisplayManager);
163 };
164
165 // -----------------------------------------------------------------------------
166
167 // Empty implementation of WindowManagerInternal.
168 class TestWindowManagerInternal : public WindowManagerInternal {
169 public:
170 TestWindowManagerInternal() {}
171 ~TestWindowManagerInternal() override {}
172
173 // WindowManagerInternal:
174 void CreateWindowManagerForViewManagerClient(
175 uint16_t connection_id,
176 ScopedMessagePipeHandle window_manager_pipe) override {}
177
178 private:
179 DISALLOW_COPY_AND_ASSIGN(TestWindowManagerInternal);
180 };
181
182 } // namespace
183
184 // -----------------------------------------------------------------------------
185
186 class ViewManagerServiceTest : public testing::Test {
187 public:
188 ViewManagerServiceTest() : wm_client_(nullptr) {}
189 ~ViewManagerServiceTest() override {}
190
191 // ViewManagerServiceImpl for the window manager.
192 ViewManagerServiceImpl* wm_connection() {
193 return connection_manager_->GetConnection(1);
194 }
195
196 TestViewManagerClient* last_view_manager_client() {
197 return delegate_.last_client();
198 }
199
200 TestClientConnection* last_client_connection() {
201 return delegate_.last_connection();
202 }
203
204 ConnectionManager* connection_manager() { return connection_manager_.get(); }
205
206 TestViewManagerClient* wm_client() { return wm_client_; }
207
208 protected:
209 // testing::Test:
210 void SetUp() override {
211 connection_manager_.reset(new ConnectionManager(
212 &delegate_, scoped_ptr<DisplayManager>(new TestDisplayManager),
213 &wm_internal_));
214 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl(
215 connection_manager_.get(), kInvalidConnectionId, std::string(),
216 std::string("mojo:window_manager"), RootViewId()));
217 scoped_ptr<TestClientConnection> client_connection(
218 new TestClientConnection(service.Pass()));
219 wm_client_ = client_connection->client();
220 ASSERT_TRUE(wm_client_ != nullptr);
221 connection_manager_->SetWindowManagerClientConnection(
222 client_connection.Pass());
223 ASSERT_TRUE(wm_connection() != nullptr);
224 ASSERT_TRUE(wm_connection()->root() != nullptr);
225 }
226
227 private:
228 // TestViewManagerClient that is used for the WM connection.
229 TestViewManagerClient* wm_client_;
230
231 TestWindowManagerInternal wm_internal_;
232 TestConnectionManagerDelegate delegate_;
233 scoped_ptr<ConnectionManager> connection_manager_;
234 base::MessageLoop message_loop_;
235
236 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest);
237 };
238
239 namespace {
240
241 const ServerView* GetFirstCloned(const ServerView* view) {
242 for (const ServerView* child : view->GetChildren()) {
243 if (child->id() == ClonedViewId())
244 return child;
245 }
246 return nullptr;
247 }
248
249 // Provides common setup for animation tests. Creates the following views:
250 // 0,1 (the root, provided by view manager)
251 // 1,1 the second connection is embedded here (view owned by wm_connection()).
252 // 2,1 bounds=1,2 11x22
253 // 2,2 bounds=2,3 6x7
254 // 2,3 bounds=3,4 6x7
255 // CloneAndAnimate() is invoked for 2,2.
256 void SetUpAnimate1(ViewManagerServiceTest* test, ViewId* embed_view_id) {
257 *embed_view_id = ViewId(test->wm_connection()->id(), 1);
258 EXPECT_EQ(ERROR_CODE_NONE, test->wm_connection()->CreateView(*embed_view_id));
259 EXPECT_TRUE(test->wm_connection()->SetViewVisibility(*embed_view_id, true));
260 EXPECT_TRUE(test->wm_connection()->AddView(*(test->wm_connection()->root()),
261 *embed_view_id));
262 EXPECT_TRUE(test->wm_connection()->Embed(
263 std::string(), *embed_view_id, InterfaceRequest<ServiceProvider>()));
264 ViewManagerServiceImpl* connection1 =
265 test->connection_manager()->GetConnectionWithRoot(*embed_view_id);
266 ASSERT_TRUE(connection1 != nullptr);
267 ASSERT_NE(connection1, test->wm_connection());
268
269 const ViewId child1(connection1->id(), 1);
270 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
271 const ViewId child2(connection1->id(), 2);
272 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
273 const ViewId child3(connection1->id(), 3);
274 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
275
276 ServerView* v1 = connection1->GetView(child1);
277 v1->SetVisible(true);
278 v1->SetBounds(gfx::Rect(1, 2, 11, 22));
279 ServerView* v2 = connection1->GetView(child2);
280 v2->SetVisible(true);
281 v2->SetBounds(gfx::Rect(2, 3, 6, 7));
282 ServerView* v3 = connection1->GetView(child3);
283 v3->SetVisible(true);
284 v3->SetBounds(gfx::Rect(3, 4, 6, 7));
285
286 EXPECT_TRUE(connection1->AddView(*embed_view_id, child1));
287 EXPECT_TRUE(connection1->AddView(child1, child2));
288 EXPECT_TRUE(connection1->AddView(child2, child3));
289
290 TestViewManagerClient* connection1_client = test->last_view_manager_client();
291 connection1_client->tracker()->changes()->clear();
292 test->wm_client()->tracker()->changes()->clear();
293 EXPECT_TRUE(test->connection_manager()->CloneAndAnimate(child2));
294 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
295 EXPECT_TRUE(test->wm_client()->tracker()->changes()->empty());
296
297 // We cloned v2. The cloned view ends up as a sibling of it.
298 const ServerView* cloned_view = GetFirstCloned(connection1->GetView(child1));
299 ASSERT_TRUE(cloned_view);
300 // |cloned_view| should have one and only one cloned child (corresponds to
301 // |child3|).
302 ASSERT_EQ(1u, cloned_view->GetChildren().size());
303 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
304
305 // Cloned views should match the bounds of the view they were cloned from.
306 EXPECT_EQ(v2->bounds(), cloned_view->bounds());
307 EXPECT_EQ(v3->bounds(), cloned_view->GetChildren()[0]->bounds());
308
309 // Cloned views are owned by the ConnectionManager and shouldn't be returned
310 // from ViewManagerServiceImpl::GetView.
311 EXPECT_TRUE(connection1->GetView(ClonedViewId()) == nullptr);
312 EXPECT_TRUE(test->wm_connection()->GetView(ClonedViewId()) == nullptr);
313 }
314
315 } // namespace
316
317 // Verifies ViewManagerService::GetViewTree() doesn't return cloned views.
318 TEST_F(ViewManagerServiceTest, ConnectionsCantSeeClonedViews) {
319 ViewId embed_view_id;
320 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
321
322 ViewManagerServiceImpl* connection1 =
323 connection_manager()->GetConnectionWithRoot(embed_view_id);
324
325 const ViewId child1(connection1->id(), 1);
326 const ViewId child2(connection1->id(), 2);
327 const ViewId child3(connection1->id(), 3);
328
329 // Verify the root doesn't see any cloned views.
330 std::vector<const ServerView*> views(
331 wm_connection()->GetViewTree(*wm_connection()->root()));
332 ASSERT_EQ(5u, views.size());
333 ASSERT_TRUE(views[0]->id() == *wm_connection()->root());
334 ASSERT_TRUE(views[1]->id() == embed_view_id);
335 ASSERT_TRUE(views[2]->id() == child1);
336 ASSERT_TRUE(views[3]->id() == child2);
337 ASSERT_TRUE(views[4]->id() == child3);
338
339 // Verify connection1 doesn't see any cloned views.
340 std::vector<const ServerView*> v1_views(
341 connection1->GetViewTree(embed_view_id));
342 ASSERT_EQ(4u, v1_views.size());
343 ASSERT_TRUE(v1_views[0]->id() == embed_view_id);
344 ASSERT_TRUE(v1_views[1]->id() == child1);
345 ASSERT_TRUE(v1_views[2]->id() == child2);
346 ASSERT_TRUE(v1_views[3]->id() == child3);
347 }
348
349 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnConnectionClose) {
350 ViewId embed_view_id;
351 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
352
353 // Destroy connection1, which should force the cloned view to become a child
354 // of where it was embedded (the embedded view still exists).
355 connection_manager()->OnConnectionError(last_client_connection());
356
357 ServerView* embed_view = wm_connection()->GetView(embed_view_id);
358 ASSERT_TRUE(embed_view != nullptr);
359 const ServerView* cloned_view = GetFirstCloned(embed_view);
360 ASSERT_TRUE(cloned_view);
361 ASSERT_EQ(1u, cloned_view->GetChildren().size());
362 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
363
364 // Because the cloned view changed parents its bounds should have changed.
365 EXPECT_EQ(gfx::Rect(3, 5, 6, 7), cloned_view->bounds());
366 // The bounds of the cloned child should not have changed though.
367 EXPECT_EQ(gfx::Rect(3, 4, 6, 7), cloned_view->GetChildren()[0]->bounds());
368 }
369
370 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnHide) {
371 ViewId embed_view_id;
372 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
373
374 ViewManagerServiceImpl* connection1 =
375 connection_manager()->GetConnectionWithRoot(embed_view_id);
376
377 // Hide the parent of the cloned view, which should force the cloned view to
378 // become a sibling of the parent.
379 const ServerView* view_to_hide =
380 connection1->GetView(ViewId(connection1->id(), 1));
381 ASSERT_TRUE(connection1->SetViewVisibility(view_to_hide->id(), false));
382
383 const ServerView* cloned_view = GetFirstCloned(view_to_hide->parent());
384 ASSERT_TRUE(cloned_view);
385 ASSERT_EQ(1u, cloned_view->GetChildren().size());
386 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
387 EXPECT_EQ(2u, cloned_view->parent()->GetChildren().size());
388 EXPECT_TRUE(cloned_view->parent()->GetChildren()[1] == cloned_view);
389 }
390
391 // Clone and animate on a tree with more depth. Basically that of
392 // SetUpAnimate1() but cloning 2,1.
393 TEST_F(ViewManagerServiceTest, CloneAndAnimateLargerDepth) {
394 const ViewId embed_view_id(wm_connection()->id(), 1);
395 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
396 EXPECT_TRUE(wm_connection()->SetViewVisibility(embed_view_id, true));
397 EXPECT_TRUE(
398 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
399 EXPECT_TRUE(wm_connection()->Embed(std::string(), embed_view_id,
400 InterfaceRequest<ServiceProvider>()));
401 ViewManagerServiceImpl* connection1 =
402 connection_manager()->GetConnectionWithRoot(embed_view_id);
403 ASSERT_TRUE(connection1 != nullptr);
404 ASSERT_NE(connection1, wm_connection());
405
406 const ViewId child1(connection1->id(), 1);
407 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
408 const ViewId child2(connection1->id(), 2);
409 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
410 const ViewId child3(connection1->id(), 3);
411 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
412
413 ServerView* v1 = connection1->GetView(child1);
414 v1->SetVisible(true);
415 connection1->GetView(child2)->SetVisible(true);
416 connection1->GetView(child3)->SetVisible(true);
417
418 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
419 EXPECT_TRUE(connection1->AddView(child1, child2));
420 EXPECT_TRUE(connection1->AddView(child2, child3));
421
422 TestViewManagerClient* connection1_client = last_view_manager_client();
423 connection1_client->tracker()->changes()->clear();
424 wm_client()->tracker()->changes()->clear();
425 EXPECT_TRUE(connection_manager()->CloneAndAnimate(child1));
426 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
427 EXPECT_TRUE(wm_client()->tracker()->changes()->empty());
428
429 // We cloned v1. The cloned view ends up as a sibling of it.
430 const ServerView* cloned_view = GetFirstCloned(v1->parent());
431 ASSERT_TRUE(cloned_view);
432 // |cloned_view| should have a child and its child should have a child.
433 ASSERT_EQ(1u, cloned_view->GetChildren().size());
434 const ServerView* cloned_view_child = cloned_view->GetChildren()[0];
435 EXPECT_EQ(1u, cloned_view_child->GetChildren().size());
436 EXPECT_TRUE(cloned_view_child->id() == ClonedViewId());
437 }
438
439 } // namespace service
440 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698