1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
content / public / test / service_worker_test_helpers.h [blame]
// 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.
#ifndef CONTENT_PUBLIC_TEST_SERVICE_WORKER_TEST_HELPERS_H_
#define CONTENT_PUBLIC_TEST_SERVICE_WORKER_TEST_HELPERS_H_
#include <cstdint>
#include <memory>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/test/simple_test_tick_clock.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_database.mojom-forward.h"
class GURL;
namespace blink {
enum class EmbeddedWorkerStatus;
struct PlatformNotificationData;
} // namespace blink
namespace content {
class ServiceWorkerContext;
class ServiceWorkerVersion;
// Helper classes.
// A class that assists with observing the state of `ServiceWorkerVersion`s
// during tests.
class ServiceWorkerTestHelper {
public:
// Starts observing every new `ServiceWorkerVersion`'s running status. If a
// valid `worker_verion_id` is provided it starts observing the
// `ServiceWorkerVersion`'s (for `worker_verion_id`'s) running status.
explicit ServiceWorkerTestHelper(
ServiceWorkerContext* context,
int64_t worker_verion_id = blink::mojom::kInvalidServiceWorkerVersionId);
~ServiceWorkerTestHelper();
ServiceWorkerTestHelper(const ServiceWorkerTestHelper&) = delete;
ServiceWorkerTestHelper& operator=(const ServiceWorkerTestHelper&) = delete;
// Proxy for ServiceWorkerVersion::Observer::OnDidRunningStatusChange().
// Override this to observe changes to running status.
virtual void OnDidRunningStatusChange(
blink::EmbeddedWorkerStatus running_status,
int64_t version_id) {}
private:
class ServiceWorkerVersionCreatedWatcher;
class ServiceWorkerVersionStateManager;
void RegisterVersionCreatedObserver(ServiceWorkerContext* context);
void RegisterStateObserver(ServiceWorkerContext* context,
int64_t worker_version_id);
// When a new version is noticed being created this starts observing it with a
// `ServiceWorkerVersionStateManager`.
void OnServiceWorkerVersionCreated(ServiceWorkerVersion* version);
// Watches for any new versions being created.
std::unique_ptr<ServiceWorkerVersionCreatedWatcher> version_created_watcher_;
// Contains observers for all versions encountered by this class.
std::vector<std::unique_ptr<ServiceWorkerVersionStateManager>>
version_state_managers_;
};
// Helper methods.
// Stops the active service worker of the registration for the given |scope|,
// and calls |complete_callback_ui| callback on UI thread when done.
//
// Can be called from UI/IO thread.
void StopServiceWorkerForScope(ServiceWorkerContext* context,
const GURL& scope,
base::OnceClosure complete_callback_ui);
// Dispatches a notification click event to the active service worker
// worker for the given |scope|.
void DispatchServiceWorkerNotificationClick(
ServiceWorkerContext* context,
const GURL& scope,
const blink::PlatformNotificationData& notification_data);
// Advance the clock of a service worker after the timeout for requests.
void AdvanceClockAfterRequestTimeout(ServiceWorkerContext* context,
int64_t service_worker_version_id,
base::SimpleTestTickClock* tick_clock);
// Tests that uses AdvanceClockAfterRequestTimeout() should call this before
// `tick_clock` is destroyed. Otherwise, `tick_clock` pointer will become
// dangling.
void ResetTickClockToDefaultForAllLiveServiceWorkerVersions(
ServiceWorkerContext* context);
// Runs the user tasks on a service worker, triggers a timeout and returns
// whether the service worker is still running.
bool TriggerTimeoutAndCheckRunningState(ServiceWorkerContext* context,
int64_t service_worker_version_id);
// Returns whether the worker appears to be in each blink::EmbeddedWorkerStatus.
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 delay);
} // namespace content
#endif // CONTENT_PUBLIC_TEST_SERVICE_WORKER_TEST_HELPERS_H_