Improve performance of registering font preferences
[chromium-blink-merge.git] / base / win / object_watcher_unittest.cc
blobe8484b98e250a049d4d27221e94ca2dc1115f8be
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 <process.h>
7 #include "base/message_loop.h"
8 #include "base/win/object_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace base {
12 namespace win {
14 namespace {
16 class QuitDelegate : public ObjectWatcher::Delegate {
17 public:
18 virtual void OnObjectSignaled(HANDLE object) {
19 MessageLoop::current()->Quit();
23 class DecrementCountDelegate : public ObjectWatcher::Delegate {
24 public:
25 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
27 virtual void OnObjectSignaled(HANDLE object) {
28 --(*counter_);
30 private:
31 int* counter_;
34 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
35 MessageLoop message_loop(message_loop_type);
37 ObjectWatcher watcher;
38 EXPECT_EQ(NULL, watcher.GetWatchedObject());
40 // A manual-reset event that is not yet signaled.
41 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
43 QuitDelegate delegate;
44 bool ok = watcher.StartWatching(event, &delegate);
45 EXPECT_TRUE(ok);
46 EXPECT_EQ(event, watcher.GetWatchedObject());
48 SetEvent(event);
50 MessageLoop::current()->Run();
52 EXPECT_EQ(NULL, watcher.GetWatchedObject());
53 CloseHandle(event);
56 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
57 MessageLoop message_loop(message_loop_type);
59 ObjectWatcher watcher;
61 // A manual-reset event that is not yet signaled.
62 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
64 QuitDelegate delegate;
65 bool ok = watcher.StartWatching(event, &delegate);
66 EXPECT_TRUE(ok);
68 watcher.StopWatching();
70 CloseHandle(event);
73 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
74 MessageLoop message_loop(message_loop_type);
76 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()->RunUntilIdle();
96 // Our delegate should not have fired.
97 EXPECT_EQ(1, counter);
99 CloseHandle(event);
102 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) {
103 MessageLoop message_loop(message_loop_type);
105 ObjectWatcher watcher;
107 // A manual-reset event that is signaled before we begin watching.
108 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL);
110 QuitDelegate delegate;
111 bool ok = watcher.StartWatching(event, &delegate);
112 EXPECT_TRUE(ok);
114 MessageLoop::current()->Run();
116 EXPECT_EQ(NULL, watcher.GetWatchedObject());
117 CloseHandle(event);
120 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
121 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily
122 // doesn't happen when people use the Thread class, but it can happen when
123 // people use the Singleton pattern or atexit.
124 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled
126 ObjectWatcher watcher;
128 MessageLoop message_loop(message_loop_type);
130 QuitDelegate delegate;
131 watcher.StartWatching(event, &delegate);
134 CloseHandle(event);
137 } // namespace
139 //-----------------------------------------------------------------------------
141 TEST(ObjectWatcherTest, BasicSignal) {
142 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT);
143 RunTest_BasicSignal(MessageLoop::TYPE_IO);
144 RunTest_BasicSignal(MessageLoop::TYPE_UI);
147 TEST(ObjectWatcherTest, BasicCancel) {
148 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT);
149 RunTest_BasicCancel(MessageLoop::TYPE_IO);
150 RunTest_BasicCancel(MessageLoop::TYPE_UI);
153 TEST(ObjectWatcherTest, CancelAfterSet) {
154 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT);
155 RunTest_CancelAfterSet(MessageLoop::TYPE_IO);
156 RunTest_CancelAfterSet(MessageLoop::TYPE_UI);
159 TEST(ObjectWatcherTest, SignalBeforeWatch) {
160 RunTest_SignalBeforeWatch(MessageLoop::TYPE_DEFAULT);
161 RunTest_SignalBeforeWatch(MessageLoop::TYPE_IO);
162 RunTest_SignalBeforeWatch(MessageLoop::TYPE_UI);
165 TEST(ObjectWatcherTest, OutlivesMessageLoop) {
166 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT);
167 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO);
168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI);
171 } // namespace win
172 } // namespace base