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

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: merge 2 trunk 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/converters/geometry/geometry_type_converters.h" 18 #include "mojo/converters/geometry/geometry_type_converters.h"
19 #include "mojo/public/cpp/application/application_connection.h" 19 #include "mojo/public/cpp/application/application_connection.h"
20 #include "mojo/public/cpp/application/application_delegate.h" 20 #include "mojo/public/cpp/application/application_delegate.h"
21 #include "mojo/public/cpp/application/application_impl.h" 21 #include "mojo/public/cpp/application/application_impl.h"
22 #include "mojo/public/cpp/application/connect.h" 22 #include "mojo/public/cpp/application/connect.h"
23 #include "mojo/public/cpp/application/interface_factory_impl.h"
23 #include "mojo/public/cpp/bindings/lib/router.h" 24 #include "mojo/public/cpp/bindings/lib/router.h"
24 #include "mojo/public/interfaces/application/service_provider.mojom.h" 25 #include "mojo/public/interfaces/application/service_provider.mojom.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/window_manager/window_manager.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 window_manager_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 WindowManagerClient* window_manager_client() {
86 return window_manager_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 WindowManagerServiceImpl;
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_window_manager_client(WindowManagerClient* client) {
242 window_manager_client_ = client;
243 }
244
234 static void RunMainLoop() { 245 static void RunMainLoop() {
235 DCHECK(!main_run_loop_); 246 DCHECK(!main_run_loop_);
236 main_run_loop_ = new base::RunLoop; 247 main_run_loop_ = new base::RunLoop;
237 main_run_loop_->Run(); 248 main_run_loop_->Run();
238 delete main_run_loop_; 249 delete main_run_loop_;
239 main_run_loop_ = NULL; 250 main_run_loop_ = NULL;
240 } 251 }
241 252
242 void QuitCountReached() { 253 void QuitCountReached() {
243 CopyChangesFromTracker(); 254 CopyChangesFromTracker();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 static ViewManagerProxy* instance_; 295 static ViewManagerProxy* instance_;
285 static base::RunLoop* main_run_loop_; 296 static base::RunLoop* main_run_loop_;
286 static bool in_embed_; 297 static bool in_embed_;
287 298
288 TestChangeTracker* tracker_; 299 TestChangeTracker* tracker_;
289 300
290 // MessageLoop of the test. 301 // MessageLoop of the test.
291 base::MessageLoop* main_loop_; 302 base::MessageLoop* main_loop_;
292 303
293 ViewManagerService* view_manager_; 304 ViewManagerService* view_manager_;
305 WindowManagerClient* window_manager_client_;
294 306
295 // Number of changes we're waiting on until we quit the current loop. 307 // Number of changes we're waiting on until we quit the current loop.
296 size_t quit_count_; 308 size_t quit_count_;
297 309
298 std::vector<Change> changes_; 310 std::vector<Change> changes_;
299 311
300 mojo::internal::Router* router_; 312 mojo::internal::Router* router_;
301 313
302 DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy); 314 DISALLOW_COPY_AND_ASSIGN(ViewManagerProxy);
303 }; 315 };
304 316
305 // static 317 // static
306 ViewManagerProxy* ViewManagerProxy::instance_ = NULL; 318 ViewManagerProxy* ViewManagerProxy::instance_ = NULL;
307 319
308 // static 320 // static
309 base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL; 321 base::RunLoop* ViewManagerProxy::main_run_loop_ = NULL;
310 322
311 // static 323 // static
312 bool ViewManagerProxy::in_embed_ = false; 324 bool ViewManagerProxy::in_embed_ = false;
313 325
314 class TestViewManagerClientConnection 326 class TestViewManagerClientConnection
315 : public InterfaceImpl<ViewManagerClient> { 327 : public InterfaceImpl<ViewManagerClient> {
316 public: 328 public:
317 TestViewManagerClientConnection() : connection_(&tracker_) { 329 TestViewManagerClientConnection() : proxy_(&tracker_) {
318 tracker_.set_delegate(&connection_); 330 tracker_.set_delegate(&proxy_);
319 } 331 }
320 332
333 TestChangeTracker* tracker() { return &tracker_; }
334
335 ViewManagerProxy* proxy() { return &proxy_; }
336
321 // InterfaceImpl: 337 // InterfaceImpl:
322 virtual void OnConnectionEstablished() override { 338 virtual void OnConnectionEstablished() override {
323 connection_.set_router(internal_state()->router()); 339 proxy_.set_router(internal_state()->router());
324 connection_.set_view_manager(client()); 340 proxy_.set_view_manager(client());
325 } 341 }
326 342
327 // ViewManagerClient: 343 // ViewManagerClient:
328 virtual void OnEmbed( 344 virtual void OnEmbed(
329 ConnectionSpecificId connection_id, 345 ConnectionSpecificId connection_id,
330 const String& creator_url, 346 const String& creator_url,
331 ViewDataPtr root, 347 ViewDataPtr root,
332 InterfaceRequest<ServiceProvider> services) override { 348 InterfaceRequest<ServiceProvider> services) override {
333 tracker_.OnEmbed(connection_id, creator_url, root.Pass()); 349 tracker_.OnEmbed(connection_id, creator_url, root.Pass());
334 } 350 }
(...skipping 18 matching lines...) Expand all
353 tracker_.OnViewVisibilityChanged(view, visible); 369 tracker_.OnViewVisibilityChanged(view, visible);
354 } 370 }
355 virtual void OnViewDrawnStateChanged(uint32_t view, bool drawn) override { 371 virtual void OnViewDrawnStateChanged(uint32_t view, bool drawn) override {
356 tracker_.OnViewDrawnStateChanged(view, drawn); 372 tracker_.OnViewDrawnStateChanged(view, drawn);
357 } 373 }
358 virtual void OnViewInputEvent(Id view_id, 374 virtual void OnViewInputEvent(Id view_id,
359 EventPtr event, 375 EventPtr event,
360 const Callback<void()>& callback) override { 376 const Callback<void()>& callback) override {
361 tracker_.OnViewInputEvent(view_id, event.Pass()); 377 tracker_.OnViewInputEvent(view_id, event.Pass());
362 } 378 }
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 379
371 private: 380 private:
372 TestChangeTracker tracker_; 381 TestChangeTracker tracker_;
373 ViewManagerProxy connection_; 382 ViewManagerProxy proxy_;
374 383
375 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection); 384 DISALLOW_COPY_AND_ASSIGN(TestViewManagerClientConnection);
376 }; 385 };
377 386
387 class WindowManagerServiceImpl : public InterfaceImpl<WindowManagerService> {
388 public:
389 explicit WindowManagerServiceImpl(TestViewManagerClientConnection* connection)
390 : connection_(connection) {}
391 virtual ~WindowManagerServiceImpl() {}
392
393 // InterfaceImpl:
394 virtual void OnConnectionEstablished() override {
395 connection_->proxy()->set_window_manager_client(client());
396 }
397
398 // WindowManagerService:
399 virtual void Embed(
400 const String& url,
401 InterfaceRequest<ServiceProvider> service_provider) override {
402 connection_->tracker()->DelegateEmbed(url);
403 }
404 virtual void OnViewInputEvent(mojo::EventPtr event) override {}
405
406 private:
407 TestViewManagerClientConnection* connection_;
408
409 DISALLOW_COPY_AND_ASSIGN(WindowManagerServiceImpl);
410 };
411
378 // Used with ViewManagerService::Embed(). Creates a 412 // Used with ViewManagerService::Embed(). Creates a
379 // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy. 413 // TestViewManagerClientConnection, which creates and owns the ViewManagerProxy.
380 class EmbedApplicationLoader : public ApplicationLoader, 414 class EmbedApplicationLoader : public ApplicationLoader,
381 ApplicationDelegate, 415 ApplicationDelegate,
382 public InterfaceFactory<ViewManagerClient> { 416 public InterfaceFactory<ViewManagerClient>,
417 public InterfaceFactory<WindowManagerService> {
383 public: 418 public:
384 EmbedApplicationLoader() {} 419 EmbedApplicationLoader() : last_view_manager_client_(nullptr) {}
385 virtual ~EmbedApplicationLoader() {} 420 virtual ~EmbedApplicationLoader() {}
386 421
387 // ApplicationLoader implementation: 422 // ApplicationLoader implementation:
388 virtual void Load(ApplicationManager* manager, 423 virtual void Load(ApplicationManager* manager,
389 const GURL& url, 424 const GURL& url,
390 scoped_refptr<LoadCallbacks> callbacks) override { 425 scoped_refptr<LoadCallbacks> callbacks) override {
391 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); 426 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
392 if (!shell_handle.is_valid()) 427 if (!shell_handle.is_valid())
393 return; 428 return;
394 scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this, 429 scoped_ptr<ApplicationImpl> app(new ApplicationImpl(this,
395 shell_handle.Pass())); 430 shell_handle.Pass()));
396 apps_.push_back(app.release()); 431 apps_.push_back(app.release());
397 } 432 }
398 virtual void OnApplicationError(ApplicationManager* manager, 433 virtual void OnApplicationError(ApplicationManager* manager,
399 const GURL& url) override {} 434 const GURL& url) override {}
400 435
401 // ApplicationDelegate implementation: 436 // ApplicationDelegate implementation:
402 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 437 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
403 override { 438 override {
404 connection->AddService(this); 439 connection->AddService<ViewManagerClient>(this);
440 connection->AddService<WindowManagerService>(this);
405 return true; 441 return true;
406 } 442 }
407 443
408 // InterfaceFactory<ViewManagerClient> implementation: 444 // InterfaceFactory<ViewManagerClient> implementation:
409 virtual void Create(ApplicationConnection* connection, 445 virtual void Create(ApplicationConnection* connection,
410 InterfaceRequest<ViewManagerClient> request) override { 446 InterfaceRequest<ViewManagerClient> request) override {
411 BindToRequest(new TestViewManagerClientConnection, &request); 447 last_view_manager_client_ = new TestViewManagerClientConnection;
448 BindToRequest(last_view_manager_client_, &request);
449 }
450 virtual void Create(ApplicationConnection* connection,
451 InterfaceRequest<WindowManagerService> request) override {
452 BindToRequest(new WindowManagerServiceImpl(last_view_manager_client_),
453 &request);
412 } 454 }
413 455
414 private: 456 private:
457 // Used so that TestViewManagerClientConnection and
458 // WindowManagerServiceImpl can share the same TestChangeTracker.
459 TestViewManagerClientConnection* last_view_manager_client_;
415 ScopedVector<ApplicationImpl> apps_; 460 ScopedVector<ApplicationImpl> apps_;
416 461
417 DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader); 462 DISALLOW_COPY_AND_ASSIGN(EmbedApplicationLoader);
418 }; 463 };
419 464
420 // Creates an id used for transport from the specified parameters. 465 // Creates an id used for transport from the specified parameters.
421 Id BuildViewId(ConnectionSpecificId connection_id, 466 Id BuildViewId(ConnectionSpecificId connection_id,
422 ConnectionSpecificId view_id) { 467 ConnectionSpecificId view_id) {
423 return (connection_id << 16) | view_id; 468 return (connection_id << 16) | view_id;
424 } 469 }
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
1183 } 1228 }
1184 1229
1185 TEST_F(ViewManagerTest, OnViewInput) { 1230 TEST_F(ViewManagerTest, OnViewInput) {
1186 ASSERT_TRUE(connection_->CreateView(BuildViewId(1, 1))); 1231 ASSERT_TRUE(connection_->CreateView(BuildViewId(1, 1)));
1187 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false)); 1232 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(false));
1188 1233
1189 // Dispatch an event to the view and verify its received. 1234 // Dispatch an event to the view and verify its received.
1190 { 1235 {
1191 EventPtr event(Event::New()); 1236 EventPtr event(Event::New());
1192 event->action = static_cast<EventType>(1); 1237 event->action = static_cast<EventType>(1);
1193 connection_->view_manager()->DispatchOnViewInputEvent(BuildViewId(1, 1), 1238 connection_->window_manager_client()->DispatchInputEventToView(
1194 event.Pass()); 1239 BuildViewId(1, 1), event.Pass());
1195 connection2_->DoRunLoopUntilChangesCount(1); 1240 connection2_->DoRunLoopUntilChangesCount(1);
1196 const Changes changes(ChangesToDescription1(connection2_->changes())); 1241 const Changes changes(ChangesToDescription1(connection2_->changes()));
1197 ASSERT_EQ(1u, changes.size()); 1242 ASSERT_EQ(1u, changes.size());
1198 EXPECT_EQ("InputEvent view=1,1 event_action=1", changes[0]); 1243 EXPECT_EQ("InputEvent view=1,1 event_action=1", changes[0]);
1199 } 1244 }
1200 } 1245 }
1201 1246
1202 TEST_F(ViewManagerTest, EmbedWithSameViewId) { 1247 TEST_F(ViewManagerTest, EmbedWithSameViewId) {
1203 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true)); 1248 ASSERT_NO_FATAL_FAILURE(EstablishSecondConnection(true));
1204 1249
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 1470
1426 // TODO(sky): add coverage of test that destroys connections and ensures other 1471 // TODO(sky): add coverage of test that destroys connections and ensures other
1427 // connections get deletion notification. 1472 // connections get deletion notification.
1428 1473
1429 // TODO(sky): need to better track changes to initial connection. For example, 1474 // 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 1475 // that SetBounsdViews/AddView and the like don't result in messages to the
1431 // originating connection. 1476 // originating connection.
1432 1477
1433 } // namespace service 1478 } // namespace service
1434 } // namespace mojo 1479 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/view_manager/view_manager_service_impl.cc ('k') | mojo/services/view_manager/window_manager_client_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698