Commit 37c343f0 authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Move StoreFindUpdateDeleteRegistration test

The tests not only tests storage operations but also tests behavior
of ServiceWorkerRegistry as FindRegistration methods returns
different results based on the state of ServiceWorkerRegistrations.
ServiceWorkerRegistryTest is a better place to place this kind of
test.

Bug: 1016064
Change-Id: I28b6a2bc56637d902cc11a645e3bc20d2c5c5692
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2467576
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Auto-Submit: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#817356}
parent f33423c5
......@@ -479,18 +479,6 @@ class ServiceWorkerStorageTest : public testing::Test {
return result.value();
}
blink::ServiceWorkerStatusCode UpdateLastUpdateCheckTime(
scoped_refptr<ServiceWorkerRegistration> registration) {
base::RunLoop loop;
base::Optional<blink::ServiceWorkerStatusCode> result;
registry()->UpdateLastUpdateCheckTime(
registration->id(), registration->scope().GetOrigin(),
registration->last_update_check(),
base::BindOnce(&StatusCallback, loop.QuitClosure(), &result));
loop.Run();
return result.value();
}
blink::ServiceWorkerStatusCode FindRegistrationForClientUrl(
const GURL& document_url,
scoped_refptr<ServiceWorkerRegistration>* registration) {
......@@ -700,195 +688,6 @@ TEST_F(ServiceWorkerStorageTest, DisabledStorage) {
EXPECT_EQ(blink::mojom::kInvalidServiceWorkerResourceId, GetNewResourceId());
}
TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) {
const GURL kScope("http://www.test.not/scope/");
const GURL kDocumentUrl("http://www.test.not/scope/document.html");
const GURL kResource1("http://www.test.not/scope/resource1.js");
const int64_t kResource1Size = 1591234;
const GURL kResource2("http://www.test.not/scope/resource2.js");
const int64_t kResource2Size = 51;
const int64_t kRegistrationId = 0;
const int64_t kVersionId = 0;
const base::Time kToday = base::Time::Now();
const base::Time kYesterday = kToday - base::TimeDelta::FromDays(1);
std::set<blink::mojom::WebFeature> used_features = {
blink::mojom::WebFeature::kServiceWorkerControlledPage,
blink::mojom::WebFeature::kReferrerPolicyHeader,
blink::mojom::WebFeature::kLocationOrigin};
scoped_refptr<ServiceWorkerRegistration> found_registration;
// We shouldn't find anything without having stored anything.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kErrorNotFound,
FindRegistrationForClientUrl(kDocumentUrl, &found_registration));
EXPECT_FALSE(found_registration.get());
EXPECT_EQ(blink::ServiceWorkerStatusCode::kErrorNotFound,
FindRegistrationForScope(kScope, &found_registration));
EXPECT_FALSE(found_registration.get());
EXPECT_EQ(blink::ServiceWorkerStatusCode::kErrorNotFound,
FindRegistrationForId(kRegistrationId, url::Origin::Create(kScope),
&found_registration));
EXPECT_FALSE(found_registration.get());
std::vector<ResourceRecord> resources;
resources.push_back(CreateResourceRecord(1, kResource1, kResource1Size));
resources.push_back(CreateResourceRecord(2, kResource2, kResource2Size));
// Store something.
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = kScope;
scoped_refptr<ServiceWorkerRegistration> live_registration =
new ServiceWorkerRegistration(options, kRegistrationId,
context()->AsWeakPtr());
scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion(
live_registration.get(), kResource1, blink::mojom::ScriptType::kClassic,
kVersionId,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>(),
context()->AsWeakPtr());
live_version->set_fetch_handler_existence(
ServiceWorkerVersion::FetchHandlerExistence::EXISTS);
live_version->SetStatus(ServiceWorkerVersion::INSTALLED);
live_version->script_cache_map()->SetResources(resources);
live_version->set_used_features(
std::set<blink::mojom::WebFeature>(used_features));
network::CrossOriginEmbedderPolicy coep_require_corp;
coep_require_corp.value =
network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp;
live_version->set_cross_origin_embedder_policy(coep_require_corp);
live_registration->SetWaitingVersion(live_version);
live_registration->set_last_update_check(kYesterday);
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
StoreRegistration(live_registration, live_version));
// Now we should find it and get the live ptr back immediately.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForClientUrl(kDocumentUrl, &found_registration));
EXPECT_EQ(live_registration, found_registration);
EXPECT_EQ(kResource1Size + kResource2Size,
live_registration->resources_total_size_bytes());
EXPECT_EQ(kResource1Size + kResource2Size,
found_registration->resources_total_size_bytes());
EXPECT_EQ(used_features,
found_registration->waiting_version()->used_features());
EXPECT_EQ(
found_registration->waiting_version()->cross_origin_embedder_policy(),
coep_require_corp);
found_registration = nullptr;
// But FindRegistrationForScope is always async.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForScope(kScope, &found_registration));
EXPECT_EQ(live_registration, found_registration);
found_registration = nullptr;
// Can be found by id too.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForId(kRegistrationId, url::Origin::Create(kScope),
&found_registration));
ASSERT_TRUE(found_registration.get());
EXPECT_EQ(kRegistrationId, found_registration->id());
EXPECT_EQ(live_registration, found_registration);
found_registration = nullptr;
// Can be found by just the id too.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForIdOnly(kRegistrationId, &found_registration));
ASSERT_TRUE(found_registration.get());
EXPECT_EQ(kRegistrationId, found_registration->id());
EXPECT_EQ(live_registration, found_registration);
found_registration = nullptr;
// Drop the live registration, but keep the version live.
live_registration = nullptr;
// Now FindRegistrationForClientUrl should be async.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForClientUrl(kDocumentUrl, &found_registration));
ASSERT_TRUE(found_registration.get());
EXPECT_EQ(kRegistrationId, found_registration->id());
EXPECT_TRUE(found_registration->HasOneRef());
// Check that sizes are populated correctly
EXPECT_EQ(live_version.get(), found_registration->waiting_version());
EXPECT_EQ(kResource1Size + kResource2Size,
found_registration->resources_total_size_bytes());
std::vector<ServiceWorkerRegistrationInfo> all_registrations;
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
GetAllRegistrationsInfos(&all_registrations));
EXPECT_EQ(1u, all_registrations.size());
ServiceWorkerRegistrationInfo info = all_registrations[0];
EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
all_registrations.clear();
// Finding by origin should provide the same result if origin is kScope.
std::vector<scoped_refptr<ServiceWorkerRegistration>>
registrations_for_origin;
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
GetRegistrationsForOrigin(url::Origin::Create(kScope),
&registrations_for_origin));
EXPECT_EQ(1u, registrations_for_origin.size());
registrations_for_origin.clear();
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
GetRegistrationsForOrigin(
url::Origin::Create(GURL("http://example.com/")),
&registrations_for_origin));
EXPECT_TRUE(registrations_for_origin.empty());
found_registration = nullptr;
// Drop the live version too.
live_version = nullptr;
// And FindRegistrationForScope is always async.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForScope(kScope, &found_registration));
ASSERT_TRUE(found_registration.get());
EXPECT_EQ(kRegistrationId, found_registration->id());
EXPECT_TRUE(found_registration->HasOneRef());
EXPECT_FALSE(found_registration->active_version());
ASSERT_TRUE(found_registration->waiting_version());
EXPECT_EQ(kYesterday, found_registration->last_update_check());
EXPECT_EQ(ServiceWorkerVersion::INSTALLED,
found_registration->waiting_version()->status());
// Update to active and update the last check time.
scoped_refptr<ServiceWorkerVersion> temp_version =
found_registration->waiting_version();
temp_version->SetStatus(ServiceWorkerVersion::ACTIVATED);
found_registration->SetActiveVersion(temp_version);
temp_version = nullptr;
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
UpdateToActiveState(found_registration));
found_registration->set_last_update_check(kToday);
UpdateLastUpdateCheckTime(found_registration.get());
found_registration = nullptr;
// Trying to update a unstored registration to active should fail.
scoped_refptr<ServiceWorkerRegistration> unstored_registration =
new ServiceWorkerRegistration(options, kRegistrationId + 1,
context()->AsWeakPtr());
EXPECT_EQ(blink::ServiceWorkerStatusCode::kErrorNotFound,
UpdateToActiveState(unstored_registration));
unstored_registration = nullptr;
// The Find methods should return a registration with an active version
// and the expected update time.
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk,
FindRegistrationForClientUrl(kDocumentUrl, &found_registration));
ASSERT_TRUE(found_registration.get());
EXPECT_EQ(kRegistrationId, found_registration->id());
EXPECT_TRUE(found_registration->HasOneRef());
EXPECT_FALSE(found_registration->waiting_version());
ASSERT_TRUE(found_registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
found_registration->active_version()->status());
EXPECT_EQ(kToday, found_registration->last_update_check());
}
TEST_F(ServiceWorkerStorageTest, StoreUserData) {
const int64_t kRegistrationId = 1;
const GURL kScope("http://www.test.not/scope/");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment