OLD | NEW |
1 | 1 |
2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
5 | 5 |
6 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 6 #include "modules/serviceworkers/ServiceWorkerContainer.h" |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 125 |
126 // Service Worker-specific tests. | 126 // Service Worker-specific tests. |
127 | 127 |
128 class NotReachedWebServiceWorkerProvider : public WebServiceWorkerProvider { | 128 class NotReachedWebServiceWorkerProvider : public WebServiceWorkerProvider { |
129 public: | 129 public: |
130 ~NotReachedWebServiceWorkerProvider() override {} | 130 ~NotReachedWebServiceWorkerProvider() override {} |
131 | 131 |
132 void RegisterServiceWorker( | 132 void RegisterServiceWorker( |
133 const WebURL& pattern, | 133 const WebURL& pattern, |
134 const WebURL& script_url, | 134 const WebURL& script_url, |
| 135 WebServiceWorkerUpdateViaCache update_via_cache, |
135 std::unique_ptr<WebServiceWorkerRegistrationCallbacks> callbacks) | 136 std::unique_ptr<WebServiceWorkerRegistrationCallbacks> callbacks) |
136 override { | 137 override { |
137 ADD_FAILURE() | 138 ADD_FAILURE() |
138 << "the provider should not be called to register a Service Worker"; | 139 << "the provider should not be called to register a Service Worker"; |
139 } | 140 } |
140 | 141 |
141 bool ValidateScopeAndScriptURL(const WebURL& scope, | 142 bool ValidateScopeAndScriptURL(const WebURL& scope, |
142 const WebURL& script_url, | 143 const WebURL& script_url, |
143 WebString* error_message) { | 144 WebString* error_message) { |
144 return true; | 145 return true; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 // StubWebServiceWorkerProvider dies. | 277 // StubWebServiceWorkerProvider dies. |
277 std::unique_ptr<WebServiceWorkerProvider> Provider() { | 278 std::unique_ptr<WebServiceWorkerProvider> Provider() { |
278 return WTF::WrapUnique(new WebServiceWorkerProviderImpl(*this)); | 279 return WTF::WrapUnique(new WebServiceWorkerProviderImpl(*this)); |
279 } | 280 } |
280 | 281 |
281 size_t RegisterCallCount() { return register_call_count_; } | 282 size_t RegisterCallCount() { return register_call_count_; } |
282 const WebURL& RegisterScope() { return register_scope_; } | 283 const WebURL& RegisterScope() { return register_scope_; } |
283 const WebURL& RegisterScriptURL() { return register_script_url_; } | 284 const WebURL& RegisterScriptURL() { return register_script_url_; } |
284 size_t GetRegistrationCallCount() { return get_registration_call_count_; } | 285 size_t GetRegistrationCallCount() { return get_registration_call_count_; } |
285 const WebURL& GetRegistrationURL() { return get_registration_url_; } | 286 const WebURL& GetRegistrationURL() { return get_registration_url_; } |
| 287 WebServiceWorkerUpdateViaCache UpdateViaCache() const { |
| 288 return update_via_cache_; |
| 289 } |
286 | 290 |
287 private: | 291 private: |
288 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { | 292 class WebServiceWorkerProviderImpl : public WebServiceWorkerProvider { |
289 public: | 293 public: |
290 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) | 294 WebServiceWorkerProviderImpl(StubWebServiceWorkerProvider& owner) |
291 : owner_(owner) {} | 295 : owner_(owner) {} |
292 | 296 |
293 ~WebServiceWorkerProviderImpl() override {} | 297 ~WebServiceWorkerProviderImpl() override {} |
294 | 298 |
295 void RegisterServiceWorker( | 299 void RegisterServiceWorker( |
296 const WebURL& pattern, | 300 const WebURL& pattern, |
297 const WebURL& script_url, | 301 const WebURL& script_url, |
| 302 WebServiceWorkerUpdateViaCache update_via_cache, |
298 std::unique_ptr<WebServiceWorkerRegistrationCallbacks> callbacks) | 303 std::unique_ptr<WebServiceWorkerRegistrationCallbacks> callbacks) |
299 override { | 304 override { |
300 owner_.register_call_count_++; | 305 owner_.register_call_count_++; |
301 owner_.register_scope_ = pattern; | 306 owner_.register_scope_ = pattern; |
302 owner_.register_script_url_ = script_url; | 307 owner_.register_script_url_ = script_url; |
| 308 owner_.update_via_cache_ = update_via_cache; |
303 registration_callbacks_to_delete_.push_back(std::move(callbacks)); | 309 registration_callbacks_to_delete_.push_back(std::move(callbacks)); |
304 } | 310 } |
305 | 311 |
306 void GetRegistration( | 312 void GetRegistration( |
307 const WebURL& document_url, | 313 const WebURL& document_url, |
308 std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks> callbacks) | 314 std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks> callbacks) |
309 override { | 315 override { |
310 owner_.get_registration_call_count_++; | 316 owner_.get_registration_call_count_++; |
311 owner_.get_registration_url_ = document_url; | 317 owner_.get_registration_url_ = document_url; |
312 get_registration_callbacks_to_delete_.push_back(std::move(callbacks)); | 318 get_registration_callbacks_to_delete_.push_back(std::move(callbacks)); |
(...skipping 12 matching lines...) Expand all Loading... |
325 Vector<std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks>> | 331 Vector<std::unique_ptr<WebServiceWorkerGetRegistrationCallbacks>> |
326 get_registration_callbacks_to_delete_; | 332 get_registration_callbacks_to_delete_; |
327 }; | 333 }; |
328 | 334 |
329 private: | 335 private: |
330 size_t register_call_count_; | 336 size_t register_call_count_; |
331 WebURL register_scope_; | 337 WebURL register_scope_; |
332 WebURL register_script_url_; | 338 WebURL register_script_url_; |
333 size_t get_registration_call_count_; | 339 size_t get_registration_call_count_; |
334 WebURL get_registration_url_; | 340 WebURL get_registration_url_; |
| 341 WebServiceWorkerUpdateViaCache update_via_cache_; |
335 }; | 342 }; |
336 | 343 |
337 TEST_F(ServiceWorkerContainerTest, | 344 TEST_F(ServiceWorkerContainerTest, |
338 RegisterUnregister_NonHttpsSecureOriginDelegatesToProvider) { | 345 RegisterUnregister_NonHttpsSecureOriginDelegatesToProvider) { |
339 SetPageURL("http://localhost/x/index.html"); | 346 SetPageURL("http://localhost/x/index.html"); |
340 | 347 |
341 StubWebServiceWorkerProvider stub_provider; | 348 StubWebServiceWorkerProvider stub_provider; |
342 Provide(stub_provider.Provider()); | 349 Provide(stub_provider.Provider()); |
343 | 350 |
344 ServiceWorkerContainer* container = ServiceWorkerContainer::Create( | 351 ServiceWorkerContainer* container = ServiceWorkerContainer::Create( |
345 GetExecutionContext(), GetNavigatorServiceWorker()); | 352 GetExecutionContext(), GetNavigatorServiceWorker()); |
346 | 353 |
347 // register | 354 // register |
348 { | 355 { |
349 ScriptState::Scope script_scope(GetScriptState()); | 356 ScriptState::Scope script_scope(GetScriptState()); |
350 RegistrationOptions options; | 357 RegistrationOptions options; |
351 options.setScope("y/"); | 358 options.setScope("y/"); |
352 container->registerServiceWorker(GetScriptState(), "/x/y/worker.js", | 359 container->registerServiceWorker(GetScriptState(), "/x/y/worker.js", |
353 options); | 360 options); |
354 | 361 |
355 EXPECT_EQ(1ul, stub_provider.RegisterCallCount()); | 362 EXPECT_EQ(1ul, stub_provider.RegisterCallCount()); |
356 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/y/")), | 363 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/y/")), |
357 stub_provider.RegisterScope()); | 364 stub_provider.RegisterScope()); |
358 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/y/worker.js")), | 365 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/y/worker.js")), |
359 stub_provider.RegisterScriptURL()); | 366 stub_provider.RegisterScriptURL()); |
| 367 EXPECT_EQ(WebServiceWorkerUpdateViaCache::kImports, |
| 368 stub_provider.UpdateViaCache()); |
360 } | 369 } |
361 } | 370 } |
362 | 371 |
363 TEST_F(ServiceWorkerContainerTest, | 372 TEST_F(ServiceWorkerContainerTest, |
364 GetRegistration_OmittedDocumentURLDefaultsToPageURL) { | 373 GetRegistration_OmittedDocumentURLDefaultsToPageURL) { |
365 SetPageURL("http://localhost/x/index.html"); | 374 SetPageURL("http://localhost/x/index.html"); |
366 | 375 |
367 StubWebServiceWorkerProvider stub_provider; | 376 StubWebServiceWorkerProvider stub_provider; |
368 Provide(stub_provider.Provider()); | 377 Provide(stub_provider.Provider()); |
369 | 378 |
370 ServiceWorkerContainer* container = ServiceWorkerContainer::Create( | 379 ServiceWorkerContainer* container = ServiceWorkerContainer::Create( |
371 GetExecutionContext(), GetNavigatorServiceWorker()); | 380 GetExecutionContext(), GetNavigatorServiceWorker()); |
372 | 381 |
373 { | 382 { |
374 ScriptState::Scope script_scope(GetScriptState()); | 383 ScriptState::Scope script_scope(GetScriptState()); |
375 container->getRegistration(GetScriptState(), ""); | 384 container->getRegistration(GetScriptState(), ""); |
376 EXPECT_EQ(1ul, stub_provider.GetRegistrationCallCount()); | 385 EXPECT_EQ(1ul, stub_provider.GetRegistrationCallCount()); |
377 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/index.html")), | 386 EXPECT_EQ(WebURL(KURL(NullURL(), "http://localhost/x/index.html")), |
378 stub_provider.GetRegistrationURL()); | 387 stub_provider.GetRegistrationURL()); |
| 388 EXPECT_EQ(WebServiceWorkerUpdateViaCache::kImports, |
| 389 stub_provider.UpdateViaCache()); |
379 } | 390 } |
380 } | 391 } |
381 | 392 |
| 393 TEST_F(ServiceWorkerContainerTest, |
| 394 RegisterUnregister_UpdateViaCacheOptionDelegatesToProvider) { |
| 395 SetPageURL("http://localhost/x/index.html"); |
| 396 |
| 397 StubWebServiceWorkerProvider stub_provider; |
| 398 Provide(stub_provider.Provider()); |
| 399 |
| 400 ServiceWorkerContainer* container = ServiceWorkerContainer::Create( |
| 401 GetExecutionContext(), GetNavigatorServiceWorker()); |
| 402 |
| 403 // register |
| 404 { |
| 405 ScriptState::Scope script_scope(GetScriptState()); |
| 406 RegistrationOptions options; |
| 407 options.setUpdateViaCache("none"); |
| 408 container->registerServiceWorker(GetScriptState(), "/x/y/worker.js", |
| 409 options); |
| 410 |
| 411 EXPECT_EQ(1ul, stub_provider.RegisterCallCount()); |
| 412 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/y/")), |
| 413 stub_provider.RegisterScope()); |
| 414 EXPECT_EQ(WebURL(KURL(KURL(), "http://localhost/x/y/worker.js")), |
| 415 stub_provider.RegisterScriptURL()); |
| 416 EXPECT_EQ(WebServiceWorkerUpdateViaCache::kNone, |
| 417 stub_provider.UpdateViaCache()); |
| 418 } |
| 419 } |
| 420 |
382 } // namespace | 421 } // namespace |
383 } // namespace blink | 422 } // namespace blink |
OLD | NEW |