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" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
13 #include "mojo/public/cpp/application/interface_factory.h" | 13 #include "mojo/public/cpp/application/interface_factory.h" |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
15 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 15 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
16 #include "mojo/shell/application_loader.h" | 16 #include "mojo/shell/application_loader.h" |
17 #include "mojo/shell/application_manager.h" | 17 #include "mojo/shell/application_manager.h" |
| 18 #include "mojo/shell/fetcher.h" |
18 #include "mojo/shell/test.mojom.h" | 19 #include "mojo/shell/test.mojom.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace mojo { | 22 namespace mojo { |
22 namespace shell { | 23 namespace shell { |
23 namespace { | 24 namespace { |
24 | 25 |
25 const char kTestURLString[] = "test:testService"; | 26 const char kTestURLString[] = "test:testService"; |
26 const char kTestAURLString[] = "test:TestA"; | 27 const char kTestAURLString[] = "test:TestA"; |
27 const char kTestBURLString[] = "test:TestB"; | 28 const char kTestBURLString[] = "test:TestB"; |
28 | 29 |
| 30 const char kTestMimeType[] = "test/mime-type"; |
| 31 |
| 32 class TestMimeTypeFetcher : public Fetcher { |
| 33 public: |
| 34 explicit TestMimeTypeFetcher(const FetchCallback& fetch_callback) |
| 35 : Fetcher(fetch_callback), url_("xxx") { |
| 36 loader_callback_.Run(make_scoped_ptr(this)); |
| 37 } |
| 38 ~TestMimeTypeFetcher() override {} |
| 39 |
| 40 // Fetcher: |
| 41 const GURL& GetURL() const override { return url_; } |
| 42 GURL GetRedirectURL() const override { return GURL("yyy"); } |
| 43 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, |
| 44 uint32_t skip) override { |
| 45 return URLResponse::New().Pass(); |
| 46 } |
| 47 void AsPath( |
| 48 base::TaskRunner* task_runner, |
| 49 base::Callback<void(const base::FilePath&, bool)> callback) override {} |
| 50 std::string MimeType() override { return kTestMimeType; } |
| 51 bool HasMojoMagic() override { return false; } |
| 52 bool PeekFirstLine(std::string* line) override { return false; } |
| 53 |
| 54 private: |
| 55 const GURL url_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(TestMimeTypeFetcher); |
| 58 }; |
| 59 |
29 struct TestContext { | 60 struct TestContext { |
30 TestContext() : num_impls(0), num_loader_deletes(0) {} | 61 TestContext() : num_impls(0), num_loader_deletes(0) {} |
31 std::string last_test_string; | 62 std::string last_test_string; |
32 int num_impls; | 63 int num_impls; |
33 int num_loader_deletes; | 64 int num_loader_deletes; |
34 }; | 65 }; |
35 | 66 |
36 void QuitClosure(bool* value) { | 67 void QuitClosure(bool* value) { |
37 *value = true; | 68 *value = true; |
38 base::MessageLoop::current()->QuitWhenIdle(); | 69 base::MessageLoop::current()->QuitWhenIdle(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 139 |
109 ~TestApplicationLoader() override { | 140 ~TestApplicationLoader() override { |
110 if (context_) | 141 if (context_) |
111 ++context_->num_loader_deletes; | 142 ++context_->num_loader_deletes; |
112 test_app_.reset(); | 143 test_app_.reset(); |
113 } | 144 } |
114 | 145 |
115 void set_context(TestContext* context) { context_ = context; } | 146 void set_context(TestContext* context) { context_ = context; } |
116 int num_loads() const { return num_loads_; } | 147 int num_loads() const { return num_loads_; } |
117 const std::vector<std::string>& GetArgs() const { return test_app_->args(); } | 148 const std::vector<std::string>& GetArgs() const { return test_app_->args(); } |
| 149 const GURL& last_requestor_url() const { return last_requestor_url_; } |
118 | 150 |
119 private: | 151 private: |
120 // ApplicationLoader implementation. | 152 // ApplicationLoader implementation. |
121 void Load(const GURL& url, | 153 void Load(const GURL& url, |
122 InterfaceRequest<Application> application_request) override { | 154 InterfaceRequest<Application> application_request) override { |
123 ++num_loads_; | 155 ++num_loads_; |
124 test_app_.reset(new ApplicationImpl(this, application_request.Pass())); | 156 test_app_.reset(new ApplicationImpl(this, application_request.Pass())); |
125 } | 157 } |
126 | 158 |
127 // ApplicationDelegate implementation. | 159 // ApplicationDelegate implementation. |
128 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 160 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
129 connection->AddService(this); | 161 connection->AddService(this); |
| 162 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); |
130 return true; | 163 return true; |
131 } | 164 } |
132 | 165 |
133 // InterfaceFactory implementation. | 166 // InterfaceFactory implementation. |
134 void Create(ApplicationConnection* connection, | 167 void Create(ApplicationConnection* connection, |
135 InterfaceRequest<TestService> request) override { | 168 InterfaceRequest<TestService> request) override { |
136 new TestServiceImpl(context_, request.Pass()); | 169 new TestServiceImpl(context_, request.Pass()); |
137 } | 170 } |
138 | 171 |
139 scoped_ptr<ApplicationImpl> test_app_; | 172 scoped_ptr<ApplicationImpl> test_app_; |
140 TestContext* context_; | 173 TestContext* context_; |
141 int num_loads_; | 174 int num_loads_; |
| 175 GURL last_requestor_url_; |
| 176 |
142 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 177 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
143 }; | 178 }; |
144 | 179 |
145 class ClosingApplicationLoader : public ApplicationLoader { | 180 class ClosingApplicationLoader : public ApplicationLoader { |
146 private: | 181 private: |
147 // ApplicationLoader implementation. | 182 // ApplicationLoader implementation. |
148 void Load(const GURL& url, | 183 void Load(const GURL& url, |
149 InterfaceRequest<Application> application_request) override {} | 184 InterfaceRequest<Application> application_request) override {} |
150 }; | 185 }; |
151 | 186 |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 } | 421 } |
387 | 422 |
388 TesterContext* context_; | 423 TesterContext* context_; |
389 scoped_ptr<ApplicationImpl> app_; | 424 scoped_ptr<ApplicationImpl> app_; |
390 std::string requestor_url_; | 425 std::string requestor_url_; |
391 ScopedVector<TestAImpl> a_bindings_; | 426 ScopedVector<TestAImpl> a_bindings_; |
392 }; | 427 }; |
393 | 428 |
394 class TestDelegate : public ApplicationManager::Delegate { | 429 class TestDelegate : public ApplicationManager::Delegate { |
395 public: | 430 public: |
| 431 TestDelegate() : create_test_fetcher_(false) {} |
| 432 ~TestDelegate() override {} |
| 433 |
396 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } | 434 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } |
397 | 435 |
| 436 void set_create_test_fetcher(bool create_test_fetcher) { |
| 437 create_test_fetcher_ = create_test_fetcher; |
| 438 } |
| 439 |
398 // ApplicationManager::Delegate | 440 // ApplicationManager::Delegate |
399 GURL ResolveMappings(const GURL& url) override { | 441 GURL ResolveMappings(const GURL& url) override { |
400 auto it = mappings_.find(url); | 442 auto it = mappings_.find(url); |
401 if (it != mappings_.end()) | 443 if (it != mappings_.end()) |
402 return it->second; | 444 return it->second; |
403 return url; | 445 return url; |
404 } | 446 } |
405 GURL ResolveMojoURL(const GURL& url) override { | 447 GURL ResolveMojoURL(const GURL& url) override { |
406 GURL mapped_url = ResolveMappings(url); | 448 GURL mapped_url = ResolveMappings(url); |
407 // The shell automatically map mojo URLs. | 449 // The shell automatically map mojo URLs. |
408 if (mapped_url.scheme() == "mojo") { | 450 if (mapped_url.scheme() == "mojo") { |
409 url::Replacements<char> replacements; | 451 url::Replacements<char> replacements; |
410 replacements.SetScheme("file", url::Component(0, 4)); | 452 replacements.SetScheme("file", url::Component(0, 4)); |
411 mapped_url = mapped_url.ReplaceComponents(replacements); | 453 mapped_url = mapped_url.ReplaceComponents(replacements); |
412 } | 454 } |
413 return mapped_url; | 455 return mapped_url; |
414 } | 456 } |
| 457 bool CreateFetcher(const GURL& url, |
| 458 const Fetcher::FetchCallback& loader_callback) override { |
| 459 if (!create_test_fetcher_) |
| 460 return false; |
| 461 new TestMimeTypeFetcher(loader_callback); |
| 462 return true; |
| 463 } |
415 | 464 |
416 private: | 465 private: |
417 std::map<GURL, GURL> mappings_; | 466 std::map<GURL, GURL> mappings_; |
| 467 bool create_test_fetcher_; |
| 468 |
| 469 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
418 }; | 470 }; |
419 | 471 |
420 class ApplicationManagerTest : public testing::Test { | 472 class ApplicationManagerTest : public testing::Test { |
421 public: | 473 public: |
422 ApplicationManagerTest() : tester_context_(&loop_) {} | 474 ApplicationManagerTest() : tester_context_(&loop_) {} |
423 | 475 |
424 ~ApplicationManagerTest() override {} | 476 ~ApplicationManagerTest() override {} |
425 | 477 |
426 void SetUp() override { | 478 void SetUp() override { |
427 application_manager_.reset(new ApplicationManager(&test_delegate_)); | 479 application_manager_.reset(new ApplicationManager(&test_delegate_)); |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 scoped_ptr<ApplicationLoader>(loader), "test"); | 784 scoped_ptr<ApplicationLoader>(loader), "test"); |
733 | 785 |
734 bool called = false; | 786 bool called = false; |
735 application_manager_->ConnectToApplication( | 787 application_manager_->ConnectToApplication( |
736 GURL("test:test"), GURL(), nullptr, nullptr, | 788 GURL("test:test"), GURL(), nullptr, nullptr, |
737 base::Bind(&QuitClosure, base::Unretained(&called))); | 789 base::Bind(&QuitClosure, base::Unretained(&called))); |
738 loop_.Run(); | 790 loop_.Run(); |
739 EXPECT_TRUE(called); | 791 EXPECT_TRUE(called); |
740 } | 792 } |
741 | 793 |
| 794 TEST(ApplicationManagerTest2, ContentHandlerConnectionGetsRequestorURL) { |
| 795 const GURL content_handler_url("http://test.content.handler"); |
| 796 const GURL requestor_url("http://requestor.url"); |
| 797 TestContext test_context; |
| 798 base::MessageLoop loop; |
| 799 TestDelegate test_delegate; |
| 800 test_delegate.set_create_test_fetcher(true); |
| 801 ApplicationManager application_manager(&test_delegate); |
| 802 application_manager.set_default_loader(nullptr); |
| 803 application_manager.RegisterContentHandler(kTestMimeType, |
| 804 content_handler_url); |
| 805 |
| 806 TestApplicationLoader* loader = new TestApplicationLoader; |
| 807 loader->set_context(&test_context); |
| 808 application_manager.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), |
| 809 content_handler_url); |
| 810 |
| 811 bool called = false; |
| 812 application_manager.ConnectToApplication( |
| 813 GURL("test:test"), requestor_url, nullptr, nullptr, |
| 814 base::Bind(&QuitClosure, base::Unretained(&called))); |
| 815 loop.Run(); |
| 816 EXPECT_TRUE(called); |
| 817 |
| 818 ASSERT_EQ(1, loader->num_loads()); |
| 819 EXPECT_EQ(requestor_url, loader->last_requestor_url()); |
| 820 } |
| 821 |
742 } // namespace | 822 } // namespace |
743 } // namespace shell | 823 } // namespace shell |
744 } // namespace mojo | 824 } // namespace mojo |
OLD | NEW |