PageLanguageDetectionTest has the failure rate of 5 - 6% on XP/Vista. Mark it
[chromium-blink-merge.git] / base / object_watcher_unittest.cc
blob3605dbb426fee2e3936f6acaac7e49ba13cdc8ce
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.
5 #include <process.h>
7 #include "base/message_loop.h"
8 #include "base/object_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 class QuitDelegate : public base::ObjectWatcher::Delegate {
14 public:
15 virtual void OnObjectSignaled(HANDLE object) {
16 MessageLoop::current()->Quit();
20 class DecrementCountDelegate : public base::ObjectWatcher::Delegate {
21 public:
22 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
24 virtual void OnObjectSignaled(HANDLE object) {
25 --(*counter_);
27 private:
28 int* counter_;
31 } // namespace
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);
44 EXPECT_TRUE(ok);
45 EXPECT_EQ(event, watcher.GetWatchedObject());
47 SetEvent(event);
49 MessageLoop::current()->Run();
51 EXPECT_EQ(NULL, watcher.GetWatchedObject());
52 CloseHandle(event);
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);
65 EXPECT_TRUE(ok);
67 watcher.StopWatching();
69 CloseHandle(event);
73 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
74 MessageLoop message_loop(message_loop_type);
76 base::ObjectWatcher watcher;
78 int counter = 1;
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);
85 EXPECT_TRUE(ok);
87 SetEvent(event);
89 // Let the background thread do its business
90 Sleep(30);
92 watcher.StopWatching();
94 MessageLoop::current()->RunAllPending();
96 // Our delegate should not have fired.
97 EXPECT_EQ(1, counter);
99 CloseHandle(event);
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);
116 CloseHandle(event);
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);