chromium/content/browser/service_worker/service_worker_version_unittest.cc

// Copyright 2014 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_version.h"

#include <stdint.h>

#include <memory>
#include <tuple>
#include <utility>

#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/fake_embedded_worker_instance_client.h"
#include "content/browser/service_worker/fake_service_worker.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_ping_controller.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_service.mojom.h"
#include "content/public/test/test_utils.h"
#include "net/base/test_completion_callback.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/service_worker/service_worker_router_rule.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_installed_scripts_manager.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"

namespace content {
namespace service_worker_version_unittest {

constexpr base::TimeDelta kTestTimeoutBeyondRequestTimeout =// Value of kRequestTimeout in service_worker_version.cc
    base::Minutes(5) +
    // A little past that.
    base::Minutes(1);

base::OnceCallback<void()> VerifyCalled(
    bool* called,
    base::OnceClosure quit_closure = base::OnceClosure()) {}

void ObserveStatusChanges(ServiceWorkerVersion* version,
                          std::vector<ServiceWorkerVersion::Status>* statuses) {}

base::Time GetYesterday() {}

enum class StorageKeyTestCase {};

class ServiceWorkerVersionTest
    : public testing::Test,
      public testing::WithParamInterface<StorageKeyTestCase> {};

INSTANTIATE_TEST_SUITE_P(
    All,
    ServiceWorkerVersionTest,
    testing::ValuesIn({};

// An instance client that breaks the Mojo connection upon receiving the
// Start() message.
class FailStartInstanceClient : public FakeEmbeddedWorkerInstanceClient {};

TEST_P(ServiceWorkerVersionTest, ConcurrentStartAndStop) {}

TEST_P(ServiceWorkerVersionTest, DispatchEventToStoppedWorker) {}

TEST_P(ServiceWorkerVersionTest, StartUnregisteredButStillLiveWorker) {}

TEST_P(ServiceWorkerVersionTest, InstallAndWaitCompletion) {}

TEST_P(ServiceWorkerVersionTest, ActivateAndWaitCompletion) {}

TEST_P(ServiceWorkerVersionTest, RepeatedlyObserveStatusChanges) {}

TEST_P(ServiceWorkerVersionTest, Doom) {}

TEST_P(ServiceWorkerVersionTest, SetDevToolsAttached) {}

// Tests that a worker containing external request with
// ServiceWorkerExternalRequestTimeoutType::kDoesNotTimeout does not stop
// if devtools is detached (after it is attached).
//
// Regression test for crbug.com/1152255#c144
TEST_P(ServiceWorkerVersionTest, DevToolsAttachThenDetach) {}

TEST_P(ServiceWorkerVersionTest, RequestTerminationWithDevToolsAttached) {}

// Test that update isn't triggered for a non-stale worker.
TEST_P(ServiceWorkerVersionTest, StaleUpdate_FreshWorker) {}

// Test that update isn't triggered for a non-active worker.
TEST_P(ServiceWorkerVersionTest, StaleUpdate_NonActiveWorker) {}

// Test that staleness is detected when starting a worker.
TEST_P(ServiceWorkerVersionTest, StaleUpdate_StartWorker) {}

// Test that staleness is detected on a running worker.
TEST_P(ServiceWorkerVersionTest, StaleUpdate_RunningWorker) {}

// Test that a stream of events doesn't restart the timer.
TEST_P(ServiceWorkerVersionTest, StaleUpdate_DoNotDeferTimer) {}

TEST_P(ServiceWorkerVersionTest, StartRequestWithNullContext) {}

// Tests the delay mechanism for self-updating service workers, to prevent
// them from running forever (see https://crbug.com/805496).
TEST_P(ServiceWorkerVersionTest, ResetUpdateDelay) {}

TEST_P(ServiceWorkerVersionTest, UpdateCachedMetadata) {}

TEST_P(ServiceWorkerVersionTest, RestartWorker) {}

class DelayMessageWorker : public FakeServiceWorker {};

TEST_P(ServiceWorkerVersionTest, RequestTimeout) {}

TEST_P(ServiceWorkerVersionTest, RequestNowTimeout) {}

TEST_P(ServiceWorkerVersionTest, RequestNowTimeoutKill) {}

TEST_P(ServiceWorkerVersionTest, RequestCustomizedTimeout) {}

TEST_P(ServiceWorkerVersionTest, MixedRequestTimeouts) {}

TEST_P(ServiceWorkerVersionTest, FailToStart_RendererCrash) {}

TEST_P(ServiceWorkerVersionTest, FailToStart_Timeout) {}

// Test that a service worker stalled in stopping will timeout and not get in a
// sticky error state.
TEST_P(ServiceWorkerVersionTest, StallInStopping_DetachThenStart) {}

// Test that a service worker stalled in stopping with a start worker
// request queued up will timeout and restart.
TEST_P(ServiceWorkerVersionTest, StallInStopping_DetachThenRestart) {}

TEST_P(ServiceWorkerVersionTest, RendererCrashDuringEvent) {}

TEST_P(ServiceWorkerVersionTest, PingController) {}

// Test starting a service worker from a disallowed origin.
TEST_P(ServiceWorkerVersionTest, BadOrigin) {}

TEST_P(ServiceWorkerVersionTest,
       ForegroundServiceWorkerCountUpdatedByControllee) {}

TEST_P(ServiceWorkerVersionTest,
       ForegroundServiceWorkerCountNotUpdatedBySameProcessControllee) {}

TEST_P(ServiceWorkerVersionTest,
       ForegroundServiceWorkerCountUpdatedByControlleeProcessIdChange) {}

TEST_P(ServiceWorkerVersionTest,
       ForegroundServiceWorkerCountUpdatedByWorkerStatus) {}

TEST_P(ServiceWorkerVersionTest,
       ForegroundServiceWorkerCountUpdatedByControlleeForegroundStateChange) {}

// An instance client whose fetch handler type is kNoHandler.
class NoFetchHandlerClient : public FakeEmbeddedWorkerInstanceClient {};

class ServiceWorkerVersionNoFetchHandlerTest : public ServiceWorkerVersionTest {};

INSTANTIATE_TEST_SUITE_P(
    All,
    ServiceWorkerVersionNoFetchHandlerTest,
    testing::ValuesIn({};

TEST_P(ServiceWorkerVersionNoFetchHandlerTest,
       ForegroundServiceWorkerCountNotUpdated) {}

TEST_P(ServiceWorkerVersionTest, FailToStart_UseNewRendererProcess) {}

TEST_P(ServiceWorkerVersionTest, FailToStart_RestartStalledWorker) {}

TEST_P(ServiceWorkerVersionTest, InstalledFetchEventHandlerExists) {}

TEST_P(ServiceWorkerVersionNoFetchHandlerTest,
       InstalledFetchEventHandlerDoesNotExist) {}

class StoreMessageServiceWorker : public FakeServiceWorker {};

TEST_P(ServiceWorkerVersionTest, AddMessageToConsole) {}

// Test that writing metadata aborts gracefully when a remote connection to
// the Storage Service is disconnected.
TEST_P(ServiceWorkerVersionTest, WriteMetadata_RemoteStorageDisconnection) {}

// Test that writing metadata aborts gracefully when the storage is disabled.
TEST_P(ServiceWorkerVersionTest, WriteMetadata_StorageDisabled) {}

// Test that writing metadata twice at the same time finishes successfully.
TEST_P(ServiceWorkerVersionTest, WriteMetadata_MultipleWrites) {}

// Tests that adding pending external requests with different
// ServiceWorkerExternalRequestTimeoutType is handled correctly within
// ServiceWorkerVersion::pending_external_requests_.
TEST_P(ServiceWorkerVersionTest, PendingExternalRequest) {}

// Tests worker lifetime with ServiceWorkerVersion::StartExternalRequest.
TEST_P(ServiceWorkerVersionTest, WorkerLifetimeWithExternalRequest) {}

// Tests that a worker containing external request with
// ServiceWorkerExternalRequestTimeoutType::kDoesNotTimeout is not
// stopped by an external request with "default" timeout.
//
// Regression test for https://crbug.com/1189678
TEST_P(ServiceWorkerVersionTest,
       DefaultTimeoutRequestDoesNotAffectMaxTimeoutRequest) {}

TEST_P(ServiceWorkerVersionTest, SetResources) {}

class ServiceWorkerVersionStaticRouterTest : public ServiceWorkerVersionTest {};

INSTANTIATE_TEST_SUITE_P(
    All,
    ServiceWorkerVersionStaticRouterTest,
    testing::ValuesIn({};

TEST_P(ServiceWorkerVersionStaticRouterTest, SetRouterEvaluator) {}

// An instance client for controlling whether it has hid event handlers or not.
class HidEventHandlerClient : public FakeEmbeddedWorkerInstanceClient {};

TEST_P(ServiceWorkerVersionTest, HasHidEventHandler) {}

TEST_P(ServiceWorkerVersionTest, NoHidEventHandler) {}

// An instance client for controlling whether it has hid event handlers or not.
class UsbEventHandlerClient : public FakeEmbeddedWorkerInstanceClient {};

TEST_P(ServiceWorkerVersionTest, HasUsbEventHandler) {}

TEST_P(ServiceWorkerVersionTest, NoUsbEventHandler) {}

}  // namespace service_worker_version_unittest
}  // namespace content