| OLD | NEW |
| 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 "base/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "mojo/public/cpp/application/application.h" | 7 #include "mojo/public/cpp/application/application.h" |
| 8 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 8 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
| 9 #include "mojo/service_manager/service_loader.h" | 9 #include "mojo/service_manager/service_loader.h" |
| 10 #include "mojo/service_manager/service_manager.h" | 10 #include "mojo/service_manager/service_manager.h" |
| 11 #include "mojo/service_manager/test.mojom.h" | 11 #include "mojo/service_manager/test.mojom.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace mojo { | 14 namespace mojo { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kTestURLString[] = "test:testService"; | 17 const char kTestURLString[] = "test:testService"; |
| 18 const char kTestAURLString[] = "test:TestA"; | 18 const char kTestAURLString[] = "test:TestA"; |
| 19 const char kTestBURLString[] = "test:TestB"; | 19 const char kTestBURLString[] = "test:TestB"; |
| 20 | 20 |
| 21 struct TestContext { | 21 struct TestContext { |
| 22 TestContext() : num_impls(0), num_loader_deletes(0) {} | 22 TestContext() : num_impls(0), num_loader_deletes(0) {} |
| 23 std::string last_test_string; | 23 std::string last_test_string; |
| 24 int num_impls; | 24 int num_impls; |
| 25 int num_loader_deletes; | 25 int num_loader_deletes; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 class QuitMessageLoopErrorHandler : public ErrorHandler { |
| 29 public: |
| 30 QuitMessageLoopErrorHandler() {} |
| 31 virtual ~QuitMessageLoopErrorHandler() {} |
| 32 |
| 33 // |ErrorHandler| implementation: |
| 34 virtual void OnConnectionError() OVERRIDE { |
| 35 base::MessageLoop::current()->QuitWhenIdle(); |
| 36 } |
| 37 |
| 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); |
| 40 }; |
| 41 |
| 28 class TestServiceImpl : public InterfaceImpl<TestService> { | 42 class TestServiceImpl : public InterfaceImpl<TestService> { |
| 29 public: | 43 public: |
| 30 explicit TestServiceImpl(TestContext* context) : context_(context) { | 44 explicit TestServiceImpl(TestContext* context) : context_(context) { |
| 31 ++context_->num_impls; | 45 ++context_->num_impls; |
| 32 } | 46 } |
| 33 | 47 |
| 34 virtual ~TestServiceImpl() { | 48 virtual ~TestServiceImpl() { |
| 35 --context_->num_impls; | 49 --context_->num_impls; |
| 36 } | 50 } |
| 37 | 51 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 121 |
| 108 scoped_ptr<Application> test_app_; | 122 scoped_ptr<Application> test_app_; |
| 109 TestContext* context_; | 123 TestContext* context_; |
| 110 int num_loads_; | 124 int num_loads_; |
| 111 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader); | 125 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader); |
| 112 }; | 126 }; |
| 113 | 127 |
| 114 // Used to test that the requestor url will be correctly passed. | 128 // Used to test that the requestor url will be correctly passed. |
| 115 class TestAImpl : public InterfaceImpl<TestA> { | 129 class TestAImpl : public InterfaceImpl<TestA> { |
| 116 public: | 130 public: |
| 117 TestAImpl(Application* app) : app_(app) {} | 131 explicit TestAImpl(Application* app) : app_(app) {} |
| 118 | 132 |
| 119 virtual void LoadB() OVERRIDE { | 133 virtual void LoadB() OVERRIDE { |
| 120 TestBPtr b; | 134 TestBPtr b; |
| 121 app_->ConnectTo(kTestBURLString, &b); | 135 app_->ConnectTo(kTestBURLString, &b); |
| 122 b->Test(); | 136 b->Test(); |
| 123 } | 137 } |
| 124 | 138 |
| 125 private: | 139 private: |
| 126 Application* app_; | 140 Application* app_; |
| 127 }; | 141 }; |
| 128 | 142 |
| 129 class TestBImpl : public InterfaceImpl<TestB> { | 143 class TestBImpl : public InterfaceImpl<TestB> { |
| 130 public: | 144 public: |
| 131 virtual void Test() OVERRIDE { | 145 virtual void Test() OVERRIDE { |
| 132 base::MessageLoop::current()->Quit(); | 146 base::MessageLoop::current()->Quit(); |
| 133 } | 147 } |
| 134 }; | 148 }; |
| 135 | 149 |
| 136 class TestApp : public Application, public ServiceLoader { | 150 class TestApp : public Application, public ServiceLoader { |
| 137 public: | 151 public: |
| 138 TestApp(std::string requestor_url) | 152 explicit TestApp(std::string requestor_url) |
| 139 : requestor_url_(requestor_url), | 153 : requestor_url_(requestor_url), |
| 140 num_connects_(0) { | 154 num_connects_(0) { |
| 141 } | 155 } |
| 142 | 156 |
| 143 int num_connects() const { return num_connects_; } | 157 int num_connects() const { return num_connects_; } |
| 144 | 158 |
| 145 private: | 159 private: |
| 146 virtual void LoadService( | 160 virtual void LoadService( |
| 147 ServiceManager* manager, | 161 ServiceManager* manager, |
| 148 const GURL& url, | 162 const GURL& url, |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 b_app->AddService<TestBImpl>(); | 361 b_app->AddService<TestBImpl>(); |
| 348 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(b_app), GURL(kTestBURLString)); | 362 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(b_app), GURL(kTestBURLString)); |
| 349 | 363 |
| 350 TestAPtr a; | 364 TestAPtr a; |
| 351 sm.ConnectTo(GURL(kTestAURLString), &a, GURL()); | 365 sm.ConnectTo(GURL(kTestAURLString), &a, GURL()); |
| 352 a->LoadB(); | 366 a->LoadB(); |
| 353 loop_.Run(); | 367 loop_.Run(); |
| 354 EXPECT_EQ(0, b_app->num_connects()); | 368 EXPECT_EQ(0, b_app->num_connects()); |
| 355 } | 369 } |
| 356 | 370 |
| 371 TEST_F(ServiceManagerTest, NoServiceNoLoad) { |
| 372 ServiceManager sm; |
| 373 |
| 374 TestApp* b_app = new TestApp(std::string()); |
| 375 b_app->AddService<TestBImpl>(); |
| 376 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(b_app), GURL(kTestBURLString)); |
| 377 |
| 378 // There is no TestA service implementation registered with ServiceManager, |
| 379 // so this cannot succeed (but also shouldn't crash). |
| 380 TestAPtr a; |
| 381 sm.ConnectTo(GURL(kTestBURLString), &a, GURL()); |
| 382 QuitMessageLoopErrorHandler quitter; |
| 383 a.set_error_handler(&quitter); |
| 384 a->LoadB(); |
| 385 |
| 386 loop_.Run(); |
| 387 EXPECT_TRUE(a.encountered_error()); |
| 388 EXPECT_EQ(0, b_app->num_connects()); |
| 389 } |
| 390 |
| 357 TEST_F(ServiceManagerTest, Interceptor) { | 391 TEST_F(ServiceManagerTest, Interceptor) { |
| 358 ServiceManager sm; | 392 ServiceManager sm; |
| 359 TestServiceInterceptor interceptor; | 393 TestServiceInterceptor interceptor; |
| 360 TestServiceLoader* default_loader = new TestServiceLoader; | 394 TestServiceLoader* default_loader = new TestServiceLoader; |
| 361 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); | 395 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); |
| 362 sm.SetInterceptor(&interceptor); | 396 sm.SetInterceptor(&interceptor); |
| 363 | 397 |
| 364 std::string url("test:test3"); | 398 std::string url("test:test3"); |
| 365 TestServicePtr test_service; | 399 TestServicePtr test_service; |
| 366 sm.ConnectTo(GURL(url), &test_service, GURL()); | 400 sm.ConnectTo(GURL(url), &test_service, GURL()); |
| 367 EXPECT_EQ(1, interceptor.call_count()); | 401 EXPECT_EQ(1, interceptor.call_count()); |
| 368 EXPECT_EQ(url, interceptor.url_spec()); | 402 EXPECT_EQ(url, interceptor.url_spec()); |
| 369 EXPECT_EQ(1, default_loader->num_loads()); | 403 EXPECT_EQ(1, default_loader->num_loads()); |
| 370 } | 404 } |
| 371 | 405 |
| 372 } // namespace mojo | 406 } // namespace mojo |
| OLD | NEW |