 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| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/browser/service_worker/service_worker_context.h" | |
| 6 | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "content/browser/browser_thread_impl.h" | |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | |
| 12 #include "content/browser/service_worker/service_worker_registration.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 class ServiceWorkerContextTest : public testing::Test { | |
| 18 public: | |
| 19 ServiceWorkerContextTest() : io_thread_(BrowserThread::IO, &message_loop_) {} | |
| 20 | |
| 21 virtual void SetUp() OVERRIDE { | |
| 22 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | |
| 23 } | |
| 24 | |
| 25 virtual void TearDown() OVERRIDE { | |
| 26 context_->Shutdown(); | |
| 27 context_.reset(); | |
| 28 } | |
| 29 | |
| 30 static ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | |
| 31 bool* called, | |
| 32 int64* store_result) { | |
| 33 return base::Bind(&SaveResponseCallback, called, store_result); | |
| 34 } | |
| 35 | |
| 36 static ServiceWorkerContextCore::UnregistrationCallback | |
| 37 MakeUnregisteredCallback(bool* called) { | |
| 38 return base::Bind(&CallCompletedCallback, called); | |
| 39 } | |
| 
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
 | |
| 40 | |
| 41 protected: | |
| 42 scoped_ptr<ServiceWorkerContextCore> context_; | |
| 43 | |
| 44 static void SaveResponseCallback(bool* called, | |
| 45 int64* store_result, | |
| 46 ServiceWorkerRegistrationStatus status, | |
| 47 int64 result) { | |
| 48 *called = true; | |
| 49 *store_result = result; | |
| 50 } | |
| 51 | |
| 52 static void CallCompletedCallback(bool* called, | |
| 53 ServiceWorkerRegistrationStatus) { | |
| 54 *called = true; | |
| 55 } | |
| 
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!
 | |
| 56 | |
| 57 base::MessageLoopForIO message_loop_; | |
| 58 BrowserThreadImpl io_thread_; | |
| 59 }; | |
| 60 | |
| 61 void RegistrationCallback( | |
| 62 scoped_refptr<ServiceWorkerRegistration>* registration, | |
| 63 const scoped_refptr<ServiceWorkerRegistration>& result) { | |
| 64 *registration = result; | |
| 65 } | |
| 
kinuko
2013/12/04 13:04:29
And this could be in the anonymous namespace too.
 
alecflett
2013/12/06 05:43:33
Done.
 | |
| 66 | |
| 67 // Make sure basic registration is working. | |
| 68 TEST_F(ServiceWorkerContextTest, Register) { | |
| 69 int64 registration_id = -1L; | |
| 70 bool called = false; | |
| 71 context_->RegisterServiceWorker( | |
| 72 GURL("http://www.example.com/*"), | |
| 73 GURL("http://www.example.com/service_worker.js"), | |
| 74 MakeRegisteredCallback(&called, ®istration_id)); | |
| 75 | |
| 76 ASSERT_FALSE(called); | |
| 77 message_loop_.RunUntilIdle(); | |
| 78 ASSERT_TRUE(called); | |
| 79 | |
| 80 ASSERT_NE(-1L, registration_id); | |
| 81 } | |
| 82 | |
| 83 // Make sure registrations are cleaned up when they are unregistered. | |
| 84 TEST_F(ServiceWorkerContextTest, Unregister) { | |
| 85 GURL pattern("http://www.example.com/*"); | |
| 86 | |
| 87 bool called = false; | |
| 88 int64 registration_id = -1L; | |
| 89 context_->RegisterServiceWorker( | |
| 90 pattern, | |
| 91 GURL("http://www.example.com/service_worker.js"), | |
| 92 MakeRegisteredCallback(&called, ®istration_id)); | |
| 93 | |
| 94 ASSERT_FALSE(called); | |
| 95 message_loop_.RunUntilIdle(); | |
| 96 ASSERT_TRUE(called); | |
| 97 | |
| 98 called = false; | |
| 99 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | |
| 100 | |
| 101 ASSERT_FALSE(called); | |
| 102 message_loop_.RunUntilIdle(); | |
| 103 ASSERT_TRUE(called); | |
| 104 } | |
| 105 | |
| 106 // Make sure that when a new registration replaces an existing | |
| 107 // registration, that the old one is cleaned up. | |
| 108 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | |
| 109 GURL pattern("http://www.example.com/*"); | |
| 110 | |
| 111 bool called = false; | |
| 112 int64 old_registration_id = -1L; | |
| 113 context_->RegisterServiceWorker( | |
| 114 pattern, | |
| 115 GURL("http://www.example.com/service_worker.js"), | |
| 116 MakeRegisteredCallback(&called, &old_registration_id)); | |
| 117 | |
| 118 ASSERT_FALSE(called); | |
| 119 message_loop_.RunUntilIdle(); | |
| 120 ASSERT_TRUE(called); | |
| 121 | |
| 122 called = false; | |
| 123 int64 new_registration_id = -1L; | |
| 124 context_->RegisterServiceWorker( | |
| 125 pattern, | |
| 126 GURL("http://www.example.com/service_worker_new.js"), | |
| 127 MakeRegisteredCallback(&called, &new_registration_id)); | |
| 128 | |
| 129 ASSERT_FALSE(called); | |
| 130 message_loop_.RunUntilIdle(); | |
| 131 ASSERT_TRUE(called); | |
| 132 | |
| 133 ASSERT_NE(old_registration_id, new_registration_id); | |
| 134 } | |
| 135 | |
| 136 // Make sure that when registering a duplicate pattern+script_url | |
| 137 // combination, that the same registration is used. | |
| 138 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | |
| 139 GURL pattern("http://www.example.com/*"); | |
| 140 GURL script_url("http://www.example.com/service_worker.js"); | |
| 141 | |
| 142 bool called = false; | |
| 143 int64 old_registration_id = -1L; | |
| 144 context_->RegisterServiceWorker( | |
| 145 pattern, | |
| 146 script_url, | |
| 147 MakeRegisteredCallback(&called, &old_registration_id)); | |
| 148 | |
| 149 ASSERT_FALSE(called); | |
| 150 message_loop_.RunUntilIdle(); | |
| 151 ASSERT_TRUE(called); | |
| 152 | |
| 153 called = false; | |
| 154 int64 new_registration_id = -1L; | |
| 155 context_->RegisterServiceWorker( | |
| 156 pattern, | |
| 157 script_url, | |
| 158 MakeRegisteredCallback(&called, &new_registration_id)); | |
| 159 | |
| 160 ASSERT_FALSE(called); | |
| 161 message_loop_.RunUntilIdle(); | |
| 162 ASSERT_TRUE(called); | |
| 163 | |
| 164 ASSERT_EQ(old_registration_id, new_registration_id); | |
| 165 } | |
| 166 | |
| 167 } // namespace content | |
| OLD | NEW |