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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
7 #include "base/synchronization/waitable_event_watcher.h" | 7 #include "base/synchronization/waitable_event_watcher.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 explicit DecrementCountDelegate(int* counter) : counter_(counter) { | 24 explicit DecrementCountDelegate(int* counter) : counter_(counter) { |
25 } | 25 } |
26 virtual void OnWaitableEventSignaled(WaitableEvent* object) { | 26 virtual void OnWaitableEventSignaled(WaitableEvent* object) { |
27 --(*counter_); | 27 --(*counter_); |
28 } | 28 } |
29 private: | 29 private: |
30 int* counter_; | 30 int* counter_; |
31 }; | 31 }; |
32 | 32 |
33 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 33 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
34 MessageLoop message_loop(message_loop_type); | 34 MessageLoop message_loop("RunTest", message_loop_type); |
35 | 35 |
36 // A manual-reset event that is not yet signaled. | 36 // A manual-reset event that is not yet signaled. |
37 WaitableEvent event(true, false); | 37 WaitableEvent event(true, false); |
38 | 38 |
39 WaitableEventWatcher watcher; | 39 WaitableEventWatcher watcher; |
40 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); | 40 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
41 | 41 |
42 QuitDelegate delegate; | 42 QuitDelegate delegate; |
43 watcher.StartWatching(&event, &delegate); | 43 watcher.StartWatching(&event, &delegate); |
44 EXPECT_EQ(&event, watcher.GetWatchedEvent()); | 44 EXPECT_EQ(&event, watcher.GetWatchedEvent()); |
45 | 45 |
46 event.Signal(); | 46 event.Signal(); |
47 | 47 |
48 MessageLoop::current()->Run(); | 48 MessageLoop::current()->Run(); |
49 | 49 |
50 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); | 50 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
51 } | 51 } |
52 | 52 |
53 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 53 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
54 MessageLoop message_loop(message_loop_type); | 54 MessageLoop message_loop("RunTest", message_loop_type); |
55 | 55 |
56 // A manual-reset event that is not yet signaled. | 56 // A manual-reset event that is not yet signaled. |
57 WaitableEvent event(true, false); | 57 WaitableEvent event(true, false); |
58 | 58 |
59 WaitableEventWatcher watcher; | 59 WaitableEventWatcher watcher; |
60 | 60 |
61 QuitDelegate delegate; | 61 QuitDelegate delegate; |
62 watcher.StartWatching(&event, &delegate); | 62 watcher.StartWatching(&event, &delegate); |
63 | 63 |
64 watcher.StopWatching(); | 64 watcher.StopWatching(); |
65 } | 65 } |
66 | 66 |
67 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { | 67 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
68 MessageLoop message_loop(message_loop_type); | 68 MessageLoop message_loop("RunTest", message_loop_type); |
69 | 69 |
70 // A manual-reset event that is not yet signaled. | 70 // A manual-reset event that is not yet signaled. |
71 WaitableEvent event(true, false); | 71 WaitableEvent event(true, false); |
72 | 72 |
73 WaitableEventWatcher watcher; | 73 WaitableEventWatcher watcher; |
74 | 74 |
75 int counter = 1; | 75 int counter = 1; |
76 DecrementCountDelegate delegate(&counter); | 76 DecrementCountDelegate delegate(&counter); |
77 | 77 |
78 watcher.StartWatching(&event, &delegate); | 78 watcher.StartWatching(&event, &delegate); |
(...skipping 12 matching lines...) Expand all Loading... |
91 } | 91 } |
92 | 92 |
93 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 93 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
94 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This | 94 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This |
95 // ordinarily doesn't happen when people use the Thread class, but it can | 95 // ordinarily doesn't happen when people use the Thread class, but it can |
96 // happen when people use the Singleton pattern or atexit. | 96 // happen when people use the Singleton pattern or atexit. |
97 WaitableEvent event(true, false); | 97 WaitableEvent event(true, false); |
98 { | 98 { |
99 WaitableEventWatcher watcher; | 99 WaitableEventWatcher watcher; |
100 { | 100 { |
101 MessageLoop message_loop(message_loop_type); | 101 MessageLoop message_loop("RunTest", message_loop_type); |
102 | 102 |
103 QuitDelegate delegate; | 103 QuitDelegate delegate; |
104 watcher.StartWatching(&event, &delegate); | 104 watcher.StartWatching(&event, &delegate); |
105 } | 105 } |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { | 109 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { |
110 // Delete the WaitableEvent out from under the Watcher. This is explictly | 110 // Delete the WaitableEvent out from under the Watcher. This is explictly |
111 // allowed by the interface. | 111 // allowed by the interface. |
112 | 112 |
113 MessageLoop message_loop(message_loop_type); | 113 MessageLoop message_loop("RunTest", message_loop_type); |
114 | 114 |
115 { | 115 { |
116 WaitableEventWatcher watcher; | 116 WaitableEventWatcher watcher; |
117 | 117 |
118 WaitableEvent* event = new WaitableEvent(false, false); | 118 WaitableEvent* event = new WaitableEvent(false, false); |
119 QuitDelegate delegate; | 119 QuitDelegate delegate; |
120 watcher.StartWatching(event, &delegate); | 120 watcher.StartWatching(event, &delegate); |
121 delete event; | 121 delete event; |
122 } | 122 } |
123 } | 123 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 #else | 156 #else |
157 #define MAYBE_DeleteUnder DeleteUnder | 157 #define MAYBE_DeleteUnder DeleteUnder |
158 #endif | 158 #endif |
159 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { | 159 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { |
160 RunTest_DeleteUnder(MessageLoop::TYPE_DEFAULT); | 160 RunTest_DeleteUnder(MessageLoop::TYPE_DEFAULT); |
161 RunTest_DeleteUnder(MessageLoop::TYPE_IO); | 161 RunTest_DeleteUnder(MessageLoop::TYPE_IO); |
162 RunTest_DeleteUnder(MessageLoop::TYPE_UI); | 162 RunTest_DeleteUnder(MessageLoop::TYPE_UI); |
163 } | 163 } |
164 | 164 |
165 } // namespace base | 165 } // namespace base |
OLD | NEW |