 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_storage.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_registration.h" | |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | |
| 13 #include "content/public/test/test_utils.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 void SaveRegistrationCallback( | |
| 20 ServiceWorkerRegistrationStatus expected_status, | |
| 21 bool* called, | |
| 22 scoped_refptr<ServiceWorkerRegistration>* registration, | |
| 23 ServiceWorkerRegistrationStatus status, | |
| 24 const scoped_refptr<ServiceWorkerRegistration>& result) { | |
| 25 EXPECT_EQ(expected_status, status); | |
| 26 *called = true; | |
| 27 *registration = result; | |
| 28 } | |
| 29 | |
| 30 // Creates a callback which both keeps track of if it's been called, | |
| 31 // as well as the resulting registration. Whent the callback is fired, | |
| 32 // it ensures that the resulting status matches the expectation. | |
| 33 // 'called' is useful for making sure a sychronous callback is or | |
| 34 // isn't called. | |
| 35 ServiceWorkerStorage::RegistrationCallback SaveRegistration( | |
| 36 ServiceWorkerRegistrationStatus expected_status, | |
| 37 bool* called, | |
| 38 scoped_refptr<ServiceWorkerRegistration>* registration) { | |
| 39 *called = false; | |
| 40 return base::Bind( | |
| 41 &SaveRegistrationCallback, expected_status, called, registration); | |
| 42 } | |
| 43 | |
| 44 void SaveUnregistrationCallback(ServiceWorkerRegistrationStatus expected_status, | |
| 45 bool* called, | |
| 46 ServiceWorkerRegistrationStatus status) { | |
| 47 EXPECT_EQ(expected_status, status); | |
| 48 *called = true; | |
| 49 } | |
| 50 | |
| 51 ServiceWorkerStorage::UnregistrationCallback SaveUnregistration( | |
| 52 ServiceWorkerRegistrationStatus expected_status, | |
| 53 bool* called) { | |
| 54 *called = false; | |
| 55 return base::Bind(&SaveUnregistrationCallback, expected_status, called); | |
| 56 } | |
| 57 | |
| 58 void RunUntilIdle() { | |
| 59 RunAllPendingInMessageLoop(BrowserThread::IO); | |
| 60 base::RunLoop().RunUntilIdle(); | |
| 
kinuko
2013/12/04 13:04:29
As I commented on another CL, just calling base::R
 
alecflett
2013/12/06 05:43:33
I feel like I added this because otherwise the thr
 | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 class ServiceWorkerStorageTest : public testing::Test { | |
| 66 public: | |
| 67 ServiceWorkerStorageTest() | |
| 68 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | |
| 69 | |
| 70 virtual void SetUp() OVERRIDE { | |
| 71 storage_.reset(new ServiceWorkerStorage(base::FilePath(), NULL)); | |
| 72 } | |
| 73 | |
| 74 virtual void TearDown() OVERRIDE { storage_->Shutdown(); } | |
| 75 | |
| 76 protected: | |
| 77 TestBrowserThreadBundle browser_thread_bundle_; | |
| 78 scoped_ptr<ServiceWorkerStorage> storage_; | |
| 79 }; | |
| 80 | |
| 81 TEST_F(ServiceWorkerStorageTest, PatternMatches) { | |
| 82 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches( | |
| 83 GURL("http://www.example.com/*"), GURL("http://www.example.com/"))); | |
| 84 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches( | |
| 85 GURL("http://www.example.com/*"), | |
| 86 GURL("http://www.example.com/page.html"))); | |
| 87 | |
| 88 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
| 89 GURL("http://www.example.com/*"), GURL("https://www.example.com/"))); | |
| 90 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
| 91 GURL("http://www.example.com/*"), | |
| 92 GURL("https://www.example.com/page.html"))); | |
| 93 | |
| 94 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
| 95 GURL("http://www.example.com/*"), GURL("http://www.foo.com/"))); | |
| 96 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
| 97 GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html"))); | |
| 98 } | |
| 99 | |
| 100 TEST_F(ServiceWorkerStorageTest, SameDocumentSameRegistration) { | |
| 101 scoped_refptr<ServiceWorkerRegistration> original_registration; | |
| 102 bool called; | |
| 103 storage_->Register( | |
| 104 GURL("http://www.example.com/*"), | |
| 105 GURL("http://www.example.com/service_worker.js"), | |
| 106 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | |
| 107 EXPECT_FALSE(called); | |
| 108 RunUntilIdle(); | |
| 109 EXPECT_TRUE(called); | |
| 110 | |
| 111 scoped_refptr<ServiceWorkerRegistration> registration1; | |
| 112 storage_->FindRegistrationForDocument( | |
| 113 GURL("http://www.example.com/"), | |
| 114 SaveRegistration(REGISTRATION_OK, &called, ®istration1)); | |
| 115 scoped_refptr<ServiceWorkerRegistration> registration2; | |
| 116 storage_->FindRegistrationForDocument( | |
| 117 GURL("http://www.example.com/"), | |
| 118 SaveRegistration(REGISTRATION_OK, &called, ®istration2)); | |
| 119 | |
| 120 ServiceWorkerRegistration* null_registration(NULL); | |
| 121 ASSERT_EQ(null_registration, registration1); | |
| 122 ASSERT_EQ(null_registration, registration2); | |
| 123 EXPECT_FALSE(called); | |
| 124 RunUntilIdle(); | |
| 125 EXPECT_TRUE(called); | |
| 126 ASSERT_NE(null_registration, registration1); | |
| 127 ASSERT_NE(null_registration, registration2); | |
| 128 | |
| 129 ASSERT_EQ(registration1, registration2); | |
| 130 } | |
| 131 | |
| 132 TEST_F(ServiceWorkerStorageTest, SameMatchSameRegistration) { | |
| 133 bool called; | |
| 134 scoped_refptr<ServiceWorkerRegistration> original_registration; | |
| 135 storage_->Register( | |
| 136 GURL("http://www.example.com/*"), | |
| 137 GURL("http://www.example.com/service_worker.js"), | |
| 138 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | |
| 139 EXPECT_FALSE(called); | |
| 140 RunUntilIdle(); | |
| 141 EXPECT_TRUE(called); | |
| 142 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), | |
| 143 original_registration.get()); | |
| 144 | |
| 145 scoped_refptr<ServiceWorkerRegistration> registration1; | |
| 146 storage_->FindRegistrationForDocument( | |
| 147 GURL("http://www.example.com/one"), | |
| 148 SaveRegistration(REGISTRATION_OK, &called, ®istration1)); | |
| 149 | |
| 150 EXPECT_FALSE(called); | |
| 151 RunUntilIdle(); | |
| 152 EXPECT_TRUE(called); | |
| 153 | |
| 154 scoped_refptr<ServiceWorkerRegistration> registration2; | |
| 155 storage_->FindRegistrationForDocument( | |
| 156 GURL("http://www.example.com/two"), | |
| 157 SaveRegistration(REGISTRATION_OK, &called, ®istration2)); | |
| 158 EXPECT_FALSE(called); | |
| 159 RunUntilIdle(); | |
| 160 EXPECT_TRUE(called); | |
| 161 | |
| 162 ASSERT_EQ(registration1, registration2); | |
| 163 } | |
| 164 | |
| 165 TEST_F(ServiceWorkerStorageTest, DifferentMatchDifferentRegistration) { | |
| 166 bool called1; | |
| 167 scoped_refptr<ServiceWorkerRegistration> original_registration1; | |
| 168 storage_->Register( | |
| 169 GURL("http://www.example.com/one/*"), | |
| 170 GURL("http://www.example.com/service_worker.js"), | |
| 171 SaveRegistration(REGISTRATION_OK, &called1, &original_registration1)); | |
| 172 | |
| 173 bool called2; | |
| 174 scoped_refptr<ServiceWorkerRegistration> original_registration2; | |
| 175 storage_->Register( | |
| 176 GURL("http://www.example.com/two/*"), | |
| 177 GURL("http://www.example.com/service_worker.js"), | |
| 178 SaveRegistration(REGISTRATION_OK, &called2, &original_registration2)); | |
| 179 | |
| 180 EXPECT_FALSE(called1); | |
| 181 EXPECT_FALSE(called2); | |
| 182 RunUntilIdle(); | |
| 183 EXPECT_TRUE(called2); | |
| 184 EXPECT_TRUE(called1); | |
| 185 | |
| 186 scoped_refptr<ServiceWorkerRegistration> registration1; | |
| 187 storage_->FindRegistrationForDocument( | |
| 188 GURL("http://www.example.com/one/"), | |
| 189 SaveRegistration(REGISTRATION_OK, &called1, ®istration1)); | |
| 190 scoped_refptr<ServiceWorkerRegistration> registration2; | |
| 191 storage_->FindRegistrationForDocument( | |
| 192 GURL("http://www.example.com/two/"), | |
| 193 SaveRegistration(REGISTRATION_OK, &called2, ®istration2)); | |
| 194 | |
| 195 EXPECT_FALSE(called1); | |
| 196 EXPECT_FALSE(called2); | |
| 197 RunUntilIdle(); | |
| 198 EXPECT_TRUE(called2); | |
| 199 EXPECT_TRUE(called1); | |
| 200 | |
| 201 ASSERT_NE(registration1, registration2); | |
| 202 } | |
| 203 | |
| 204 // Make sure basic registration is working. | |
| 205 TEST_F(ServiceWorkerStorageTest, Register) { | |
| 206 bool called = false; | |
| 207 scoped_refptr<ServiceWorkerRegistration> registration; | |
| 208 storage_->Register(GURL("http://www.example.com/*"), | |
| 209 GURL("http://www.example.com/service_worker.js"), | |
| 210 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | |
| 211 | |
| 212 ASSERT_FALSE(called); | |
| 213 RunUntilIdle(); | |
| 214 ASSERT_TRUE(called); | |
| 215 | |
| 216 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | |
| 217 } | |
| 218 | |
| 219 // Make sure registrations are cleaned up when they are unregistered. | |
| 220 TEST_F(ServiceWorkerStorageTest, Unregister) { | |
| 221 GURL pattern("http://www.example.com/*"); | |
| 222 | |
| 223 bool called; | |
| 224 scoped_refptr<ServiceWorkerRegistration> registration; | |
| 225 storage_->Register(pattern, | |
| 226 GURL("http://www.example.com/service_worker.js"), | |
| 227 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | |
| 228 | |
| 229 ASSERT_FALSE(called); | |
| 230 RunUntilIdle(); | |
| 231 ASSERT_TRUE(called); | |
| 232 | |
| 233 storage_->Unregister(pattern, SaveUnregistration(REGISTRATION_OK, &called)); | |
| 234 | |
| 235 ASSERT_FALSE(called); | |
| 236 RunUntilIdle(); | |
| 237 ASSERT_TRUE(called); | |
| 238 | |
| 239 ASSERT_TRUE(registration->HasOneRef()); | |
| 240 | |
| 241 storage_->FindRegistrationForPattern( | |
| 242 pattern, | |
| 243 SaveRegistration(REGISTRATION_NOT_FOUND, &called, ®istration)); | |
| 244 | |
| 245 ASSERT_FALSE(called); | |
| 246 RunUntilIdle(); | |
| 247 ASSERT_TRUE(called); | |
| 248 | |
| 249 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | |
| 250 } | |
| 251 | |
| 252 // Make sure that when a new registration replaces an existing | |
| 253 // registration, that the old one is cleaned up. | |
| 254 TEST_F(ServiceWorkerStorageTest, RegisterNewScript) { | |
| 255 GURL pattern("http://www.example.com/*"); | |
| 256 | |
| 257 bool called; | |
| 258 scoped_refptr<ServiceWorkerRegistration> old_registration; | |
| 259 storage_->Register( | |
| 260 pattern, | |
| 261 GURL("http://www.example.com/service_worker.js"), | |
| 262 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | |
| 263 | |
| 264 ASSERT_FALSE(called); | |
| 265 RunUntilIdle(); | |
| 266 ASSERT_TRUE(called); | |
| 267 | |
| 268 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | |
| 269 storage_->FindRegistrationForPattern( | |
| 270 pattern, | |
| 271 SaveRegistration(REGISTRATION_OK, &called, &old_registration_by_pattern)); | |
| 272 | |
| 273 ASSERT_FALSE(called); | |
| 274 RunUntilIdle(); | |
| 275 ASSERT_TRUE(called); | |
| 276 | |
| 277 ASSERT_EQ(old_registration, old_registration_by_pattern); | |
| 278 old_registration_by_pattern = NULL; | |
| 279 | |
| 280 scoped_refptr<ServiceWorkerRegistration> new_registration; | |
| 281 storage_->Register( | |
| 282 pattern, | |
| 283 GURL("http://www.example.com/service_worker_new.js"), | |
| 284 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | |
| 285 | |
| 286 ASSERT_FALSE(called); | |
| 287 RunUntilIdle(); | |
| 288 ASSERT_TRUE(called); | |
| 289 | |
| 290 ASSERT_TRUE(old_registration->HasOneRef()); | |
| 291 | |
| 292 ASSERT_NE(old_registration, new_registration); | |
| 293 | |
| 294 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | |
| 295 storage_->FindRegistrationForPattern( | |
| 296 pattern, SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | |
| 297 | |
| 298 ASSERT_FALSE(called); | |
| 299 RunUntilIdle(); | |
| 300 ASSERT_TRUE(called); | |
| 301 | |
| 302 ASSERT_NE(new_registration_by_pattern, old_registration); | |
| 303 } | |
| 304 | |
| 305 // Make sure that when registering a duplicate pattern+script_url | |
| 306 // combination, that the same registration is used. | |
| 307 TEST_F(ServiceWorkerStorageTest, RegisterDuplicateScript) { | |
| 308 GURL pattern("http://www.example.com/*"); | |
| 309 GURL script_url("http://www.example.com/service_worker.js"); | |
| 310 | |
| 311 bool called; | |
| 312 scoped_refptr<ServiceWorkerRegistration> old_registration; | |
| 313 storage_->Register( | |
| 314 pattern, | |
| 315 script_url, | |
| 316 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | |
| 317 | |
| 318 ASSERT_FALSE(called); | |
| 319 RunUntilIdle(); | |
| 320 ASSERT_TRUE(called); | |
| 321 | |
| 322 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | |
| 323 storage_->FindRegistrationForPattern( | |
| 324 pattern, | |
| 325 SaveRegistration(REGISTRATION_OK, &called, &old_registration_by_pattern)); | |
| 326 ASSERT_FALSE(called); | |
| 327 RunUntilIdle(); | |
| 328 ASSERT_TRUE(called); | |
| 329 | |
| 330 ASSERT_TRUE(old_registration_by_pattern); | |
| 331 | |
| 332 scoped_refptr<ServiceWorkerRegistration> new_registration; | |
| 333 storage_->Register( | |
| 334 pattern, | |
| 335 script_url, | |
| 336 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | |
| 337 | |
| 338 ASSERT_FALSE(called); | |
| 339 RunUntilIdle(); | |
| 340 ASSERT_TRUE(called); | |
| 341 | |
| 342 ASSERT_EQ(old_registration, new_registration); | |
| 343 | |
| 344 ASSERT_FALSE(old_registration->HasOneRef()); | |
| 345 | |
| 346 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | |
| 347 storage_->FindRegistrationForPattern( | |
| 348 pattern, | |
| 349 SaveRegistration(REGISTRATION_OK, &called, &new_registration_by_pattern)); | |
| 350 | |
| 351 ASSERT_FALSE(called); | |
| 352 RunUntilIdle(); | |
| 353 ASSERT_TRUE(called); | |
| 354 | |
| 355 ASSERT_EQ(new_registration, old_registration); | |
| 356 } | |
| 357 | |
| 358 } // namespace content | |
| OLD | NEW |