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
base / win / object_watcher_unittest.cc [blame]
// Copyright 2011 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/win/object_watcher.h"
#include <windows.h>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
class QuitDelegate : public ObjectWatcher::Delegate {
public:
explicit QuitDelegate(base::OnceClosure quit_closure)
: quit_closure_(std::move(quit_closure)) {}
void OnObjectSignaled(HANDLE object) override {
std::move(quit_closure_).Run();
}
private:
base::OnceClosure quit_closure_;
};
class DecrementCountDelegate : public ObjectWatcher::Delegate {
public:
explicit DecrementCountDelegate(int* counter) : counter_(counter) {}
void OnObjectSignaled(HANDLE object) override { --(*counter_); }
private:
raw_ptr<int> counter_;
};
void RunTest_BasicSignal(
test::TaskEnvironment::MainThreadType main_thread_type) {
test::TaskEnvironment task_environment(main_thread_type);
ObjectWatcher watcher;
EXPECT_FALSE(watcher.IsWatching());
// A manual-reset event that is not yet signaled.
HANDLE event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
base::RunLoop loop;
QuitDelegate delegate(loop.QuitWhenIdleClosure());
bool ok = watcher.StartWatchingOnce(event, &delegate);
EXPECT_TRUE(ok);
EXPECT_TRUE(watcher.IsWatching());
EXPECT_EQ(event, watcher.GetWatchedObject());
SetEvent(event);
loop.Run();
EXPECT_FALSE(watcher.IsWatching());
CloseHandle(event);
}
void RunTest_BasicCancel(
test::TaskEnvironment::MainThreadType main_thread_type) {
test::TaskEnvironment task_environment(main_thread_type);
ObjectWatcher watcher;
// A manual-reset event that is not yet signaled.
HANDLE event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
base::RunLoop loop;
QuitDelegate delegate(loop.QuitWhenIdleClosure());
bool ok = watcher.StartWatchingOnce(event, &delegate);
EXPECT_TRUE(ok);
watcher.StopWatching();
CloseHandle(event);
}
void RunTest_CancelAfterSet(
test::TaskEnvironment::MainThreadType main_thread_type) {
test::TaskEnvironment task_environment(main_thread_type);
ObjectWatcher watcher;
int counter = 1;
DecrementCountDelegate delegate(&counter);
base::RunLoop loop;
// A manual-reset event that is not yet signaled.
HANDLE event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
bool ok = watcher.StartWatchingOnce(event, &delegate);
EXPECT_TRUE(ok);
SetEvent(event);
// Let the background thread do its business
Sleep(30);
watcher.StopWatching();
loop.RunUntilIdle();
// Our delegate should not have fired.
EXPECT_EQ(1, counter);
CloseHandle(event);
}
void RunTest_SignalBeforeWatch(
test::TaskEnvironment::MainThreadType main_thread_type) {
test::TaskEnvironment task_environment(main_thread_type);
ObjectWatcher watcher;
// A manual-reset event that is signaled before we begin watching.
HANDLE event = CreateEvent(nullptr, TRUE, TRUE, nullptr);
base::RunLoop loop;
QuitDelegate delegate(loop.QuitWhenIdleClosure());
bool ok = watcher.StartWatchingOnce(event, &delegate);
EXPECT_TRUE(ok);
loop.Run();
EXPECT_FALSE(watcher.IsWatching());
CloseHandle(event);
}
void RunTest_OutlivesTaskEnvironment(
test::TaskEnvironment::MainThreadType main_thread_type) {
// Simulate a task environment that dies before an ObjectWatcher. This
// ordinarily doesn't happen when people use the Thread class, but it can
// happen when people use the Singleton pattern or atexit.
HANDLE event = CreateEvent(nullptr, TRUE, FALSE, nullptr); // not signaled
{
ObjectWatcher watcher;
{
test::TaskEnvironment task_environment(main_thread_type);
base::RunLoop loop;
QuitDelegate delegate(loop.QuitWhenIdleClosure());
watcher.StartWatchingOnce(event, &delegate);
}
}
CloseHandle(event);
}
class QuitAfterMultipleDelegate : public ObjectWatcher::Delegate {
public:
QuitAfterMultipleDelegate(HANDLE event,
int iterations,
base::OnceClosure quit_closure)
: event_(event),
iterations_(iterations),
quit_closure_(std::move(quit_closure)) {}
void OnObjectSignaled(HANDLE object) override {
if (--iterations_) {
SetEvent(event_);
} else {
std::move(quit_closure_).Run();
}
}
private:
HANDLE event_;
int iterations_;
base::OnceClosure quit_closure_;
};
void RunTest_ExecuteMultipleTimes(
test::TaskEnvironment::MainThreadType main_thread_type) {
test::TaskEnvironment task_environment(main_thread_type);
ObjectWatcher watcher;
EXPECT_FALSE(watcher.IsWatching());
// An auto-reset event that is not yet signaled.
HANDLE event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
base::RunLoop loop;
QuitAfterMultipleDelegate delegate(event, 2, loop.QuitWhenIdleClosure());
bool ok = watcher.StartWatchingMultipleTimes(event, &delegate);
EXPECT_TRUE(ok);
EXPECT_TRUE(watcher.IsWatching());
EXPECT_EQ(event, watcher.GetWatchedObject());
SetEvent(event);
loop.Run();
EXPECT_TRUE(watcher.IsWatching());
EXPECT_TRUE(watcher.StopWatching());
CloseHandle(event);
}
} // namespace
//-----------------------------------------------------------------------------
TEST(ObjectWatcherTest, BasicSignal) {
RunTest_BasicSignal(test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_BasicSignal(test::TaskEnvironment::MainThreadType::IO);
RunTest_BasicSignal(test::TaskEnvironment::MainThreadType::UI);
}
TEST(ObjectWatcherTest, BasicCancel) {
RunTest_BasicCancel(test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_BasicCancel(test::TaskEnvironment::MainThreadType::IO);
RunTest_BasicCancel(test::TaskEnvironment::MainThreadType::UI);
}
TEST(ObjectWatcherTest, CancelAfterSet) {
RunTest_CancelAfterSet(test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_CancelAfterSet(test::TaskEnvironment::MainThreadType::IO);
RunTest_CancelAfterSet(test::TaskEnvironment::MainThreadType::UI);
}
TEST(ObjectWatcherTest, SignalBeforeWatch) {
RunTest_SignalBeforeWatch(test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_SignalBeforeWatch(test::TaskEnvironment::MainThreadType::IO);
RunTest_SignalBeforeWatch(test::TaskEnvironment::MainThreadType::UI);
}
TEST(ObjectWatcherTest, OutlivesTaskEnvironment) {
RunTest_OutlivesTaskEnvironment(
test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_OutlivesTaskEnvironment(test::TaskEnvironment::MainThreadType::IO);
RunTest_OutlivesTaskEnvironment(test::TaskEnvironment::MainThreadType::UI);
}
TEST(ObjectWatcherTest, ExecuteMultipleTimes) {
RunTest_ExecuteMultipleTimes(test::TaskEnvironment::MainThreadType::DEFAULT);
RunTest_ExecuteMultipleTimes(test::TaskEnvironment::MainThreadType::IO);
RunTest_ExecuteMultipleTimes(test::TaskEnvironment::MainThreadType::UI);
}
} // namespace win
} // namespace base