| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/fetcher/about_fetcher.h" | 5 #include "mojo/shell/fetcher/about_fetcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "mojo/application/public/cpp/application_connection.h" | 17 #include "mojo/application/public/cpp/application_connection.h" |
| 18 #include "mojo/application/public/cpp/application_delegate.h" | 18 #include "mojo/application/public/cpp/application_delegate.h" |
| 19 #include "mojo/application/public/cpp/application_impl.h" | 19 #include "mojo/application/public/cpp/application_impl.h" |
| 20 #include "mojo/application/public/cpp/interface_factory.h" | 20 #include "mojo/application/public/cpp/interface_factory.h" |
| 21 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 21 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
| 22 #include "mojo/common/weak_binding_set.h" | 22 #include "mojo/common/weak_binding_set.h" |
| 23 #include "mojo/shell/application_loader.h" | 23 #include "mojo/shell/application_loader.h" |
| 24 #include "mojo/shell/application_manager.h" | 24 #include "mojo/shell/application_manager.h" |
| 25 #include "mojo/shell/package_manager/package_manager_impl.h" | 25 #include "mojo/shell/package_manager/package_manager_impl.h" |
| 26 #include "mojo/util/filename_util.h" | 26 #include "mojo/util/filename_util.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 namespace mojo { | 29 namespace mojo { |
| 30 namespace fetcher { | 30 namespace shell { |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 class TestContentHandler : public ApplicationDelegate, | 33 class TestContentHandler : public ApplicationDelegate, |
| 34 public InterfaceFactory<ContentHandler>, | 34 public InterfaceFactory<ContentHandler>, |
| 35 public ContentHandler { | 35 public ContentHandler { |
| 36 public: | 36 public: |
| 37 TestContentHandler() : response_number_(0) {} | 37 TestContentHandler() : response_number_(0) {} |
| 38 ~TestContentHandler() override {} | 38 ~TestContentHandler() override {} |
| 39 | 39 |
| 40 size_t response_number() const { return response_number_; } | 40 size_t response_number() const { return response_number_; } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 70 // that StartApplication() has been called. | 70 // that StartApplication() has been called. |
| 71 } | 71 } |
| 72 | 72 |
| 73 size_t response_number_; | 73 size_t response_number_; |
| 74 URLResponsePtr latest_response_; | 74 URLResponsePtr latest_response_; |
| 75 WeakBindingSet<ContentHandler> bindings_; | 75 WeakBindingSet<ContentHandler> bindings_; |
| 76 | 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); | 77 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 class TestLoader : public shell::ApplicationLoader { | 80 class TestLoader : public ApplicationLoader { |
| 81 public: | 81 public: |
| 82 explicit TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {} | 82 explicit TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {} |
| 83 ~TestLoader() override {} | 83 ~TestLoader() override {} |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 // Overridden from ApplicationLoader: | 86 // Overridden from ApplicationLoader: |
| 87 void Load(const GURL& url, InterfaceRequest<Application> request) override { | 87 void Load(const GURL& url, InterfaceRequest<Application> request) override { |
| 88 app_.reset(new ApplicationImpl(delegate_, std::move(request))); | 88 app_.reset(new ApplicationImpl(delegate_, std::move(request))); |
| 89 } | 89 } |
| 90 | 90 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 110 ServiceProviderPtr service_provider; | 110 ServiceProviderPtr service_provider; |
| 111 InterfaceRequest<ServiceProvider> service_provider_request = | 111 InterfaceRequest<ServiceProvider> service_provider_request = |
| 112 GetProxy(&service_provider); | 112 GetProxy(&service_provider); |
| 113 // This connection error handler will be called when: | 113 // This connection error handler will be called when: |
| 114 // - TestContentHandler::StartApplication() has been called (please see | 114 // - TestContentHandler::StartApplication() has been called (please see |
| 115 // comments in that method); or | 115 // comments in that method); or |
| 116 // - the application manager fails to fetch the requested URL. | 116 // - the application manager fails to fetch the requested URL. |
| 117 service_provider.set_connection_error_handler( | 117 service_provider.set_connection_error_handler( |
| 118 [&run_loop]() { run_loop.Quit(); }); | 118 [&run_loop]() { run_loop.Quit(); }); |
| 119 | 119 |
| 120 scoped_ptr<shell::ConnectToApplicationParams> params( | 120 scoped_ptr<ConnectToApplicationParams> params( |
| 121 new shell::ConnectToApplicationParams); | 121 new ConnectToApplicationParams); |
| 122 params->SetTargetURL(GURL(url)); | 122 params->SetTargetURL(GURL(url)); |
| 123 params->set_services(std::move(service_provider_request)); | 123 params->set_services(std::move(service_provider_request)); |
| 124 application_manager_->ConnectToApplication(std::move(params)); | 124 application_manager_->ConnectToApplication(std::move(params)); |
| 125 | 125 |
| 126 run_loop.Run(); | 126 run_loop.Run(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Overridden from testing::Test: | 129 // Overridden from testing::Test: |
| 130 void SetUp() override { | 130 void SetUp() override { |
| 131 base::FilePath shell_dir; | 131 base::FilePath shell_dir; |
| 132 PathService::Get(base::DIR_MODULE, &shell_dir); | 132 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 133 scoped_ptr<shell::PackageManagerImpl> package_manager( | 133 scoped_ptr<PackageManagerImpl> package_manager( |
| 134 new shell::PackageManagerImpl(shell_dir, nullptr)); | 134 new PackageManagerImpl(shell_dir, nullptr)); |
| 135 package_manager->RegisterContentHandler( | 135 package_manager->RegisterContentHandler( |
| 136 "text/html", GURL("test:html_content_handler")); | 136 "text/html", GURL("test:html_content_handler")); |
| 137 application_manager_.reset( | 137 application_manager_.reset( |
| 138 new shell::ApplicationManager(std::move(package_manager))); | 138 new ApplicationManager(std::move(package_manager))); |
| 139 application_manager_->SetLoaderForURL( | 139 application_manager_->SetLoaderForURL( |
| 140 make_scoped_ptr(new TestLoader(&html_content_handler_)), | 140 make_scoped_ptr(new TestLoader(&html_content_handler_)), |
| 141 GURL("test:html_content_handler")); | 141 GURL("test:html_content_handler")); |
| 142 } | 142 } |
| 143 | 143 |
| 144 void TearDown() override { application_manager_.reset(); } | 144 void TearDown() override { application_manager_.reset(); } |
| 145 | 145 |
| 146 private: | 146 private: |
| 147 base::ShadowingAtExitManager at_exit_; | 147 base::ShadowingAtExitManager at_exit_; |
| 148 TestContentHandler html_content_handler_; | 148 TestContentHandler html_content_handler_; |
| 149 base::MessageLoop loop_; | 149 base::MessageLoop loop_; |
| 150 scoped_ptr<shell::ApplicationManager> application_manager_; | 150 scoped_ptr<ApplicationManager> application_manager_; |
| 151 | 151 |
| 152 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest); | 152 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest); |
| 153 }; | 153 }; |
| 154 | 154 |
| 155 TEST_F(AboutFetcherTest, AboutBlank) { | 155 TEST_F(AboutFetcherTest, AboutBlank) { |
| 156 ConnectAndWait("about:blank"); | 156 ConnectAndWait("about:blank"); |
| 157 | 157 |
| 158 ASSERT_EQ(1u, html_content_handler()->response_number()); | 158 ASSERT_EQ(1u, html_content_handler()->response_number()); |
| 159 | 159 |
| 160 const URLResponse* response = html_content_handler()->latest_response(); | 160 const URLResponse* response = html_content_handler()->latest_response(); |
| 161 EXPECT_EQ("about:blank", response->url); | 161 EXPECT_EQ("about:blank", response->url); |
| 162 EXPECT_EQ(200u, response->status_code); | 162 EXPECT_EQ(200u, response->status_code); |
| 163 EXPECT_EQ("text/html", response->mime_type); | 163 EXPECT_EQ("text/html", response->mime_type); |
| 164 EXPECT_FALSE(response->body.is_valid()); | 164 EXPECT_FALSE(response->body.is_valid()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 TEST_F(AboutFetcherTest, UnrecognizedURL) { | 167 TEST_F(AboutFetcherTest, UnrecognizedURL) { |
| 168 ConnectAndWait("about:some_unrecognized_url"); | 168 ConnectAndWait("about:some_unrecognized_url"); |
| 169 | 169 |
| 170 ASSERT_EQ(1u, html_content_handler()->response_number()); | 170 ASSERT_EQ(1u, html_content_handler()->response_number()); |
| 171 | 171 |
| 172 const URLResponse* response = html_content_handler()->latest_response(); | 172 const URLResponse* response = html_content_handler()->latest_response(); |
| 173 EXPECT_EQ("about:some_unrecognized_url", response->url); | 173 EXPECT_EQ("about:some_unrecognized_url", response->url); |
| 174 EXPECT_EQ(404u, response->status_code); | 174 EXPECT_EQ(404u, response->status_code); |
| 175 EXPECT_EQ("text/html", response->mime_type); | 175 EXPECT_EQ("text/html", response->mime_type); |
| 176 EXPECT_FALSE(response->body.is_valid()); | 176 EXPECT_FALSE(response->body.is_valid()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 } // namespace | 179 } // namespace |
| 180 } // namespace fetcher | 180 } // namespace shell |
| 181 } // namespace mojo | 181 } // namespace mojo |
| OLD | NEW |