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

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

Issue 636363002: Splits window manager like methods into ViewManagerServiceDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor cleanup Created 6 years, 2 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
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 "base/at_exit.h" 8 #include "base/at_exit.h"
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "mojo/application_manager/application_manager.h" 16 #include "mojo/application_manager/application_manager.h"
17 #include "mojo/common/common_type_converters.h" 17 #include "mojo/common/common_type_converters.h"
18 #include "mojo/public/cpp/application/application_connection.h" 18 #include "mojo/public/cpp/application/application_connection.h"
19 #include "mojo/public/cpp/application/application_delegate.h" 19 #include "mojo/public/cpp/application/application_delegate.h"
20 #include "mojo/public/cpp/application/application_impl.h" 20 #include "mojo/public/cpp/application/application_impl.h"
21 #include "mojo/public/cpp/application/connect.h" 21 #include "mojo/public/cpp/application/connect.h"
22 #include "mojo/public/cpp/application/interface_factory_impl.h"
22 #include "mojo/public/cpp/bindings/lib/router.h" 23 #include "mojo/public/cpp/bindings/lib/router.h"
23 #include "mojo/public/interfaces/application/service_provider.mojom.h" 24 #include "mojo/public/interfaces/application/service_provider.mojom.h"
24 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" 25 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
25 #include "mojo/services/public/cpp/view_manager/types.h" 26 #include "mojo/services/public/cpp/view_manager/types.h"
26 #include "mojo/services/public/cpp/view_manager/util.h" 27 #include "mojo/services/public/cpp/view_manager/util.h"
27 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" 28 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
29 #include "mojo/services/public/interfaces/view_manager/view_manager_service_dele gate.mojom.h"
28 #include "mojo/services/view_manager/ids.h" 30 #include "mojo/services/view_manager/ids.h"
29 #include "mojo/services/view_manager/test_change_tracker.h" 31 #include "mojo/services/view_manager/test_change_tracker.h"
30 #include "mojo/shell/shell_test_helper.h" 32 #include "mojo/shell/shell_test_helper.h"
31 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
32 #include "ui/gfx/geometry/rect.h" 34 #include "ui/gfx/geometry/rect.h"
33 35
34 #if defined(OS_WIN) 36 #if defined(OS_WIN)
35 #include "ui/gfx/win/window_impl.h" 37 #include "ui/gfx/win/window_impl.h"
36 #endif 38 #endif
37 39
38 namespace mojo { 40 namespace mojo {
39 namespace service { 41 namespace service {
40 42
41 namespace { 43 namespace {
42 44
43 const char kTestServiceURL[] = "mojo:test_url"; 45 const char kTestServiceURL[] = "mojo:test_url";
44 const char kTestServiceURL2[] = "mojo:test_url2"; 46 const char kTestServiceURL2[] = "mojo:test_url2";
45 47
46 // ViewManagerProxy is a proxy to an ViewManagerService. It handles invoking 48 // ViewManagerProxy is a proxy to an ViewManagerService. It handles invoking
47 // ViewManagerService functions on the right thread in a synchronous manner 49 // ViewManagerService functions on the right thread in a synchronous manner
48 // (each ViewManagerService cover function blocks until the response from the 50 // (each ViewManagerService cover function blocks until the response from the
49 // ViewManagerService is returned). In addition it tracks the set of 51 // ViewManagerService is returned). In addition it tracks the set of
50 // ViewManagerClient messages received by way of a vector of Changes. Use 52 // ViewManagerClient messages received by way of a vector of Changes. Use
51 // DoRunLoopUntilChangesCount() to wait for a certain number of messages to be 53 // DoRunLoopUntilChangesCount() to wait for a certain number of messages to be
52 // received. 54 // received.
53 class ViewManagerProxy : public TestChangeTracker::Delegate { 55 class ViewManagerProxy : public TestChangeTracker::Delegate {
54 public: 56 public:
55 explicit ViewManagerProxy(TestChangeTracker* tracker) 57 explicit ViewManagerProxy(TestChangeTracker* tracker)
56 : tracker_(tracker), 58 : tracker_(tracker),
57 main_loop_(NULL), 59 main_loop_(nullptr),
58 view_manager_(NULL), 60 view_manager_(nullptr),
61 view_manager_delegate_client_(nullptr),
59 quit_count_(0), 62 quit_count_(0),
60 router_(NULL) { 63 router_(nullptr) {
61 SetInstance(this); 64 SetInstance(this);
62 } 65 }
63 66
64 virtual ~ViewManagerProxy() { 67 virtual ~ViewManagerProxy() {
65 } 68 }
66 69
67 // Returns true if in an initial state. If this returns false it means the 70 // Returns true if in an initial state. If this returns false it means the
68 // last test didn't clean up properly, or most likely didn't invoke 71 // last test didn't clean up properly, or most likely didn't invoke
69 // WaitForInstance() when it needed to. 72 // WaitForInstance() when it needed to.
70 static bool IsInInitialState() { return instance_ == NULL; } 73 static bool IsInInitialState() { return instance_ == NULL; }
71 74
72 // Runs a message loop until the single instance has been created. 75 // Runs a message loop until the single instance has been created.
73 static ViewManagerProxy* WaitForInstance() { 76 static ViewManagerProxy* WaitForInstance() {
74 if (!instance_) 77 if (!instance_)
75 RunMainLoop(); 78 RunMainLoop();
76 ViewManagerProxy* instance = instance_; 79 ViewManagerProxy* instance = instance_;
77 instance_ = NULL; 80 instance_ = NULL;
78 return instance; 81 return instance;
79 } 82 }
80 83
81 ViewManagerService* view_manager() { return view_manager_; } 84 ViewManagerService* view_manager() { return view_manager_; }
85 ViewManagerServiceDelegateClient* view_manager_delegate_client() {
86 return view_manager_delegate_client_;
87 }
82 88
83 // Runs the main loop until |count| changes have been received. 89 // Runs the main loop until |count| changes have been received.
84 std::vector<Change> DoRunLoopUntilChangesCount(size_t count) { 90 std::vector<Change> DoRunLoopUntilChangesCount(size_t count) {
85 DCHECK_EQ(0u, quit_count_); 91 DCHECK_EQ(0u, quit_count_);
86 if (tracker_->changes()->size() >= count) { 92 if (tracker_->changes()->size() >= count) {
87 CopyChangesFromTracker(); 93 CopyChangesFromTracker();
88 return changes_; 94 return changes_;
89 } 95 }
90 quit_count_ = count - tracker_->changes()->size(); 96 quit_count_ = count - tracker_->changes()->size();
91 // Run the current message loop. When |count| Changes have been received, 97 // Run the current message loop. When |count| Changes have been received,
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 view_id, 223 view_id,
218 visible, 224 visible,
219 base::Bind( 225 base::Bind(
220 &ViewManagerProxy::GotResult, base::Unretained(this), &result)); 226 &ViewManagerProxy::GotResult, base::Unretained(this), &result));
221 RunMainLoop(); 227 RunMainLoop();
222 return result; 228 return result;
223 } 229 }
224 230
225 private: 231 private:
226 friend class TestViewManagerClientConnection; 232 friend class TestViewManagerClientConnection;
233 friend class ViewManagerServiceDelegateImpl;
227 234
228 void set_router(mojo::internal::Router* router) { router_ = router; } 235 void set_router(mojo::internal::Router* router) { router_ = router; }
229 236
230 void set_view_manager(ViewManagerService* view_manager) { 237 void set_view_manager(ViewManagerService* view_manager) {
231 view_manager_ = view_manager; 238 view_manager_ = view_manager;
232 } 239 }
233 240
241 void set_view_manager_delegate_client(
242 ViewManagerServiceDelegateClient* client) {
243 view_manager_delegate_client_ = client;
244 }
245
234 static void RunMainLoop() { 246 static void RunMainLoop() {
235 DCHECK(!main_run_loop_); 247 DCHECK(!main_run_loop_);
236 main_run_loop_ = new base::RunLoop; 248 main_run_loop_ = new base::RunLoop;
237 main_run_loop_->Run(); 249 main_run_loop_->Run();
238 delete main_run_loop_; 250 delete main_run_loop_;
239 main_run_loop_ = NULL; 251 main_run_loop_ = NULL;
240 } 252 }
241 253
242 void QuitCountReached() { 254 void QuitCountReached() {
243 CopyChangesFromTracker(); 255 CopyChangesFromTracker();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 static ViewManagerProxy* instance_; 296 static ViewManagerProxy* instance_;
285 static base::RunLoop* main_run_loop_; 297 static base::RunLoop* main_run_loop_;
286 static bool in_embed_; 298 static bool in_embed_;
287 299
288 TestChangeTracker* tracker_; 300 TestChangeTracker* tracker_;
289 301
290 // MessageLoop of the test. 302 // MessageLoop of the test.
291 base::MessageLoop* main_loop_; 303 base::MessageLoop* main_loop_;
292 304
293 ViewManagerService* view_manager_; 305 ViewManagerService* view_manager_;
306 ViewManagerServiceDelegateClient* view_manager_delegate_client_;
294 307
295 // Number of changes we're waiting on until we quit the current loop. 308 // Number of changes we're waiting on until we quit the current loop.
296 size_t quit_count_; 309 size_t quit_count_;
297 310
298 std::vector<Change> changes_; 311 std::vector<Change> changes_;
299 312
300 mojo::internal::Router* router_; 313 mojo::internal::Router* router_;
301 314
302 DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy); 315 DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy);
303 }; 316 };
304 317
305 // static 318 // static
306 ViewManagerProxy* ViewManagerProxy::instance_ = NULL; 319 ViewManagerProxy* ViewManagerProxy::instance_ = NULL;
307 320
308 // static 321 // static
309 base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL; 322 base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL;
310 323
311 // static 324 // static
312 bool ViewManagerProxy::in_embed_ = false; 325 bool ViewManagerProxy::in_embed_ = false;
313 326
314 class TestViewManagerClientConnection 327 class TestViewManagerClientConnection
315 : public InterfaceImpl<ViewManagerClient> { 328 : public InterfaceImpl<ViewManagerClient> {
316 public: 329 public:
317 TestViewManagerClientConnection() : connection_(&tracker_) { 330 TestViewManagerClientConnection() : proxy_(&tracker_) {
318 tracker_.set_delegate(&connection_); 331 tracker_.set_delegate(&proxy_);
319 } 332 }
320 333
334 TestChangeTracker* tracker() { return &tracker_; }
335
336 ViewManagerProxy* proxy() { return &proxy_; }
337
321 // InterfaceImpl: 338 // InterfaceImpl:
322 virtual void OnConnectionEstablished() override { 339 virtual void OnConnectionEstablished() override {
323 connection_.set_router(internal_state()->router()); 340 proxy_.set_router(internal_state()->router());
324 connection_.set_view_manager(client()); 341 proxy_.set_view_manager(client());
325 } 342 }
326 343
327 // ViewManagerClient: 344 // ViewManagerClient:
328 virtual void OnEmbed( 345 virtual void OnEmbed(
329 ConnectionSpecificId connection_id, 346 ConnectionSpecificId connection_id,
330 const String& creator_url, 347 const String& creator_url,
331 ViewDataPtr root, 348 ViewDataPtr root,
332 InterfaceRequest<ServiceProvider> services) override { 349 InterfaceRequest<ServiceProvider> services) override {
333 tracker_.OnEmbed(connection_id, creator_url, root.Pass()); 350 tracker_.OnEmbed(connection_id, creator_url, root.Pass());
334 } 351 }
(...skipping 18 matching lines...) Expand all
353 tracker_.OnViewVisibilityChanged(view, visible); 370 tracker_.OnViewVisibilityChanged(view, visible);
354 } 371 }
355 virtual void OnViewDrawnStateChanged(uint32_t view, bool drawn) override { 372 virtual void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
356 tracker_.OnViewDrawnStateChanged(view, drawn); 373 tracker_.OnViewDrawnStateChanged(view, drawn);
357 } 374 }
358 virtual void OnViewInputEvent(Id view_id, 375 virtual void OnViewInputEvent(Id view_id,
359 EventPtr event, 376 EventPtr event,
360 const Callback<void()>& callback) override { 377 const Callback<void()>& callback) override {
361 tracker_.OnViewInputEvent(view_id, event.Pass()); 378 tracker_.OnViewInputEvent(view_id, event.Pass());
362 } 379 }
363 virtual void Embed(
364 const String& url,
365 InterfaceRequest<ServiceProvider> service_provider) override {
366 tracker_.DelegateEmbed(url);
367 }
368 virtual void DispatchOnViewInputEvent(mojo::EventPtr event) override {
369 }
370 380
371 private: 381 private:
372 TestChangeTracker tracker_; 382 TestChangeTracker tracker_;
373 ViewManagerProxy connection_; 383 ViewManagerProxy proxy_;
374 384
375 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection); 385 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection);
376 }; 386 };
377 387
388 class ViewManagerServiceDelegateImpl
389 : public InterfaceImpl<ViewManagerServiceDelegate> {
390 public:
391 explicit ViewManagerServiceDelegateImpl(
392 TestViewManagerClientConnection* connection)
393 : connection_(connection) {}
394 virtual ~ViewManagerServiceDelegateImpl() {}
395
396 // InterfaceImpl:
397 virtual void OnConnectionEstablished() override {
398 connection_->proxy()->set_view_manager_delegate_client(client());
399 }
400
401 // ViewManagerServiceDelegate:
402 virtual void Embed(
403 const String& url,
404 InterfaceRequest<ServiceProvider> service_provider) override {
405 connection_->tracker()->DelegateEmbed(url);
406 }
407 virtual void OnViewInputEvent(mojo::EventPtr event) override {}
408
409 private:
410 TestViewManagerClientConnection* connection_;
411
412 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceDelegateImpl);
413 };
414
378 // Used with ViewManagerService::Embed(). Creates a 415 // Used with ViewManagerService::Embed(). Creates a
379 // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy. 416 // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy.
380 class EmbedApplicationLoader : public ApplicationLoader, 417 class EmbedApplicationLoader
381 ApplicationDelegate, 418 : public ApplicationLoader,
382 public InterfaceFactory<ViewManagerClient> { 419 ApplicationDelegate,
420 public InterfaceFactory<ViewManagerClient>,
421 public InterfaceFactory<ViewManagerServiceDelegate> {
383 public: 422 public:
384 EmbedApplicationLoader() {} 423 EmbedApplicationLoader() : last_view_manager_client_(nullptr) {}
385 virtual ~EmbedApplicationLoader() {} 424 virtual ~EmbedApplicationLoader() {}
386 425
387 // ApplicationLoader implementation: 426 // ApplicationLoader implementation:
388 virtual void Load(ApplicationManager* manager, 427 virtual void Load(ApplicationManager* manager,
389 const GURL& url, 428 const GURL& url,
390 scoped_refptr<LoadCallbacks> callbacks) override { 429 scoped_refptr<LoadCallbacks> callbacks) override {
391 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); 430 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
392 if (!shell_handle.is_valid()) 431 if (!shell_handle.is_valid())
393 return; 432 return;
394 scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this, 433 scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this,
395 shell_handle.Pass())); 434 shell_handle.Pass()));
396 apps_.push_back(app.release()); 435 apps_.push_back(app.release());
397 } 436 }
398 virtual void OnApplicationError(ApplicationManager* manager, 437 virtual void OnApplicationError(ApplicationManager* manager,
399 const GURL& url) override {} 438 const GURL& url) override {}
400 439
401 // ApplicationDelegate implementation: 440 // ApplicationDelegate implementation:
402 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 441 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
403 override { 442 override {
404 connection->AddService(this); 443 connection->AddService<ViewManagerClient>(this);
444 connection->AddService<ViewManagerServiceDelegate>(this);
405 return true; 445 return true;
406 } 446 }
407 447
408 // InterfaceFactory<ViewManagerClient> implementation: 448 // InterfaceFactory<ViewManagerClient> implementation:
409 virtual void Create(ApplicationConnection* connection, 449 virtual void Create(ApplicationConnection* connection,
410 InterfaceRequest<ViewManagerClient> request) override { 450 InterfaceRequest<ViewManagerClient> request) override {
411 BindToRequest(new TestViewManagerClientConnection, &request); 451 last_view_manager_client_ = new TestViewManagerClientConnection;
452 BindToRequest(last_view_manager_client_, &request);
453 }
454 virtual void Create(
455 ApplicationConnection* connection,
456 InterfaceRequest<ViewManagerServiceDelegate> request) override {
457 BindToRequest(new ViewManagerServiceDelegateImpl(last_view_manager_client_),
458 &request);
412 } 459 }
413 460
414 private: 461 private:
462 // Used so that TestViewManagerClientConnection and
463 // ViewManagerServiceDelegateImpl can share the same TestChangeTracker.
464 TestViewManagerClientConnection* last_view_manager_client_;
415 ScopedVector<ApplicationImpl> apps_; 465 ScopedVector<ApplicationImpl> apps_;
416 466
417 DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader); 467 DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader);
418 }; 468 };
419 469
420 // Creates an id used for transport from the specified parameters. 470 // Creates an id used for transport from the specified parameters.
421 Id BuildViewId(ConnectionSpecificId connection_id, 471 Id BuildViewId(ConnectionSpecificId connection_id,
422 ConnectionSpecificId view_id) { 472 ConnectionSpecificId view_id) {
423 return (connection_id << 16) | view_id; 473 return (connection_id << 16) | view_id;
424 } 474 }
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
1183 } 1233 }
1184 1234
1185 TEST_F(ViewManagerTest, OnViewInput) { 1235 TEST_F(ViewManagerTest, OnViewInput) {
1186 ASSERT_TRUE(connection_->CreateView(BuildViewId(1, 1))); 1236 ASSERT_TRUE(connection_->CreateView(BuildViewId(1, 1)));
1187 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); 1237 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1188 1238
1189 // Dispatch an event to the view and verify its received. 1239 // Dispatch an event to the view and verify its received.
1190 { 1240 {
1191 EventPtr event(Event::New()); 1241 EventPtr event(Event::New());
1192 event->action = static_cast<EventType>(1); 1242 event->action = static_cast<EventType>(1);
1193 connection_->view_manager()->DispatchOnViewInputEvent(BuildViewId(1, 1), 1243 connection_->view_manager_delegate_client()->DispatchInputEventToView(
1194 event.Pass()); 1244 BuildViewId(1, 1), event.Pass());
1195 connection2_->DoRunLoopUntilChangesCount(1); 1245 connection2_->DoRunLoopUntilChangesCount(1);
1196 const Changes changes(ChangesToDescription1(connection2_->changes())); 1246 const Changes changes(ChangesToDescription1(connection2_->changes()));
1197 ASSERT_EQ(1u, changes.size()); 1247 ASSERT_EQ(1u, changes.size());
1198 EXPECT_EQ("InputEvent view=1,1 event_action=1", changes[0]); 1248 EXPECT_EQ("InputEvent view=1,1 event_action=1", changes[0]);
1199 } 1249 }
1200 } 1250 }
1201 1251
1202 TEST_F(ViewManagerTest, EmbedWithSameViewId) { 1252 TEST_F(ViewManagerTest, EmbedWithSameViewId) {
1203 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); 1253 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1204 1254
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 1475
1426 // TODO(sky): add coverage of test that destroys connections and ensures other 1476 // TODO(sky): add coverage of test that destroys connections and ensures other
1427 // connections get deletion notification. 1477 // connections get deletion notification.
1428 1478
1429 // TODO(sky): need to better track changes to initial connection. For example, 1479 // TODO(sky): need to better track changes to initial connection. For example,
1430 // that SetBounsdViews/AddView and the like don't result in messages to the 1480 // that SetBounsdViews/AddView and the like don't result in messages to the
1431 // originating connection. 1481 // originating connection.
1432 1482
1433 } // namespace service 1483 } // namespace service
1434 } // namespace mojo 1484 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698