| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/public/browser/service_worker_context.h" | 5 #include "content/public/browser/service_worker_context.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 109 |
| 110 // Make sure basic registration is working. | 110 // Make sure basic registration is working. |
| 111 TEST_F(ServiceWorkerContextTest, Register) { | 111 TEST_F(ServiceWorkerContextTest, Register) { |
| 112 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 112 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 113 int64 version_id = kInvalidServiceWorkerVersionId; | 113 int64 version_id = kInvalidServiceWorkerVersionId; |
| 114 bool called = false; | 114 bool called = false; |
| 115 context_->RegisterServiceWorker( | 115 context_->RegisterServiceWorker( |
| 116 GURL("http://www.example.com/*"), | 116 GURL("http://www.example.com/*"), |
| 117 GURL("http://www.example.com/service_worker.js"), | 117 GURL("http://www.example.com/service_worker.js"), |
| 118 render_process_id_, | 118 render_process_id_, |
| 119 NULL, |
| 119 MakeRegisteredCallback(&called, ®istration_id, &version_id)); | 120 MakeRegisteredCallback(&called, ®istration_id, &version_id)); |
| 120 | 121 |
| 121 ASSERT_FALSE(called); | 122 ASSERT_FALSE(called); |
| 122 base::RunLoop().RunUntilIdle(); | 123 base::RunLoop().RunUntilIdle(); |
| 123 EXPECT_TRUE(called); | 124 EXPECT_TRUE(called); |
| 124 | 125 |
| 125 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); | 126 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); |
| 126 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 127 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 127 ServiceWorkerMsg_InstallEvent::ID)); | 128 ServiceWorkerMsg_InstallEvent::ID)); |
| 128 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 129 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { | 162 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) { |
| 162 helper_.reset( | 163 helper_.reset( |
| 163 new RejectInstallTestHelper(context_.get(), render_process_id_)); | 164 new RejectInstallTestHelper(context_.get(), render_process_id_)); |
| 164 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 165 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 165 int64 version_id = kInvalidServiceWorkerVersionId; | 166 int64 version_id = kInvalidServiceWorkerVersionId; |
| 166 bool called = false; | 167 bool called = false; |
| 167 context_->RegisterServiceWorker( | 168 context_->RegisterServiceWorker( |
| 168 GURL("http://www.example.com/*"), | 169 GURL("http://www.example.com/*"), |
| 169 GURL("http://www.example.com/service_worker.js"), | 170 GURL("http://www.example.com/service_worker.js"), |
| 170 render_process_id_, | 171 render_process_id_, |
| 172 NULL, |
| 171 MakeRegisteredCallback(&called, ®istration_id, &version_id)); | 173 MakeRegisteredCallback(&called, ®istration_id, &version_id)); |
| 172 | 174 |
| 173 ASSERT_FALSE(called); | 175 ASSERT_FALSE(called); |
| 174 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 175 EXPECT_TRUE(called); | 177 EXPECT_TRUE(called); |
| 176 | 178 |
| 177 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); | 179 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); |
| 178 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 180 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( |
| 179 ServiceWorkerMsg_InstallEvent::ID)); | 181 ServiceWorkerMsg_InstallEvent::ID)); |
| 180 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 182 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 195 TEST_F(ServiceWorkerContextTest, Register_DuplicateScriptNoActiveWorker) { | 197 TEST_F(ServiceWorkerContextTest, Register_DuplicateScriptNoActiveWorker) { |
| 196 helper_.reset( | 198 helper_.reset( |
| 197 new RejectInstallTestHelper(context_.get(), render_process_id_)); | 199 new RejectInstallTestHelper(context_.get(), render_process_id_)); |
| 198 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 200 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| 199 int64 old_version_id = kInvalidServiceWorkerVersionId; | 201 int64 old_version_id = kInvalidServiceWorkerVersionId; |
| 200 bool called = false; | 202 bool called = false; |
| 201 context_->RegisterServiceWorker( | 203 context_->RegisterServiceWorker( |
| 202 GURL("http://www.example.com/*"), | 204 GURL("http://www.example.com/*"), |
| 203 GURL("http://www.example.com/service_worker.js"), | 205 GURL("http://www.example.com/service_worker.js"), |
| 204 render_process_id_, | 206 render_process_id_, |
| 207 NULL, |
| 205 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); | 208 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); |
| 206 | 209 |
| 207 ASSERT_FALSE(called); | 210 ASSERT_FALSE(called); |
| 208 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
| 209 EXPECT_TRUE(called); | 212 EXPECT_TRUE(called); |
| 210 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); | 213 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); |
| 211 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); | 214 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); |
| 212 | 215 |
| 213 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); | 216 EXPECT_EQ(2UL, helper_->ipc_sink()->message_count()); |
| 214 | 217 |
| 215 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; | 218 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; |
| 216 int64 new_version_id = kInvalidServiceWorkerVersionId; | 219 int64 new_version_id = kInvalidServiceWorkerVersionId; |
| 217 called = false; | 220 called = false; |
| 218 context_->RegisterServiceWorker( | 221 context_->RegisterServiceWorker( |
| 219 GURL("http://www.example.com/*"), | 222 GURL("http://www.example.com/*"), |
| 220 GURL("http://www.example.com/service_worker.js"), | 223 GURL("http://www.example.com/service_worker.js"), |
| 221 render_process_id_, | 224 render_process_id_, |
| 225 NULL, |
| 222 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); | 226 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); |
| 223 | 227 |
| 224 ASSERT_FALSE(called); | 228 ASSERT_FALSE(called); |
| 225 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
| 226 EXPECT_TRUE(called); | 230 EXPECT_TRUE(called); |
| 227 | 231 |
| 228 EXPECT_EQ(old_registration_id, new_registration_id); | 232 EXPECT_EQ(old_registration_id, new_registration_id); |
| 229 // Our current implementation does the full registration flow on re-register, | 233 // Our current implementation does the full registration flow on re-register, |
| 230 // so the worker receives another start message and install message. | 234 // so the worker receives another start message and install message. |
| 231 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); | 235 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count()); |
| 232 } | 236 } |
| 233 | 237 |
| 234 // Make sure registrations are cleaned up when they are unregistered. | 238 // Make sure registrations are cleaned up when they are unregistered. |
| 235 TEST_F(ServiceWorkerContextTest, Unregister) { | 239 TEST_F(ServiceWorkerContextTest, Unregister) { |
| 236 GURL pattern("http://www.example.com/*"); | 240 GURL pattern("http://www.example.com/*"); |
| 237 | 241 |
| 238 bool called = false; | 242 bool called = false; |
| 239 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 243 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 240 int64 version_id = kInvalidServiceWorkerVersionId; | 244 int64 version_id = kInvalidServiceWorkerVersionId; |
| 241 context_->RegisterServiceWorker( | 245 context_->RegisterServiceWorker( |
| 242 pattern, | 246 pattern, |
| 243 GURL("http://www.example.com/service_worker.js"), | 247 GURL("http://www.example.com/service_worker.js"), |
| 244 render_process_id_, | 248 render_process_id_, |
| 249 NULL, |
| 245 MakeRegisteredCallback(&called, ®istration_id, &version_id)); | 250 MakeRegisteredCallback(&called, ®istration_id, &version_id)); |
| 246 | 251 |
| 247 ASSERT_FALSE(called); | 252 ASSERT_FALSE(called); |
| 248 base::RunLoop().RunUntilIdle(); | 253 base::RunLoop().RunUntilIdle(); |
| 249 ASSERT_TRUE(called); | 254 ASSERT_TRUE(called); |
| 250 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); | 255 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); |
| 251 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id); | 256 EXPECT_NE(kInvalidServiceWorkerVersionId, version_id); |
| 252 | 257 |
| 253 called = false; | 258 called = false; |
| 254 context_->UnregisterServiceWorker( | 259 context_->UnregisterServiceWorker( |
| 255 pattern, render_process_id_, MakeUnregisteredCallback(&called)); | 260 pattern, render_process_id_, NULL, MakeUnregisteredCallback(&called)); |
| 256 | 261 |
| 257 ASSERT_FALSE(called); | 262 ASSERT_FALSE(called); |
| 258 base::RunLoop().RunUntilIdle(); | 263 base::RunLoop().RunUntilIdle(); |
| 259 ASSERT_TRUE(called); | 264 ASSERT_TRUE(called); |
| 260 | 265 |
| 261 context_->storage()->FindRegistrationForId( | 266 context_->storage()->FindRegistrationForId( |
| 262 registration_id, | 267 registration_id, |
| 263 base::Bind(&ExpectRegisteredWorkers, | 268 base::Bind(&ExpectRegisteredWorkers, |
| 264 SERVICE_WORKER_ERROR_NOT_FOUND, | 269 SERVICE_WORKER_ERROR_NOT_FOUND, |
| 265 kInvalidServiceWorkerVersionId, | 270 kInvalidServiceWorkerVersionId, |
| 266 false /* expect_pending */, | 271 false /* expect_pending */, |
| 267 false /* expect_active */)); | 272 false /* expect_active */)); |
| 268 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
| 269 } | 274 } |
| 270 | 275 |
| 271 // Make sure that when a new registration replaces an existing | 276 // Make sure that when a new registration replaces an existing |
| 272 // registration, that the old one is cleaned up. | 277 // registration, that the old one is cleaned up. |
| 273 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 278 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| 274 GURL pattern("http://www.example.com/*"); | 279 GURL pattern("http://www.example.com/*"); |
| 275 | 280 |
| 276 bool called = false; | 281 bool called = false; |
| 277 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 282 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| 278 int64 old_version_id = kInvalidServiceWorkerVersionId; | 283 int64 old_version_id = kInvalidServiceWorkerVersionId; |
| 279 context_->RegisterServiceWorker( | 284 context_->RegisterServiceWorker( |
| 280 pattern, | 285 pattern, |
| 281 GURL("http://www.example.com/service_worker.js"), | 286 GURL("http://www.example.com/service_worker.js"), |
| 282 render_process_id_, | 287 render_process_id_, |
| 288 NULL, |
| 283 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); | 289 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); |
| 284 | 290 |
| 285 ASSERT_FALSE(called); | 291 ASSERT_FALSE(called); |
| 286 base::RunLoop().RunUntilIdle(); | 292 base::RunLoop().RunUntilIdle(); |
| 287 ASSERT_TRUE(called); | 293 ASSERT_TRUE(called); |
| 288 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); | 294 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); |
| 289 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); | 295 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); |
| 290 | 296 |
| 291 called = false; | 297 called = false; |
| 292 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; | 298 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; |
| 293 int64 new_version_id = kInvalidServiceWorkerVersionId; | 299 int64 new_version_id = kInvalidServiceWorkerVersionId; |
| 294 context_->RegisterServiceWorker( | 300 context_->RegisterServiceWorker( |
| 295 pattern, | 301 pattern, |
| 296 GURL("http://www.example.com/service_worker_new.js"), | 302 GURL("http://www.example.com/service_worker_new.js"), |
| 297 render_process_id_, | 303 render_process_id_, |
| 304 NULL, |
| 298 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); | 305 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); |
| 299 | 306 |
| 300 ASSERT_FALSE(called); | 307 ASSERT_FALSE(called); |
| 301 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
| 302 ASSERT_TRUE(called); | 309 ASSERT_TRUE(called); |
| 303 | 310 |
| 304 // Returned IDs should be valid, and should differ from the values | 311 // Returned IDs should be valid, and should differ from the values |
| 305 // returned for the previous registration. | 312 // returned for the previous registration. |
| 306 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); | 313 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); |
| 307 EXPECT_NE(kInvalidServiceWorkerVersionId, new_version_id); | 314 EXPECT_NE(kInvalidServiceWorkerVersionId, new_version_id); |
| 308 EXPECT_NE(old_registration_id, new_registration_id); | 315 EXPECT_NE(old_registration_id, new_registration_id); |
| 309 EXPECT_NE(old_version_id, new_version_id); | 316 EXPECT_NE(old_version_id, new_version_id); |
| 310 } | 317 } |
| 311 | 318 |
| 312 // Make sure that when registering a duplicate pattern+script_url | 319 // Make sure that when registering a duplicate pattern+script_url |
| 313 // combination, that the same registration is used. | 320 // combination, that the same registration is used. |
| 314 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 321 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 315 GURL pattern("http://www.example.com/*"); | 322 GURL pattern("http://www.example.com/*"); |
| 316 GURL script_url("http://www.example.com/service_worker.js"); | 323 GURL script_url("http://www.example.com/service_worker.js"); |
| 317 | 324 |
| 318 bool called = false; | 325 bool called = false; |
| 319 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; | 326 int64 old_registration_id = kInvalidServiceWorkerRegistrationId; |
| 320 int64 old_version_id = kInvalidServiceWorkerVersionId; | 327 int64 old_version_id = kInvalidServiceWorkerVersionId; |
| 321 context_->RegisterServiceWorker( | 328 context_->RegisterServiceWorker( |
| 322 pattern, | 329 pattern, |
| 323 script_url, | 330 script_url, |
| 324 render_process_id_, | 331 render_process_id_, |
| 332 NULL, |
| 325 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); | 333 MakeRegisteredCallback(&called, &old_registration_id, &old_version_id)); |
| 326 | 334 |
| 327 ASSERT_FALSE(called); | 335 ASSERT_FALSE(called); |
| 328 base::RunLoop().RunUntilIdle(); | 336 base::RunLoop().RunUntilIdle(); |
| 329 ASSERT_TRUE(called); | 337 ASSERT_TRUE(called); |
| 330 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); | 338 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); |
| 331 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); | 339 EXPECT_NE(kInvalidServiceWorkerVersionId, old_version_id); |
| 332 | 340 |
| 333 called = false; | 341 called = false; |
| 334 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; | 342 int64 new_registration_id = kInvalidServiceWorkerRegistrationId; |
| 335 int64 new_version_id = kInvalidServiceWorkerVersionId; | 343 int64 new_version_id = kInvalidServiceWorkerVersionId; |
| 336 context_->RegisterServiceWorker( | 344 context_->RegisterServiceWorker( |
| 337 pattern, | 345 pattern, |
| 338 script_url, | 346 script_url, |
| 339 render_process_id_, | 347 render_process_id_, |
| 348 NULL, |
| 340 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); | 349 MakeRegisteredCallback(&called, &new_registration_id, &new_version_id)); |
| 341 | 350 |
| 342 ASSERT_FALSE(called); | 351 ASSERT_FALSE(called); |
| 343 base::RunLoop().RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
| 344 ASSERT_TRUE(called); | 353 ASSERT_TRUE(called); |
| 345 EXPECT_EQ(old_registration_id, new_registration_id); | 354 EXPECT_EQ(old_registration_id, new_registration_id); |
| 346 EXPECT_EQ(old_version_id, new_version_id); | 355 EXPECT_EQ(old_version_id, new_version_id); |
| 347 } | 356 } |
| 348 | 357 |
| 349 } // namespace content | 358 } // namespace content |
| OLD | NEW |