Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: base/object_watcher_unittest.cc

Issue 5971008: move base/object_watcher into base/win and add the win namespace. Fixup calle... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/object_watcher.cc ('k') | base/waitable_event_watcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include <process.h>
6
7 #include "base/message_loop.h"
8 #include "base/object_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 class QuitDelegate : public base::ObjectWatcher::Delegate {
14 public:
15 virtual void OnObjectSignaled(HANDLE object) {
16 MessageLoop::current()->Quit();
17 }
18 };
19
20 class DecrementCountDelegate : public base::ObjectWatcher::Delegate {
21 public:
22 explicit DecrementCountDelegate(int* counter) : counter_(counter) {
23 }
24 virtual void OnObjectSignaled(HANDLE object) {
25 --(*counter_);
26 }
27 private:
28 int* counter_;
29 };
30
31 } // namespace
32
33 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
34 MessageLoop message_loop(message_loop_type);
35
36 base::ObjectWatcher watcher;
37 EXPECT_EQ(NULL, watcher.GetWatchedObject());
38
39 // A manual-reset event that is not yet signaled.
40 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
41
42 QuitDelegate delegate;
43 bool ok = watcher.StartWatching(event, &delegate);
44 EXPECT_TRUE(ok);
45 EXPECT_EQ(event, watcher.GetWatchedObject());
46
47 SetEvent(event);
48
49 MessageLoop::current()->Run();
50
51 EXPECT_EQ(NULL, watcher.GetWatchedObject());
52 CloseHandle(event);
53 }
54
55 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
56 MessageLoop message_loop(message_loop_type);
57
58 base::ObjectWatcher watcher;
59
60 // A manual-reset event that is not yet signaled.
61 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
62
63 QuitDelegate delegate;
64 bool ok = watcher.StartWatching(event, &delegate);
65 EXPECT_TRUE(ok);
66
67 watcher.StopWatching();
68
69 CloseHandle(event);
70 }
71
72
73 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
74 MessageLoop message_loop(message_loop_type);
75
76 base::ObjectWatcher watcher;
77
78 int counter = 1;
79 DecrementCountDelegate delegate(&counter);
80
81 // A manual-reset event that is not yet signaled.
82 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
83
84 bool ok = watcher.StartWatching(event, &delegate);
85 EXPECT_TRUE(ok);
86
87 SetEvent(event);
88
89 // Let the background thread do its business
90 Sleep(30);
91
92 watcher.StopWatching();
93
94 MessageLoop::current()->RunAllPending();
95
96 // Our delegate should not have fired.
97 EXPECT_EQ(1, counter);
98
99 CloseHandle(event);
100 }
101
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
107 {
108 base::ObjectWatcher watcher;
109 {
110 MessageLoop message_loop(message_loop_type);
111
112 QuitDelegate delegate;
113 watcher.StartWatching(event, &delegate);
114 }
115 }
116 CloseHandle(event);
117 }
118
119 //-----------------------------------------------------------------------------
120
121 TEST(ObjectWatcherTest, BasicSignal) {
122 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT);
123 RunTest_BasicSignal(MessageLoop::TYPE_IO);
124 RunTest_BasicSignal(MessageLoop::TYPE_UI);
125 }
126
127 TEST(ObjectWatcherTest, BasicCancel) {
128 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT);
129 RunTest_BasicCancel(MessageLoop::TYPE_IO);
130 RunTest_BasicCancel(MessageLoop::TYPE_UI);
131 }
132
133 TEST(ObjectWatcherTest, CancelAfterSet) {
134 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT);
135 RunTest_CancelAfterSet(MessageLoop::TYPE_IO);
136 RunTest_CancelAfterSet(MessageLoop::TYPE_UI);
137 }
138
139 TEST(ObjectWatcherTest, OutlivesMessageLoop) {
140 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT);
141 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO);
142 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI);
143 }
OLDNEW
« no previous file with comments | « base/object_watcher.cc ('k') | base/waitable_event_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698