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