| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" | 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| 11 #include "content/browser/service_worker/service_worker_registration_status.h" | 11 #include "content/browser/service_worker/service_worker_registration_status.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 // Unit tests for testing all job registration tasks. | 15 // Unit tests for testing all job registration tasks. |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 void SaveRegistrationCallback( | 20 void SaveRegistrationCallback( |
| 21 ServiceWorkerRegistrationStatus expected_status, | 21 ServiceWorkerStatusCode expected_status, |
| 22 bool* called, | 22 bool* called, |
| 23 scoped_refptr<ServiceWorkerRegistration>* registration, | 23 scoped_refptr<ServiceWorkerRegistration>* registration, |
| 24 ServiceWorkerRegistrationStatus status, | 24 ServiceWorkerStatusCode status, |
| 25 const scoped_refptr<ServiceWorkerRegistration>& result) { | 25 const scoped_refptr<ServiceWorkerRegistration>& result) { |
| 26 EXPECT_EQ(expected_status, status); | 26 EXPECT_EQ(expected_status, status); |
| 27 *called = true; | 27 *called = true; |
| 28 *registration = result; | 28 *registration = result; |
| 29 } | 29 } |
| 30 | 30 |
| 31 void SaveFoundRegistrationCallback( | 31 void SaveFoundRegistrationCallback( |
| 32 bool expected_found, | 32 bool expected_found, |
| 33 ServiceWorkerRegistrationStatus expected_status, | 33 ServiceWorkerStatusCode expected_status, |
| 34 bool* called, | 34 bool* called, |
| 35 scoped_refptr<ServiceWorkerRegistration>* registration, | 35 scoped_refptr<ServiceWorkerRegistration>* registration, |
| 36 bool found, | 36 bool found, |
| 37 ServiceWorkerRegistrationStatus status, | 37 ServiceWorkerStatusCode status, |
| 38 const scoped_refptr<ServiceWorkerRegistration>& result) { | 38 const scoped_refptr<ServiceWorkerRegistration>& result) { |
| 39 EXPECT_EQ(expected_found, found); | 39 EXPECT_EQ(expected_found, found); |
| 40 EXPECT_EQ(expected_status, status); | 40 EXPECT_EQ(expected_status, status); |
| 41 *called = true; | 41 *called = true; |
| 42 *registration = result; | 42 *registration = result; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Creates a callback which both keeps track of if it's been called, | 45 // Creates a callback which both keeps track of if it's been called, |
| 46 // as well as the resulting registration. Whent the callback is fired, | 46 // as well as the resulting registration. Whent the callback is fired, |
| 47 // it ensures that the resulting status matches the expectation. | 47 // it ensures that the resulting status matches the expectation. |
| 48 // 'called' is useful for making sure a sychronous callback is or | 48 // 'called' is useful for making sure a sychronous callback is or |
| 49 // isn't called. | 49 // isn't called. |
| 50 ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration( | 50 ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration( |
| 51 ServiceWorkerRegistrationStatus expected_status, | 51 ServiceWorkerStatusCode expected_status, |
| 52 bool* called, | 52 bool* called, |
| 53 scoped_refptr<ServiceWorkerRegistration>* registration) { | 53 scoped_refptr<ServiceWorkerRegistration>* registration) { |
| 54 *called = false; | 54 *called = false; |
| 55 return base::Bind( | 55 return base::Bind( |
| 56 &SaveRegistrationCallback, expected_status, called, registration); | 56 &SaveRegistrationCallback, expected_status, called, registration); |
| 57 } | 57 } |
| 58 | 58 |
| 59 ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration( | 59 ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration( |
| 60 bool expected_found, | 60 bool expected_found, |
| 61 ServiceWorkerRegistrationStatus expected_status, | 61 ServiceWorkerStatusCode expected_status, |
| 62 bool* called, | 62 bool* called, |
| 63 scoped_refptr<ServiceWorkerRegistration>* registration) { | 63 scoped_refptr<ServiceWorkerRegistration>* registration) { |
| 64 *called = false; | 64 *called = false; |
| 65 return base::Bind(&SaveFoundRegistrationCallback, | 65 return base::Bind(&SaveFoundRegistrationCallback, |
| 66 expected_found, | 66 expected_found, |
| 67 expected_status, | 67 expected_status, |
| 68 called, | 68 called, |
| 69 registration); | 69 registration); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void SaveUnregistrationCallback(ServiceWorkerRegistrationStatus expected_status, | 72 void SaveUnregistrationCallback(ServiceWorkerStatusCode expected_status, |
| 73 bool* called, | 73 bool* called, |
| 74 ServiceWorkerRegistrationStatus status) { | 74 ServiceWorkerStatusCode status) { |
| 75 EXPECT_EQ(expected_status, status); | 75 EXPECT_EQ(expected_status, status); |
| 76 *called = true; | 76 *called = true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 ServiceWorkerRegisterJob::UnregistrationCallback SaveUnregistration( | 79 ServiceWorkerRegisterJob::UnregistrationCallback SaveUnregistration( |
| 80 ServiceWorkerRegistrationStatus expected_status, | 80 ServiceWorkerStatusCode expected_status, |
| 81 bool* called) { | 81 bool* called) { |
| 82 *called = false; | 82 *called = false; |
| 83 return base::Bind(&SaveUnregistrationCallback, expected_status, called); | 83 return base::Bind(&SaveUnregistrationCallback, expected_status, called); |
| 84 } | 84 } |
| 85 | 85 |
| 86 } // namespace | 86 } // namespace |
| 87 | 87 |
| 88 class ServiceWorkerJobTest : public testing::Test { | 88 class ServiceWorkerJobTest : public testing::Test { |
| 89 public: | 89 public: |
| 90 ServiceWorkerJobTest() | 90 ServiceWorkerJobTest() |
| (...skipping 11 matching lines...) Expand all Loading... |
| 102 scoped_ptr<ServiceWorkerStorage> storage_; | 102 scoped_ptr<ServiceWorkerStorage> storage_; |
| 103 scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_; | 103 scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) { | 106 TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) { |
| 107 scoped_refptr<ServiceWorkerRegistration> original_registration; | 107 scoped_refptr<ServiceWorkerRegistration> original_registration; |
| 108 bool called; | 108 bool called; |
| 109 job_coordinator_->Register( | 109 job_coordinator_->Register( |
| 110 GURL("http://www.example.com/*"), | 110 GURL("http://www.example.com/*"), |
| 111 GURL("http://www.example.com/service_worker.js"), | 111 GURL("http://www.example.com/service_worker.js"), |
| 112 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | 112 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); |
| 113 EXPECT_FALSE(called); | 113 EXPECT_FALSE(called); |
| 114 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
| 115 EXPECT_TRUE(called); | 115 EXPECT_TRUE(called); |
| 116 | 116 |
| 117 scoped_refptr<ServiceWorkerRegistration> registration1; | 117 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 118 storage_->FindRegistrationForDocument( | 118 storage_->FindRegistrationForDocument( |
| 119 GURL("http://www.example.com/"), | 119 GURL("http://www.example.com/"), |
| 120 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); | 120 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); |
| 121 scoped_refptr<ServiceWorkerRegistration> registration2; | 121 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 122 storage_->FindRegistrationForDocument( | 122 storage_->FindRegistrationForDocument( |
| 123 GURL("http://www.example.com/"), | 123 GURL("http://www.example.com/"), |
| 124 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); | 124 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); |
| 125 | 125 |
| 126 ServiceWorkerRegistration* null_registration(NULL); | 126 ServiceWorkerRegistration* null_registration(NULL); |
| 127 ASSERT_EQ(null_registration, registration1); | 127 ASSERT_EQ(null_registration, registration1); |
| 128 ASSERT_EQ(null_registration, registration2); | 128 ASSERT_EQ(null_registration, registration2); |
| 129 EXPECT_FALSE(called); | 129 EXPECT_FALSE(called); |
| 130 base::RunLoop().RunUntilIdle(); | 130 base::RunLoop().RunUntilIdle(); |
| 131 EXPECT_TRUE(called); | 131 EXPECT_TRUE(called); |
| 132 ASSERT_NE(null_registration, registration1); | 132 ASSERT_NE(null_registration, registration1); |
| 133 ASSERT_NE(null_registration, registration2); | 133 ASSERT_NE(null_registration, registration2); |
| 134 | 134 |
| 135 ASSERT_EQ(registration1, registration2); | 135 ASSERT_EQ(registration1, registration2); |
| 136 } | 136 } |
| 137 | 137 |
| 138 TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) { | 138 TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) { |
| 139 bool called; | 139 bool called; |
| 140 scoped_refptr<ServiceWorkerRegistration> original_registration; | 140 scoped_refptr<ServiceWorkerRegistration> original_registration; |
| 141 job_coordinator_->Register( | 141 job_coordinator_->Register( |
| 142 GURL("http://www.example.com/*"), | 142 GURL("http://www.example.com/*"), |
| 143 GURL("http://www.example.com/service_worker.js"), | 143 GURL("http://www.example.com/service_worker.js"), |
| 144 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | 144 SaveRegistration(SERVICE_WORKER_OK, &called, &original_registration)); |
| 145 EXPECT_FALSE(called); | 145 EXPECT_FALSE(called); |
| 146 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
| 147 EXPECT_TRUE(called); | 147 EXPECT_TRUE(called); |
| 148 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), | 148 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), |
| 149 original_registration.get()); | 149 original_registration.get()); |
| 150 | 150 |
| 151 scoped_refptr<ServiceWorkerRegistration> registration1; | 151 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 152 storage_->FindRegistrationForDocument( | 152 storage_->FindRegistrationForDocument( |
| 153 GURL("http://www.example.com/one"), | 153 GURL("http://www.example.com/one"), |
| 154 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); | 154 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration1)); |
| 155 | 155 |
| 156 EXPECT_FALSE(called); | 156 EXPECT_FALSE(called); |
| 157 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_TRUE(called); | 158 EXPECT_TRUE(called); |
| 159 | 159 |
| 160 scoped_refptr<ServiceWorkerRegistration> registration2; | 160 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 161 storage_->FindRegistrationForDocument( | 161 storage_->FindRegistrationForDocument( |
| 162 GURL("http://www.example.com/two"), | 162 GURL("http://www.example.com/two"), |
| 163 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); | 163 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, ®istration2)); |
| 164 EXPECT_FALSE(called); | 164 EXPECT_FALSE(called); |
| 165 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
| 166 EXPECT_TRUE(called); | 166 EXPECT_TRUE(called); |
| 167 | 167 |
| 168 ASSERT_EQ(registration1, registration2); | 168 ASSERT_EQ(registration1, registration2); |
| 169 } | 169 } |
| 170 | 170 |
| 171 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { | 171 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { |
| 172 bool called1; | 172 bool called1; |
| 173 scoped_refptr<ServiceWorkerRegistration> original_registration1; | 173 scoped_refptr<ServiceWorkerRegistration> original_registration1; |
| 174 job_coordinator_->Register( | 174 job_coordinator_->Register( |
| 175 GURL("http://www.example.com/one/*"), | 175 GURL("http://www.example.com/one/*"), |
| 176 GURL("http://www.example.com/service_worker.js"), | 176 GURL("http://www.example.com/service_worker.js"), |
| 177 SaveRegistration(REGISTRATION_OK, &called1, &original_registration1)); | 177 SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1)); |
| 178 | 178 |
| 179 bool called2; | 179 bool called2; |
| 180 scoped_refptr<ServiceWorkerRegistration> original_registration2; | 180 scoped_refptr<ServiceWorkerRegistration> original_registration2; |
| 181 job_coordinator_->Register( | 181 job_coordinator_->Register( |
| 182 GURL("http://www.example.com/two/*"), | 182 GURL("http://www.example.com/two/*"), |
| 183 GURL("http://www.example.com/service_worker.js"), | 183 GURL("http://www.example.com/service_worker.js"), |
| 184 SaveRegistration(REGISTRATION_OK, &called2, &original_registration2)); | 184 SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2)); |
| 185 | 185 |
| 186 EXPECT_FALSE(called1); | 186 EXPECT_FALSE(called1); |
| 187 EXPECT_FALSE(called2); | 187 EXPECT_FALSE(called2); |
| 188 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
| 189 EXPECT_TRUE(called2); | 189 EXPECT_TRUE(called2); |
| 190 EXPECT_TRUE(called1); | 190 EXPECT_TRUE(called1); |
| 191 | 191 |
| 192 scoped_refptr<ServiceWorkerRegistration> registration1; | 192 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 193 storage_->FindRegistrationForDocument( | 193 storage_->FindRegistrationForDocument( |
| 194 GURL("http://www.example.com/one/"), | 194 GURL("http://www.example.com/one/"), |
| 195 SaveFoundRegistration(true, REGISTRATION_OK, &called1, ®istration1)); | 195 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called1, ®istration1)); |
| 196 scoped_refptr<ServiceWorkerRegistration> registration2; | 196 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 197 storage_->FindRegistrationForDocument( | 197 storage_->FindRegistrationForDocument( |
| 198 GURL("http://www.example.com/two/"), | 198 GURL("http://www.example.com/two/"), |
| 199 SaveFoundRegistration(true, REGISTRATION_OK, &called2, ®istration2)); | 199 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called2, ®istration2)); |
| 200 | 200 |
| 201 EXPECT_FALSE(called1); | 201 EXPECT_FALSE(called1); |
| 202 EXPECT_FALSE(called2); | 202 EXPECT_FALSE(called2); |
| 203 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
| 204 EXPECT_TRUE(called2); | 204 EXPECT_TRUE(called2); |
| 205 EXPECT_TRUE(called1); | 205 EXPECT_TRUE(called1); |
| 206 | 206 |
| 207 ASSERT_NE(registration1, registration2); | 207 ASSERT_NE(registration1, registration2); |
| 208 } | 208 } |
| 209 | 209 |
| 210 // Make sure basic registration is working. | 210 // Make sure basic registration is working. |
| 211 TEST_F(ServiceWorkerJobTest, Register) { | 211 TEST_F(ServiceWorkerJobTest, Register) { |
| 212 bool called = false; | 212 bool called = false; |
| 213 scoped_refptr<ServiceWorkerRegistration> registration; | 213 scoped_refptr<ServiceWorkerRegistration> registration; |
| 214 job_coordinator_->Register( | 214 job_coordinator_->Register( |
| 215 GURL("http://www.example.com/*"), | 215 GURL("http://www.example.com/*"), |
| 216 GURL("http://www.example.com/service_worker.js"), | 216 GURL("http://www.example.com/service_worker.js"), |
| 217 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | 217 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); |
| 218 | 218 |
| 219 ASSERT_FALSE(called); | 219 ASSERT_FALSE(called); |
| 220 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
| 221 ASSERT_TRUE(called); | 221 ASSERT_TRUE(called); |
| 222 | 222 |
| 223 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 223 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 224 } | 224 } |
| 225 | 225 |
| 226 // Make sure registrations are cleaned up when they are unregistered. | 226 // Make sure registrations are cleaned up when they are unregistered. |
| 227 TEST_F(ServiceWorkerJobTest, Unregister) { | 227 TEST_F(ServiceWorkerJobTest, Unregister) { |
| 228 GURL pattern("http://www.example.com/*"); | 228 GURL pattern("http://www.example.com/*"); |
| 229 | 229 |
| 230 bool called; | 230 bool called; |
| 231 scoped_refptr<ServiceWorkerRegistration> registration; | 231 scoped_refptr<ServiceWorkerRegistration> registration; |
| 232 job_coordinator_->Register( | 232 job_coordinator_->Register( |
| 233 pattern, | 233 pattern, |
| 234 GURL("http://www.example.com/service_worker.js"), | 234 GURL("http://www.example.com/service_worker.js"), |
| 235 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | 235 SaveRegistration(SERVICE_WORKER_OK, &called, ®istration)); |
| 236 | 236 |
| 237 ASSERT_FALSE(called); | 237 ASSERT_FALSE(called); |
| 238 base::RunLoop().RunUntilIdle(); | 238 base::RunLoop().RunUntilIdle(); |
| 239 ASSERT_TRUE(called); | 239 ASSERT_TRUE(called); |
| 240 | 240 |
| 241 job_coordinator_->Unregister(pattern, | 241 job_coordinator_->Unregister(pattern, |
| 242 SaveUnregistration(REGISTRATION_OK, &called)); | 242 SaveUnregistration(SERVICE_WORKER_OK, &called)); |
| 243 | 243 |
| 244 ASSERT_FALSE(called); | 244 ASSERT_FALSE(called); |
| 245 base::RunLoop().RunUntilIdle(); | 245 base::RunLoop().RunUntilIdle(); |
| 246 ASSERT_TRUE(called); | 246 ASSERT_TRUE(called); |
| 247 | 247 |
| 248 ASSERT_TRUE(registration->HasOneRef()); | 248 ASSERT_TRUE(registration->HasOneRef()); |
| 249 | 249 |
| 250 storage_->FindRegistrationForPattern( | 250 storage_->FindRegistrationForPattern( |
| 251 pattern, | 251 pattern, |
| 252 SaveFoundRegistration(false, REGISTRATION_OK, &called, ®istration)); | 252 SaveFoundRegistration(false, SERVICE_WORKER_OK, &called, ®istration)); |
| 253 | 253 |
| 254 ASSERT_FALSE(called); | 254 ASSERT_FALSE(called); |
| 255 base::RunLoop().RunUntilIdle(); | 255 base::RunLoop().RunUntilIdle(); |
| 256 ASSERT_TRUE(called); | 256 ASSERT_TRUE(called); |
| 257 | 257 |
| 258 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 258 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
| 259 } | 259 } |
| 260 | 260 |
| 261 // Make sure that when a new registration replaces an existing | 261 // Make sure that when a new registration replaces an existing |
| 262 // registration, that the old one is cleaned up. | 262 // registration, that the old one is cleaned up. |
| 263 TEST_F(ServiceWorkerJobTest, RegisterNewScript) { | 263 TEST_F(ServiceWorkerJobTest, RegisterNewScript) { |
| 264 GURL pattern("http://www.example.com/*"); | 264 GURL pattern("http://www.example.com/*"); |
| 265 | 265 |
| 266 bool called; | 266 bool called; |
| 267 scoped_refptr<ServiceWorkerRegistration> old_registration; | 267 scoped_refptr<ServiceWorkerRegistration> old_registration; |
| 268 job_coordinator_->Register( | 268 job_coordinator_->Register( |
| 269 pattern, | 269 pattern, |
| 270 GURL("http://www.example.com/service_worker.js"), | 270 GURL("http://www.example.com/service_worker.js"), |
| 271 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | 271 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); |
| 272 | 272 |
| 273 ASSERT_FALSE(called); | 273 ASSERT_FALSE(called); |
| 274 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
| 275 ASSERT_TRUE(called); | 275 ASSERT_TRUE(called); |
| 276 | 276 |
| 277 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 277 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
| 278 storage_->FindRegistrationForPattern( | 278 storage_->FindRegistrationForPattern( |
| 279 pattern, | 279 pattern, |
| 280 SaveFoundRegistration( | 280 SaveFoundRegistration( |
| 281 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); | 281 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); |
| 282 | 282 |
| 283 ASSERT_FALSE(called); | 283 ASSERT_FALSE(called); |
| 284 base::RunLoop().RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
| 285 ASSERT_TRUE(called); | 285 ASSERT_TRUE(called); |
| 286 | 286 |
| 287 ASSERT_EQ(old_registration, old_registration_by_pattern); | 287 ASSERT_EQ(old_registration, old_registration_by_pattern); |
| 288 old_registration_by_pattern = NULL; | 288 old_registration_by_pattern = NULL; |
| 289 | 289 |
| 290 scoped_refptr<ServiceWorkerRegistration> new_registration; | 290 scoped_refptr<ServiceWorkerRegistration> new_registration; |
| 291 job_coordinator_->Register( | 291 job_coordinator_->Register( |
| 292 pattern, | 292 pattern, |
| 293 GURL("http://www.example.com/service_worker_new.js"), | 293 GURL("http://www.example.com/service_worker_new.js"), |
| 294 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | 294 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); |
| 295 | 295 |
| 296 ASSERT_FALSE(called); | 296 ASSERT_FALSE(called); |
| 297 base::RunLoop().RunUntilIdle(); | 297 base::RunLoop().RunUntilIdle(); |
| 298 ASSERT_TRUE(called); | 298 ASSERT_TRUE(called); |
| 299 | 299 |
| 300 ASSERT_TRUE(old_registration->HasOneRef()); | 300 ASSERT_TRUE(old_registration->HasOneRef()); |
| 301 | 301 |
| 302 ASSERT_NE(old_registration, new_registration); | 302 ASSERT_NE(old_registration, new_registration); |
| 303 | 303 |
| 304 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 304 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
| 305 storage_->FindRegistrationForPattern( | 305 storage_->FindRegistrationForPattern( |
| 306 pattern, | 306 pattern, |
| 307 SaveFoundRegistration(true, REGISTRATION_OK, &called, &new_registration)); | 307 SaveFoundRegistration(true, SERVICE_WORKER_OK, &called, |
| 308 &new_registration)); |
| 308 | 309 |
| 309 ASSERT_FALSE(called); | 310 ASSERT_FALSE(called); |
| 310 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 311 ASSERT_TRUE(called); | 312 ASSERT_TRUE(called); |
| 312 | 313 |
| 313 ASSERT_NE(new_registration_by_pattern, old_registration); | 314 ASSERT_NE(new_registration_by_pattern, old_registration); |
| 314 } | 315 } |
| 315 | 316 |
| 316 // Make sure that when registering a duplicate pattern+script_url | 317 // Make sure that when registering a duplicate pattern+script_url |
| 317 // combination, that the same registration is used. | 318 // combination, that the same registration is used. |
| 318 TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) { | 319 TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) { |
| 319 GURL pattern("http://www.example.com/*"); | 320 GURL pattern("http://www.example.com/*"); |
| 320 GURL script_url("http://www.example.com/service_worker.js"); | 321 GURL script_url("http://www.example.com/service_worker.js"); |
| 321 | 322 |
| 322 bool called; | 323 bool called; |
| 323 scoped_refptr<ServiceWorkerRegistration> old_registration; | 324 scoped_refptr<ServiceWorkerRegistration> old_registration; |
| 324 job_coordinator_->Register( | 325 job_coordinator_->Register( |
| 325 pattern, | 326 pattern, |
| 326 script_url, | 327 script_url, |
| 327 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | 328 SaveRegistration(SERVICE_WORKER_OK, &called, &old_registration)); |
| 328 | 329 |
| 329 ASSERT_FALSE(called); | 330 ASSERT_FALSE(called); |
| 330 base::RunLoop().RunUntilIdle(); | 331 base::RunLoop().RunUntilIdle(); |
| 331 ASSERT_TRUE(called); | 332 ASSERT_TRUE(called); |
| 332 | 333 |
| 333 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 334 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
| 334 storage_->FindRegistrationForPattern( | 335 storage_->FindRegistrationForPattern( |
| 335 pattern, | 336 pattern, |
| 336 SaveFoundRegistration( | 337 SaveFoundRegistration( |
| 337 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); | 338 true, SERVICE_WORKER_OK, &called, &old_registration_by_pattern)); |
| 338 ASSERT_FALSE(called); | 339 ASSERT_FALSE(called); |
| 339 base::RunLoop().RunUntilIdle(); | 340 base::RunLoop().RunUntilIdle(); |
| 340 ASSERT_TRUE(called); | 341 ASSERT_TRUE(called); |
| 341 | 342 |
| 342 ASSERT_TRUE(old_registration_by_pattern); | 343 ASSERT_TRUE(old_registration_by_pattern); |
| 343 | 344 |
| 344 scoped_refptr<ServiceWorkerRegistration> new_registration; | 345 scoped_refptr<ServiceWorkerRegistration> new_registration; |
| 345 job_coordinator_->Register( | 346 job_coordinator_->Register( |
| 346 pattern, | 347 pattern, |
| 347 script_url, | 348 script_url, |
| 348 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | 349 SaveRegistration(SERVICE_WORKER_OK, &called, &new_registration)); |
| 349 | 350 |
| 350 ASSERT_FALSE(called); | 351 ASSERT_FALSE(called); |
| 351 base::RunLoop().RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
| 352 ASSERT_TRUE(called); | 353 ASSERT_TRUE(called); |
| 353 | 354 |
| 354 ASSERT_EQ(old_registration, new_registration); | 355 ASSERT_EQ(old_registration, new_registration); |
| 355 | 356 |
| 356 ASSERT_FALSE(old_registration->HasOneRef()); | 357 ASSERT_FALSE(old_registration->HasOneRef()); |
| 357 | 358 |
| 358 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 359 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
| 359 storage_->FindRegistrationForPattern( | 360 storage_->FindRegistrationForPattern( |
| 360 pattern, | 361 pattern, |
| 361 SaveFoundRegistration( | 362 SaveFoundRegistration( |
| 362 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); | 363 true, SERVICE_WORKER_OK, &called, &new_registration_by_pattern)); |
| 363 | 364 |
| 364 ASSERT_FALSE(called); | 365 ASSERT_FALSE(called); |
| 365 base::RunLoop().RunUntilIdle(); | 366 base::RunLoop().RunUntilIdle(); |
| 366 ASSERT_TRUE(called); | 367 ASSERT_TRUE(called); |
| 367 | 368 |
| 368 ASSERT_EQ(new_registration, old_registration); | 369 ASSERT_EQ(new_registration, old_registration); |
| 369 } | 370 } |
| 370 | 371 |
| 371 // Register and then unregister the pattern, in parallel. Job coordinator should | 372 // Register and then unregister the pattern, in parallel. Job coordinator should |
| 372 // process jobs until the last job. | 373 // process jobs until the last job. |
| 373 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { | 374 TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) { |
| 374 GURL pattern("http://www.example.com/*"); | 375 GURL pattern("http://www.example.com/*"); |
| 375 GURL script_url("http://www.example.com/service_worker.js"); | 376 GURL script_url("http://www.example.com/service_worker.js"); |
| 376 | 377 |
| 377 bool registration_called = false; | 378 bool registration_called = false; |
| 378 scoped_refptr<ServiceWorkerRegistration> registration; | 379 scoped_refptr<ServiceWorkerRegistration> registration; |
| 379 job_coordinator_->Register( | 380 job_coordinator_->Register( |
| 380 pattern, | 381 pattern, |
| 381 script_url, | 382 script_url, |
| 382 SaveRegistration(REGISTRATION_OK, ®istration_called, ®istration)); | 383 SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration)); |
| 383 | 384 |
| 384 bool unregistration_called = false; | 385 bool unregistration_called = false; |
| 385 job_coordinator_->Unregister( | 386 job_coordinator_->Unregister( |
| 386 pattern, SaveUnregistration(REGISTRATION_OK, &unregistration_called)); | 387 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called)); |
| 387 | 388 |
| 388 ASSERT_FALSE(registration_called); | 389 ASSERT_FALSE(registration_called); |
| 389 ASSERT_FALSE(unregistration_called); | 390 ASSERT_FALSE(unregistration_called); |
| 390 base::RunLoop().RunUntilIdle(); | 391 base::RunLoop().RunUntilIdle(); |
| 391 ASSERT_TRUE(registration_called); | 392 ASSERT_TRUE(registration_called); |
| 392 ASSERT_TRUE(unregistration_called); | 393 ASSERT_TRUE(unregistration_called); |
| 393 | 394 |
| 394 ASSERT_TRUE(registration->is_shutdown()); | 395 ASSERT_TRUE(registration->is_shutdown()); |
| 395 | 396 |
| 396 bool find_called = false; | 397 bool find_called = false; |
| 397 storage_->FindRegistrationForPattern( | 398 storage_->FindRegistrationForPattern( |
| 398 pattern, | 399 pattern, |
| 399 SaveFoundRegistration( | 400 SaveFoundRegistration( |
| 400 false, REGISTRATION_OK, &find_called, ®istration)); | 401 false, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 401 | 402 |
| 402 base::RunLoop().RunUntilIdle(); | 403 base::RunLoop().RunUntilIdle(); |
| 403 | 404 |
| 404 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); | 405 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); |
| 405 } | 406 } |
| 406 | 407 |
| 407 // Register conflicting scripts for the same pattern. The most recent | 408 // Register conflicting scripts for the same pattern. The most recent |
| 408 // registration should win, and the old registration should have been | 409 // registration should win, and the old registration should have been |
| 409 // shutdown. | 410 // shutdown. |
| 410 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { | 411 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
| 411 GURL pattern("http://www.example.com/*"); | 412 GURL pattern("http://www.example.com/*"); |
| 412 | 413 |
| 413 GURL script_url1("http://www.example.com/service_worker1.js"); | 414 GURL script_url1("http://www.example.com/service_worker1.js"); |
| 414 bool registration1_called = false; | 415 bool registration1_called = false; |
| 415 scoped_refptr<ServiceWorkerRegistration> registration1; | 416 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 416 job_coordinator_->Register( | 417 job_coordinator_->Register( |
| 417 pattern, | 418 pattern, |
| 418 script_url1, | 419 script_url1, |
| 419 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); | 420 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, |
| 421 ®istration1)); |
| 420 | 422 |
| 421 GURL script_url2("http://www.example.com/service_worker2.js"); | 423 GURL script_url2("http://www.example.com/service_worker2.js"); |
| 422 bool registration2_called = false; | 424 bool registration2_called = false; |
| 423 scoped_refptr<ServiceWorkerRegistration> registration2; | 425 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 424 job_coordinator_->Register( | 426 job_coordinator_->Register( |
| 425 pattern, | 427 pattern, |
| 426 script_url2, | 428 script_url2, |
| 427 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); | 429 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, |
| 430 ®istration2)); |
| 428 | 431 |
| 429 ASSERT_FALSE(registration1_called); | 432 ASSERT_FALSE(registration1_called); |
| 430 ASSERT_FALSE(registration2_called); | 433 ASSERT_FALSE(registration2_called); |
| 431 base::RunLoop().RunUntilIdle(); | 434 base::RunLoop().RunUntilIdle(); |
| 432 ASSERT_TRUE(registration1_called); | 435 ASSERT_TRUE(registration1_called); |
| 433 ASSERT_TRUE(registration2_called); | 436 ASSERT_TRUE(registration2_called); |
| 434 | 437 |
| 435 scoped_refptr<ServiceWorkerRegistration> registration; | 438 scoped_refptr<ServiceWorkerRegistration> registration; |
| 436 bool find_called = false; | 439 bool find_called = false; |
| 437 storage_->FindRegistrationForPattern( | 440 storage_->FindRegistrationForPattern( |
| 438 pattern, | 441 pattern, |
| 439 SaveFoundRegistration( | 442 SaveFoundRegistration( |
| 440 true, REGISTRATION_OK, &find_called, ®istration)); | 443 true, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 441 | 444 |
| 442 base::RunLoop().RunUntilIdle(); | 445 base::RunLoop().RunUntilIdle(); |
| 443 | 446 |
| 444 EXPECT_TRUE(registration1->is_shutdown()); | 447 EXPECT_TRUE(registration1->is_shutdown()); |
| 445 EXPECT_FALSE(registration2->is_shutdown()); | 448 EXPECT_FALSE(registration2->is_shutdown()); |
| 446 ASSERT_EQ(registration2, registration); | 449 ASSERT_EQ(registration2, registration); |
| 447 } | 450 } |
| 448 | 451 |
| 449 // Register the exact same pattern + script. Requests should be | 452 // Register the exact same pattern + script. Requests should be |
| 450 // coalesced such that both callers get the exact same registration | 453 // coalesced such that both callers get the exact same registration |
| 451 // object. | 454 // object. |
| 452 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { | 455 TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) { |
| 453 GURL pattern("http://www.example.com/*"); | 456 GURL pattern("http://www.example.com/*"); |
| 454 | 457 |
| 455 GURL script_url("http://www.example.com/service_worker1.js"); | 458 GURL script_url("http://www.example.com/service_worker1.js"); |
| 456 bool registration1_called = false; | 459 bool registration1_called = false; |
| 457 scoped_refptr<ServiceWorkerRegistration> registration1; | 460 scoped_refptr<ServiceWorkerRegistration> registration1; |
| 458 job_coordinator_->Register( | 461 job_coordinator_->Register( |
| 459 pattern, | 462 pattern, |
| 460 script_url, | 463 script_url, |
| 461 SaveRegistration(REGISTRATION_OK, ®istration1_called, ®istration1)); | 464 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, |
| 465 ®istration1)); |
| 462 | 466 |
| 463 bool registration2_called = false; | 467 bool registration2_called = false; |
| 464 scoped_refptr<ServiceWorkerRegistration> registration2; | 468 scoped_refptr<ServiceWorkerRegistration> registration2; |
| 465 job_coordinator_->Register( | 469 job_coordinator_->Register( |
| 466 pattern, | 470 pattern, |
| 467 script_url, | 471 script_url, |
| 468 SaveRegistration(REGISTRATION_OK, ®istration2_called, ®istration2)); | 472 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, |
| 473 ®istration2)); |
| 469 | 474 |
| 470 ASSERT_FALSE(registration1_called); | 475 ASSERT_FALSE(registration1_called); |
| 471 ASSERT_FALSE(registration2_called); | 476 ASSERT_FALSE(registration2_called); |
| 472 base::RunLoop().RunUntilIdle(); | 477 base::RunLoop().RunUntilIdle(); |
| 473 ASSERT_TRUE(registration1_called); | 478 ASSERT_TRUE(registration1_called); |
| 474 ASSERT_TRUE(registration2_called); | 479 ASSERT_TRUE(registration2_called); |
| 475 | 480 |
| 476 ASSERT_EQ(registration1, registration2); | 481 ASSERT_EQ(registration1, registration2); |
| 477 | 482 |
| 478 scoped_refptr<ServiceWorkerRegistration> registration; | 483 scoped_refptr<ServiceWorkerRegistration> registration; |
| 479 bool find_called = false; | 484 bool find_called = false; |
| 480 storage_->FindRegistrationForPattern( | 485 storage_->FindRegistrationForPattern( |
| 481 pattern, | 486 pattern, |
| 482 SaveFoundRegistration( | 487 SaveFoundRegistration( |
| 483 true, REGISTRATION_OK, &find_called, ®istration)); | 488 true, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 484 | 489 |
| 485 base::RunLoop().RunUntilIdle(); | 490 base::RunLoop().RunUntilIdle(); |
| 486 ASSERT_EQ(registration, registration1); | 491 ASSERT_EQ(registration, registration1); |
| 487 } | 492 } |
| 488 | 493 |
| 489 // Call simulataneous unregister calls. | 494 // Call simulataneous unregister calls. |
| 490 TEST_F(ServiceWorkerJobTest, ParallelUnreg) { | 495 TEST_F(ServiceWorkerJobTest, ParallelUnreg) { |
| 491 GURL pattern("http://www.example.com/*"); | 496 GURL pattern("http://www.example.com/*"); |
| 492 | 497 |
| 493 GURL script_url("http://www.example.com/service_worker.js"); | 498 GURL script_url("http://www.example.com/service_worker.js"); |
| 494 bool unregistration1_called = false; | 499 bool unregistration1_called = false; |
| 495 job_coordinator_->Unregister( | 500 job_coordinator_->Unregister( |
| 496 pattern, SaveUnregistration(REGISTRATION_OK, &unregistration1_called)); | 501 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration1_called)); |
| 497 | 502 |
| 498 bool unregistration2_called = false; | 503 bool unregistration2_called = false; |
| 499 job_coordinator_->Unregister( | 504 job_coordinator_->Unregister( |
| 500 pattern, SaveUnregistration(REGISTRATION_OK, &unregistration2_called)); | 505 pattern, SaveUnregistration(SERVICE_WORKER_OK, &unregistration2_called)); |
| 501 | 506 |
| 502 ASSERT_FALSE(unregistration1_called); | 507 ASSERT_FALSE(unregistration1_called); |
| 503 ASSERT_FALSE(unregistration2_called); | 508 ASSERT_FALSE(unregistration2_called); |
| 504 base::RunLoop().RunUntilIdle(); | 509 base::RunLoop().RunUntilIdle(); |
| 505 ASSERT_TRUE(unregistration1_called); | 510 ASSERT_TRUE(unregistration1_called); |
| 506 ASSERT_TRUE(unregistration2_called); | 511 ASSERT_TRUE(unregistration2_called); |
| 507 | 512 |
| 508 // There isn't really a way to test that they are being coalesced, | 513 // There isn't really a way to test that they are being coalesced, |
| 509 // but we can make sure they can exist simultaneously without | 514 // but we can make sure they can exist simultaneously without |
| 510 // crashing. | 515 // crashing. |
| 511 scoped_refptr<ServiceWorkerRegistration> registration; | 516 scoped_refptr<ServiceWorkerRegistration> registration; |
| 512 bool find_called = false; | 517 bool find_called = false; |
| 513 storage_->FindRegistrationForPattern( | 518 storage_->FindRegistrationForPattern( |
| 514 pattern, | 519 pattern, |
| 515 SaveFoundRegistration( | 520 SaveFoundRegistration( |
| 516 false, REGISTRATION_OK, &find_called, ®istration)); | 521 false, SERVICE_WORKER_OK, &find_called, ®istration)); |
| 517 | 522 |
| 518 base::RunLoop().RunUntilIdle(); | 523 base::RunLoop().RunUntilIdle(); |
| 519 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); | 524 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration); |
| 520 } | 525 } |
| 521 | 526 |
| 522 } // namespace content | 527 } // namespace content |
| OLD | NEW |