Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / base / win / object_watcher_unittest.cc
blobb30ca41a4fe4d3f6cb37455a72ed57ed69d063e4
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 void OnObjectSignaled(HANDLE object) override {
21 MessageLoop::current()->QuitWhenIdle();
25 class DecrementCountDelegate : public ObjectWatcher::Delegate {
26 public:
27 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
29 void OnObjectSignaled(HANDLE object) override { --(*counter_); }
31 private:
32 int* counter_;
35 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
36 MessageLoop message_loop(message_loop_type);
38 ObjectWatcher watcher;
39 EXPECT_FALSE(watcher.IsWatching());
41 // A manual-reset event that is not yet signaled.
42 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
44 QuitDelegate delegate;
45 bool ok = watcher.StartWatching(event, &delegate);
46 EXPECT_TRUE(ok);
47 EXPECT_TRUE(watcher.IsWatching());
48 EXPECT_EQ(event, watcher.GetWatchedObject());
50 SetEvent(event);
52 MessageLoop::current()->Run();
54 EXPECT_FALSE(watcher.IsWatching());
55 CloseHandle(event);
58 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
59 MessageLoop message_loop(message_loop_type);
61 ObjectWatcher watcher;
63 // A manual-reset event that is not yet signaled.
64 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
66 QuitDelegate delegate;
67 bool ok = watcher.StartWatching(event, &delegate);
68 EXPECT_TRUE(ok);
70 watcher.StopWatching();
72 CloseHandle(event);
75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
76 MessageLoop message_loop(message_loop_type);
78 ObjectWatcher watcher;
80 int counter = 1;
81 DecrementCountDelegate delegate(&counter);
83 // A manual-reset event that is not yet signaled.
84 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
86 bool ok = watcher.StartWatching(event, &delegate);
87 EXPECT_TRUE(ok);
89 SetEvent(event);
91 // Let the background thread do its business
92 Sleep(30);
94 watcher.StopWatching();
96 RunLoop().RunUntilIdle();
98 // Our delegate should not have fired.
99 EXPECT_EQ(1, counter);
101 CloseHandle(event);
104 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) {
105 MessageLoop message_loop(message_loop_type);
107 ObjectWatcher watcher;
109 // A manual-reset event that is signaled before we begin watching.
110 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL);
112 QuitDelegate delegate;
113 bool ok = watcher.StartWatching(event, &delegate);
114 EXPECT_TRUE(ok);
116 MessageLoop::current()->Run();
118 EXPECT_FALSE(watcher.IsWatching());
119 CloseHandle(event);
122 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
123 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
124 // doesn't happen when people use the Thread class, but it can happen when
125 // people use the Singleton pattern or atexit.
126 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled
128 ObjectWatcher watcher;
130 MessageLoop message_loop(message_loop_type);
132 QuitDelegate delegate;
133 watcher.StartWatching(event, &delegate);
136 CloseHandle(event);
139 } // namespace
141 //-----------------------------------------------------------------------------
143 TEST(ObjectWatcherTest, BasicSignal) {
144 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT);
145 RunTest_BasicSignal(MessageLoop::TYPE_IO);
146 RunTest_BasicSignal(MessageLoop::TYPE_UI);
149 TEST(ObjectWatcherTest, BasicCancel) {
150 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT);
151 RunTest_BasicCancel(MessageLoop::TYPE_IO);
152 RunTest_BasicCancel(MessageLoop::TYPE_UI);
155 TEST(ObjectWatcherTest, CancelAfterSet) {
156 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT);
157 RunTest_CancelAfterSet(MessageLoop::TYPE_IO);
158 RunTest_CancelAfterSet(MessageLoop::TYPE_UI);
161 TEST(ObjectWatcherTest, SignalBeforeWatch) {
162 RunTest_SignalBeforeWatch(MessageLoop::TYPE_DEFAULT);
163 RunTest_SignalBeforeWatch(MessageLoop::TYPE_IO);
164 RunTest_SignalBeforeWatch(MessageLoop::TYPE_UI);
167 TEST(ObjectWatcherTest, OutlivesMessageLoop) {
168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT);
169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO);
170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI);
173 } // namespace win
174 } // namespace base