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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
content / browser / indexed_db / indexed_db_feature_observer_browsertest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/browser/feature_observer.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/feature_observer_client.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_content_browser_client.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif
namespace content::indexed_db {
namespace {
void RunLoopWithTimeout() {
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout());
run_loop.Run();
}
class TestBrowserClient : public ContentBrowserTestContentBrowserClient {
public:
explicit TestBrowserClient(FeatureObserverClient* feature_observer_client)
: feature_observer_client_(feature_observer_client) {}
~TestBrowserClient() override = default;
FeatureObserverClient* GetFeatureObserverClient() override {
return feature_observer_client_;
}
TestBrowserClient(const TestBrowserClient&) = delete;
TestBrowserClient& operator=(const TestBrowserClient&) = delete;
private:
raw_ptr<FeatureObserverClient> feature_observer_client_;
};
class MockObserverClient : public FeatureObserverClient {
public:
MockObserverClient() = default;
~MockObserverClient() override = default;
// PerformanceManagerFeatureObserver implementation:
MOCK_METHOD2(OnStartUsing,
void(GlobalRenderFrameHostId id,
blink::mojom::ObservedFeatureType type));
MOCK_METHOD2(OnStopUsing,
void(GlobalRenderFrameHostId id,
blink::mojom::ObservedFeatureType type));
};
class IndexedDBFeatureObserverBrowserTest : public ContentBrowserTest {
public:
IndexedDBFeatureObserverBrowserTest() = default;
~IndexedDBFeatureObserverBrowserTest() override = default;
// Check if the test can run on the current system. If the test can run,
// navigates to the test page and returns true. Otherwise, returns false.
bool CheckShouldRunTestAndNavigate() const {
EXPECT_TRUE(NavigateToURL(shell(), GetTestURL("a.com")));
return true;
}
GURL GetTestURL(const std::string& hostname) const {
return server_.GetURL(hostname,
"/indexeddb/open_connection/open_connection.html");
}
protected:
testing::StrictMock<MockObserverClient> mock_observer_client_;
private:
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
browser_client_ =
std::make_unique<TestBrowserClient>(&mock_observer_client_);
host_resolver()->AddRule("*", "127.0.0.1");
server_.ServeFilesFromSourceDirectory(GetTestDataFilePath());
ASSERT_TRUE(server_.Start());
}
void TearDownOnMainThread() override {
browser_client_.reset();
ContentBrowserTest::TearDownOnMainThread();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
mock_cert_verifier_.SetUpCommandLine(command_line);
}
void SetUpInProcessBrowserTestFixture() override {
ContentBrowserTest::SetUpInProcessBrowserTestFixture();
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void TearDownInProcessBrowserTestFixture() override {
ContentBrowserTest::TearDownInProcessBrowserTestFixture();
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}
content::ContentMockCertVerifier mock_cert_verifier_;
net::EmbeddedTestServer server_{net::EmbeddedTestServer::TYPE_HTTPS};
std::unique_ptr<TestBrowserClient> browser_client_;
IndexedDBFeatureObserverBrowserTest(
const IndexedDBFeatureObserverBrowserTest&) = delete;
IndexedDBFeatureObserverBrowserTest& operator=(
const IndexedDBFeatureObserverBrowserTest&) = delete;
};
bool OpenConnectionA(RenderFrameHost* rfh) {
return EvalJs(rfh, R"(
(async () => {
return await OpenConnection('A');
}) ();
)")
.ExtractBool();
}
bool OpenConnectionB(RenderFrameHost* rfh) {
return EvalJs(rfh, R"(
(async () => {
return await OpenConnection('B');
}) ();
)")
.ExtractBool();
}
} // namespace
// Verify that content::FeatureObserver is notified when a frame opens/closes an
// IndexedDB connection.
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest,
ObserverSingleConnection) {
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
GlobalRenderFrameHostId rfh_id = rfh->GetGlobalId();
{
// Open a connection. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStartUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(OpenConnectionA(rfh));
run_loop.Run();
}
{
// Close the connection. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStopUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(ExecJs(rfh, "CloseConnection('A');"));
run_loop.Run();
}
}
// Verify that content::FeatureObserver is notified when a frame opens multiple
// IndexedDB connections (notifications only when the number of held connections
// switches between zero and non-zero).
// Disabled on ChromeOS release build for flakiness. See crbug.com/1030733.
#if BUILDFLAG(IS_CHROMEOS_ASH) && defined(NDEBUG)
#define MAYBE_ObserverTwoLocks DISABLED_ObserverTwoLocks
#else
#define MAYBE_ObserverTwoLocks ObserverTwoLocks
#endif
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest,
MAYBE_ObserverTwoLocks) {
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
GlobalRenderFrameHostId rfh_id = rfh->GetGlobalId();
{
// Open a connection. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStartUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(OpenConnectionA(rfh));
run_loop.Run();
}
// Open a second connection. Don't expect a notification.
EXPECT_TRUE(OpenConnectionB(rfh));
// Wait a short timeout to make sure that the observer is not notified.
RunLoopWithTimeout();
// Close the connection. Don't expect a notification.
EXPECT_TRUE(ExecJs(rfh, "CloseConnection('B');"));
// Wait a short timeout to make sure that the observer is not notified.
RunLoopWithTimeout();
{
// Close the connection. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStopUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(ExecJs(rfh, "CloseConnection('A');"));
run_loop.Run();
}
}
// Verify that content::FeatureObserver is notified when a frame with active
// IndexedDB connections is navigated away.
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest, ObserverNavigate) {
// The test expects the OnStopUsing() method to be called, which won't happen
// if the BackForwardCache is enabled.
// TODO(crbug.com/40777894): Figure out why this is happening.
shell()
->web_contents()
->GetController()
.GetBackForwardCache()
.DisableForTesting(content::BackForwardCache::TEST_REQUIRES_NO_CACHING);
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
GlobalRenderFrameHostId rfh_id = rfh->GetGlobalId();
{
// Open a connection. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStartUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(OpenConnectionA(rfh));
run_loop.Run();
}
{
// Navigate away. Expect observer notification.
base::RunLoop run_loop;
EXPECT_CALL(
mock_observer_client_,
OnStopUsing(rfh_id,
blink::mojom::ObservedFeatureType::kIndexedDBConnection))
.WillOnce([&](GlobalRenderFrameHostId,
blink::mojom::ObservedFeatureType) { run_loop.Quit(); });
EXPECT_TRUE(NavigateToURL(shell(), GetTestURL("b.com")));
run_loop.Run();
}
}
// Verify that content::FeatureObserver is *not* notified when a dedicated
// worker opens/closes an IndexedDB connection.
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest,
ObserverDedicatedWorker) {
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
// Use EvalJs() instead of ExecJs() to ensure that this doesn't return before
// the lock is acquired and released by the worker.
EXPECT_TRUE(EvalJs(rfh, R"(
(async () => {
await OpenConnectionFromDedicatedWorker();
return true;
}) ();
)")
.ExtractBool());
// Wait a short timeout to make sure that the observer is not notified.
RunLoopWithTimeout();
}
// SharedWorkers are not enabled on Android. https://crbug.com/154571
#if !BUILDFLAG(IS_ANDROID)
// Verify that content::FeatureObserver is *not* notified when a shared worker
// opens/closes an IndexedDB connection.
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest,
ObserverSharedWorker) {
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
// Use EvalJs() instead of ExecJs() to ensure that this doesn't return before
// the lock is acquired and released by the worker.
EXPECT_TRUE(EvalJs(rfh, R"(
(async () => {
await OpenConnectionFromSharedWorker();
return true;
}) ();
)")
.ExtractBool());
// Wait a short timeout to make sure that the observer is not notified.
RunLoopWithTimeout();
}
#endif // !BUILDFLAG(IS_ANDROID)
// Verify that content::FeatureObserver is *not* notified when a service worker
// opens/closes an IndexedDB connection.
IN_PROC_BROWSER_TEST_F(IndexedDBFeatureObserverBrowserTest,
ObserverServiceWorker) {
if (!CheckShouldRunTestAndNavigate())
return;
RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame();
// Use EvalJs() instead of ExecJs() to ensure that this doesn't return before
// the lock is acquired and released by the worker.
EXPECT_TRUE(EvalJs(rfh, R"(
(async () => {
await OpenConnectionFromServiceWorker();
return true;
}) ();
)")
.ExtractBool());
// Wait a short timeout to make sure that the observer is not notified.
RunLoopWithTimeout();
}
} // namespace content::indexed_db