chromium/content/browser/service_worker/service_worker_registry_unittest.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/service_worker_registry.h"

#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/services/storage/service_worker/service_worker_storage.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "storage/browser/test/quota_manager_proxy_sync.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/origin_trials/scoped_test_origin_trial_policy.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"

namespace content {

namespace {

Eq;
Pointee;

struct ReadResponseHeadResult {};

struct GetStorageUsageForStorageKeyResult {};

storage::mojom::ServiceWorkerResourceRecordPtr
CreateResourceRecord(int64_t resource_id, const GURL& url, int64_t size_bytes) {}

storage::mojom::ServiceWorkerRegistrationDataPtr CreateRegistrationData(
    int64_t registration_id,
    int64_t version_id,
    const GURL& scope,
    const blink::StorageKey& key,
    const GURL& script_url,
    const std::vector<storage::mojom::ServiceWorkerResourceRecordPtr>&
        resources) {}

int WriteResponse(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id,
    const std::string& headers,
    mojo_base::BigBuffer body) {}

int WriteStringResponse(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id,
    const std::string& headers,
    const std::string& body) {}

int WriteBasicResponse(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id) {}

ReadResponseHeadResult ReadResponseHead(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id) {}

bool VerifyBasicResponse(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id,
    bool expected_positive_result) {}

int WriteResponseMetadata(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id,
    const std::string& metadata) {}

int WriteMetadata(ServiceWorkerVersion* version,
                  const GURL& url,
                  const std::string& metadata) {}

int ClearMetadata(ServiceWorkerVersion* version, const GURL& url) {}

bool VerifyResponseMetadata(
    mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
    int64_t id,
    const std::string& expected_metadata) {}

}  // namespace

class ServiceWorkerRegistryTest : public testing::Test {};

TEST_F(ServiceWorkerRegistryTest, RegisteredStorageKeyCount) {}

TEST_F(ServiceWorkerRegistryTest, CreateNewRegistration) {}

TEST_F(ServiceWorkerRegistryTest, GetOrCreateBucketError) {}

TEST_F(ServiceWorkerRegistryTest, StoreFindUpdateDeleteRegistration) {}

TEST_F(ServiceWorkerRegistryTest, InstallingRegistrationsAreFindable) {}

TEST_F(ServiceWorkerRegistryTest, FindRegistration_LongestScopeMatch) {}

class ServiceWorkerRegistryMergeTest
    : public ServiceWorkerRegistryTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(ServiceWorkerRegistryMergeTest, MergeDuplicateFindRegistrationCalls) {}

class ServiceWorkerScopeAndRegistrationCacheTest
    : public ServiceWorkerRegistryTest {};

TEST_F(ServiceWorkerScopeAndRegistrationCacheTest, SkipMojoCallIfPossible) {}

TEST_F(ServiceWorkerScopeAndRegistrationCacheTest,
       RegistrationCacheSizeAndScopeCacheLimitPerKey) {}

TEST_F(ServiceWorkerScopeAndRegistrationCacheTest, CanHandleNewRegistration) {}

TEST_F(ServiceWorkerScopeAndRegistrationCacheTest,
       ServiceWorkerScopeCacheLimit) {}

// Tests that fields of ServiceWorkerRegistrationInfo are filled correctly.
TEST_F(ServiceWorkerRegistryTest, RegistrationInfoFields) {}

TEST_F(ServiceWorkerRegistryTest, OriginTrialsAbsentEntryAndEmptyEntry) {}

// Tests loading a registration that has no navigation preload state.
TEST_F(ServiceWorkerRegistryTest, AbsentNavigationPreloadState) {}

// Tests loading a registration with an enabled navigation preload state, as
// well as a custom header value.
TEST_F(ServiceWorkerRegistryTest, EnabledNavigationPreloadState) {}

// Tests storing the script response time for DevTools.
TEST_F(ServiceWorkerRegistryTest, ScriptResponseTime) {}

// Tests loading a registration with a disabled navigation preload
// state.
TEST_F(ServiceWorkerRegistryTest, DisabledNavigationPreloadState) {}

// Tests that storage policy changes are observed.
TEST_F(ServiceWorkerRegistryTest, StoragePolicyChange) {}

// Tests that callbacks of storage operations are always called even when the
// remote storage is disconnected.
TEST_F(ServiceWorkerRegistryTest, RemoteStorageDisconnection) {}

// Tests that inflight remote calls are retried after the remote storage is
// restarted.
TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls) {}

// Tests that FindRegistrationForId methods are retried after the remote storage
// is restarted. Separated from other FindRegistration method tests because
// these methods skip remote calls when live registrations are alive.
TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_FindRegistrationForId) {}

TEST_F(ServiceWorkerRegistryTest,
       RetryInflightCalls_CreateNewRegistrationAndVersion) {}

TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_UserData) {}

TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_DeleteAndStartOver) {}

TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_PerformStorageCleanup) {}

TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_Disable) {}

// Similar to `StoragePolicyChange` test but restart the remote storage to make
// sure ApplyPolicyUpdates() is retried.
TEST_F(ServiceWorkerRegistryTest, RetryInflightCalls_ApplyPolicyUpdates) {}

// Regression test for https://crbug.com/1165784.
// Tests that callbacks of ServiceWorkerRegistry are always called. Calls
// ServiceWorkerRegistry methods and destroys the instance immediately by
// simulating restarts.
TEST_F(ServiceWorkerRegistryTest, DestroyRegistryDuringInflightCall) {}

TEST_F(ServiceWorkerRegistryTest,
       DestroyRegistryDuringInflightCall_StoreUserData) {}

TEST_F(ServiceWorkerRegistryTest,
       DestroyRegistryDuringInflightCall_ClearUserData) {}

TEST_F(ServiceWorkerRegistryTest,
       DestroyRegistryDuringInflightCall_ClearUserDataByKeyPrefixes) {}

TEST_F(
    ServiceWorkerRegistryTest,
    DestroyRegistryDuringInflightCall_ClearUserDataForAllRegistrationsByKeyPrefix) {}

TEST_F(ServiceWorkerRegistryTest,
       DestroyRegistryDuringInflightCall_GetUserDataForAllRegistrations) {}

TEST_F(
    ServiceWorkerRegistryTest,
    DestroyRegistryDuringInflightCall_GetUserDataForAllRegistrationsByKeyPrefix) {}

// Tests loading registration that has hid event handlers.
TEST_F(ServiceWorkerRegistryTest, HasHidEventHandler) {}

// Tests loading registration that does not have hid event handlers.
TEST_F(ServiceWorkerRegistryTest, NoHidEventHandler) {}

// Tests loading registration that has usb event handlers.
TEST_F(ServiceWorkerRegistryTest, HasUsbEventHandler) {}

// Tests loading registration that does not have usb event handlers.
TEST_F(ServiceWorkerRegistryTest, NoUsbEventHandler) {}

class ServiceWorkerRegistryOriginTrialsTest : public ServiceWorkerRegistryTest {};

TEST_F(ServiceWorkerRegistryOriginTrialsTest, FromMainScript) {}

class ServiceWorkerRegistryResourceTest : public ServiceWorkerRegistryTest {};

TEST_F(ServiceWorkerRegistryResourceTest,
       WriteMetadataWithServiceWorkerResponseMetadataWriter) {}

TEST_F(ServiceWorkerRegistryResourceTest,
       WriteMetadataWithServiceWorkerScriptCacheMap) {}

TEST_F(ServiceWorkerRegistryResourceTest, DeleteRegistration_NoLiveVersion) {}

TEST_F(ServiceWorkerRegistryResourceTest, DeleteRegistration_WaitingVersion) {}

TEST_F(ServiceWorkerRegistryResourceTest, DeleteRegistration_ActiveVersion) {}

TEST_F(ServiceWorkerRegistryResourceTest, UpdateRegistration) {}

TEST_F(ServiceWorkerRegistryResourceTest, UpdateRegistration_NoLiveVersion) {}

TEST_F(ServiceWorkerRegistryResourceTest, CleanupOnRestart) {}

// Tests resource purging with storage service restarts.
TEST_F(ServiceWorkerRegistryResourceTest, Restart_LiveVersion) {}

// Tests that StoreUncommittedResourceId() and DoomUncommittedResource() are
// automatically retried after storage restarts.
TEST_F(ServiceWorkerRegistryResourceTest, RetryInflightCalls_Resources) {}

}  // namespace content