1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/win/object_watcher.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
18 class QuitDelegate
: public ObjectWatcher::Delegate
{
20 virtual void OnObjectSignaled(HANDLE object
) {
21 MessageLoop::current()->QuitWhenIdle();
25 class DecrementCountDelegate
: public ObjectWatcher::Delegate
{
27 explicit DecrementCountDelegate(int* counter
) : counter_(counter
) {
29 virtual void OnObjectSignaled(HANDLE object
) {
36 void RunTest_BasicSignal(MessageLoop::Type message_loop_type
) {
37 MessageLoop
message_loop(message_loop_type
);
39 ObjectWatcher watcher
;
40 EXPECT_FALSE(watcher
.IsWatching());
42 // A manual-reset event that is not yet signaled.
43 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
45 QuitDelegate delegate
;
46 bool ok
= watcher
.StartWatching(event
, &delegate
);
48 EXPECT_TRUE(watcher
.IsWatching());
49 EXPECT_EQ(event
, watcher
.GetWatchedObject());
53 MessageLoop::current()->Run();
55 EXPECT_FALSE(watcher
.IsWatching());
59 void RunTest_BasicCancel(MessageLoop::Type message_loop_type
) {
60 MessageLoop
message_loop(message_loop_type
);
62 ObjectWatcher watcher
;
64 // A manual-reset event that is not yet signaled.
65 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
67 QuitDelegate delegate
;
68 bool ok
= watcher
.StartWatching(event
, &delegate
);
71 watcher
.StopWatching();
76 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type
) {
77 MessageLoop
message_loop(message_loop_type
);
79 ObjectWatcher watcher
;
82 DecrementCountDelegate
delegate(&counter
);
84 // A manual-reset event that is not yet signaled.
85 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
87 bool ok
= watcher
.StartWatching(event
, &delegate
);
92 // Let the background thread do its business
95 watcher
.StopWatching();
97 RunLoop().RunUntilIdle();
99 // Our delegate should not have fired.
100 EXPECT_EQ(1, counter
);
105 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type
) {
106 MessageLoop
message_loop(message_loop_type
);
108 ObjectWatcher watcher
;
110 // A manual-reset event that is signaled before we begin watching.
111 HANDLE event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
113 QuitDelegate delegate
;
114 bool ok
= watcher
.StartWatching(event
, &delegate
);
117 MessageLoop::current()->Run();
119 EXPECT_FALSE(watcher
.IsWatching());
123 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type
) {
124 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
125 // doesn't happen when people use the Thread class, but it can happen when
126 // people use the Singleton pattern or atexit.
127 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
); // not signaled
129 ObjectWatcher watcher
;
131 MessageLoop
message_loop(message_loop_type
);
133 QuitDelegate delegate
;
134 watcher
.StartWatching(event
, &delegate
);
142 //-----------------------------------------------------------------------------
144 TEST(ObjectWatcherTest
, BasicSignal
) {
145 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT
);
146 RunTest_BasicSignal(MessageLoop::TYPE_IO
);
147 RunTest_BasicSignal(MessageLoop::TYPE_UI
);
150 TEST(ObjectWatcherTest
, BasicCancel
) {
151 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT
);
152 RunTest_BasicCancel(MessageLoop::TYPE_IO
);
153 RunTest_BasicCancel(MessageLoop::TYPE_UI
);
156 TEST(ObjectWatcherTest
, CancelAfterSet
) {
157 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT
);
158 RunTest_CancelAfterSet(MessageLoop::TYPE_IO
);
159 RunTest_CancelAfterSet(MessageLoop::TYPE_UI
);
162 TEST(ObjectWatcherTest
, SignalBeforeWatch
) {
163 RunTest_SignalBeforeWatch(MessageLoop::TYPE_DEFAULT
);
164 RunTest_SignalBeforeWatch(MessageLoop::TYPE_IO
);
165 RunTest_SignalBeforeWatch(MessageLoop::TYPE_UI
);
168 TEST(ObjectWatcherTest
, OutlivesMessageLoop
) {
169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT
);
170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO
);
171 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI
);