| 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/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "mojo/public/cpp/application/application.h" | |
| 7 #include "mojo/public/cpp/application/application.h" | |
| 8 #include "mojo/public/cpp/bindings/allocation_scope.h" | 6 #include "mojo/public/cpp/bindings/allocation_scope.h" |
| 9 #include "mojo/public/cpp/environment/environment.h" | 7 #include "mojo/public/cpp/environment/environment.h" |
| 10 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 8 #include "mojo/public/cpp/shell/application.h" |
| 9 #include "mojo/public/interfaces/shell/shell.mojom.h" |
| 11 #include "mojo/service_manager/service_loader.h" | 10 #include "mojo/service_manager/service_loader.h" |
| 12 #include "mojo/service_manager/service_manager.h" | 11 #include "mojo/service_manager/service_manager.h" |
| 13 #include "mojo/service_manager/test.mojom.h" | 12 #include "mojo/service_manager/test.mojom.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace mojo { | 15 namespace mojo { |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 const char kTestURLString[] = "test:testService"; | 18 const char kTestURLString[] = "test:testService"; |
| 20 | 19 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 86 } |
| 88 | 87 |
| 89 void set_context(TestContext* context) { context_ = context; } | 88 void set_context(TestContext* context) { context_ = context; } |
| 90 void set_quit_after_error(bool quit_after_error) { | 89 void set_quit_after_error(bool quit_after_error) { |
| 91 quit_after_error_ = quit_after_error; | 90 quit_after_error_ = quit_after_error; |
| 92 } | 91 } |
| 93 | 92 |
| 94 int num_loads() const { return num_loads_; } | 93 int num_loads() const { return num_loads_; } |
| 95 | 94 |
| 96 private: | 95 private: |
| 97 virtual void LoadService( | 96 virtual void LoadService(ServiceManager* manager, |
| 98 ServiceManager* manager, | 97 const GURL& url, |
| 99 const GURL& url, | 98 ScopedMessagePipeHandle shell_handle) OVERRIDE { |
| 100 ScopedMessagePipeHandle service_provider_handle) OVERRIDE { | |
| 101 ++num_loads_; | 99 ++num_loads_; |
| 102 test_app_.reset(new Application(service_provider_handle.Pass())); | 100 test_app_.reset(new Application(shell_handle.Pass())); |
| 103 test_app_->AddService<TestServiceImpl>(context_); | 101 test_app_->AddService<TestServiceImpl>(context_); |
| 104 } | 102 } |
| 105 | 103 |
| 106 virtual void OnServiceError(ServiceManager* manager, | 104 virtual void OnServiceError(ServiceManager* manager, |
| 107 const GURL& url) OVERRIDE { | 105 const GURL& url) OVERRIDE { |
| 108 if (quit_after_error_) { | 106 if (quit_after_error_) { |
| 109 base::MessageLoop::current()->PostTask(FROM_HERE, | 107 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 110 base::MessageLoop::QuitClosure()); | 108 base::MessageLoop::QuitClosure()); |
| 111 } | 109 } |
| 112 } | 110 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 MessagePipe pipe; | 159 MessagePipe pipe; |
| 162 TestServicePtr service_proxy = MakeProxy<TestService>(pipe.handle0.Pass()); | 160 TestServicePtr service_proxy = MakeProxy<TestService>(pipe.handle0.Pass()); |
| 163 test_client_.reset(new TestClientImpl(service_proxy.Pass())); | 161 test_client_.reset(new TestClientImpl(service_proxy.Pass())); |
| 164 | 162 |
| 165 TestServiceLoader* default_loader = new TestServiceLoader; | 163 TestServiceLoader* default_loader = new TestServiceLoader; |
| 166 default_loader->set_context(&context_); | 164 default_loader->set_context(&context_); |
| 167 default_loader->set_quit_after_error(true); | 165 default_loader->set_quit_after_error(true); |
| 168 service_manager_->set_default_loader( | 166 service_manager_->set_default_loader( |
| 169 scoped_ptr<ServiceLoader>(default_loader)); | 167 scoped_ptr<ServiceLoader>(default_loader)); |
| 170 | 168 |
| 171 service_manager_->ConnectToService(test_url, pipe.handle1.Pass()); | 169 service_manager_->Connect(test_url, pipe.handle1.Pass()); |
| 172 } | 170 } |
| 173 | 171 |
| 174 virtual void TearDown() OVERRIDE { | 172 virtual void TearDown() OVERRIDE { |
| 175 test_client_.reset(NULL); | 173 test_client_.reset(NULL); |
| 176 service_manager_.reset(NULL); | 174 service_manager_.reset(NULL); |
| 177 } | 175 } |
| 178 | 176 |
| 179 bool HasFactoryForTestURL() { | 177 bool HasFactoryForTestURL() { |
| 180 ServiceManager::TestAPI manager_test_api(service_manager_.get()); | 178 ServiceManager::TestAPI manager_test_api(service_manager_.get()); |
| 181 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); | 179 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 ServiceManager sm; | 234 ServiceManager sm; |
| 237 TestServiceLoader* default_loader = new TestServiceLoader; | 235 TestServiceLoader* default_loader = new TestServiceLoader; |
| 238 TestServiceLoader* url_loader = new TestServiceLoader; | 236 TestServiceLoader* url_loader = new TestServiceLoader; |
| 239 TestServiceLoader* scheme_loader = new TestServiceLoader; | 237 TestServiceLoader* scheme_loader = new TestServiceLoader; |
| 240 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); | 238 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); |
| 241 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1")); | 239 sm.SetLoaderForURL(scoped_ptr<ServiceLoader>(url_loader), GURL("test:test1")); |
| 242 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test"); | 240 sm.SetLoaderForScheme(scoped_ptr<ServiceLoader>(scheme_loader), "test"); |
| 243 | 241 |
| 244 // test::test1 should go to url_loader. | 242 // test::test1 should go to url_loader. |
| 245 MessagePipe pipe1; | 243 MessagePipe pipe1; |
| 246 sm.ConnectToService(GURL("test:test1"), pipe1.handle0.Pass()); | 244 sm.Connect(GURL("test:test1"), pipe1.handle0.Pass()); |
| 247 EXPECT_EQ(1, url_loader->num_loads()); | 245 EXPECT_EQ(1, url_loader->num_loads()); |
| 248 EXPECT_EQ(0, scheme_loader->num_loads()); | 246 EXPECT_EQ(0, scheme_loader->num_loads()); |
| 249 EXPECT_EQ(0, default_loader->num_loads()); | 247 EXPECT_EQ(0, default_loader->num_loads()); |
| 250 | 248 |
| 251 // test::test2 should go to scheme loader. | 249 // test::test2 should go to scheme loader. |
| 252 MessagePipe pipe2; | 250 MessagePipe pipe2; |
| 253 sm.ConnectToService(GURL("test:test2"), pipe2.handle0.Pass()); | 251 sm.Connect(GURL("test:test2"), pipe2.handle0.Pass()); |
| 254 EXPECT_EQ(1, url_loader->num_loads()); | 252 EXPECT_EQ(1, url_loader->num_loads()); |
| 255 EXPECT_EQ(1, scheme_loader->num_loads()); | 253 EXPECT_EQ(1, scheme_loader->num_loads()); |
| 256 EXPECT_EQ(0, default_loader->num_loads()); | 254 EXPECT_EQ(0, default_loader->num_loads()); |
| 257 | 255 |
| 258 // http::test1 should go to default loader. | 256 // http::test1 should go to default loader. |
| 259 MessagePipe pipe3; | 257 MessagePipe pipe3; |
| 260 sm.ConnectToService(GURL("http:test1"), pipe3.handle0.Pass()); | 258 sm.Connect(GURL("http:test1"), pipe3.handle0.Pass()); |
| 261 EXPECT_EQ(1, url_loader->num_loads()); | 259 EXPECT_EQ(1, url_loader->num_loads()); |
| 262 EXPECT_EQ(1, scheme_loader->num_loads()); | 260 EXPECT_EQ(1, scheme_loader->num_loads()); |
| 263 EXPECT_EQ(1, default_loader->num_loads()); | 261 EXPECT_EQ(1, default_loader->num_loads()); |
| 264 } | 262 } |
| 265 | 263 |
| 266 TEST_F(ServiceManagerTest, Interceptor) { | 264 TEST_F(ServiceManagerTest, Interceptor) { |
| 267 ServiceManager sm; | 265 ServiceManager sm; |
| 268 TestServiceInterceptor interceptor; | 266 TestServiceInterceptor interceptor; |
| 269 TestServiceLoader* default_loader = new TestServiceLoader; | 267 TestServiceLoader* default_loader = new TestServiceLoader; |
| 270 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); | 268 sm.set_default_loader(scoped_ptr<ServiceLoader>(default_loader)); |
| 271 sm.SetInterceptor(&interceptor); | 269 sm.SetInterceptor(&interceptor); |
| 272 | 270 |
| 273 std::string url("test:test3"); | 271 std::string url("test:test3"); |
| 274 MessagePipe pipe1; | 272 MessagePipe pipe1; |
| 275 sm.ConnectToService(GURL(url), pipe1.handle0.Pass()); | 273 sm.Connect(GURL(url), pipe1.handle0.Pass()); |
| 276 EXPECT_EQ(1, interceptor.call_count()); | 274 EXPECT_EQ(1, interceptor.call_count()); |
| 277 EXPECT_EQ(url, interceptor.url_spec()); | 275 EXPECT_EQ(url, interceptor.url_spec()); |
| 278 EXPECT_EQ(1, default_loader->num_loads()); | 276 EXPECT_EQ(1, default_loader->num_loads()); |
| 279 } | 277 } |
| 280 | 278 |
| 281 } // namespace mojo | 279 } // namespace mojo |
| OLD | NEW |