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
content / public / test / embedded_worker_instance_test_harness.h [blame]
// Copyright 2022 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_EMBEDDED_WORKER_INSTANCE_TEST_HARNESS_H_
#define CONTENT_PUBLIC_TEST_EMBEDDED_WORKER_INSTANCE_TEST_HARNESS_H_
#include <stdint.h>
#include "browser_task_environment.h"
#include "content/public/browser/browser_context.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/hid/hid.mojom.h"
#include "third_party/blink/public/mojom/usb/web_usb_service.mojom.h"
namespace content {
class EmbeddedWorkerInstance;
class EmbeddedWorkerTestHelper;
class ServiceWorkerVersion;
// EmbeddedWorkerInstanceTestHarness provides helper functions to set up a test
// environment with an EmbeddedWorkerInstance, and allow a test to test the
// worker with bound services.
//
// Example:
//
// class MyWorkerTest : public EmbeddedWorkerInstanceTestHarness {
// std::unique_ptr<BrowserContext> CreateBrowserContext() override {
// // Create the BrowserContext and store the raw pointer
// // to the created BrowserContext.
// ...
// }
// };
//
// CreateAndStartWorker(); // Create and start a worker!
//
// mojo::Remote<Xyz> xyz_service;
// // Note a new binding function might need to be added to this harness file
// // if that is not available.
// BindXyzServiceToWorker(origin, xyz_service.BindNewPipeAndPassReceiver()));
// xyz_service->DoSomething();
//
// StopAndResetWorker(); // Stop and delete the worker!
class EmbeddedWorkerInstanceTestHarness : public testing::Test {
public:
// Constructs a EmbeddedWorkerInstanceTestHarness which uses |traits| to
// initialize its BrowserTaskEnvironment.
template <typename... TaskEnvironmentTraits>
explicit EmbeddedWorkerInstanceTestHarness(TaskEnvironmentTraits&&... traits)
: EmbeddedWorkerInstanceTestHarness(
std::make_unique<BrowserTaskEnvironment>(
std::forward<TaskEnvironmentTraits>(traits)...)) {}
EmbeddedWorkerInstanceTestHarness(const EmbeddedWorkerInstanceTestHarness&) =
delete;
EmbeddedWorkerInstanceTestHarness& operator=(
const EmbeddedWorkerInstanceTestHarness&) = delete;
~EmbeddedWorkerInstanceTestHarness() override;
void SetUp() override;
void TearDown() override;
virtual std::unique_ptr<BrowserContext> CreateBrowserContext();
void CreateAndStartWorker(const GURL& origin, const GURL& worker_url);
void StopAndResetWorker();
#if !BUILDFLAG(IS_ANDROID)
void BindHidServiceToWorker(
const GURL& origin,
mojo::PendingReceiver<blink::mojom::HidService> receiver);
#endif
void BindUsbServiceToWorker(
const GURL& origin,
mojo::PendingReceiver<blink::mojom::WebUsbService> receiver);
protected:
// The template constructor has to be in the header but it delegates to this
// constructor to initialize all other members out-of-line.
explicit EmbeddedWorkerInstanceTestHarness(
std::unique_ptr<BrowserTaskEnvironment> task_environment);
std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
private:
std::unique_ptr<BrowserTaskEnvironment> task_environment_;
scoped_refptr<content::ServiceWorkerVersion> worker_version_;
};
} // namespace content
#endif // CONTENT_PUBLIC_TEST_EMBEDDED_WORKER_INSTANCE_TEST_HARNESS_H_