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 "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "mojo/application/public/cpp/application_connection.h" | 11 #include "mojo/application/public/cpp/application_connection.h" |
12 #include "mojo/application/public/cpp/application_delegate.h" | 12 #include "mojo/application/public/cpp/application_delegate.h" |
13 #include "mojo/application/public/cpp/application_impl.h" | 13 #include "mojo/application/public/cpp/application_impl.h" |
14 #include "mojo/application/public/cpp/interface_factory.h" | 14 #include "mojo/application/public/cpp/interface_factory.h" |
15 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 15 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
16 #include "mojo/application/public/interfaces/service_provider.mojom.h" | 16 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
17 #include "mojo/public/cpp/bindings/strong_binding.h" | 17 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 18 #include "mojo/shell/application_fetcher.h" |
18 #include "mojo/shell/application_loader.h" | 19 #include "mojo/shell/application_loader.h" |
19 #include "mojo/shell/application_manager.h" | 20 #include "mojo/shell/application_manager.h" |
20 #include "mojo/shell/fetcher.h" | 21 #include "mojo/shell/fetcher.h" |
21 #include "mojo/shell/test.mojom.h" | 22 #include "mojo/shell/test.mojom.h" |
22 #include "mojo/shell/test_package_manager.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace mojo { | 25 namespace mojo { |
26 namespace shell { | 26 namespace shell { |
27 namespace { | 27 namespace { |
28 | 28 |
29 const char kTestURLString[] = "test:testService"; | 29 const char kTestURLString[] = "test:testService"; |
30 const char kTestAURLString[] = "test:TestA"; | 30 const char kTestAURLString[] = "test:TestA"; |
31 const char kTestBURLString[] = "test:TestB"; | 31 const char kTestBURLString[] = "test:TestB"; |
32 | 32 |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 InterfaceRequest<TestC> request) override { | 449 InterfaceRequest<TestC> request) override { |
450 new TestCImpl(connection, context_, request.Pass()); | 450 new TestCImpl(connection, context_, request.Pass()); |
451 } | 451 } |
452 | 452 |
453 TesterContext* context_; | 453 TesterContext* context_; |
454 scoped_ptr<ApplicationImpl> app_; | 454 scoped_ptr<ApplicationImpl> app_; |
455 std::string requestor_url_; | 455 std::string requestor_url_; |
456 ScopedVector<TestAImpl> a_bindings_; | 456 ScopedVector<TestAImpl> a_bindings_; |
457 }; | 457 }; |
458 | 458 |
459 class AMTestPackageManager : public TestPackageManager { | 459 class TestApplicationFetcher : public ApplicationFetcher { |
460 public: | 460 public: |
461 AMTestPackageManager() | 461 TestApplicationFetcher() |
462 : create_test_fetcher_(false), | 462 : create_test_fetcher_(false), |
463 fetcher_url_("xxx"), | 463 fetcher_url_("xxx"), |
464 mime_type_(kTestMimeType) {} | 464 mime_type_(kTestMimeType) {} |
465 ~AMTestPackageManager() override {} | 465 ~TestApplicationFetcher() override {} |
466 | 466 |
467 void set_create_test_fetcher(bool create_test_fetcher) { | 467 void set_create_test_fetcher(bool create_test_fetcher) { |
468 create_test_fetcher_ = create_test_fetcher; | 468 create_test_fetcher_ = create_test_fetcher; |
469 } | 469 } |
470 | 470 |
471 void set_fetcher_url(const GURL& url) { fetcher_url_ = url; } | 471 void set_fetcher_url(const GURL& url) { fetcher_url_ = url; } |
472 | 472 |
473 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; } | 473 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; } |
474 | 474 |
475 // TestPackageManager: | 475 // ApplicationManager::Delegate |
| 476 void SetApplicationManager(ApplicationManager* manager) override {} |
476 GURL ResolveURL(const GURL& url) override { | 477 GURL ResolveURL(const GURL& url) override { |
477 GURL resolved_url = url; | 478 GURL resolved_url = url; |
478 // The shell automatically map mojo URLs. | 479 // The shell automatically map mojo URLs. |
479 if (resolved_url.scheme() == "mojo") { | 480 if (resolved_url.scheme() == "mojo") { |
480 url::Replacements<char> replacements; | 481 url::Replacements<char> replacements; |
481 replacements.SetScheme("file", url::Component(0, 4)); | 482 replacements.SetScheme("file", url::Component(0, 4)); |
482 resolved_url = resolved_url.ReplaceComponents(replacements); | 483 resolved_url = resolved_url.ReplaceComponents(replacements); |
483 } | 484 } |
484 return resolved_url; | 485 return resolved_url; |
485 } | 486 } |
486 void FetchRequest(URLRequestPtr request, | 487 void FetchRequest(URLRequestPtr request, |
487 const Fetcher::FetchCallback& loader_callback) override { | 488 const Fetcher::FetchCallback& loader_callback) override { |
488 if (create_test_fetcher_) | 489 if (create_test_fetcher_) |
489 new TestMimeTypeFetcher(loader_callback, fetcher_url_, mime_type_); | 490 new TestMimeTypeFetcher(loader_callback, fetcher_url_, mime_type_); |
490 } | 491 } |
491 | 492 |
492 private: | 493 private: |
493 bool create_test_fetcher_; | 494 bool create_test_fetcher_; |
494 GURL fetcher_url_; | 495 GURL fetcher_url_; |
495 std::string mime_type_; | 496 std::string mime_type_; |
496 | 497 |
497 DISALLOW_COPY_AND_ASSIGN(AMTestPackageManager); | 498 DISALLOW_COPY_AND_ASSIGN(TestApplicationFetcher); |
498 }; | 499 }; |
499 | 500 |
500 class ApplicationManagerTest : public testing::Test { | 501 class ApplicationManagerTest : public testing::Test { |
501 public: | 502 public: |
502 ApplicationManagerTest() : tester_context_(&loop_) {} | 503 ApplicationManagerTest() : tester_context_(&loop_) {} |
503 | 504 |
504 ~ApplicationManagerTest() override {} | 505 ~ApplicationManagerTest() override {} |
505 | 506 |
506 void SetUp() override { | 507 void SetUp() override { |
507 application_manager_.reset(new ApplicationManager( | 508 application_manager_.reset(new ApplicationManager( |
508 make_scoped_ptr(new AMTestPackageManager))); | 509 make_scoped_ptr(new TestApplicationFetcher))); |
509 test_loader_ = new TestApplicationLoader; | 510 test_loader_ = new TestApplicationLoader; |
510 test_loader_->set_context(&context_); | 511 test_loader_->set_context(&context_); |
511 application_manager_->set_default_loader( | 512 application_manager_->set_default_loader( |
512 scoped_ptr<ApplicationLoader>(test_loader_)); | 513 scoped_ptr<ApplicationLoader>(test_loader_)); |
513 | 514 |
514 TestServicePtr service_proxy; | 515 TestServicePtr service_proxy; |
515 application_manager_->ConnectToService(GURL(kTestURLString), | 516 application_manager_->ConnectToService(GURL(kTestURLString), |
516 &service_proxy); | 517 &service_proxy); |
517 test_client_.reset(new TestClient(service_proxy.Pass())); | 518 test_client_.reset(new TestClient(service_proxy.Pass())); |
518 } | 519 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 loop_.Run(); | 556 loop_.Run(); |
556 EXPECT_EQ(1, context_.num_impls); | 557 EXPECT_EQ(1, context_.num_impls); |
557 test_client_.reset(); | 558 test_client_.reset(); |
558 loop_.Run(); | 559 loop_.Run(); |
559 EXPECT_EQ(0, context_.num_impls); | 560 EXPECT_EQ(0, context_.num_impls); |
560 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString))); | 561 EXPECT_TRUE(HasRunningInstanceForURL(GURL(kTestURLString))); |
561 } | 562 } |
562 | 563 |
563 TEST_F(ApplicationManagerTest, Deletes) { | 564 TEST_F(ApplicationManagerTest, Deletes) { |
564 { | 565 { |
565 ApplicationManager am(make_scoped_ptr(new AMTestPackageManager)); | 566 ApplicationManager am(make_scoped_ptr(new TestApplicationFetcher)); |
566 TestApplicationLoader* default_loader = new TestApplicationLoader; | 567 TestApplicationLoader* default_loader = new TestApplicationLoader; |
567 default_loader->set_context(&context_); | 568 default_loader->set_context(&context_); |
568 TestApplicationLoader* url_loader1 = new TestApplicationLoader; | 569 TestApplicationLoader* url_loader1 = new TestApplicationLoader; |
569 TestApplicationLoader* url_loader2 = new TestApplicationLoader; | 570 TestApplicationLoader* url_loader2 = new TestApplicationLoader; |
570 url_loader1->set_context(&context_); | 571 url_loader1->set_context(&context_); |
571 url_loader2->set_context(&context_); | 572 url_loader2->set_context(&context_); |
572 am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); | 573 am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); |
573 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), | 574 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), |
574 GURL("test:test1")); | 575 GURL("test:test1")); |
575 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), | 576 am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 application_manager_->ConnectToApplication(params.Pass()); | 702 application_manager_->ConnectToApplication(params.Pass()); |
702 loop_.Run(); | 703 loop_.Run(); |
703 EXPECT_TRUE(called); | 704 EXPECT_TRUE(called); |
704 } | 705 } |
705 | 706 |
706 TEST(ApplicationManagerTest2, ContentHandlerConnectionGetsRequestorURL) { | 707 TEST(ApplicationManagerTest2, ContentHandlerConnectionGetsRequestorURL) { |
707 const GURL content_handler_url("http://test.content.handler"); | 708 const GURL content_handler_url("http://test.content.handler"); |
708 const GURL requestor_url("http://requestor.url"); | 709 const GURL requestor_url("http://requestor.url"); |
709 TestContext test_context; | 710 TestContext test_context; |
710 base::MessageLoop loop; | 711 base::MessageLoop loop; |
711 scoped_ptr<AMTestPackageManager> test_package_manager( | 712 scoped_ptr<TestApplicationFetcher> test_application_fetcher( |
712 new AMTestPackageManager); | 713 new TestApplicationFetcher); |
713 test_package_manager->set_create_test_fetcher(true); | 714 test_application_fetcher->set_create_test_fetcher(true); |
714 test_package_manager->RegisterContentHandler(kTestMimeType, | 715 ApplicationManager application_manager(test_application_fetcher.Pass()); |
715 content_handler_url); | |
716 ApplicationManager application_manager(test_package_manager.Pass()); | |
717 application_manager.set_default_loader(nullptr); | 716 application_manager.set_default_loader(nullptr); |
| 717 application_manager.RegisterContentHandler(kTestMimeType, |
| 718 content_handler_url); |
718 | 719 |
719 TestApplicationLoader* loader = new TestApplicationLoader; | 720 TestApplicationLoader* loader = new TestApplicationLoader; |
720 loader->set_context(&test_context); | 721 loader->set_context(&test_context); |
721 application_manager.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), | 722 application_manager.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), |
722 content_handler_url); | 723 content_handler_url); |
723 | 724 |
724 bool called = false; | 725 bool called = false; |
725 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); | 726 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); |
726 params->set_originator_identity(Identity(requestor_url)); | 727 params->set_originator_identity(Identity(requestor_url)); |
727 params->set_originator_filter(GetPermissiveCapabilityFilter()); | 728 params->set_originator_filter(GetPermissiveCapabilityFilter()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 GURL("http://www.another_domain.org/abc"), &test_service); | 768 GURL("http://www.another_domain.org/abc"), &test_service); |
768 EXPECT_EQ(4, test_loader_->num_loads()); | 769 EXPECT_EQ(4, test_loader_->num_loads()); |
769 } | 770 } |
770 | 771 |
771 TEST(ApplicationManagerTest2, | 772 TEST(ApplicationManagerTest2, |
772 MultipleConnectionsToContentHandlerGetSameContentHandlerId) { | 773 MultipleConnectionsToContentHandlerGetSameContentHandlerId) { |
773 base::MessageLoop loop; | 774 base::MessageLoop loop; |
774 const GURL content_handler_url("http://test.content.handler"); | 775 const GURL content_handler_url("http://test.content.handler"); |
775 const GURL requestor_url("http://requestor.url"); | 776 const GURL requestor_url("http://requestor.url"); |
776 TestContext test_context; | 777 TestContext test_context; |
777 scoped_ptr<AMTestPackageManager> test_package_manager( | 778 scoped_ptr<TestApplicationFetcher> test_application_fetcher( |
778 new AMTestPackageManager); | 779 new TestApplicationFetcher); |
779 test_package_manager->set_fetcher_url(GURL("test:test")); | 780 test_application_fetcher->set_fetcher_url(GURL("test:test")); |
780 test_package_manager->set_create_test_fetcher(true); | 781 test_application_fetcher->set_create_test_fetcher(true); |
781 test_package_manager->RegisterContentHandler(kTestMimeType, | 782 ApplicationManager application_manager(test_application_fetcher.Pass()); |
782 content_handler_url); | |
783 ApplicationManager application_manager(test_package_manager.Pass()); | |
784 application_manager.set_default_loader(nullptr); | 783 application_manager.set_default_loader(nullptr); |
| 784 application_manager.RegisterContentHandler(kTestMimeType, |
| 785 content_handler_url); |
785 | 786 |
786 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; | 787 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
787 content_handler_loader->set_create_content_handler(true); | 788 content_handler_loader->set_create_content_handler(true); |
788 content_handler_loader->set_context(&test_context); | 789 content_handler_loader->set_context(&test_context); |
789 application_manager.SetLoaderForURL( | 790 application_manager.SetLoaderForURL( |
790 scoped_ptr<ApplicationLoader>(content_handler_loader), | 791 scoped_ptr<ApplicationLoader>(content_handler_loader), |
791 content_handler_url); | 792 content_handler_url); |
792 | 793 |
793 uint32_t content_handler_id; | 794 uint32_t content_handler_id; |
794 { | 795 { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); | 827 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); |
827 } | 828 } |
828 EXPECT_EQ(content_handler_id, content_handler_id2); | 829 EXPECT_EQ(content_handler_id, content_handler_id2); |
829 } | 830 } |
830 | 831 |
831 TEST(ApplicationManagerTest2, DifferedContentHandlersGetDifferentIDs) { | 832 TEST(ApplicationManagerTest2, DifferedContentHandlersGetDifferentIDs) { |
832 base::MessageLoop loop; | 833 base::MessageLoop loop; |
833 const GURL content_handler_url("http://test.content.handler"); | 834 const GURL content_handler_url("http://test.content.handler"); |
834 const GURL requestor_url("http://requestor.url"); | 835 const GURL requestor_url("http://requestor.url"); |
835 TestContext test_context; | 836 TestContext test_context; |
836 AMTestPackageManager* test_package_manager = new AMTestPackageManager; | 837 TestApplicationFetcher* test_application_fetcher = new TestApplicationFetcher; |
837 test_package_manager->set_fetcher_url(GURL("test:test")); | 838 test_application_fetcher->set_fetcher_url(GURL("test:test")); |
838 test_package_manager->set_create_test_fetcher(true); | 839 test_application_fetcher->set_create_test_fetcher(true); |
839 test_package_manager->RegisterContentHandler(kTestMimeType, | 840 ApplicationManager application_manager( |
840 content_handler_url); | 841 make_scoped_ptr(test_application_fetcher)); |
841 ApplicationManager application_manager(make_scoped_ptr(test_package_manager)); | |
842 application_manager.set_default_loader(nullptr); | 842 application_manager.set_default_loader(nullptr); |
| 843 application_manager.RegisterContentHandler(kTestMimeType, |
| 844 content_handler_url); |
843 | 845 |
844 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; | 846 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
845 content_handler_loader->set_create_content_handler(true); | 847 content_handler_loader->set_create_content_handler(true); |
846 content_handler_loader->set_context(&test_context); | 848 content_handler_loader->set_context(&test_context); |
847 application_manager.SetLoaderForURL( | 849 application_manager.SetLoaderForURL( |
848 scoped_ptr<ApplicationLoader>(content_handler_loader), | 850 scoped_ptr<ApplicationLoader>(content_handler_loader), |
849 content_handler_url); | 851 content_handler_url); |
850 | 852 |
851 uint32_t content_handler_id; | 853 uint32_t content_handler_id; |
852 { | 854 { |
853 base::RunLoop run_loop; | 855 base::RunLoop run_loop; |
854 scoped_ptr<ConnectToApplicationParams> params( | 856 scoped_ptr<ConnectToApplicationParams> params( |
855 new ConnectToApplicationParams); | 857 new ConnectToApplicationParams); |
856 params->set_originator_identity(Identity(requestor_url)); | 858 params->set_originator_identity(Identity(requestor_url)); |
857 params->set_originator_filter(GetPermissiveCapabilityFilter()); | 859 params->set_originator_filter(GetPermissiveCapabilityFilter()); |
858 params->SetURLInfo(GURL("test:test")); | 860 params->SetURLInfo(GURL("test:test")); |
859 params->set_filter(GetPermissiveCapabilityFilter()); | 861 params->set_filter(GetPermissiveCapabilityFilter()); |
860 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { | 862 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { |
861 content_handler_id = t; | 863 content_handler_id = t; |
862 run_loop.Quit(); | 864 run_loop.Quit(); |
863 }); | 865 }); |
864 application_manager.ConnectToApplication(params.Pass()); | 866 application_manager.ConnectToApplication(params.Pass()); |
865 run_loop.Run(); | 867 run_loop.Run(); |
866 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); | 868 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); |
867 } | 869 } |
868 | 870 |
869 const std::string mime_type2("test/mime-type2"); | 871 const std::string mime_type2("test/mime-type2"); |
870 const GURL content_handler_url2("http://test.content2.handler"); | 872 const GURL content_handler_url2("http://test.content2.handler"); |
871 test_package_manager->set_fetcher_url(GURL("test2:test2")); | 873 test_application_fetcher->set_fetcher_url(GURL("test2:test2")); |
872 test_package_manager->set_mime_type(mime_type2); | 874 test_application_fetcher->set_mime_type(mime_type2); |
873 test_package_manager->RegisterContentHandler(mime_type2, | 875 application_manager.RegisterContentHandler(mime_type2, content_handler_url2); |
874 content_handler_url2); | |
875 | 876 |
876 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader; | 877 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader; |
877 content_handler_loader->set_create_content_handler(true); | 878 content_handler_loader->set_create_content_handler(true); |
878 content_handler_loader->set_context(&test_context); | 879 content_handler_loader->set_context(&test_context); |
879 application_manager.SetLoaderForURL( | 880 application_manager.SetLoaderForURL( |
880 scoped_ptr<ApplicationLoader>(content_handler_loader2), | 881 scoped_ptr<ApplicationLoader>(content_handler_loader2), |
881 content_handler_url2); | 882 content_handler_url2); |
882 | 883 |
883 uint32_t content_handler_id2; | 884 uint32_t content_handler_id2; |
884 { | 885 { |
(...skipping 27 matching lines...) Expand all Loading... |
912 params->set_filter(GetPermissiveCapabilityFilter()); | 913 params->set_filter(GetPermissiveCapabilityFilter()); |
913 params->set_connect_callback( | 914 params->set_connect_callback( |
914 [&content_handler_id](uint32_t t) { content_handler_id = t; }); | 915 [&content_handler_id](uint32_t t) { content_handler_id = t; }); |
915 application_manager_->ConnectToApplication(params.Pass()); | 916 application_manager_->ConnectToApplication(params.Pass()); |
916 EXPECT_EQ(0u, content_handler_id); | 917 EXPECT_EQ(0u, content_handler_id); |
917 } | 918 } |
918 | 919 |
919 } // namespace | 920 } // namespace |
920 } // namespace shell | 921 } // namespace shell |
921 } // namespace mojo | 922 } // namespace mojo |
OLD | NEW |