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 <string> | 5 #include <string> |
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/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 320 |
321 scoped_ptr<ServiceWorkerContextCore> context_; | 321 scoped_ptr<ServiceWorkerContextCore> context_; |
322 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 322 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
323 TestBrowserThreadBundle browser_thread_bundle_; | 323 TestBrowserThreadBundle browser_thread_bundle_; |
324 }; | 324 }; |
325 | 325 |
326 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { | 326 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { |
327 const GURL kScope("http://www.test.not/scope/"); | 327 const GURL kScope("http://www.test.not/scope/"); |
328 const GURL kScript("http://www.test.not/script.js"); | 328 const GURL kScript("http://www.test.not/script.js"); |
329 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); | 329 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); |
| 330 const GURL kResource1("http://www.test.not/scope/resource1.js"); |
| 331 const int64 kResource1Size = 1591234; |
| 332 const GURL kResource2("http://www.test.not/scope/resource2.js"); |
| 333 const int64 kResource2Size = 51; |
330 const int64 kRegistrationId = 0; | 334 const int64 kRegistrationId = 0; |
331 const int64 kVersionId = 0; | 335 const int64 kVersionId = 0; |
332 const base::Time kToday = base::Time::Now(); | 336 const base::Time kToday = base::Time::Now(); |
333 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1); | 337 const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1); |
334 | 338 |
335 scoped_refptr<ServiceWorkerRegistration> found_registration; | 339 scoped_refptr<ServiceWorkerRegistration> found_registration; |
336 | 340 |
337 // We shouldn't find anything without having stored anything. | 341 // We shouldn't find anything without having stored anything. |
338 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 342 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
339 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 343 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
340 EXPECT_FALSE(found_registration.get()); | 344 EXPECT_FALSE(found_registration.get()); |
341 | 345 |
342 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 346 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
343 FindRegistrationForPattern(kScope, &found_registration)); | 347 FindRegistrationForPattern(kScope, &found_registration)); |
344 EXPECT_FALSE(found_registration.get()); | 348 EXPECT_FALSE(found_registration.get()); |
345 | 349 |
346 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, | 350 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, |
347 FindRegistrationForId( | 351 FindRegistrationForId( |
348 kRegistrationId, kScope.GetOrigin(), &found_registration)); | 352 kRegistrationId, kScope.GetOrigin(), &found_registration)); |
349 EXPECT_FALSE(found_registration.get()); | 353 EXPECT_FALSE(found_registration.get()); |
350 | 354 |
| 355 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 356 resources.push_back( |
| 357 ServiceWorkerDatabase::ResourceRecord(1, kResource1, kResource1Size)); |
| 358 resources.push_back( |
| 359 ServiceWorkerDatabase::ResourceRecord(2, kResource2, kResource2Size)); |
| 360 |
351 // Store something. | 361 // Store something. |
352 scoped_refptr<ServiceWorkerRegistration> live_registration = | 362 scoped_refptr<ServiceWorkerRegistration> live_registration = |
353 new ServiceWorkerRegistration( | 363 new ServiceWorkerRegistration( |
354 kScope, kRegistrationId, context_ptr_); | 364 kScope, kRegistrationId, context_ptr_); |
355 scoped_refptr<ServiceWorkerVersion> live_version = | 365 scoped_refptr<ServiceWorkerVersion> live_version = |
356 new ServiceWorkerVersion( | 366 new ServiceWorkerVersion( |
357 live_registration.get(), kScript, kVersionId, context_ptr_); | 367 live_registration.get(), kScript, kVersionId, context_ptr_); |
358 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); | 368 live_version->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 369 live_version->script_cache_map()->SetResources(resources); |
359 live_registration->SetWaitingVersion(live_version.get()); | 370 live_registration->SetWaitingVersion(live_version.get()); |
360 live_registration->set_last_update_check(kYesterday); | 371 live_registration->set_last_update_check(kYesterday); |
361 EXPECT_EQ(SERVICE_WORKER_OK, | 372 EXPECT_EQ(SERVICE_WORKER_OK, |
362 StoreRegistration(live_registration, live_version)); | 373 StoreRegistration(live_registration, live_version)); |
363 | 374 |
364 // Now we should find it and get the live ptr back immediately. | 375 // Now we should find it and get the live ptr back immediately. |
365 EXPECT_EQ(SERVICE_WORKER_OK, | 376 EXPECT_EQ(SERVICE_WORKER_OK, |
366 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 377 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
367 EXPECT_EQ(live_registration, found_registration); | 378 EXPECT_EQ(live_registration, found_registration); |
| 379 EXPECT_EQ(kResource1Size + kResource2Size, |
| 380 live_registration->resources_total_size_bytes()); |
| 381 EXPECT_EQ(kResource1Size + kResource2Size, |
| 382 found_registration->resources_total_size_bytes()); |
368 found_registration = NULL; | 383 found_registration = NULL; |
369 | 384 |
370 // But FindRegistrationForPattern is always async. | 385 // But FindRegistrationForPattern is always async. |
371 EXPECT_EQ(SERVICE_WORKER_OK, | 386 EXPECT_EQ(SERVICE_WORKER_OK, |
372 FindRegistrationForPattern(kScope, &found_registration)); | 387 FindRegistrationForPattern(kScope, &found_registration)); |
373 EXPECT_EQ(live_registration, found_registration); | 388 EXPECT_EQ(live_registration, found_registration); |
374 found_registration = NULL; | 389 found_registration = NULL; |
375 | 390 |
376 // Can be found by id too. | 391 // Can be found by id too. |
377 EXPECT_EQ(SERVICE_WORKER_OK, | 392 EXPECT_EQ(SERVICE_WORKER_OK, |
378 FindRegistrationForId( | 393 FindRegistrationForId( |
379 kRegistrationId, kScope.GetOrigin(), &found_registration)); | 394 kRegistrationId, kScope.GetOrigin(), &found_registration)); |
380 ASSERT_TRUE(found_registration.get()); | 395 ASSERT_TRUE(found_registration.get()); |
381 EXPECT_EQ(kRegistrationId, found_registration->id()); | 396 EXPECT_EQ(kRegistrationId, found_registration->id()); |
382 EXPECT_EQ(live_registration, found_registration); | 397 EXPECT_EQ(live_registration, found_registration); |
383 found_registration = NULL; | 398 found_registration = NULL; |
384 | 399 |
385 // Drop the live registration, but keep the version live. | 400 // Drop the live registration, but keep the version live. |
386 live_registration = NULL; | 401 live_registration = NULL; |
387 | 402 |
388 // Now FindRegistrationForDocument should be async. | 403 // Now FindRegistrationForDocument should be async. |
389 EXPECT_EQ(SERVICE_WORKER_OK, | 404 EXPECT_EQ(SERVICE_WORKER_OK, |
390 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 405 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
391 ASSERT_TRUE(found_registration.get()); | 406 ASSERT_TRUE(found_registration.get()); |
392 EXPECT_EQ(kRegistrationId, found_registration->id()); | 407 EXPECT_EQ(kRegistrationId, found_registration->id()); |
393 EXPECT_TRUE(found_registration->HasOneRef()); | 408 EXPECT_TRUE(found_registration->HasOneRef()); |
| 409 |
| 410 // Check that sizes are populated correctly |
394 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); | 411 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); |
| 412 EXPECT_EQ(kResource1Size + kResource2Size, |
| 413 found_registration->resources_total_size_bytes()); |
| 414 std::vector<ServiceWorkerRegistrationInfo> all_registrations; |
| 415 GetAllRegistrations(&all_registrations); |
| 416 EXPECT_EQ(1u, all_registrations.size()); |
| 417 ServiceWorkerRegistrationInfo info = all_registrations[0]; |
| 418 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes); |
| 419 all_registrations.clear(); |
| 420 |
395 found_registration = NULL; | 421 found_registration = NULL; |
396 | 422 |
397 // Drop the live version too. | 423 // Drop the live version too. |
398 live_version = NULL; | 424 live_version = NULL; |
399 | 425 |
400 // And FindRegistrationForPattern is always async. | 426 // And FindRegistrationForPattern is always async. |
401 EXPECT_EQ(SERVICE_WORKER_OK, | 427 EXPECT_EQ(SERVICE_WORKER_OK, |
402 FindRegistrationForPattern(kScope, &found_registration)); | 428 FindRegistrationForPattern(kScope, &found_registration)); |
403 ASSERT_TRUE(found_registration.get()); | 429 ASSERT_TRUE(found_registration.get()); |
404 EXPECT_EQ(kRegistrationId, found_registration->id()); | 430 EXPECT_EQ(kRegistrationId, found_registration->id()); |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 are_equal = true; | 1073 are_equal = true; |
1048 storage()->CompareScriptResources( | 1074 storage()->CompareScriptResources( |
1049 5, 6, | 1075 5, 6, |
1050 base::Bind(&OnCompareComplete, &status, &are_equal)); | 1076 base::Bind(&OnCompareComplete, &status, &are_equal)); |
1051 base::RunLoop().RunUntilIdle(); | 1077 base::RunLoop().RunUntilIdle(); |
1052 EXPECT_EQ(SERVICE_WORKER_OK, status); | 1078 EXPECT_EQ(SERVICE_WORKER_OK, status); |
1053 EXPECT_FALSE(are_equal); | 1079 EXPECT_FALSE(are_equal); |
1054 } | 1080 } |
1055 | 1081 |
1056 } // namespace content | 1082 } // namespace content |
OLD | NEW |