1 // Copyright (c) 2006-2008 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/object_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 class QuitDelegate
: public base::ObjectWatcher::Delegate
{
15 virtual void OnObjectSignaled(HANDLE object
) {
16 MessageLoop::current()->Quit();
20 class DecrementCountDelegate
: public base::ObjectWatcher::Delegate
{
22 explicit DecrementCountDelegate(int* counter
) : counter_(counter
) {
24 virtual void OnObjectSignaled(HANDLE object
) {
33 void RunTest_BasicSignal(MessageLoop::Type message_loop_type
) {
34 MessageLoop
message_loop(message_loop_type
);
36 base::ObjectWatcher watcher
;
37 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
39 // A manual-reset event that is not yet signaled.
40 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
42 QuitDelegate delegate
;
43 bool ok
= watcher
.StartWatching(event
, &delegate
);
45 EXPECT_EQ(event
, watcher
.GetWatchedObject());
49 MessageLoop::current()->Run();
51 EXPECT_EQ(NULL
, watcher
.GetWatchedObject());
55 void RunTest_BasicCancel(MessageLoop::Type message_loop_type
) {
56 MessageLoop
message_loop(message_loop_type
);
58 base::ObjectWatcher watcher
;
60 // A manual-reset event that is not yet signaled.
61 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
63 QuitDelegate delegate
;
64 bool ok
= watcher
.StartWatching(event
, &delegate
);
67 watcher
.StopWatching();
73 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type
) {
74 MessageLoop
message_loop(message_loop_type
);
76 base::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()->RunAllPending();
96 // Our delegate should not have fired.
97 EXPECT_EQ(1, counter
);
102 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type
) {
103 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
104 // doesn't happen when people use the Thread class, but it can happen when
105 // people use the Singleton pattern or atexit.
106 HANDLE event
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
); // not signaled
108 base::ObjectWatcher watcher
;
110 MessageLoop
message_loop(message_loop_type
);
112 QuitDelegate delegate
;
113 watcher
.StartWatching(event
, &delegate
);
119 //-----------------------------------------------------------------------------
121 TEST(ObjectWatcherTest
, BasicSignal
) {
122 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT
);
123 RunTest_BasicSignal(MessageLoop::TYPE_IO
);
124 RunTest_BasicSignal(MessageLoop::TYPE_UI
);
127 TEST(ObjectWatcherTest
, BasicCancel
) {
128 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT
);
129 RunTest_BasicCancel(MessageLoop::TYPE_IO
);
130 RunTest_BasicCancel(MessageLoop::TYPE_UI
);
133 TEST(ObjectWatcherTest
, CancelAfterSet
) {
134 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT
);
135 RunTest_CancelAfterSet(MessageLoop::TYPE_IO
);
136 RunTest_CancelAfterSet(MessageLoop::TYPE_UI
);
139 TEST(ObjectWatcherTest
, OutlivesMessageLoop
) {
140 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT
);
141 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO
);
142 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI
);