chromium/content/public/test/service_worker_test_helpers.cc

// Copyright 2017 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/public/test/service_worker_test_helpers.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/scoped_observation_traits.h"
#include "base/time/default_tick_clock.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_worker_context.h"
#include "third_party/blink/public/common/notifications/platform_notification_data.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_database.mojom-forward.h"
#include "url/gurl.h"

// Allow `ServiceWorkerVersionCreatedWatcher` to scoped observe the custom
// observer add/remove methods on `ServiceWorkerContextCore`.
namespace base {
template <>
struct ScopedObservationTraits<
    content::ServiceWorkerContextCore,
    content::ServiceWorkerContextCore::TestVersionObserver> {};
}  // namespace base

namespace content {

namespace {

class StoppedObserver : public base::RefCountedThreadSafe<StoppedObserver> {};

void StopServiceWorkerForRegistration(
    scoped_refptr<ServiceWorkerContextWrapper> context_wrapper,
    base::OnceClosure completion_callback_ui,
    blink::ServiceWorkerStatusCode service_worker_status,
    scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {}

void DispatchNotificationClickForRegistration(
    const blink::PlatformNotificationData& notification_data,
    blink::ServiceWorkerStatusCode service_worker_status,
    scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {}

}  // namespace

// Implementation for `content::ServiceWorkerContextCore::TestVersionObserver`.
// Observes new versions created and sets a state observer new versions that it
// observes being created.
class ServiceWorkerTestHelper::ServiceWorkerVersionCreatedWatcher
    : public content::ServiceWorkerContextCore::TestVersionObserver {};

// Observes state changes of the `ServiceWorkerVersion` it is observing.
class ServiceWorkerTestHelper::ServiceWorkerVersionStateManager
    : public ServiceWorkerVersion::Observer {};

ServiceWorkerTestHelper::ServiceWorkerTestHelper(ServiceWorkerContext* context,
                                                 int64_t worker_version_id) {}

ServiceWorkerTestHelper::~ServiceWorkerTestHelper() {}

void ServiceWorkerTestHelper::RegisterVersionCreatedObserver(
    ServiceWorkerContext* context) {}

void ServiceWorkerTestHelper::RegisterStateObserver(
    ServiceWorkerContext* context,
    int64_t worker_version_id) {}

void ServiceWorkerTestHelper::OnServiceWorkerVersionCreated(
    ServiceWorkerVersion* version) {}

void StopServiceWorkerForScope(ServiceWorkerContext* context,
                               const GURL& scope,
                               base::OnceClosure completion_callback_ui) {}

void DispatchServiceWorkerNotificationClick(
    ServiceWorkerContext* context,
    const GURL& scope,
    const blink::PlatformNotificationData& notification_data) {}

void AdvanceClockAfterRequestTimeout(ServiceWorkerContext* context,
                                     int64_t service_worker_version_id,
                                     base::SimpleTestTickClock* tick_clock) {}

void ResetTickClockToDefaultForAllLiveServiceWorkerVersions(
    ServiceWorkerContext* context) {}

bool TriggerTimeoutAndCheckRunningState(ServiceWorkerContext* context,
                                        int64_t service_worker_version_id) {}

bool CheckServiceWorkerIsRunning(ServiceWorkerContext* context,
                                 int64_t service_worker_version_id) {}

bool CheckServiceWorkerIsStarting(ServiceWorkerContext* context,
                                  int64_t service_worker_version_id) {}

bool CheckServiceWorkerIsStopping(ServiceWorkerContext* context,
                                  int64_t service_worker_version_id) {}

bool CheckServiceWorkerIsStopped(ServiceWorkerContext* context,
                                 int64_t service_worker_version_id) {}

void SetServiceWorkerIdleDelay(ServiceWorkerContext* context,
                               int64_t service_worker_version_id,
                               base::TimeDelta delta) {}

}  // namespace content