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.
7 #include "base/message_loop.h"
8 #include "base/win/object_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
16 class QuitDelegate
: public ObjectWatcher::Delegate
{
18 virtual void OnObjectSignaled(HANDLE object
) {
19 MessageLoop::current()->Quit();
23 class DecrementCountDelegate
: public ObjectWatcher::Delegate
{
25 explicit DecrementCountDelegate(int* counter
) : counter_(counter
) {
27 virtual void OnObjectSignaled(HANDLE object
) {
34 void RunTest_BasicSignal(MessageLoop::Type message_loop_type
) {
35 MessageLoop
message_loop(message_loop_type
);
37 ObjectWatcher watcher
;
38 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
40 // A manual-reset event that is not yet signaled.
41 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
43 QuitDelegate delegate
;
44 bool ok
= watcher
.StartWatching(event
, &delegate
);
46 EXPECT_EQ(event
, watcher
.GetWatchedObject());
50 MessageLoop::current()->Run();
52 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
56 void RunTest_BasicCancel(MessageLoop::Type message_loop_type
) {
57 MessageLoop
message_loop(message_loop_type
);
59 ObjectWatcher watcher
;
61 // A manual-reset event that is not yet signaled.
62 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
64 QuitDelegate delegate
;
65 bool ok
= watcher
.StartWatching(event
, &delegate
);
68 watcher
.StopWatching();
73 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type
) {
74 MessageLoop
message_loop(message_loop_type
);
76 ObjectWatcher watcher
;
79 DecrementCountDelegate
delegate(&counter
);
81 // A manual-reset event that is not yet signaled.
82 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
84 bool ok
= watcher
.StartWatching(event
, &delegate
);
89 // Let the background thread do its business
92 watcher
.StopWatching();
94 MessageLoop::current()->RunUntilIdle();
96 // Our delegate should not have fired.
97 EXPECT_EQ(1, counter
);
102 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type
) {
103 MessageLoop
message_loop(message_loop_type
);
105 ObjectWatcher watcher
;
107 // A manual-reset event that is signaled before we begin watching.
108 HANDLE event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
110 QuitDelegate delegate
;
111 bool ok
= watcher
.StartWatching(event
, &delegate
);
114 MessageLoop::current()->Run();
116 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
120 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type
) {
121 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
122 // doesn't happen when people use the Thread class, but it can happen when
123 // people use the Singleton pattern or atexit.
124 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
); // not signaled
126 ObjectWatcher watcher
;
128 MessageLoop
message_loop(message_loop_type
);
130 QuitDelegate delegate
;
131 watcher
.StartWatching(event
, &delegate
);
139 //-----------------------------------------------------------------------------
141 TEST(ObjectWatcherTest
, BasicSignal
) {
142 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT
);
143 RunTest_BasicSignal(MessageLoop::TYPE_IO
);
144 RunTest_BasicSignal(MessageLoop::TYPE_UI
);
147 TEST(ObjectWatcherTest
, BasicCancel
) {
148 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT
);
149 RunTest_BasicCancel(MessageLoop::TYPE_IO
);
150 RunTest_BasicCancel(MessageLoop::TYPE_UI
);
153 TEST(ObjectWatcherTest
, CancelAfterSet
) {
154 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT
);
155 RunTest_CancelAfterSet(MessageLoop::TYPE_IO
);
156 RunTest_CancelAfterSet(MessageLoop::TYPE_UI
);
159 TEST(ObjectWatcherTest
, SignalBeforeWatch
) {
160 RunTest_SignalBeforeWatch(MessageLoop::TYPE_DEFAULT
);
161 RunTest_SignalBeforeWatch(MessageLoop::TYPE_IO
);
162 RunTest_SignalBeforeWatch(MessageLoop::TYPE_UI
);
165 TEST(ObjectWatcherTest
, OutlivesMessageLoop
) {
166 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT
);
167 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO
);
168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI
);