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
121
content / browser / background_fetch / background_fetch_test_base.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_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_fetch/background_fetch_test_browser_context.h"
#include "content/browser/background_fetch/background_fetch_test_service_worker.h"
#include "content/browser/devtools/devtools_background_services_context_impl.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/origin.h"
namespace content {
class ServiceWorkerRegistration;
class StoragePartitionImpl;
// Base class containing common functionality needed in unit tests written for
// the Background Fetch feature.
class BackgroundFetchTestBase : public ::testing::Test {
public:
using TestResponse = MockBackgroundFetchDelegate::TestResponse;
using TestResponseBuilder = MockBackgroundFetchDelegate::TestResponseBuilder;
BackgroundFetchTestBase();
BackgroundFetchTestBase(const BackgroundFetchTestBase&) = delete;
BackgroundFetchTestBase& operator=(const BackgroundFetchTestBase&) = delete;
~BackgroundFetchTestBase() override;
// ::testing::Test overrides.
void SetUp() override;
void TearDown() override;
// Registers a Service Worker for the testing origin and returns its
// |service_worker_registration_id|. If registration failed, this will be
// |blink::mojom::kInvalidServiceWorkerRegistrationId|. The
// ServiceWorkerRegistration will be kept alive for the test's lifetime.
int64_t RegisterServiceWorker();
// `RegisterServiceWorker` but for the provided |origin|.
int64_t RegisterServiceWorkerForOrigin(const url::Origin& origin);
// Unregisters the test Service Worker and verifies that the unregistration
// succeeded.
void UnregisterServiceWorker(int64_t service_worker_registration_id);
// Creates a FetchAPIRequestPtr instance for the given details and
// provides a faked |response|.
blink::mojom::FetchAPIRequestPtr CreateRequestWithProvidedResponse(
const std::string& method,
const GURL& url,
std::unique_ptr<TestResponse> response);
// Creates a blink::mojom::BackgroundFetchRegistrationDataPtr object.
blink::mojom::BackgroundFetchRegistrationDataPtr
CreateBackgroundFetchRegistrationData(
const std::string& developer_id,
blink::mojom::BackgroundFetchResult result,
blink::mojom::BackgroundFetchFailureReason failure_reason);
// Returns the embedded worker test helper instance, which can be used to
// influence the behavior of the Service Worker events.
EmbeddedWorkerTestHelper* embedded_worker_test_helper() {
return &embedded_worker_test_helper_;
}
// Returns the browser context that should be used for the tests.
TestBrowserContext* browser_context() { return &browser_context_; }
// Returns the once-initialized default storage partition to be used in tests.
base::WeakPtr<StoragePartitionImpl> storage_partition() {
return storage_partition_factory_.GetWeakPtr();
}
// Returns the storage key that should be used for Background Fetch tests.
const blink::StorageKey& storage_key() const { return storage_key_; }
// Returns the DevTools context for logging events.
DevToolsBackgroundServicesContextImpl& devtools_context();
protected:
BrowserTaskEnvironment task_environment_; // Must be first member.
private:
BackgroundFetchTestBrowserContext browser_context_;
raw_ptr<MockBackgroundFetchDelegate> delegate_;
EmbeddedWorkerTestHelper embedded_worker_test_helper_;
blink::StorageKey storage_key_;
base::WeakPtrFactory<StoragePartitionImpl> storage_partition_factory_;
int next_pattern_id_ = 0;
// Vector of ServiceWorkerRegistration instances that have to be kept alive
// for the lifetime of this test.
std::vector<scoped_refptr<ServiceWorkerRegistration>>
service_worker_registrations_;
bool set_up_called_ = false;
bool tear_down_called_ = false;
};
} // namespace content
#endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_