Port Android relocation packer to chromium build
[chromium-blink-merge.git] / base / win / object_watcher_unittest.cc
blob1a5e9e2a1a73e124e983c851b8b6cce5ea82dd82
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"
7 #include <process.h>
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
14 namespace win {
16 namespace {
18 class QuitDelegate : public ObjectWatcher::Delegate {
19 public:
20 virtual void OnObjectSignaled(HANDLE object) {
21 MessageLoop::current()->QuitWhenIdle();
25 class DecrementCountDelegate : public ObjectWatcher::Delegate {
26 public:
27 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
29 virtual void OnObjectSignaled(HANDLE object) {
30 --(*counter_);
32 private:
33 int* counter_;
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);
47 EXPECT_TRUE(ok);
48 EXPECT_TRUE(watcher.IsWatching());
49 EXPECT_EQ(event, watcher.GetWatchedObject());
51 SetEvent(event);
53 MessageLoop::current()->Run();
55 EXPECT_FALSE(watcher.IsWatching());
56 CloseHandle(event);
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);
69 EXPECT_TRUE(ok);
71 watcher.StopWatching();
73 CloseHandle(event);
76 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
77 MessageLoop message_loop(message_loop_type);
79 ObjectWatcher watcher;
81 int counter = 1;
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);
88 EXPECT_TRUE(ok);
90 SetEvent(event);
92 // Let the background thread do its business
93 Sleep(30);
95 watcher.StopWatching();
97 RunLoop().RunUntilIdle();
99 // Our delegate should not have fired.
100 EXPECT_EQ(1, counter);
102 CloseHandle(event);
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);
115 EXPECT_TRUE(ok);
117 MessageLoop::current()->Run();
119 EXPECT_FALSE(watcher.IsWatching());
120 CloseHandle(event);
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);
137 CloseHandle(event);
140 } // namespace
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);
174 } // namespace win
175 } // namespace base