 Chromium Code Reviews
 Chromium Code Reviews Issue 62203007:
  Implement memory-persistent registration  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 62203007:
  Implement memory-persistent registration  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: content/browser/service_worker/service_worker_context_unittest.cc | 
| diff --git a/content/browser/service_worker/service_worker_context_unittest.cc b/content/browser/service_worker/service_worker_context_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..938d0da65ad6adb868b3e8794ddf88143a870f7d | 
| --- /dev/null | 
| +++ b/content/browser/service_worker/service_worker_context_unittest.cc | 
| @@ -0,0 +1,167 @@ | 
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "content/browser/service_worker/service_worker_context.h" | 
| + | 
| +#include "base/files/scoped_temp_dir.h" | 
| +#include "base/logging.h" | 
| +#include "base/message_loop/message_loop.h" | 
| +#include "content/browser/browser_thread_impl.h" | 
| +#include "content/browser/service_worker/service_worker_context_core.h" | 
| +#include "content/browser/service_worker/service_worker_registration.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace content { | 
| + | 
| +class ServiceWorkerContextTest : public testing::Test { | 
| + public: | 
| + ServiceWorkerContextTest() : io_thread_(BrowserThread::IO, &message_loop_) {} | 
| + | 
| + virtual void SetUp() OVERRIDE { | 
| + context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | 
| + } | 
| + | 
| + virtual void TearDown() OVERRIDE { | 
| + context_->Shutdown(); | 
| + context_.reset(); | 
| + } | 
| + | 
| + static ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 
| + bool* called, | 
| + int64* store_result) { | 
| + return base::Bind(&SaveResponseCallback, called, store_result); | 
| + } | 
| + | 
| + static ServiceWorkerContextCore::UnregistrationCallback | 
| + MakeUnregisteredCallback(bool* called) { | 
| + return base::Bind(&CallCompletedCallback, called); | 
| + } | 
| 
kinuko
2013/12/04 13:04:29
Do we need these static methods?  Just calling bas
 
alecflett
2013/12/06 05:43:33
I found these helpful both for readability but als
 | 
| + | 
| + protected: | 
| + scoped_ptr<ServiceWorkerContextCore> context_; | 
| + | 
| + static void SaveResponseCallback(bool* called, | 
| + int64* store_result, | 
| + ServiceWorkerRegistrationStatus status, | 
| + int64 result) { | 
| + *called = true; | 
| + *store_result = result; | 
| + } | 
| + | 
| + static void CallCompletedCallback(bool* called, | 
| + ServiceWorkerRegistrationStatus) { | 
| + *called = true; | 
| + } | 
| 
kinuko
2013/12/04 13:04:29
All these static methods can be placed outside thi
 
alecflett
2013/12/06 05:43:33
oops, I thought I had done that.. apparently not!
 | 
| + | 
| + base::MessageLoopForIO message_loop_; | 
| + BrowserThreadImpl io_thread_; | 
| +}; | 
| + | 
| +void RegistrationCallback( | 
| + scoped_refptr<ServiceWorkerRegistration>* registration, | 
| + const scoped_refptr<ServiceWorkerRegistration>& result) { | 
| + *registration = result; | 
| +} | 
| 
kinuko
2013/12/04 13:04:29
And this could be in the anonymous namespace too.
 
alecflett
2013/12/06 05:43:33
Done.
 | 
| + | 
| +// Make sure basic registration is working. | 
| +TEST_F(ServiceWorkerContextTest, Register) { | 
| + int64 registration_id = -1L; | 
| + bool called = false; | 
| + context_->RegisterServiceWorker( | 
| + GURL("http://www.example.com/*"), | 
| + GURL("http://www.example.com/service_worker.js"), | 
| + MakeRegisteredCallback(&called, ®istration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + ASSERT_NE(-1L, registration_id); | 
| +} | 
| + | 
| +// Make sure registrations are cleaned up when they are unregistered. | 
| +TEST_F(ServiceWorkerContextTest, Unregister) { | 
| + GURL pattern("http://www.example.com/*"); | 
| + | 
| + bool called = false; | 
| + int64 registration_id = -1L; | 
| + context_->RegisterServiceWorker( | 
| + pattern, | 
| + GURL("http://www.example.com/service_worker.js"), | 
| + MakeRegisteredCallback(&called, ®istration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + called = false; | 
| + context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| +} | 
| + | 
| +// Make sure that when a new registration replaces an existing | 
| +// registration, that the old one is cleaned up. | 
| +TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 
| + GURL pattern("http://www.example.com/*"); | 
| + | 
| + bool called = false; | 
| + int64 old_registration_id = -1L; | 
| + context_->RegisterServiceWorker( | 
| + pattern, | 
| + GURL("http://www.example.com/service_worker.js"), | 
| + MakeRegisteredCallback(&called, &old_registration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + called = false; | 
| + int64 new_registration_id = -1L; | 
| + context_->RegisterServiceWorker( | 
| + pattern, | 
| + GURL("http://www.example.com/service_worker_new.js"), | 
| + MakeRegisteredCallback(&called, &new_registration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + ASSERT_NE(old_registration_id, new_registration_id); | 
| +} | 
| + | 
| +// Make sure that when registering a duplicate pattern+script_url | 
| +// combination, that the same registration is used. | 
| +TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 
| + GURL pattern("http://www.example.com/*"); | 
| + GURL script_url("http://www.example.com/service_worker.js"); | 
| + | 
| + bool called = false; | 
| + int64 old_registration_id = -1L; | 
| + context_->RegisterServiceWorker( | 
| + pattern, | 
| + script_url, | 
| + MakeRegisteredCallback(&called, &old_registration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + called = false; | 
| + int64 new_registration_id = -1L; | 
| + context_->RegisterServiceWorker( | 
| + pattern, | 
| + script_url, | 
| + MakeRegisteredCallback(&called, &new_registration_id)); | 
| + | 
| + ASSERT_FALSE(called); | 
| + message_loop_.RunUntilIdle(); | 
| + ASSERT_TRUE(called); | 
| + | 
| + ASSERT_EQ(old_registration_id, new_registration_id); | 
| +} | 
| + | 
| +} // namespace content |