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_EQ(NULL
, watcher
.GetWatchedObject());
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_EQ(event
, watcher
.GetWatchedObject());
52 MessageLoop::current()->Run();
54 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
58 void RunTest_BasicCancel(MessageLoop::Type message_loop_type
) {
59 MessageLoop
message_loop(message_loop_type
);
61 ObjectWatcher watcher
;
63 // A manual-reset event that is not yet signaled.
64 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
66 QuitDelegate delegate
;
67 bool ok
= watcher
.StartWatching(event
, &delegate
);
70 watcher
.StopWatching();
75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type
) {
76 MessageLoop
message_loop(message_loop_type
);
78 ObjectWatcher watcher
;
81 DecrementCountDelegate
delegate(&counter
);
83 // A manual-reset event that is not yet signaled.
84 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
86 bool ok
= watcher
.StartWatching(event
, &delegate
);
91 // Let the background thread do its business
94 watcher
.StopWatching();
96 RunLoop().RunUntilIdle();
98 // Our delegate should not have fired.
99 EXPECT_EQ(1, counter
);
104 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type
) {
105 MessageLoop
message_loop(message_loop_type
);
107 ObjectWatcher watcher
;
109 // A manual-reset event that is signaled before we begin watching.
110 HANDLE event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
112 QuitDelegate delegate
;
113 bool ok
= watcher
.StartWatching(event
, &delegate
);
116 MessageLoop::current()->Run();
118 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
122 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type
) {
123 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
124 // doesn't happen when people use the Thread class, but it can happen when
125 // people use the Singleton pattern or atexit.
126 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
); // not signaled
128 ObjectWatcher watcher
;
130 MessageLoop
message_loop(message_loop_type
);
132 QuitDelegate delegate
;
133 watcher
.StartWatching(event
, &delegate
);
141 //-----------------------------------------------------------------------------
143 TEST(ObjectWatcherTest
, BasicSignal
) {
144 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT
);
145 RunTest_BasicSignal(MessageLoop::TYPE_IO
);
146 RunTest_BasicSignal(MessageLoop::TYPE_UI
);
149 TEST(ObjectWatcherTest
, BasicCancel
) {
150 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT
);
151 RunTest_BasicCancel(MessageLoop::TYPE_IO
);
152 RunTest_BasicCancel(MessageLoop::TYPE_UI
);
155 TEST(ObjectWatcherTest
, CancelAfterSet
) {
156 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT
);
157 RunTest_CancelAfterSet(MessageLoop::TYPE_IO
);
158 RunTest_CancelAfterSet(MessageLoop::TYPE_UI
);
161 TEST(ObjectWatcherTest
, SignalBeforeWatch
) {
162 RunTest_SignalBeforeWatch(MessageLoop::TYPE_DEFAULT
);
163 RunTest_SignalBeforeWatch(MessageLoop::TYPE_IO
);
164 RunTest_SignalBeforeWatch(MessageLoop::TYPE_UI
);
167 TEST(ObjectWatcherTest
, OutlivesMessageLoop
) {
168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT
);
169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO
);
170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI
);