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 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 replacements.SetScheme("file", url::Component(0, 4)); | 412 replacements.SetScheme("file", url::Component(0, 4)); |
413 mapped_url = mapped_url.ReplaceComponents(replacements); | 413 mapped_url = mapped_url.ReplaceComponents(replacements); |
414 } | 414 } |
415 return mapped_url; | 415 return mapped_url; |
416 } | 416 } |
417 | 417 |
418 private: | 418 private: |
419 std::map<GURL, GURL> mappings_; | 419 std::map<GURL, GURL> mappings_; |
420 }; | 420 }; |
421 | 421 |
422 class TestExternal : public ApplicationDelegate { | |
423 public: | |
424 TestExternal() : configure_incoming_connection_called_(false) {} | |
425 | |
426 void Initialize(ApplicationImpl* app) override { | |
427 initialize_args_ = app->args(); | |
428 base::MessageLoop::current()->Quit(); | |
429 } | |
430 | |
431 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
432 configure_incoming_connection_called_ = true; | |
433 base::MessageLoop::current()->Quit(); | |
434 return true; | |
435 } | |
436 | |
437 const std::vector<std::string>& initialize_args() const { | |
438 return initialize_args_; | |
439 } | |
440 | |
441 bool configure_incoming_connection_called() const { | |
442 return configure_incoming_connection_called_; | |
443 } | |
444 | |
445 private: | |
446 std::vector<std::string> initialize_args_; | |
447 bool configure_incoming_connection_called_; | |
448 }; | |
449 | |
450 class ApplicationManagerTest : public testing::Test { | 422 class ApplicationManagerTest : public testing::Test { |
451 public: | 423 public: |
452 ApplicationManagerTest() : tester_context_(&loop_) {} | 424 ApplicationManagerTest() : tester_context_(&loop_) {} |
453 | 425 |
454 ~ApplicationManagerTest() override {} | 426 ~ApplicationManagerTest() override {} |
455 | 427 |
456 void SetUp() override { | 428 void SetUp() override { |
457 application_manager_.reset(new ApplicationManager(&test_delegate_)); | 429 application_manager_.reset(new ApplicationManager(&test_delegate_)); |
458 test_loader_ = new TestApplicationLoader; | 430 test_loader_ = new TestApplicationLoader; |
459 test_loader_->set_context(&context_); | 431 test_loader_->set_context(&context_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | 480 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); |
509 TestServicePtr test_service; | 481 TestServicePtr test_service; |
510 am.ConnectToService(test_url, &test_service); | 482 am.ConnectToService(test_url, &test_service); |
511 TestClient test_client(test_service.Pass()); | 483 TestClient test_client(test_service.Pass()); |
512 test_client.Test("test"); | 484 test_client.Test("test"); |
513 loop_.Run(); | 485 loop_.Run(); |
514 std::vector<std::string> app_args = loader->GetArgs(); | 486 std::vector<std::string> app_args = loader->GetArgs(); |
515 EXPECT_EQ(0U, app_args.size()); | 487 EXPECT_EQ(0U, app_args.size()); |
516 } | 488 } |
517 | 489 |
518 // Confirm that arguments are sent to an application. | |
519 TEST_F(ApplicationManagerTest, Args) { | |
520 ApplicationManager am(&test_delegate_); | |
521 GURL test_url("test:test"); | |
522 std::vector<std::string> args; | |
523 args.push_back("test_arg1"); | |
524 args.push_back("test_arg2"); | |
525 am.SetArgsForURL(args, test_url); | |
526 TestApplicationLoader* loader = new TestApplicationLoader; | |
527 loader->set_context(&context_); | |
528 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url); | |
529 TestServicePtr test_service; | |
530 am.ConnectToService(test_url, &test_service); | |
531 TestClient test_client(test_service.Pass()); | |
532 test_client.Test("test"); | |
533 loop_.Run(); | |
534 std::vector<std::string> app_args = loader->GetArgs(); | |
535 ASSERT_EQ(args.size(), app_args.size()); | |
536 EXPECT_EQ(args[0], app_args[0]); | |
537 EXPECT_EQ(args[1], app_args[1]); | |
538 } | |
539 | |
540 // Confirm that arguments are aggregated through mappings. | |
541 TEST_F(ApplicationManagerTest, ArgsAndMapping) { | |
542 ApplicationManager am(&test_delegate_); | |
543 GURL test_url("test:test"); | |
544 GURL test_url2("test:test2"); | |
545 test_delegate_.AddMapping(test_url, test_url2); | |
546 std::vector<std::string> args; | |
547 args.push_back("test_arg1"); | |
548 args.push_back("test_arg2"); | |
549 am.SetArgsForURL(args, test_url); | |
550 std::vector<std::string> args2; | |
551 args2.push_back("test_arg3"); | |
552 args2.push_back("test_arg4"); | |
553 am.SetArgsForURL(args2, test_url2); | |
554 TestApplicationLoader* loader = new TestApplicationLoader; | |
555 loader->set_context(&context_); | |
556 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url2); | |
557 { | |
558 // Connext to the mapped url | |
559 TestServicePtr test_service; | |
560 am.ConnectToService(test_url, &test_service); | |
561 TestClient test_client(test_service.Pass()); | |
562 test_client.Test("test"); | |
563 loop_.Run(); | |
564 std::vector<std::string> app_args = loader->GetArgs(); | |
565 ASSERT_EQ(args.size() + args2.size(), app_args.size()); | |
566 EXPECT_EQ(args[0], app_args[0]); | |
567 EXPECT_EQ(args[1], app_args[1]); | |
568 EXPECT_EQ(args2[0], app_args[2]); | |
569 EXPECT_EQ(args2[1], app_args[3]); | |
570 } | |
571 { | |
572 // Connext to the target url | |
573 TestServicePtr test_service; | |
574 am.ConnectToService(test_url2, &test_service); | |
575 TestClient test_client(test_service.Pass()); | |
576 test_client.Test("test"); | |
577 loop_.Run(); | |
578 std::vector<std::string> app_args = loader->GetArgs(); | |
579 ASSERT_EQ(args.size() + args2.size(), app_args.size()); | |
580 EXPECT_EQ(args[0], app_args[0]); | |
581 EXPECT_EQ(args[1], app_args[1]); | |
582 EXPECT_EQ(args2[0], app_args[2]); | |
583 EXPECT_EQ(args2[1], app_args[3]); | |
584 } | |
585 } | |
586 | |
587 TEST_F(ApplicationManagerTest, ClientError) { | 490 TEST_F(ApplicationManagerTest, ClientError) { |
588 test_client_->Test("test"); | 491 test_client_->Test("test"); |
589 EXPECT_TRUE(HasFactoryForTestURL()); | 492 EXPECT_TRUE(HasFactoryForTestURL()); |
590 loop_.Run(); | 493 loop_.Run(); |
591 EXPECT_EQ(1, context_.num_impls); | 494 EXPECT_EQ(1, context_.num_impls); |
592 test_client_.reset(); | 495 test_client_.reset(); |
593 loop_.Run(); | 496 loop_.Run(); |
594 EXPECT_EQ(0, context_.num_impls); | 497 EXPECT_EQ(0, context_.num_impls); |
595 EXPECT_TRUE(HasFactoryForTestURL()); | 498 EXPECT_TRUE(HasFactoryForTestURL()); |
596 } | 499 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 application_manager_->SetLoaderForURL(make_scoped_ptr(custom_loader), | 669 application_manager_->SetLoaderForURL(make_scoped_ptr(custom_loader), |
767 GURL("mojo:foo")); | 670 GURL("mojo:foo")); |
768 test_delegate_.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo")); | 671 test_delegate_.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo")); |
769 | 672 |
770 TestServicePtr test_service; | 673 TestServicePtr test_service; |
771 application_manager_->ConnectToService(GURL("mojo:foo2"), &test_service); | 674 application_manager_->ConnectToService(GURL("mojo:foo2"), &test_service); |
772 EXPECT_EQ(1, custom_loader->num_loads()); | 675 EXPECT_EQ(1, custom_loader->num_loads()); |
773 custom_loader->set_context(nullptr); | 676 custom_loader->set_context(nullptr); |
774 } | 677 } |
775 | 678 |
776 TEST_F(ApplicationManagerTest, ExternalApp) { | |
777 ApplicationPtr application; | |
778 TestExternal external; | |
779 std::vector<std::string> args; | |
780 args.push_back("test"); | |
781 ApplicationImpl app(&external, GetProxy(&application)); | |
782 application_manager_->RegisterExternalApplication(GURL("mojo:test"), args, | |
783 application.Pass()); | |
784 loop_.Run(); | |
785 EXPECT_EQ(args, external.initialize_args()); | |
786 application_manager_->ConnectToServiceByName(GURL("mojo:test"), | |
787 std::string()); | |
788 loop_.Run(); | |
789 EXPECT_TRUE(external.configure_incoming_connection_called()); | |
790 }; | |
791 | |
792 TEST_F(ApplicationManagerTest, TestQueryWithLoaders) { | 679 TEST_F(ApplicationManagerTest, TestQueryWithLoaders) { |
793 TestApplicationLoader* url_loader = new TestApplicationLoader; | 680 TestApplicationLoader* url_loader = new TestApplicationLoader; |
794 TestApplicationLoader* scheme_loader = new TestApplicationLoader; | 681 TestApplicationLoader* scheme_loader = new TestApplicationLoader; |
795 application_manager_->SetLoaderForURL( | 682 application_manager_->SetLoaderForURL( |
796 scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1")); | 683 scoped_ptr<ApplicationLoader>(url_loader), GURL("test:test1")); |
797 application_manager_->SetLoaderForScheme( | 684 application_manager_->SetLoaderForScheme( |
798 scoped_ptr<ApplicationLoader>(scheme_loader), "test"); | 685 scoped_ptr<ApplicationLoader>(scheme_loader), "test"); |
799 | 686 |
800 // test::test1 should go to url_loader. | 687 // test::test1 should go to url_loader. |
801 TestServicePtr test_service; | 688 TestServicePtr test_service; |
(...skipping 18 matching lines...) Expand all Loading... |
820 application_manager_->ConnectToApplication( | 707 application_manager_->ConnectToApplication( |
821 GURL("test:test"), GURL(), nullptr, nullptr, | 708 GURL("test:test"), GURL(), nullptr, nullptr, |
822 base::Bind(&QuitClosure, base::Unretained(&called))); | 709 base::Bind(&QuitClosure, base::Unretained(&called))); |
823 loop_.Run(); | 710 loop_.Run(); |
824 EXPECT_TRUE(called); | 711 EXPECT_TRUE(called); |
825 } | 712 } |
826 | 713 |
827 } // namespace | 714 } // namespace |
828 } // namespace shell | 715 } // namespace shell |
829 } // namespace mojo | 716 } // namespace mojo |
OLD | NEW |