OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/win/object_watcher.h" | 5 #include "base/win/object_watcher.h" |
6 | 6 |
7 #include <process.h> | 7 #include <process.h> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 int* counter_; | 32 int* counter_; |
33 }; | 33 }; |
34 | 34 |
35 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 35 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
36 MessageLoop message_loop(message_loop_type); | 36 MessageLoop message_loop(message_loop_type); |
37 | 37 |
38 ObjectWatcher watcher; | 38 ObjectWatcher watcher; |
39 EXPECT_FALSE(watcher.IsWatching()); | 39 EXPECT_FALSE(watcher.IsWatching()); |
40 | 40 |
41 // A manual-reset event that is not yet signaled. | 41 // A manual-reset event that is not yet signaled. |
42 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 42 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); |
43 | 43 |
44 QuitDelegate delegate; | 44 QuitDelegate delegate; |
45 bool ok = watcher.StartWatching(event.Get(), &delegate); | 45 bool ok = watcher.StartWatching(event, &delegate); |
46 EXPECT_TRUE(ok); | 46 EXPECT_TRUE(ok); |
47 EXPECT_TRUE(watcher.IsWatching()); | 47 EXPECT_TRUE(watcher.IsWatching()); |
48 EXPECT_EQ(event.Get(), watcher.GetWatchedObject()); | 48 EXPECT_EQ(event, watcher.GetWatchedObject()); |
49 | 49 |
50 SetEvent(event.Get()); | 50 SetEvent(event); |
51 | 51 |
52 MessageLoop::current()->Run(); | 52 MessageLoop::current()->Run(); |
53 | 53 |
54 EXPECT_FALSE(watcher.IsWatching()); | 54 EXPECT_FALSE(watcher.IsWatching()); |
| 55 CloseHandle(event); |
55 } | 56 } |
56 | 57 |
57 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 58 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
58 MessageLoop message_loop(message_loop_type); | 59 MessageLoop message_loop(message_loop_type); |
59 | 60 |
60 ObjectWatcher watcher; | 61 ObjectWatcher watcher; |
61 | 62 |
62 // A manual-reset event that is not yet signaled. | 63 // A manual-reset event that is not yet signaled. |
63 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 64 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); |
64 | 65 |
65 QuitDelegate delegate; | 66 QuitDelegate delegate; |
66 bool ok = watcher.StartWatching(event.Get(), &delegate); | 67 bool ok = watcher.StartWatching(event, &delegate); |
67 EXPECT_TRUE(ok); | 68 EXPECT_TRUE(ok); |
68 | 69 |
69 watcher.StopWatching(); | 70 watcher.StopWatching(); |
| 71 |
| 72 CloseHandle(event); |
70 } | 73 } |
71 | 74 |
72 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { | 75 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
73 MessageLoop message_loop(message_loop_type); | 76 MessageLoop message_loop(message_loop_type); |
74 | 77 |
75 ObjectWatcher watcher; | 78 ObjectWatcher watcher; |
76 | 79 |
77 int counter = 1; | 80 int counter = 1; |
78 DecrementCountDelegate delegate(&counter); | 81 DecrementCountDelegate delegate(&counter); |
79 | 82 |
80 // A manual-reset event that is not yet signaled. | 83 // A manual-reset event that is not yet signaled. |
81 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 84 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); |
82 | 85 |
83 bool ok = watcher.StartWatching(event.Get(), &delegate); | 86 bool ok = watcher.StartWatching(event, &delegate); |
84 EXPECT_TRUE(ok); | 87 EXPECT_TRUE(ok); |
85 | 88 |
86 SetEvent(event.Get()); | 89 SetEvent(event); |
87 | 90 |
88 // Let the background thread do its business | 91 // Let the background thread do its business |
89 Sleep(30); | 92 Sleep(30); |
90 | 93 |
91 watcher.StopWatching(); | 94 watcher.StopWatching(); |
92 | 95 |
93 RunLoop().RunUntilIdle(); | 96 RunLoop().RunUntilIdle(); |
94 | 97 |
95 // Our delegate should not have fired. | 98 // Our delegate should not have fired. |
96 EXPECT_EQ(1, counter); | 99 EXPECT_EQ(1, counter); |
| 100 |
| 101 CloseHandle(event); |
97 } | 102 } |
98 | 103 |
99 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { | 104 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { |
100 MessageLoop message_loop(message_loop_type); | 105 MessageLoop message_loop(message_loop_type); |
101 | 106 |
102 ObjectWatcher watcher; | 107 ObjectWatcher watcher; |
103 | 108 |
104 // A manual-reset event that is signaled before we begin watching. | 109 // A manual-reset event that is signaled before we begin watching. |
105 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, TRUE, NULL)); | 110 HANDLE event = CreateEvent(NULL, TRUE, TRUE, NULL); |
106 | 111 |
107 QuitDelegate delegate; | 112 QuitDelegate delegate; |
108 bool ok = watcher.StartWatching(event.Get(), &delegate); | 113 bool ok = watcher.StartWatching(event, &delegate); |
109 EXPECT_TRUE(ok); | 114 EXPECT_TRUE(ok); |
110 | 115 |
111 MessageLoop::current()->Run(); | 116 MessageLoop::current()->Run(); |
112 | 117 |
113 EXPECT_FALSE(watcher.IsWatching()); | 118 EXPECT_FALSE(watcher.IsWatching()); |
| 119 CloseHandle(event); |
114 } | 120 } |
115 | 121 |
116 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 122 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
117 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily | 123 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily |
118 // doesn't happen when people use the Thread class, but it can happen when | 124 // doesn't happen when people use the Thread class, but it can happen when |
119 // people use the Singleton pattern or atexit. | 125 // people use the Singleton pattern or atexit. |
120 // Note that |event| is not signaled | 126 HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled |
121 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | |
122 { | 127 { |
123 ObjectWatcher watcher; | 128 ObjectWatcher watcher; |
124 { | 129 { |
125 MessageLoop message_loop(message_loop_type); | 130 MessageLoop message_loop(message_loop_type); |
126 | 131 |
127 QuitDelegate delegate; | 132 QuitDelegate delegate; |
128 watcher.StartWatching(event.Get(), &delegate); | 133 watcher.StartWatching(event, &delegate); |
129 } | 134 } |
130 } | 135 } |
| 136 CloseHandle(event); |
131 } | 137 } |
132 | 138 |
133 } // namespace | 139 } // namespace |
134 | 140 |
135 //----------------------------------------------------------------------------- | 141 //----------------------------------------------------------------------------- |
136 | 142 |
137 TEST(ObjectWatcherTest, BasicSignal) { | 143 TEST(ObjectWatcherTest, BasicSignal) { |
138 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); | 144 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); |
139 RunTest_BasicSignal(MessageLoop::TYPE_IO); | 145 RunTest_BasicSignal(MessageLoop::TYPE_IO); |
140 RunTest_BasicSignal(MessageLoop::TYPE_UI); | 146 RunTest_BasicSignal(MessageLoop::TYPE_UI); |
(...skipping 18 matching lines...) Expand all Loading... |
159 } | 165 } |
160 | 166 |
161 TEST(ObjectWatcherTest, OutlivesMessageLoop) { | 167 TEST(ObjectWatcherTest, OutlivesMessageLoop) { |
162 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); | 168 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); |
163 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); | 169 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); |
164 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); | 170 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); |
165 } | 171 } |
166 | 172 |
167 } // namespace win | 173 } // namespace win |
168 } // namespace base | 174 } // namespace base |
OLD | NEW |