| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/at_exit.h" | |
| 6 #include "base/macros.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "mojo/application/public/cpp/application_connection.h" | |
| 12 #include "mojo/application/public/cpp/application_delegate.h" | |
| 13 #include "mojo/application/public/cpp/application_impl.h" | |
| 14 #include "mojo/application/public/cpp/interface_factory.h" | |
| 15 #include "mojo/application/public/interfaces/content_handler.mojom.h" | |
| 16 #include "mojo/common/weak_binding_set.h" | |
| 17 #include "mojo/fetcher/about_fetcher.h" | |
| 18 #include "mojo/fetcher/base_application_fetcher.h" | |
| 19 #include "mojo/runner/context.h" | |
| 20 #include "mojo/shell/application_loader.h" | |
| 21 #include "mojo/shell/application_manager.h" | |
| 22 #include "mojo/util/filename_util.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 namespace mojo { | |
| 26 namespace fetcher { | |
| 27 namespace { | |
| 28 | |
| 29 class TestContentHandler : public ApplicationDelegate, | |
| 30 public InterfaceFactory<ContentHandler>, | |
| 31 public ContentHandler { | |
| 32 public: | |
| 33 TestContentHandler() : response_number_(0) {} | |
| 34 ~TestContentHandler() override {} | |
| 35 | |
| 36 size_t response_number() const { return response_number_; } | |
| 37 const URLResponse* latest_response() const { return latest_response_.get(); } | |
| 38 | |
| 39 private: | |
| 40 // Overridden from ApplicationDelegate: | |
| 41 void Initialize(ApplicationImpl* app) override {} | |
| 42 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
| 43 connection->AddService<ContentHandler>(this); | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 // Overridden from InterfaceFactory<ContentHandler>: | |
| 48 void Create(ApplicationConnection* connection, | |
| 49 InterfaceRequest<ContentHandler> request) override { | |
| 50 bindings_.AddBinding(this, request.Pass()); | |
| 51 } | |
| 52 | |
| 53 // Overridden from ContentHandler: | |
| 54 void StartApplication(InterfaceRequest<Application> application, | |
| 55 URLResponsePtr response) override { | |
| 56 response_number_++; | |
| 57 latest_response_ = response.Pass(); | |
| 58 | |
| 59 // Drop |application| request. This results in the application manager | |
| 60 // dropping the ServiceProvider interface request provided by the client | |
| 61 // who made the ConnectToApplication() call. Therefore the client could | |
| 62 // listen for connection error of the ServiceProvider interface to learn | |
| 63 // that StartApplication() has been called. | |
| 64 } | |
| 65 | |
| 66 size_t response_number_; | |
| 67 URLResponsePtr latest_response_; | |
| 68 WeakBindingSet<ContentHandler> bindings_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); | |
| 71 }; | |
| 72 | |
| 73 class TestLoader : public shell::ApplicationLoader { | |
| 74 public: | |
| 75 explicit TestLoader(ApplicationDelegate* delegate) : delegate_(delegate) {} | |
| 76 ~TestLoader() override {} | |
| 77 | |
| 78 private: | |
| 79 // Overridden from ApplicationLoader: | |
| 80 void Load(const GURL& url, InterfaceRequest<Application> request) override { | |
| 81 app_.reset(new ApplicationImpl(delegate_, request.Pass())); | |
| 82 } | |
| 83 | |
| 84 ApplicationDelegate* delegate_; | |
| 85 scoped_ptr<ApplicationImpl> app_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(TestLoader); | |
| 88 }; | |
| 89 | |
| 90 class AboutFetcherTest : public testing::Test { | |
| 91 public: | |
| 92 AboutFetcherTest() {} | |
| 93 ~AboutFetcherTest() override {} | |
| 94 | |
| 95 protected: | |
| 96 const TestContentHandler* html_content_handler() const { | |
| 97 return &html_content_handler_; | |
| 98 } | |
| 99 | |
| 100 void ConnectAndWait(const std::string& url) { | |
| 101 base::RunLoop run_loop; | |
| 102 | |
| 103 ServiceProviderPtr service_provider; | |
| 104 InterfaceRequest<ServiceProvider> service_provider_request = | |
| 105 GetProxy(&service_provider); | |
| 106 // This connection error handler will be called when: | |
| 107 // - TestContentHandler::StartApplication() has been called (please see | |
| 108 // comments in that method); or | |
| 109 // - the application manager fails to fetch the requested URL. | |
| 110 service_provider.set_connection_error_handler( | |
| 111 [&run_loop]() { run_loop.Quit(); }); | |
| 112 | |
| 113 URLRequestPtr request(URLRequest::New()); | |
| 114 request->url = url; | |
| 115 application_manager_->ConnectToApplication( | |
| 116 nullptr, request.Pass(), std::string(), service_provider_request.Pass(), | |
| 117 nullptr, shell::CapabilityFilter(), base::Closure(), | |
| 118 shell::EmptyConnectCallback()); | |
| 119 | |
| 120 run_loop.Run(); | |
| 121 } | |
| 122 | |
| 123 // Overridden from testing::Test: | |
| 124 void SetUp() override { | |
| 125 runner::Context::EnsureEmbedderIsInitialized(); | |
| 126 base::FilePath shell_dir; | |
| 127 PathService::Get(base::DIR_MODULE, &shell_dir); | |
| 128 application_manager_.reset(new shell::ApplicationManager( | |
| 129 make_scoped_ptr(new BaseApplicationFetcher(shell_dir)))); | |
| 130 application_manager_->SetLoaderForURL( | |
| 131 make_scoped_ptr(new TestLoader(&html_content_handler_)), | |
| 132 GURL("test:html_content_handler")); | |
| 133 application_manager_->RegisterContentHandler( | |
| 134 "text/html", GURL("test:html_content_handler")); | |
| 135 } | |
| 136 | |
| 137 void TearDown() override { application_manager_.reset(); } | |
| 138 | |
| 139 private: | |
| 140 base::ShadowingAtExitManager at_exit_; | |
| 141 TestContentHandler html_content_handler_; | |
| 142 base::MessageLoop loop_; | |
| 143 scoped_ptr<shell::ApplicationManager> application_manager_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(AboutFetcherTest); | |
| 146 }; | |
| 147 | |
| 148 TEST_F(AboutFetcherTest, AboutBlank) { | |
| 149 ConnectAndWait("about:blank"); | |
| 150 | |
| 151 ASSERT_EQ(1u, html_content_handler()->response_number()); | |
| 152 | |
| 153 const URLResponse* response = html_content_handler()->latest_response(); | |
| 154 EXPECT_EQ("about:blank", response->url); | |
| 155 EXPECT_EQ(200u, response->status_code); | |
| 156 EXPECT_EQ("text/html", response->mime_type); | |
| 157 EXPECT_FALSE(response->body.is_valid()); | |
| 158 } | |
| 159 | |
| 160 TEST_F(AboutFetcherTest, UnrecognizedURL) { | |
| 161 ConnectAndWait("about:some_unrecognized_url"); | |
| 162 | |
| 163 ASSERT_EQ(0u, html_content_handler()->response_number()); | |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| 167 } // namespace fetcher | |
| 168 } // namespace mojo | |
| OLD | NEW |