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
content / browser / background_sync / one_shot_background_sync_service_impl_unittest.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 "content/browser/background_sync/one_shot_background_sync_service_impl.h"
#include "base/memory/raw_ptr.h"
#include "content/browser/background_sync/background_sync_service_impl_test_harness.h"
#include "content/public/test/mock_render_process_host.h"
#include "url/origin.h"
namespace content {
class OneShotBackgroundSyncServiceImplTest
: public BackgroundSyncServiceImplTestHarness {
public:
OneShotBackgroundSyncServiceImplTest() = default;
~OneShotBackgroundSyncServiceImplTest() override = default;
void SetUp() override {
BackgroundSyncServiceImplTestHarness::SetUp();
CreateOneShotBackgroundSyncServiceImpl();
}
protected:
void CreateOneShotBackgroundSyncServiceImpl() {
render_process_host_ =
std::make_unique<MockRenderProcessHost>(browser_context());
// Create a dummy mojo channel so that the OneShotBackgroundSyncServiceImpl
// can be instantiated.
mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService> receiver =
one_shot_sync_service_remote_.BindNewPipeAndPassReceiver();
// Create a new OneShotBackgroundSyncServiceImpl bound to the dummy channel.
background_sync_context_->CreateOneShotSyncService(
url::Origin::Create(GURL(kServiceWorkerOrigin)),
render_process_host_.get(), std::move(receiver));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(GetOneShotBackgroundSyncServiceImpl());
}
// Helpers for testing *BackgroundSyncServiceImpl methods
void RegisterOneShotSync(
blink::mojom::SyncRegistrationOptionsPtr sync,
blink::mojom::OneShotBackgroundSyncService::RegisterCallback callback) {
GetOneShotBackgroundSyncServiceImpl()->Register(
std::move(sync), sw_registration_id_, std::move(callback));
base::RunLoop().RunUntilIdle();
}
void GetOneShotSyncRegistrations(
blink::mojom::OneShotBackgroundSyncService::GetRegistrationsCallback
callback) {
GetOneShotBackgroundSyncServiceImpl()->GetRegistrations(
sw_registration_id_, std::move(callback));
base::RunLoop().RunUntilIdle();
}
OneShotBackgroundSyncServiceImpl* GetOneShotBackgroundSyncServiceImpl() {
return background_sync_context_->one_shot_sync_services_.begin()->get();
}
std::unique_ptr<MockRenderProcessHost> render_process_host_;
mojo::Remote<blink::mojom::OneShotBackgroundSyncService>
one_shot_sync_service_remote_;
};
// Tests
TEST_F(OneShotBackgroundSyncServiceImplTest, RegisterOneShotSync) {
bool called = false;
blink::mojom::BackgroundSyncError error;
blink::mojom::SyncRegistrationOptionsPtr reg;
RegisterOneShotSync(
default_sync_registration_.Clone(),
base::BindOnce(&ErrorAndRegistrationCallback, &called, &error, ®));
ASSERT_TRUE(called);
EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
EXPECT_EQ("", reg->tag);
}
TEST_F(OneShotBackgroundSyncServiceImplTest, RegisterWithInvalidOptions) {
bool called = false;
blink::mojom::BackgroundSyncError error;
blink::mojom::SyncRegistrationOptionsPtr reg;
auto to_register = default_sync_registration_.Clone();
to_register->min_interval = 3600;
mojo::FakeMessageDispatchContext fake_dispatch_context;
RegisterOneShotSync(
std::move(to_register),
base::BindOnce(&ErrorAndRegistrationCallback, &called, &error, ®));
ASSERT_TRUE(called);
EXPECT_EQ(mojo_bad_messages_.size(), 1u);
}
TEST_F(OneShotBackgroundSyncServiceImplTest,
GetOneShotSyncRegistrationsNoSyncRegistered) {
bool called = false;
blink::mojom::BackgroundSyncError error;
unsigned long array_size = 0UL;
GetOneShotSyncRegistrations(base::BindOnce(&ErrorAndRegistrationListCallback,
&called, &error, &array_size));
ASSERT_TRUE(called);
EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
EXPECT_EQ(0UL, array_size);
}
TEST_F(OneShotBackgroundSyncServiceImplTest,
GetOneShotSyncRegistrationsWithRegisteredSync) {
bool register_called = false;
bool get_registrations_called = false;
blink::mojom::BackgroundSyncError register_error;
blink::mojom::BackgroundSyncError get_registrations_error;
blink::mojom::SyncRegistrationOptionsPtr registered_reg;
unsigned long array_size = 0UL;
RegisterOneShotSync(
default_sync_registration_.Clone(),
base::BindOnce(&ErrorAndRegistrationCallback, ®ister_called,
®ister_error, ®istered_reg));
ASSERT_TRUE(register_called);
EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, register_error);
GetOneShotSyncRegistrations(base::BindOnce(
&ErrorAndRegistrationListCallback, &get_registrations_called,
&get_registrations_error, &array_size));
ASSERT_TRUE(get_registrations_called);
EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, get_registrations_error);
EXPECT_EQ(1UL, array_size);
}
} // namespace content