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/bind.h" |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "mojo/application/public/cpp/application_connection.h" |
| 13 #include "mojo/application/public/cpp/application_delegate.h" |
| 14 #include "mojo/application/public/cpp/application_impl.h" |
| 15 #include "mojo/application/public/cpp/interface_factory.h" |
| 16 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
| 17 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
| 18 #include "mojo/package_manager/package_manager_impl.h" |
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 20 #include "mojo/shell/application_loader.h" |
| 21 #include "mojo/shell/application_manager.h" |
| 22 #include "mojo/shell/connect_util.h" |
| 23 #include "mojo/shell/fetcher.h" |
| 24 #include "mojo/shell/test_package_manager.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 namespace mojo { |
| 28 namespace package_manager { |
| 29 namespace test { |
| 30 namespace { |
| 31 |
| 32 const char kTestMimeType[] = "test/mime-type"; |
| 33 |
| 34 class TestFetcher : public shell::Fetcher { |
| 35 public: |
| 36 TestFetcher(const FetchCallback& fetch_callback, |
| 37 const GURL& url, |
| 38 const std::string& mime_type) |
| 39 : Fetcher(fetch_callback), url_(url), mime_type_(mime_type) { |
| 40 loader_callback_.Run(make_scoped_ptr(this)); |
| 41 } |
| 42 ~TestFetcher() override {} |
| 43 |
| 44 // Fetcher: |
| 45 const GURL& GetURL() const override { return url_; } |
| 46 GURL GetRedirectURL() const override { return GURL("yyy"); } |
| 47 GURL GetRedirectReferer() const override { return GURL(); } |
| 48 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, |
| 49 uint32_t skip) override { |
| 50 return URLResponse::New().Pass(); |
| 51 } |
| 52 void AsPath( |
| 53 base::TaskRunner* task_runner, |
| 54 base::Callback<void(const base::FilePath&, bool)> callback) override {} |
| 55 std::string MimeType() override { return mime_type_; } |
| 56 bool HasMojoMagic() override { return false; } |
| 57 bool PeekFirstLine(std::string* line) override { return false; } |
| 58 |
| 59 private: |
| 60 const GURL url_; |
| 61 const std::string mime_type_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(TestFetcher); |
| 64 }; |
| 65 |
| 66 void QuitClosure(bool* value) { |
| 67 *value = true; |
| 68 base::MessageLoop::current()->QuitWhenIdle(); |
| 69 } |
| 70 |
| 71 class TestContentHandler : public ContentHandler, public ApplicationDelegate { |
| 72 public: |
| 73 TestContentHandler(ApplicationConnection* connection, |
| 74 InterfaceRequest<ContentHandler> request) |
| 75 : binding_(this, request.Pass()) {} |
| 76 |
| 77 // ContentHandler: |
| 78 void StartApplication(InterfaceRequest<Application> application_request, |
| 79 URLResponsePtr response) override { |
| 80 apps_.push_back(new ApplicationImpl(this, application_request.Pass())); |
| 81 } |
| 82 |
| 83 private: |
| 84 StrongBinding<ContentHandler> binding_; |
| 85 ScopedVector<ApplicationImpl> apps_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(TestContentHandler); |
| 88 }; |
| 89 |
| 90 class TestApplicationLoader : public shell::ApplicationLoader, |
| 91 public ApplicationDelegate, |
| 92 public InterfaceFactory<ContentHandler> { |
| 93 public: |
| 94 TestApplicationLoader() : num_loads_(0) {} |
| 95 ~TestApplicationLoader() override {} |
| 96 |
| 97 int num_loads() const { return num_loads_; } |
| 98 const GURL& last_requestor_url() const { return last_requestor_url_; } |
| 99 |
| 100 private: |
| 101 // ApplicationLoader implementation. |
| 102 void Load(const GURL& url, |
| 103 InterfaceRequest<Application> application_request) override { |
| 104 ++num_loads_; |
| 105 test_app_.reset(new ApplicationImpl(this, application_request.Pass())); |
| 106 } |
| 107 |
| 108 // ApplicationDelegate implementation. |
| 109 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 110 connection->AddService<ContentHandler>(this); |
| 111 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); |
| 112 return true; |
| 113 } |
| 114 // InterfaceFactory<ContentHandler> implementation. |
| 115 void Create(ApplicationConnection* connection, |
| 116 InterfaceRequest<ContentHandler> request) override { |
| 117 new TestContentHandler(connection, request.Pass()); |
| 118 } |
| 119 |
| 120 scoped_ptr<ApplicationImpl> test_app_; |
| 121 int num_loads_; |
| 122 GURL last_requestor_url_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
| 125 }; |
| 126 |
| 127 class TestPackageManager : public PackageManagerImpl { |
| 128 public: |
| 129 explicit TestPackageManager(const base::FilePath& package_path) |
| 130 : PackageManagerImpl(package_path, nullptr), |
| 131 mime_type_(kTestMimeType) {} |
| 132 ~TestPackageManager() override {} |
| 133 |
| 134 void set_mime_type(const std::string& mime_type) { |
| 135 mime_type_ = mime_type; |
| 136 } |
| 137 |
| 138 // PackageManagerImpl: |
| 139 void FetchRequest( |
| 140 URLRequestPtr request, |
| 141 const shell::Fetcher::FetchCallback& loader_callback) override { |
| 142 new TestFetcher(loader_callback, GURL(request->url), mime_type_); |
| 143 } |
| 144 |
| 145 private: |
| 146 std::string mime_type_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(TestPackageManager); |
| 149 }; |
| 150 |
| 151 } // namespace |
| 152 |
| 153 class ContentHandlerTest : public testing::Test { |
| 154 public: |
| 155 ContentHandlerTest() |
| 156 : content_handler_url_("http://test.content.handler"), |
| 157 requestor_url_("http://requestor.url") {} |
| 158 ~ContentHandlerTest() override {} |
| 159 |
| 160 void SetUp() override { |
| 161 base::FilePath shell_dir; |
| 162 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 163 test_package_manager_ = new TestPackageManager(shell_dir); |
| 164 test_package_manager_->RegisterContentHandler(kTestMimeType, |
| 165 content_handler_url_); |
| 166 application_manager_.reset(new shell::ApplicationManager( |
| 167 make_scoped_ptr(test_package_manager_))); |
| 168 } |
| 169 |
| 170 void TearDown() override { |
| 171 test_package_manager_ = nullptr; |
| 172 application_manager_.reset(); |
| 173 } |
| 174 |
| 175 protected: |
| 176 const GURL content_handler_url_; |
| 177 const GURL requestor_url_; |
| 178 |
| 179 base::MessageLoop loop_; |
| 180 scoped_ptr<shell::ApplicationManager> application_manager_; |
| 181 // Owned by ApplicationManager. |
| 182 TestPackageManager* test_package_manager_; |
| 183 |
| 184 DISALLOW_COPY_AND_ASSIGN(ContentHandlerTest); |
| 185 }; |
| 186 |
| 187 TEST_F(ContentHandlerTest, ContentHandlerConnectionGetsRequestorURL) { |
| 188 TestApplicationLoader* loader = new TestApplicationLoader; |
| 189 application_manager_->SetLoaderForURL( |
| 190 scoped_ptr<shell::ApplicationLoader>(loader), |
| 191 content_handler_url_); |
| 192 |
| 193 bool called = false; |
| 194 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 195 new shell::ConnectToApplicationParams); |
| 196 params->set_source(shell::Identity(requestor_url_)); |
| 197 params->SetTargetURL(GURL("test:test")); |
| 198 params->set_on_application_end( |
| 199 base::Bind(&QuitClosure, base::Unretained(&called))); |
| 200 application_manager_->ConnectToApplication(params.Pass()); |
| 201 loop_.Run(); |
| 202 EXPECT_TRUE(called); |
| 203 |
| 204 ASSERT_EQ(1, loader->num_loads()); |
| 205 EXPECT_EQ(requestor_url_, loader->last_requestor_url()); |
| 206 } |
| 207 |
| 208 TEST_F(ContentHandlerTest, |
| 209 MultipleConnectionsToContentHandlerGetSameContentHandlerId) { |
| 210 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
| 211 application_manager_->SetLoaderForURL( |
| 212 scoped_ptr<shell::ApplicationLoader>(content_handler_loader), |
| 213 content_handler_url_); |
| 214 |
| 215 uint32_t content_handler_id; |
| 216 { |
| 217 base::RunLoop run_loop; |
| 218 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 219 new shell::ConnectToApplicationParams); |
| 220 params->set_source(shell::Identity(requestor_url_)); |
| 221 params->SetTargetURL(GURL("test:test")); |
| 222 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { |
| 223 content_handler_id = t; |
| 224 run_loop.Quit(); |
| 225 }); |
| 226 application_manager_->ConnectToApplication(params.Pass()); |
| 227 run_loop.Run(); |
| 228 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); |
| 229 } |
| 230 |
| 231 uint32_t content_handler_id2; |
| 232 { |
| 233 base::RunLoop run_loop; |
| 234 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 235 new shell::ConnectToApplicationParams); |
| 236 params->set_source(shell::Identity(requestor_url_)); |
| 237 params->SetTargetURL(GURL("test:test")); |
| 238 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { |
| 239 content_handler_id2 = t; |
| 240 run_loop.Quit(); |
| 241 }); |
| 242 application_manager_->ConnectToApplication(params.Pass()); |
| 243 run_loop.Run(); |
| 244 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); |
| 245 } |
| 246 EXPECT_EQ(content_handler_id, content_handler_id2); |
| 247 } |
| 248 |
| 249 TEST_F(ContentHandlerTest, DifferedContentHandlersGetDifferentIDs) { |
| 250 TestApplicationLoader* content_handler_loader = new TestApplicationLoader; |
| 251 application_manager_->SetLoaderForURL( |
| 252 scoped_ptr<shell::ApplicationLoader>(content_handler_loader), |
| 253 content_handler_url_); |
| 254 |
| 255 uint32_t content_handler_id; |
| 256 { |
| 257 base::RunLoop run_loop; |
| 258 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 259 new shell::ConnectToApplicationParams); |
| 260 params->set_source(shell::Identity(requestor_url_)); |
| 261 params->SetTargetURL(GURL("test:test")); |
| 262 params->set_connect_callback([&content_handler_id, &run_loop](uint32_t t) { |
| 263 content_handler_id = t; |
| 264 run_loop.Quit(); |
| 265 }); |
| 266 application_manager_->ConnectToApplication(params.Pass()); |
| 267 run_loop.Run(); |
| 268 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id); |
| 269 } |
| 270 |
| 271 const std::string mime_type2 = "test/mime-type2"; |
| 272 const GURL content_handler_url2("http://test.content.handler2"); |
| 273 test_package_manager_->set_mime_type(mime_type2); |
| 274 test_package_manager_->RegisterContentHandler(mime_type2, |
| 275 content_handler_url2); |
| 276 |
| 277 TestApplicationLoader* content_handler_loader2 = new TestApplicationLoader; |
| 278 application_manager_->SetLoaderForURL( |
| 279 scoped_ptr<shell::ApplicationLoader>(content_handler_loader2), |
| 280 content_handler_url2); |
| 281 |
| 282 uint32_t content_handler_id2; |
| 283 { |
| 284 base::RunLoop run_loop; |
| 285 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 286 new shell::ConnectToApplicationParams); |
| 287 params->set_source(shell::Identity(requestor_url_)); |
| 288 params->SetTargetURL(GURL("test2:test2")); |
| 289 params->set_connect_callback([&content_handler_id2, &run_loop](uint32_t t) { |
| 290 content_handler_id2 = t; |
| 291 run_loop.Quit(); |
| 292 }); |
| 293 application_manager_->ConnectToApplication(params.Pass()); |
| 294 run_loop.Run(); |
| 295 EXPECT_NE(Shell::kInvalidContentHandlerID, content_handler_id2); |
| 296 } |
| 297 EXPECT_NE(content_handler_id, content_handler_id2); |
| 298 } |
| 299 |
| 300 TEST_F(ContentHandlerTest, |
| 301 ConnectWithNoContentHandlerGetsInvalidContentHandlerId) { |
| 302 application_manager_->SetLoaderForURL( |
| 303 scoped_ptr<shell::ApplicationLoader>(new TestApplicationLoader), |
| 304 GURL("test:test")); |
| 305 |
| 306 uint32_t content_handler_id = 1u; |
| 307 scoped_ptr<shell::ConnectToApplicationParams> params( |
| 308 new shell::ConnectToApplicationParams); |
| 309 params->SetTargetURL(GURL("test:test")); |
| 310 params->set_connect_callback( |
| 311 [&content_handler_id](uint32_t t) { content_handler_id = t; }); |
| 312 application_manager_->ConnectToApplication(params.Pass()); |
| 313 EXPECT_EQ(0u, content_handler_id); |
| 314 } |
| 315 |
| 316 } // namespace test |
| 317 } // namespace package_manager |
| 318 } // namespace mojo |
OLD | NEW |