| 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/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 ~TestServiceImpl() override { --context_->num_impls; } | 54 ~TestServiceImpl() override { --context_->num_impls; } |
| 55 | 55 |
| 56 void OnConnectionError() override { | 56 void OnConnectionError() override { |
| 57 if (!base::MessageLoop::current()->is_running()) | 57 if (!base::MessageLoop::current()->is_running()) |
| 58 return; | 58 return; |
| 59 base::MessageLoop::current()->Quit(); | 59 base::MessageLoop::current()->Quit(); |
| 60 InterfaceImpl::OnConnectionError(); | 60 InterfaceImpl::OnConnectionError(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // TestService implementation: | 63 // TestService implementation: |
| 64 void Test(const String& test_string) override { | 64 void Test(const String& test_string, |
| 65 const mojo::Callback<void()>& callback) override { |
| 65 context_->last_test_string = test_string; | 66 context_->last_test_string = test_string; |
| 66 client()->AckTest(); | 67 callback.Run(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 TestContext* context_; | 71 TestContext* context_; |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 class TestClientImpl : public TestClient { | 74 class TestClient { |
| 74 public: | 75 public: |
| 75 explicit TestClientImpl(TestServicePtr service) | 76 explicit TestClient(TestServicePtr service) |
| 76 : service_(service.Pass()), quit_after_ack_(false) { | 77 : service_(service.Pass()), quit_after_ack_(false) {} |
| 77 service_.set_client(this); | |
| 78 } | |
| 79 | 78 |
| 80 ~TestClientImpl() override { service_.reset(); } | 79 ~TestClient() { service_.reset(); } |
| 81 | 80 |
| 82 void AckTest() override { | 81 void AckTest() { |
| 83 if (quit_after_ack_) | 82 if (quit_after_ack_) |
| 84 base::MessageLoop::current()->Quit(); | 83 base::MessageLoop::current()->Quit(); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void Test(const std::string& test_string) { | 86 void Test(const std::string& test_string) { |
| 88 quit_after_ack_ = true; | 87 quit_after_ack_ = true; |
| 89 service_->Test(test_string); | 88 service_->Test(test_string, |
| 89 base::Bind(&TestClient::AckTest, base::Unretained(this))); |
| 90 } | 90 } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 TestServicePtr service_; | 93 TestServicePtr service_; |
| 94 bool quit_after_ack_; | 94 bool quit_after_ack_; |
| 95 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); | 95 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 class TestApplicationLoader : public ApplicationLoader, | 98 class TestApplicationLoader : public ApplicationLoader, |
| 99 public ApplicationDelegate, | 99 public ApplicationDelegate, |
| 100 public InterfaceFactory<TestService> { | 100 public InterfaceFactory<TestService> { |
| 101 public: | 101 public: |
| 102 TestApplicationLoader() : context_(NULL), num_loads_(0) {} | 102 TestApplicationLoader() : context_(NULL), num_loads_(0) {} |
| 103 | 103 |
| 104 ~TestApplicationLoader() override { | 104 ~TestApplicationLoader() override { |
| 105 if (context_) | 105 if (context_) |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 void SetUp() override { | 414 void SetUp() override { |
| 415 application_manager_.reset(new ApplicationManager(&test_delegate_)); | 415 application_manager_.reset(new ApplicationManager(&test_delegate_)); |
| 416 test_loader_ = new TestApplicationLoader; | 416 test_loader_ = new TestApplicationLoader; |
| 417 test_loader_->set_context(&context_); | 417 test_loader_->set_context(&context_); |
| 418 application_manager_->set_default_loader( | 418 application_manager_->set_default_loader( |
| 419 scoped_ptr<ApplicationLoader>(test_loader_)); | 419 scoped_ptr<ApplicationLoader>(test_loader_)); |
| 420 | 420 |
| 421 TestServicePtr service_proxy; | 421 TestServicePtr service_proxy; |
| 422 application_manager_->ConnectToService(GURL(kTestURLString), | 422 application_manager_->ConnectToService(GURL(kTestURLString), |
| 423 &service_proxy); | 423 &service_proxy); |
| 424 test_client_.reset(new TestClientImpl(service_proxy.Pass())); | 424 test_client_.reset(new TestClient(service_proxy.Pass())); |
| 425 } | 425 } |
| 426 | 426 |
| 427 void TearDown() override { | 427 void TearDown() override { |
| 428 test_client_.reset(NULL); | 428 test_client_.reset(NULL); |
| 429 application_manager_.reset(NULL); | 429 application_manager_.reset(NULL); |
| 430 } | 430 } |
| 431 | 431 |
| 432 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { | 432 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { |
| 433 application_manager_->SetLoaderForURL( | 433 application_manager_->SetLoaderForURL( |
| 434 make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url); | 434 make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url); |
| 435 } | 435 } |
| 436 | 436 |
| 437 bool HasFactoryForTestURL() { | 437 bool HasFactoryForTestURL() { |
| 438 ApplicationManager::TestAPI manager_test_api(application_manager_.get()); | 438 ApplicationManager::TestAPI manager_test_api(application_manager_.get()); |
| 439 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); | 439 return manager_test_api.HasFactoryForURL(GURL(kTestURLString)); |
| 440 } | 440 } |
| 441 | 441 |
| 442 protected: | 442 protected: |
| 443 base::ShadowingAtExitManager at_exit_; | 443 base::ShadowingAtExitManager at_exit_; |
| 444 TestDelegate test_delegate_; | 444 TestDelegate test_delegate_; |
| 445 TestApplicationLoader* test_loader_; | 445 TestApplicationLoader* test_loader_; |
| 446 TesterContext tester_context_; | 446 TesterContext tester_context_; |
| 447 TestContext context_; | 447 TestContext context_; |
| 448 base::MessageLoop loop_; | 448 base::MessageLoop loop_; |
| 449 scoped_ptr<TestClientImpl> test_client_; | 449 scoped_ptr<TestClient> test_client_; |
| 450 scoped_ptr<ApplicationManager> application_manager_; | 450 scoped_ptr<ApplicationManager> application_manager_; |
| 451 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest); | 451 DISALLOW_COPY_AND_ASSIGN(ApplicationManagerTest); |
| 452 }; | 452 }; |
| 453 | 453 |
| 454 TEST_F(ApplicationManagerTest, Basic) { | 454 TEST_F(ApplicationManagerTest, Basic) { |
| 455 test_client_->Test("test"); | 455 test_client_->Test("test"); |
| 456 loop_.Run(); | 456 loop_.Run(); |
| 457 EXPECT_EQ(std::string("test"), context_.last_test_string); | 457 EXPECT_EQ(std::string("test"), context_.last_test_string); |
| 458 } | 458 } |
| 459 | 459 |
| 460 // Confirm that no arguments are sent to an application by default. | 460 // Confirm that no arguments are sent to an application by default. |
| 461 TEST_F(ApplicationManagerTest, NoArgs) { | 461 TEST_F(ApplicationManagerTest, NoArgs) { |
| 462 ApplicationManager am(&test_delegate_); | 462 ApplicationManager am(&test_delegate_); |
| 463 GURL test_url("test:test"); | 463 GURL test_url("test:test"); |
| 464 TestApplicationLoader* loader = new TestApplicationLoader; | 464 TestApplicationLoader* loader = new TestApplicationLoader; |
| 465 loader->set_context(&context_); | 465 loader->set_context(&context_); |
| 466 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | 466 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 467 TestServicePtr test_service; | 467 TestServicePtr test_service; |
| 468 am.ConnectToService(test_url, &test_service); | 468 am.ConnectToService(test_url, &test_service); |
| 469 TestClientImpl test_client(test_service.Pass()); | 469 TestClient test_client(test_service.Pass()); |
| 470 test_client.Test("test"); | 470 test_client.Test("test"); |
| 471 loop_.Run(); | 471 loop_.Run(); |
| 472 std::vector<std::string> app_args = loader->GetArgs(); | 472 std::vector<std::string> app_args = loader->GetArgs(); |
| 473 EXPECT_EQ(0U, app_args.size()); | 473 EXPECT_EQ(0U, app_args.size()); |
| 474 } | 474 } |
| 475 | 475 |
| 476 // Confirm that arguments are sent to an application. | 476 // Confirm that arguments are sent to an application. |
| 477 TEST_F(ApplicationManagerTest, Args) { | 477 TEST_F(ApplicationManagerTest, Args) { |
| 478 ApplicationManager am(&test_delegate_); | 478 ApplicationManager am(&test_delegate_); |
| 479 GURL test_url("test:test"); | 479 GURL test_url("test:test"); |
| 480 std::vector<std::string> args; | 480 std::vector<std::string> args; |
| 481 args.push_back("test_arg1"); | 481 args.push_back("test_arg1"); |
| 482 args.push_back("test_arg2"); | 482 args.push_back("test_arg2"); |
| 483 am.SetArgsForURL(args, test_url); | 483 am.SetArgsForURL(args, test_url); |
| 484 TestApplicationLoader* loader = new TestApplicationLoader; | 484 TestApplicationLoader* loader = new TestApplicationLoader; |
| 485 loader->set_context(&context_); | 485 loader->set_context(&context_); |
| 486 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | 486 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
| 487 TestServicePtr test_service; | 487 TestServicePtr test_service; |
| 488 am.ConnectToService(test_url, &test_service); | 488 am.ConnectToService(test_url, &test_service); |
| 489 TestClientImpl test_client(test_service.Pass()); | 489 TestClient test_client(test_service.Pass()); |
| 490 test_client.Test("test"); | 490 test_client.Test("test"); |
| 491 loop_.Run(); | 491 loop_.Run(); |
| 492 std::vector<std::string> app_args = loader->GetArgs(); | 492 std::vector<std::string> app_args = loader->GetArgs(); |
| 493 ASSERT_EQ(args.size(), app_args.size()); | 493 ASSERT_EQ(args.size(), app_args.size()); |
| 494 EXPECT_EQ(args[0], app_args[0]); | 494 EXPECT_EQ(args[0], app_args[0]); |
| 495 EXPECT_EQ(args[1], app_args[1]); | 495 EXPECT_EQ(args[1], app_args[1]); |
| 496 } | 496 } |
| 497 | 497 |
| 498 TEST_F(ApplicationManagerTest, ClientError) { | 498 TEST_F(ApplicationManagerTest, ClientError) { |
| 499 test_client_->Test("test"); | 499 test_client_->Test("test"); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 TestServicePtr test_service2; | 664 TestServicePtr test_service2; |
| 665 application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2); | 665 application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2); |
| 666 EXPECT_EQ(2, test_loader_->num_loads()); | 666 EXPECT_EQ(2, test_loader_->num_loads()); |
| 667 | 667 |
| 668 TestServicePtr test_service3; | 668 TestServicePtr test_service3; |
| 669 application_manager_->ConnectToService(GURL("bar:bar"), &test_service2); | 669 application_manager_->ConnectToService(GURL("bar:bar"), &test_service2); |
| 670 EXPECT_EQ(3, test_loader_->num_loads()); | 670 EXPECT_EQ(3, test_loader_->num_loads()); |
| 671 } | 671 } |
| 672 | 672 |
| 673 } // namespace mojo | 673 } // namespace mojo |
| OLD | NEW |