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

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

Issue 720883003: Adds a CloneAndAnimate function to WindowManagerInternalClient (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: feedback Created 6 years, 1 month 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "mojo/common/common_type_converters.h" 8 #include "base/message_loop/message_loop.h"
9 #include "mojo/converters/geometry/geometry_type_converters.h" 9 #include "mojo/converters/geometry/geometry_type_converters.h"
10 #include "mojo/public/interfaces/application/service_provider.mojom.h" 10 #include "mojo/public/interfaces/application/service_provider.mojom.h"
11 #include "mojo/services/public/cpp/view_manager/types.h" 11 #include "mojo/services/public/cpp/view_manager/types.h"
12 #include "mojo/services/public/cpp/view_manager/util.h" 12 #include "mojo/services/public/cpp/view_manager/util.h"
13 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.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" 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" 15 #include "mojo/services/public/interfaces/window_manager/window_manager_internal .mojom.h"
16 #include "mojo/services/view_manager/client_connection.h" 16 #include "mojo/services/view_manager/client_connection.h"
17 #include "mojo/services/view_manager/connection_manager.h" 17 #include "mojo/services/view_manager/connection_manager.h"
18 #include "mojo/services/view_manager/connection_manager_delegate.h" 18 #include "mojo/services/view_manager/connection_manager_delegate.h"
19 #include "mojo/services/view_manager/display_manager.h" 19 #include "mojo/services/view_manager/display_manager.h"
20 #include "mojo/services/view_manager/ids.h" 20 #include "mojo/services/view_manager/ids.h"
21 #include "mojo/services/view_manager/server_view.h"
21 #include "mojo/services/view_manager/test_change_tracker.h" 22 #include "mojo/services/view_manager/test_change_tracker.h"
22 #include "mojo/services/view_manager/view_manager_service_impl.h" 23 #include "mojo/services/view_manager/view_manager_service_impl.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gfx/geometry/rect.h" 25 #include "ui/gfx/geometry/rect.h"
25 26
26 namespace mojo { 27 namespace mojo {
27 namespace service { 28 namespace service {
28 namespace { 29 namespace {
29 30
30 // ----------------------------------------------------------------------------- 31 // -----------------------------------------------------------------------------
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 TestViewManagerClient client_; 107 TestViewManagerClient client_;
107 108
108 DISALLOW_COPY_AND_ASSIGN(TestClientConnection); 109 DISALLOW_COPY_AND_ASSIGN(TestClientConnection);
109 }; 110 };
110 111
111 // ----------------------------------------------------------------------------- 112 // -----------------------------------------------------------------------------
112 113
113 // Empty implementation of ConnectionManagerDelegate. 114 // Empty implementation of ConnectionManagerDelegate.
114 class TestConnectionManagerDelegate : public ConnectionManagerDelegate { 115 class TestConnectionManagerDelegate : public ConnectionManagerDelegate {
115 public: 116 public:
116 TestConnectionManagerDelegate() : last_client_(nullptr) {} 117 TestConnectionManagerDelegate() : last_connection_(nullptr) {}
117 ~TestConnectionManagerDelegate() override {} 118 ~TestConnectionManagerDelegate() override {}
118 119
119 // TestViewManagerClient that was created by the last 120 TestViewManagerClient* last_client() {
120 // CreateClientConnectionForEmbedAtView() call. 121 return last_connection_ ? last_connection_->client() : nullptr;
121 TestViewManagerClient* last_client() { return last_client_; } 122 }
123
124 TestClientConnection* last_connection() { return last_connection_; }
122 125
123 private: 126 private:
124 // ConnectionManagerDelegate: 127 // ConnectionManagerDelegate:
125 void OnLostConnectionToWindowManager() override {} 128 void OnLostConnectionToWindowManager() override {}
126 129
127 ClientConnection* CreateClientConnectionForEmbedAtView( 130 ClientConnection* CreateClientConnectionForEmbedAtView(
128 ConnectionManager* connection_manager, 131 ConnectionManager* connection_manager,
129 ConnectionSpecificId creator_id, 132 ConnectionSpecificId creator_id,
130 const std::string& creator_url, 133 const std::string& creator_url,
131 const std::string& url, 134 const std::string& url,
132 const ViewId& root_id) override { 135 const ViewId& root_id) override {
133 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( 136 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl(
134 connection_manager, creator_id, creator_url, url, root_id)); 137 connection_manager, creator_id, creator_url, url, root_id));
135 TestClientConnection* connection = new TestClientConnection(service.Pass()); 138 last_connection_ = new TestClientConnection(service.Pass());
136 last_client_ = connection->client(); 139 return last_connection_;
137 return connection;
138 } 140 }
139 141
140 TestViewManagerClient* last_client_; 142 TestClientConnection* last_connection_;
141 143
142 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate); 144 DISALLOW_COPY_AND_ASSIGN(TestConnectionManagerDelegate);
143 }; 145 };
144 146
145 // ----------------------------------------------------------------------------- 147 // -----------------------------------------------------------------------------
146 148
147 // Empty implementation of DisplayManager. 149 // Empty implementation of DisplayManager.
148 class TestDisplayManager : public DisplayManager { 150 class TestDisplayManager : public DisplayManager {
149 public: 151 public:
150 TestDisplayManager() {} 152 TestDisplayManager() {}
(...skipping 28 matching lines...) Expand all
179 181
180 } // namespace 182 } // namespace
181 183
182 // ----------------------------------------------------------------------------- 184 // -----------------------------------------------------------------------------
183 185
184 class ViewManagerServiceTest : public testing::Test { 186 class ViewManagerServiceTest : public testing::Test {
185 public: 187 public:
186 ViewManagerServiceTest() : wm_client_(nullptr) {} 188 ViewManagerServiceTest() : wm_client_(nullptr) {}
187 ~ViewManagerServiceTest() override {} 189 ~ViewManagerServiceTest() override {}
188 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
189 protected: 208 protected:
190 // testing::Test: 209 // testing::Test:
191 void SetUp() override { 210 void SetUp() override {
192 connection_manager_.reset(new ConnectionManager( 211 connection_manager_.reset(new ConnectionManager(
193 &delegate_, scoped_ptr<DisplayManager>(new TestDisplayManager), 212 &delegate_, scoped_ptr<DisplayManager>(new TestDisplayManager),
194 &wm_internal_)); 213 &wm_internal_));
195
196 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl( 214 scoped_ptr<ViewManagerServiceImpl> service(new ViewManagerServiceImpl(
197 connection_manager_.get(), kInvalidConnectionId, std::string(), 215 connection_manager_.get(), kInvalidConnectionId, std::string(),
198 std::string("mojo:window_manager"), RootViewId())); 216 std::string("mojo:window_manager"), RootViewId()));
199 scoped_ptr<TestClientConnection> client_connection( 217 scoped_ptr<TestClientConnection> client_connection(
200 new TestClientConnection(service.Pass())); 218 new TestClientConnection(service.Pass()));
201 wm_client_ = client_connection->client(); 219 wm_client_ = client_connection->client();
220 ASSERT_TRUE(wm_client_ != nullptr);
202 connection_manager_->SetWindowManagerClientConnection( 221 connection_manager_->SetWindowManagerClientConnection(
203 client_connection.Pass()); 222 client_connection.Pass());
204 } 223 ASSERT_TRUE(wm_connection() != nullptr);
205 224 ASSERT_TRUE(wm_connection()->root() != nullptr);
206 // ViewManagerServiceImpl for the window manager. 225 }
207 ViewManagerServiceImpl* wm_connection() { 226
208 return connection_manager_->GetConnection(1); 227 private:
209 }
210
211 // TestViewManagerClient that is used for the WM connection. 228 // TestViewManagerClient that is used for the WM connection.
212 TestViewManagerClient* wm_client_; 229 TestViewManagerClient* wm_client_;
213 230
214 private:
215 TestWindowManagerInternal wm_internal_; 231 TestWindowManagerInternal wm_internal_;
216 TestConnectionManagerDelegate delegate_; 232 TestConnectionManagerDelegate delegate_;
217 scoped_ptr<ConnectionManager> connection_manager_; 233 scoped_ptr<ConnectionManager> connection_manager_;
234 base::MessageLoop message_loop_;
218 235
219 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest); 236 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceTest);
220 }; 237 };
221 238
222 TEST_F(ViewManagerServiceTest, Basic) { 239 namespace {
223 wm_connection()->CreateView(ViewId(wm_connection()->id(), 1)); 240
224 ASSERT_EQ(1u, wm_client_->tracker()->changes()->size()); 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()->AddView(*(test->wm_connection()->root()),
260 *embed_view_id));
261 EXPECT_TRUE(test->wm_connection()->Embed(
262 std::string(), *embed_view_id, InterfaceRequest<ServiceProvider>()));
263 ViewManagerServiceImpl* connection1 =
264 test->connection_manager()->GetConnectionWithRoot(*embed_view_id);
265 ASSERT_TRUE(connection1 != nullptr);
266 ASSERT_NE(connection1, test->wm_connection());
267
268 const ViewId child1(connection1->id(), 1);
269 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
270 const ViewId child2(connection1->id(), 2);
271 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
272 const ViewId child3(connection1->id(), 3);
273 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
274
275 ServerView* v1 = connection1->GetView(child1);
276 v1->SetBounds(gfx::Rect(1, 2, 11, 22));
277 ServerView* v2 = connection1->GetView(child2);
278 v2->SetBounds(gfx::Rect(2, 3, 6, 7));
279 ServerView* v3 = connection1->GetView(child3);
280 v3->SetBounds(gfx::Rect(3, 4, 6, 7));
281
282 EXPECT_TRUE(connection1->AddView(*embed_view_id, child1));
283 EXPECT_TRUE(connection1->AddView(child1, child2));
284 EXPECT_TRUE(connection1->AddView(child2, child3));
285
286 TestViewManagerClient* connection1_client = test->last_view_manager_client();
287 connection1_client->tracker()->changes()->clear();
288 test->wm_client()->tracker()->changes()->clear();
289 EXPECT_TRUE(test->connection_manager()->CloneAndAnimate(child2));
290 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
291 EXPECT_TRUE(test->wm_client()->tracker()->changes()->empty());
292
293 // We cloned v2. The cloned view ends up as a sibling of it.
294 const ServerView* cloned_view = GetFirstCloned(connection1->GetView(child1));
295 ASSERT_TRUE(cloned_view);
296 // |cloned_view| should have one and only one cloned child (corresponds to
297 // |child3|).
298 ASSERT_EQ(1u, cloned_view->GetChildren().size());
299 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
300
301 // Cloned views should match the bounds of the view they were cloned from.
302 EXPECT_EQ(v2->bounds(), cloned_view->bounds());
303 EXPECT_EQ(v3->bounds(), cloned_view->GetChildren()[0]->bounds());
304 }
305
306 } // namespace
307
308 TEST_F(ViewManagerServiceTest, ConnectionsCantSeeClonedViews) {
309 ViewId embed_view_id;
310 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
311
312 ViewManagerServiceImpl* connection1 =
313 connection_manager()->GetConnectionWithRoot(embed_view_id);
314
315 const ViewId child1(connection1->id(), 1);
316 const ViewId child2(connection1->id(), 2);
317 const ViewId child3(connection1->id(), 3);
318
319 // Verify the root doesn't see any cloned views.
320 std::vector<const ServerView*> views(
321 wm_connection()->GetViewTree(*wm_connection()->root()));
322 ASSERT_EQ(5u, views.size());
323 ASSERT_TRUE(views[0]->id() == *wm_connection()->root());
324 ASSERT_TRUE(views[1]->id() == embed_view_id);
325 ASSERT_TRUE(views[2]->id() == child1);
326 ASSERT_TRUE(views[3]->id() == child2);
327 ASSERT_TRUE(views[4]->id() == child3);
328
329 // Verify connection1 doesn't see any cloned views.
330 std::vector<const ServerView*> v1_views(
331 connection1->GetViewTree(embed_view_id));
332 ASSERT_EQ(4u, v1_views.size());
333 ASSERT_TRUE(v1_views[0]->id() == embed_view_id);
334 ASSERT_TRUE(v1_views[1]->id() == child1);
335 ASSERT_TRUE(v1_views[2]->id() == child2);
336 ASSERT_TRUE(v1_views[3]->id() == child3);
337 }
338
339 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnConnectionClose) {
340 ViewId embed_view_id;
341 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
342
343 // Destroy connection1, which should force the cloned view to become a child
344 // of where it was embedded (the embedded view still exists).
345 connection_manager()->OnConnectionError(last_client_connection());
346
347 ServerView* embed_view = wm_connection()->GetView(embed_view_id);
348 ASSERT_TRUE(embed_view != nullptr);
349 const ServerView* cloned_view = GetFirstCloned(embed_view);
350 ASSERT_TRUE(cloned_view);
351 ASSERT_EQ(1u, cloned_view->GetChildren().size());
352 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
353
354 // Because the cloned view changed parents its bounds should have changed.
355 EXPECT_EQ(gfx::Rect(3, 5, 6, 7), cloned_view->bounds());
356 // The bounds of the cloned child should not have changed though.
357 EXPECT_EQ(gfx::Rect(3, 4, 6, 7), cloned_view->GetChildren()[0]->bounds());
358 }
359
360 TEST_F(ViewManagerServiceTest, ClonedViewsPromotedOnHide) {
361 ViewId embed_view_id;
362 EXPECT_NO_FATAL_FAILURE(SetUpAnimate1(this, &embed_view_id));
363
364 ViewManagerServiceImpl* connection1 =
365 connection_manager()->GetConnectionWithRoot(embed_view_id);
366
367 // Hide the parent of the cloned view, which should force the cloned view to
368 // become a sibling of the parent.
369 const ServerView* view_to_hide =
370 connection1->GetView(ViewId(connection1->id(), 1));
371 ASSERT_TRUE(connection1->SetViewVisibility(view_to_hide->id(), false));
372
373 const ServerView* cloned_view = GetFirstCloned(view_to_hide->parent());
374 ASSERT_TRUE(cloned_view);
375 ASSERT_EQ(1u, cloned_view->GetChildren().size());
376 EXPECT_TRUE(cloned_view->GetChildren()[0]->id() == ClonedViewId());
377 EXPECT_EQ(2u, cloned_view->parent()->GetChildren().size());
378 EXPECT_TRUE(cloned_view->parent()->GetChildren()[1] == cloned_view);
379 }
380
381 // Clone and animate on a tree with more depth. Basically that of
382 // SetUpAnimate1() but cloning 2,1.
383 TEST_F(ViewManagerServiceTest, CloneAndAnimateLargerDepth) {
384 const ViewId embed_view_id(wm_connection()->id(), 1);
385 EXPECT_EQ(ERROR_CODE_NONE, wm_connection()->CreateView(embed_view_id));
386 EXPECT_TRUE(
387 wm_connection()->AddView(*(wm_connection()->root()), embed_view_id));
388 EXPECT_TRUE(wm_connection()->Embed(std::string(), embed_view_id,
389 InterfaceRequest<ServiceProvider>()));
390 ViewManagerServiceImpl* connection1 =
391 connection_manager()->GetConnectionWithRoot(embed_view_id);
392 ASSERT_TRUE(connection1 != nullptr);
393 ASSERT_NE(connection1, wm_connection());
394
395 const ViewId child1(connection1->id(), 1);
396 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child1));
397 const ViewId child2(connection1->id(), 2);
398 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child2));
399 const ViewId child3(connection1->id(), 3);
400 EXPECT_EQ(ERROR_CODE_NONE, connection1->CreateView(child3));
401
402 ServerView* v1 = connection1->GetView(child1);
403
404 EXPECT_TRUE(connection1->AddView(embed_view_id, child1));
405 EXPECT_TRUE(connection1->AddView(child1, child2));
406 EXPECT_TRUE(connection1->AddView(child2, child3));
407
408 TestViewManagerClient* connection1_client = last_view_manager_client();
409 connection1_client->tracker()->changes()->clear();
410 wm_client()->tracker()->changes()->clear();
411 EXPECT_TRUE(connection_manager()->CloneAndAnimate(child1));
412 EXPECT_TRUE(connection1_client->tracker()->changes()->empty());
413 EXPECT_TRUE(wm_client()->tracker()->changes()->empty());
414
415 // We cloned v1. The cloned view ends up as a sibling of it.
416 const ServerView* cloned_view = GetFirstCloned(v1->parent());
417 ASSERT_TRUE(cloned_view);
418 // |cloned_view| should have a child and its child should have a child.
419 ASSERT_EQ(1u, cloned_view->GetChildren().size());
420 const ServerView* cloned_view_child = cloned_view->GetChildren()[0];
421 EXPECT_EQ(1u, cloned_view_child->GetChildren().size());
422 EXPECT_TRUE(cloned_view_child->id() == ClonedViewId());
225 } 423 }
226 424
227 } // namespace service 425 } // namespace service
228 } // namespace mojo 426 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698