| Index: mojo/shell/application_manager_unittest.cc
|
| diff --git a/mojo/shell/application_manager_unittest.cc b/mojo/shell/application_manager_unittest.cc
|
| index b0b55e4457ce1948279b23a10b86f5fc606d2dc7..56d2fb5bbff54da65d2e2d214966c76324cdc89e 100644
|
| --- a/mojo/shell/application_manager_unittest.cc
|
| +++ b/mojo/shell/application_manager_unittest.cc
|
| @@ -15,7 +15,6 @@
|
| #include "mojo/application/public/interfaces/content_handler.mojom.h"
|
| #include "mojo/application/public/interfaces/service_provider.mojom.h"
|
| #include "mojo/public/cpp/bindings/strong_binding.h"
|
| -#include "mojo/shell/application_fetcher.h"
|
| #include "mojo/shell/application_loader.h"
|
| #include "mojo/shell/application_manager.h"
|
| #include "mojo/shell/fetcher.h"
|
| @@ -456,13 +455,15 @@
|
| ScopedVector<TestAImpl> a_bindings_;
|
| };
|
|
|
| -class TestApplicationFetcher : public ApplicationFetcher {
|
| - public:
|
| - TestApplicationFetcher()
|
| +class TestDelegate : public ApplicationManager::Delegate {
|
| + public:
|
| + TestDelegate()
|
| : create_test_fetcher_(false),
|
| fetcher_url_("xxx"),
|
| mime_type_(kTestMimeType) {}
|
| - ~TestApplicationFetcher() override {}
|
| + ~TestDelegate() override {}
|
| +
|
| + void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; }
|
|
|
| void set_create_test_fetcher(bool create_test_fetcher) {
|
| create_test_fetcher_ = create_test_fetcher;
|
| @@ -473,29 +474,37 @@
|
| void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
|
|
|
| // ApplicationManager::Delegate
|
| - void SetApplicationManager(ApplicationManager* manager) override {}
|
| - GURL ResolveURL(const GURL& url) override {
|
| - GURL resolved_url = url;
|
| + GURL ResolveMappings(const GURL& url) override {
|
| + auto it = mappings_.find(url);
|
| + if (it != mappings_.end())
|
| + return it->second;
|
| + return url;
|
| + }
|
| + GURL ResolveMojoURL(const GURL& url) override {
|
| + GURL mapped_url = ResolveMappings(url);
|
| // The shell automatically map mojo URLs.
|
| - if (resolved_url.scheme() == "mojo") {
|
| + if (mapped_url.scheme() == "mojo") {
|
| url::Replacements<char> replacements;
|
| replacements.SetScheme("file", url::Component(0, 4));
|
| - resolved_url = resolved_url.ReplaceComponents(replacements);
|
| + mapped_url = mapped_url.ReplaceComponents(replacements);
|
| }
|
| - return resolved_url;
|
| - }
|
| - void FetchRequest(URLRequestPtr request,
|
| - const Fetcher::FetchCallback& loader_callback) override {
|
| - if (create_test_fetcher_)
|
| - new TestMimeTypeFetcher(loader_callback, fetcher_url_, mime_type_);
|
| - }
|
| -
|
| - private:
|
| + return mapped_url;
|
| + }
|
| + bool CreateFetcher(const GURL& url,
|
| + const Fetcher::FetchCallback& loader_callback) override {
|
| + if (!create_test_fetcher_)
|
| + return false;
|
| + new TestMimeTypeFetcher(loader_callback, fetcher_url_, mime_type_);
|
| + return true;
|
| + }
|
| +
|
| + private:
|
| + std::map<GURL, GURL> mappings_;
|
| bool create_test_fetcher_;
|
| GURL fetcher_url_;
|
| std::string mime_type_;
|
|
|
| - DISALLOW_COPY_AND_ASSIGN(TestApplicationFetcher);
|
| + DISALLOW_COPY_AND_ASSIGN(TestDelegate);
|
| };
|
|
|
| class ApplicationManagerTest : public testing::Test {
|
| @@ -505,8 +514,7 @@
|
| ~ApplicationManagerTest() override {}
|
|
|
| void SetUp() override {
|
| - application_manager_.reset(new ApplicationManager(
|
| - make_scoped_ptr(new TestApplicationFetcher)));
|
| + application_manager_.reset(new ApplicationManager(&test_delegate_));
|
| test_loader_ = new TestApplicationLoader;
|
| test_loader_->set_context(&context_);
|
| application_manager_->set_default_loader(
|
| @@ -535,6 +543,7 @@
|
|
|
| protected:
|
| base::ShadowingAtExitManager at_exit_;
|
| + TestDelegate test_delegate_;
|
| TestApplicationLoader* test_loader_;
|
| TesterContext tester_context_;
|
| TestContext context_;
|
| @@ -548,6 +557,33 @@
|
| test_client_->Test("test");
|
| loop_.Run();
|
| EXPECT_EQ(std::string("test"), context_.last_test_string);
|
| +}
|
| +
|
| +// Confirm that url mappings are respected.
|
| +TEST_F(ApplicationManagerTest, URLMapping) {
|
| + ApplicationManager am(&test_delegate_);
|
| + GURL test_url("test:test");
|
| + GURL test_url2("test:test2");
|
| + test_delegate_.AddMapping(test_url, test_url2);
|
| + TestApplicationLoader* loader = new TestApplicationLoader;
|
| + loader->set_context(&context_);
|
| + am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url2);
|
| + {
|
| + // Connect to the mapped url
|
| + TestServicePtr test_service;
|
| + am.ConnectToService(test_url, &test_service);
|
| + TestClient test_client(test_service.Pass());
|
| + test_client.Test("test");
|
| + loop_.Run();
|
| + }
|
| + {
|
| + // Connect to the target url
|
| + TestServicePtr test_service;
|
| + am.ConnectToService(test_url2, &test_service);
|
| + TestClient test_client(test_service.Pass());
|
| + test_client.Test("test");
|
| + loop_.Run();
|
| + }
|
| }
|
|
|
| TEST_F(ApplicationManagerTest, ClientError) {
|
| @@ -563,7 +599,7 @@
|
|
|
| TEST_F(ApplicationManagerTest, Deletes) {
|
| {
|
| - ApplicationManager am(make_scoped_ptr(new TestApplicationFetcher));
|
| + ApplicationManager am(&test_delegate_);
|
| TestApplicationLoader* default_loader = new TestApplicationLoader;
|
| default_loader->set_context(&context_);
|
| TestApplicationLoader* url_loader1 = new TestApplicationLoader;
|
| @@ -707,6 +743,41 @@
|
| EXPECT_TRUE(c.encountered_error());
|
| }
|
|
|
| +TEST_F(ApplicationManagerTest, MappedURLsShouldNotCauseDuplicateLoad) {
|
| + test_delegate_.AddMapping(GURL("foo:foo2"), GURL("foo:foo"));
|
| + // 1 because ApplicationManagerTest connects once at startup.
|
| + EXPECT_EQ(1, test_loader_->num_loads());
|
| +
|
| + TestServicePtr test_service;
|
| + application_manager_->ConnectToService(GURL("foo:foo"), &test_service);
|
| + EXPECT_EQ(2, test_loader_->num_loads());
|
| +
|
| + TestServicePtr test_service2;
|
| + application_manager_->ConnectToService(GURL("foo:foo2"), &test_service2);
|
| + EXPECT_EQ(2, test_loader_->num_loads());
|
| +
|
| + TestServicePtr test_service3;
|
| + application_manager_->ConnectToService(GURL("bar:bar"), &test_service2);
|
| + EXPECT_EQ(3, test_loader_->num_loads());
|
| +}
|
| +
|
| +TEST_F(ApplicationManagerTest, MappedURLsShouldWorkWithLoaders) {
|
| + TestApplicationLoader* custom_loader = new TestApplicationLoader;
|
| + TestContext context;
|
| + custom_loader->set_context(&context);
|
| + application_manager_->SetLoaderForURL(make_scoped_ptr(custom_loader),
|
| + GURL("mojo:foo"));
|
| + test_delegate_.AddMapping(GURL("mojo:foo2"), GURL("mojo:foo"));
|
| +
|
| + TestServicePtr test_service;
|
| + application_manager_->ConnectToService(GURL("mojo:foo2"), &test_service);
|
| + EXPECT_EQ(1, custom_loader->num_loads());
|
| + custom_loader->set_context(nullptr);
|
| +
|
| + EXPECT_TRUE(HasRunningInstanceForURL(GURL("mojo:foo2")));
|
| + EXPECT_FALSE(HasRunningInstanceForURL(GURL("mojo:foo")));
|
| +}
|
| +
|
| TEST_F(ApplicationManagerTest, TestQueryWithLoaders) {
|
| TestApplicationLoader* url_loader = new TestApplicationLoader;
|
| TestApplicationLoader* scheme_loader = new TestApplicationLoader;
|
| @@ -750,10 +821,9 @@
|
| const GURL requestor_url("http://requestor.url");
|
| TestContext test_context;
|
| base::MessageLoop loop;
|
| - scoped_ptr<TestApplicationFetcher> test_application_fetcher(
|
| - new TestApplicationFetcher);
|
| - test_application_fetcher->set_create_test_fetcher(true);
|
| - ApplicationManager application_manager(test_application_fetcher.Pass());
|
| + TestDelegate test_delegate;
|
| + test_delegate.set_create_test_fetcher(true);
|
| + ApplicationManager application_manager(&test_delegate);
|
| application_manager.set_default_loader(nullptr);
|
| application_manager.RegisterContentHandler(kTestMimeType,
|
| content_handler_url);
|
| @@ -816,11 +886,10 @@
|
| const GURL content_handler_url("http://test.content.handler");
|
| const GURL requestor_url("http://requestor.url");
|
| TestContext test_context;
|
| - scoped_ptr<TestApplicationFetcher> test_application_fetcher(
|
| - new TestApplicationFetcher);
|
| - test_application_fetcher->set_fetcher_url(GURL("test:test"));
|
| - test_application_fetcher->set_create_test_fetcher(true);
|
| - ApplicationManager application_manager(test_application_fetcher.Pass());
|
| + TestDelegate test_delegate;
|
| + test_delegate.set_fetcher_url(GURL("test:test"));
|
| + test_delegate.set_create_test_fetcher(true);
|
| + ApplicationManager application_manager(&test_delegate);
|
| application_manager.set_default_loader(nullptr);
|
| application_manager.RegisterContentHandler(kTestMimeType,
|
| content_handler_url);
|
| @@ -875,11 +944,10 @@
|
| const GURL content_handler_url("http://test.content.handler");
|
| const GURL requestor_url("http://requestor.url");
|
| TestContext test_context;
|
| - TestApplicationFetcher* test_application_fetcher = new TestApplicationFetcher;
|
| - test_application_fetcher->set_fetcher_url(GURL("test:test"));
|
| - test_application_fetcher->set_create_test_fetcher(true);
|
| - ApplicationManager application_manager(
|
| - make_scoped_ptr(test_application_fetcher));
|
| + TestDelegate test_delegate;
|
| + test_delegate.set_fetcher_url(GURL("test:test"));
|
| + test_delegate.set_create_test_fetcher(true);
|
| + ApplicationManager application_manager(&test_delegate);
|
| application_manager.set_default_loader(nullptr);
|
| application_manager.RegisterContentHandler(kTestMimeType,
|
| content_handler_url);
|
| @@ -911,8 +979,8 @@
|
|
|
| const std::string mime_type2("test/mime-type2");
|
| const GURL content_handler_url2("http://test.content2.handler");
|
| - test_application_fetcher->set_fetcher_url(GURL("test2:test2"));
|
| - test_application_fetcher->set_mime_type(mime_type2);
|
| + test_delegate.set_fetcher_url(GURL("test2:test2"));
|
| + test_delegate.set_mime_type(mime_type2);
|
| application_manager.RegisterContentHandler(mime_type2, content_handler_url2);
|
|
|
| TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader;
|
|
|