| 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/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "mojo/public/cpp/application/interface_factory.h" | 11 #include "mojo/public/cpp/application/interface_factory.h" |
| 12 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 12 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 13 #include "mojo/service_manager/background_service_loader.h" | 13 #include "mojo/service_manager/background_shell_service_loader.h" |
| 14 #include "mojo/service_manager/service_loader.h" | 14 #include "mojo/service_manager/service_loader.h" |
| 15 #include "mojo/service_manager/service_manager.h" | 15 #include "mojo/service_manager/service_manager.h" |
| 16 #include "mojo/service_manager/test.mojom.h" | 16 #include "mojo/service_manager/test.mojom.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace mojo { | 19 namespace mojo { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kTestURLString[] = "test:testService"; | 22 const char kTestURLString[] = "test:testService"; |
| 23 const char kTestAURLString[] = "test:TestA"; | 23 const char kTestAURLString[] = "test:TestA"; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 base::MessageLoop* loop_; | 244 base::MessageLoop* loop_; |
| 245 }; | 245 }; |
| 246 | 246 |
| 247 // Used to test that the requestor url will be correctly passed. | 247 // Used to test that the requestor url will be correctly passed. |
| 248 class TestAImpl : public InterfaceImpl<TestA> { | 248 class TestAImpl : public InterfaceImpl<TestA> { |
| 249 public: | 249 public: |
| 250 TestAImpl(ApplicationConnection* connection, TesterContext* test_context) | 250 TestAImpl(ApplicationConnection* connection, TesterContext* test_context) |
| 251 : test_context_(test_context) { | 251 : test_context_(test_context) { |
| 252 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); | 252 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); |
| 253 } | 253 } |
| 254 virtual ~TestAImpl() { test_context_->IncrementNumADeletes(); } | 254 virtual ~TestAImpl() { |
| 255 test_context_->IncrementNumADeletes(); |
| 256 if (base::MessageLoop::current()->is_running()) |
| 257 Quit(); |
| 258 } |
| 255 | 259 |
| 256 private: | 260 private: |
| 257 virtual void CallB() OVERRIDE { | 261 virtual void CallB() OVERRIDE { |
| 258 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 262 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
| 259 } | 263 } |
| 260 | 264 |
| 261 virtual void CallCFromB() OVERRIDE { | 265 virtual void CallCFromB() OVERRIDE { |
| 262 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 266 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
| 263 } | 267 } |
| 264 | 268 |
| 265 void Quit() { | 269 void Quit() { |
| 270 base::MessageLoop::current()->Quit(); |
| 266 test_context_->set_a_called_quit(); | 271 test_context_->set_a_called_quit(); |
| 267 test_context_->QuitSoon(); | 272 test_context_->QuitSoon(); |
| 268 } | 273 } |
| 269 | 274 |
| 270 TesterContext* test_context_; | 275 TesterContext* test_context_; |
| 271 TestBPtr b_; | 276 TestBPtr b_; |
| 272 }; | 277 }; |
| 273 | 278 |
| 274 class TestBImpl : public InterfaceImpl<TestB> { | 279 class TestBImpl : public InterfaceImpl<TestB> { |
| 275 public: | 280 public: |
| 276 TestBImpl(ApplicationConnection* connection, TesterContext* test_context) | 281 TestBImpl(ApplicationConnection* connection, TesterContext* test_context) |
| 277 : test_context_(test_context) { | 282 : test_context_(test_context) { |
| 278 connection->ConnectToService(&c_); | 283 connection->ConnectToService(&c_); |
| 279 } | 284 } |
| 280 | 285 |
| 281 virtual ~TestBImpl() { | 286 virtual ~TestBImpl() { |
| 282 test_context_->IncrementNumBDeletes(); | 287 test_context_->IncrementNumBDeletes(); |
| 288 if (base::MessageLoop::current()->is_running()) |
| 289 base::MessageLoop::current()->Quit(); |
| 283 test_context_->QuitSoon(); | 290 test_context_->QuitSoon(); |
| 284 } | 291 } |
| 285 | 292 |
| 286 private: | 293 private: |
| 287 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE { | 294 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE { |
| 288 test_context_->IncrementNumBCalls(); | 295 test_context_->IncrementNumBCalls(); |
| 289 callback.Run(); | 296 callback.Run(); |
| 290 } | 297 } |
| 291 | 298 |
| 292 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE { | 299 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 341 |
| 335 virtual void OnServiceError(ServiceManager* manager, | 342 virtual void OnServiceError(ServiceManager* manager, |
| 336 const GURL& url) OVERRIDE {} | 343 const GURL& url) OVERRIDE {} |
| 337 | 344 |
| 338 virtual bool ConfigureIncomingConnection( | 345 virtual bool ConfigureIncomingConnection( |
| 339 ApplicationConnection* connection) OVERRIDE { | 346 ApplicationConnection* connection) OVERRIDE { |
| 340 if (!requestor_url_.empty() && | 347 if (!requestor_url_.empty() && |
| 341 requestor_url_ != connection->GetRemoteApplicationURL()) { | 348 requestor_url_ != connection->GetRemoteApplicationURL()) { |
| 342 context_->set_tester_called_quit(); | 349 context_->set_tester_called_quit(); |
| 343 context_->QuitSoon(); | 350 context_->QuitSoon(); |
| 351 base::MessageLoop::current()->Quit(); |
| 344 return false; | 352 return false; |
| 345 } | 353 } |
| 346 // If we're coming from A, then add B, otherwise A. | 354 // If we're coming from A, then add B, otherwise A. |
| 347 if (connection->GetRemoteApplicationURL() == kTestAURLString) | 355 if (connection->GetRemoteApplicationURL() == kTestAURLString) |
| 348 connection->AddService<TestB>(this); | 356 connection->AddService<TestB>(this); |
| 349 else | 357 else |
| 350 connection->AddService<TestA>(this); | 358 connection->AddService<TestA>(this); |
| 351 return true; | 359 return true; |
| 352 } | 360 } |
| 353 | 361 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 TestServicePtr service_proxy; | 432 TestServicePtr service_proxy; |
| 425 service_manager_->ConnectToService(GURL(kTestURLString), &service_proxy); | 433 service_manager_->ConnectToService(GURL(kTestURLString), &service_proxy); |
| 426 test_client_.reset(new TestClientImpl(service_proxy.Pass())); | 434 test_client_.reset(new TestClientImpl(service_proxy.Pass())); |
| 427 } | 435 } |
| 428 | 436 |
| 429 virtual void TearDown() OVERRIDE { | 437 virtual void TearDown() OVERRIDE { |
| 430 test_client_.reset(NULL); | 438 test_client_.reset(NULL); |
| 431 service_manager_.reset(NULL); | 439 service_manager_.reset(NULL); |
| 432 } | 440 } |
| 433 | 441 |
| 434 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { | 442 scoped_ptr<BackgroundShellServiceLoader> MakeLoader( |
| 443 const std::string& requestor_url) { |
| 435 scoped_ptr<ServiceLoader> real_loader( | 444 scoped_ptr<ServiceLoader> real_loader( |
| 436 new Tester(&tester_context_, requestor_url)); | 445 new Tester(&tester_context_, requestor_url)); |
| 446 scoped_ptr<BackgroundShellServiceLoader> loader( |
| 447 new BackgroundShellServiceLoader(real_loader.Pass(), std::string(), |
| 448 base::MessageLoop::TYPE_DEFAULT)); |
| 449 return loader.Pass(); |
| 450 } |
| 451 |
| 452 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { |
| 437 service_manager_->SetLoaderForURL( | 453 service_manager_->SetLoaderForURL( |
| 438 scoped_ptr<ServiceLoader>(new BackgroundServiceLoader( | 454 MakeLoader(requestor_url).PassAs<ServiceLoader>(), url); |
| 439 real_loader.Pass(), "", base::MessageLoop::TYPE_DEFAULT)), | |
| 440 url); | |
| 441 } | 455 } |
| 442 | 456 |
| 443 bool HasFactoryForTestURL() { | 457 bool HasFactoryForTestURL() { |
| 444 ServiceManager::TestAPI manager_test_api(service_manager_.get()); | 458 ServiceManager::TestAPI manager_test_api(service_manager_.get()); |
| 445 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); | 459 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); |
| 446 } | 460 } |
| 447 | 461 |
| 448 protected: | 462 protected: |
| 449 base::ShadowingAtExitManager at_exit_; | 463 base::ShadowingAtExitManager at_exit_; |
| 450 TesterContext tester_context_; | 464 TesterContext tester_context_; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 AddLoaderForURL(GURL(kTestAURLString), std::string()); | 605 AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 592 | 606 |
| 593 // Only c can load b, so this will fail. | 607 // Only c can load b, so this will fail. |
| 594 AddLoaderForURL(GURL(kTestBURLString), "test:TestC"); | 608 AddLoaderForURL(GURL(kTestBURLString), "test:TestC"); |
| 595 | 609 |
| 596 TestAPtr a; | 610 TestAPtr a; |
| 597 service_manager_->ConnectToService(GURL(kTestAURLString), &a); | 611 service_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 598 a->CallB(); | 612 a->CallB(); |
| 599 loop_.Run(); | 613 loop_.Run(); |
| 600 EXPECT_EQ(0, tester_context_.num_b_calls()); | 614 EXPECT_EQ(0, tester_context_.num_b_calls()); |
| 615 |
| 616 EXPECT_FALSE(tester_context_.a_called_quit()); |
| 601 EXPECT_TRUE(tester_context_.tester_called_quit()); | 617 EXPECT_TRUE(tester_context_.tester_called_quit()); |
| 602 } | 618 } |
| 603 | 619 |
| 604 TEST_F(ServiceManagerTest, NoServiceNoLoad) { | 620 TEST_F(ServiceManagerTest, NoServiceNoLoad) { |
| 605 AddLoaderForURL(GURL(kTestAURLString), std::string()); | 621 // Because we'll never successfully connect to anything here and apps are not |
| 622 // capable of noticing zero incoming connections and quitting, we need to use |
| 623 // a quittable loader. |
| 624 scoped_ptr<BackgroundShellServiceLoader> loader(MakeLoader(std::string())); |
| 625 loader->set_quit_on_shutdown(); |
| 626 service_manager_->SetLoaderForURL(loader.PassAs<ServiceLoader>(), |
| 627 GURL(kTestAURLString)); |
| 628 |
| 606 | 629 |
| 607 // There is no TestC service implementation registered with ServiceManager, | 630 // There is no TestC service implementation registered with ServiceManager, |
| 608 // so this cannot succeed (but also shouldn't crash). | 631 // so this cannot succeed (but also shouldn't crash). |
| 609 TestCPtr c; | 632 TestCPtr c; |
| 610 service_manager_->ConnectToService(GURL(kTestAURLString), &c); | 633 service_manager_->ConnectToService(GURL(kTestAURLString), &c); |
| 611 QuitMessageLoopErrorHandler quitter; | 634 QuitMessageLoopErrorHandler quitter; |
| 612 c.set_error_handler(&quitter); | 635 c.set_error_handler(&quitter); |
| 613 | 636 |
| 614 loop_.Run(); | 637 loop_.Run(); |
| 615 EXPECT_TRUE(c.encountered_error()); | 638 EXPECT_TRUE(c.encountered_error()); |
| 616 } | 639 } |
| 617 | 640 |
| 618 TEST_F(ServiceManagerTest, Interceptor) { | 641 TEST_F(ServiceManagerTest, Interceptor) { |
| 619 TestServiceInterceptor interceptor; | 642 TestServiceInterceptor interceptor; |
| 620 TestServiceLoader* default_loader = new TestServiceLoader; | 643 TestServiceLoader* default_loader = new TestServiceLoader; |
| 621 service_manager_->set_default_loader( | 644 service_manager_->set_default_loader( |
| 622 scoped_ptr<ServiceLoader>(default_loader)); | 645 scoped_ptr<ServiceLoader>(default_loader)); |
| 623 service_manager_->SetInterceptor(&interceptor); | 646 service_manager_->SetInterceptor(&interceptor); |
| 624 | 647 |
| 625 std::string url("test:test3"); | 648 std::string url("test:test3"); |
| 626 TestServicePtr test_service; | 649 TestServicePtr test_service; |
| 627 service_manager_->ConnectToService(GURL(url), &test_service); | 650 service_manager_->ConnectToService(GURL(url), &test_service); |
| 628 | 651 |
| 629 EXPECT_EQ(1, interceptor.call_count()); | 652 EXPECT_EQ(1, interceptor.call_count()); |
| 630 EXPECT_EQ(url, interceptor.url_spec()); | 653 EXPECT_EQ(url, interceptor.url_spec()); |
| 631 EXPECT_EQ(1, default_loader->num_loads()); | 654 EXPECT_EQ(1, default_loader->num_loads()); |
| 632 } | 655 } |
| 633 | 656 |
| 634 } // namespace mojo | 657 } // namespace mojo |
| OLD | NEW |