Fix iOS build for XCode 4.6.
[chromium-blink-merge.git] / base / synchronization / waitable_event_watcher_unittest.cc
blob8744aa29085f20af08e59d100cf700ad8e831376
1 // Copyright (c) 2012 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/message_loop.h"
6 #include "base/synchronization/waitable_event.h"
7 #include "base/synchronization/waitable_event_watcher.h"
8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace base {
13 namespace {
15 // The message loops on which each waitable event timer should be tested.
16 const MessageLoop::Type testing_message_loops[] = {
17 MessageLoop::TYPE_DEFAULT,
18 MessageLoop::TYPE_IO,
19 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
20 MessageLoop::TYPE_UI,
21 #endif
24 const int kNumTestingMessageLoops = arraysize(testing_message_loops);
26 class QuitDelegate : public WaitableEventWatcher::Delegate {
27 public:
28 virtual void OnWaitableEventSignaled(WaitableEvent* event) OVERRIDE {
29 MessageLoop::current()->Quit();
33 class DecrementCountDelegate : public WaitableEventWatcher::Delegate {
34 public:
35 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
37 virtual void OnWaitableEventSignaled(WaitableEvent* object) OVERRIDE {
38 --(*counter_);
40 private:
41 int* counter_;
44 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
45 MessageLoop message_loop(message_loop_type);
47 // A manual-reset event that is not yet signaled.
48 WaitableEvent event(true, false);
50 WaitableEventWatcher watcher;
51 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
53 QuitDelegate delegate;
54 watcher.StartWatching(&event, &delegate);
55 EXPECT_EQ(&event, watcher.GetWatchedEvent());
57 event.Signal();
59 MessageLoop::current()->Run();
61 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL);
64 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
65 MessageLoop message_loop(message_loop_type);
67 // A manual-reset event that is not yet signaled.
68 WaitableEvent event(true, false);
70 WaitableEventWatcher watcher;
72 QuitDelegate delegate;
73 watcher.StartWatching(&event, &delegate);
75 watcher.StopWatching();
78 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
79 MessageLoop message_loop(message_loop_type);
81 // A manual-reset event that is not yet signaled.
82 WaitableEvent event(true, false);
84 WaitableEventWatcher watcher;
86 int counter = 1;
87 DecrementCountDelegate delegate(&counter);
89 watcher.StartWatching(&event, &delegate);
91 event.Signal();
93 // Let the background thread do its business
94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30));
96 watcher.StopWatching();
98 MessageLoop::current()->RunUntilIdle();
100 // Our delegate should not have fired.
101 EXPECT_EQ(1, counter);
104 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
105 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This
106 // ordinarily doesn't happen when people use the Thread class, but it can
107 // happen when people use the Singleton pattern or atexit.
108 WaitableEvent event(true, false);
110 WaitableEventWatcher watcher;
112 MessageLoop message_loop(message_loop_type);
114 QuitDelegate delegate;
115 watcher.StartWatching(&event, &delegate);
120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) {
121 // Delete the WaitableEvent out from under the Watcher. This is explictly
122 // allowed by the interface.
124 MessageLoop message_loop(message_loop_type);
127 WaitableEventWatcher watcher;
129 WaitableEvent* event = new WaitableEvent(false, false);
130 QuitDelegate delegate;
131 watcher.StartWatching(event, &delegate);
132 delete event;
136 } // namespace
138 //-----------------------------------------------------------------------------
140 TEST(WaitableEventWatcherTest, BasicSignal) {
141 for (int i = 0; i < kNumTestingMessageLoops; i++) {
142 RunTest_BasicSignal(testing_message_loops[i]);
146 TEST(WaitableEventWatcherTest, BasicCancel) {
147 for (int i = 0; i < kNumTestingMessageLoops; i++) {
148 RunTest_BasicCancel(testing_message_loops[i]);
152 TEST(WaitableEventWatcherTest, CancelAfterSet) {
153 for (int i = 0; i < kNumTestingMessageLoops; i++) {
154 RunTest_CancelAfterSet(testing_message_loops[i]);
158 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
159 for (int i = 0; i < kNumTestingMessageLoops; i++) {
160 RunTest_OutlivesMessageLoop(testing_message_loops[i]);
164 #if defined(OS_WIN)
165 // Crashes sometimes on vista. http://crbug.com/62119
166 #define MAYBE_DeleteUnder DISABLED_DeleteUnder
167 #else
168 #define MAYBE_DeleteUnder DeleteUnder
169 #endif
170 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) {
171 for (int i = 0; i < kNumTestingMessageLoops; i++) {
172 RunTest_DeleteUnder(testing_message_loops[i]);
176 } // namespace base