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 24 matching lines...) Expand all Loading... |
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 base::win::ScopedHandle 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.Get(), &delegate, false); |
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.Get(), watcher.GetWatchedObject()); |
49 | 49 |
50 SetEvent(event.Get()); | 50 SetEvent(event.Get()); |
51 | 51 |
52 MessageLoop::current()->Run(); | 52 MessageLoop::current()->Run(); |
53 | 53 |
54 EXPECT_FALSE(watcher.IsWatching()); | 54 EXPECT_FALSE(watcher.IsWatching()); |
55 } | 55 } |
56 | 56 |
57 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 57 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
58 MessageLoop message_loop(message_loop_type); | 58 MessageLoop message_loop(message_loop_type); |
59 | 59 |
60 ObjectWatcher watcher; | 60 ObjectWatcher watcher; |
61 | 61 |
62 // A manual-reset event that is not yet signaled. | 62 // A manual-reset event that is not yet signaled. |
63 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 63 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
64 | 64 |
65 QuitDelegate delegate; | 65 QuitDelegate delegate; |
66 bool ok = watcher.StartWatching(event.Get(), &delegate); | 66 bool ok = watcher.StartWatching(event.Get(), &delegate, false); |
67 EXPECT_TRUE(ok); | 67 EXPECT_TRUE(ok); |
68 | 68 |
69 watcher.StopWatching(); | 69 watcher.StopWatching(); |
70 } | 70 } |
71 | 71 |
72 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { | 72 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
73 MessageLoop message_loop(message_loop_type); | 73 MessageLoop message_loop(message_loop_type); |
74 | 74 |
75 ObjectWatcher watcher; | 75 ObjectWatcher watcher; |
76 | 76 |
77 int counter = 1; | 77 int counter = 1; |
78 DecrementCountDelegate delegate(&counter); | 78 DecrementCountDelegate delegate(&counter); |
79 | 79 |
80 // A manual-reset event that is not yet signaled. | 80 // A manual-reset event that is not yet signaled. |
81 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 81 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
82 | 82 |
83 bool ok = watcher.StartWatching(event.Get(), &delegate); | 83 bool ok = watcher.StartWatching(event.Get(), &delegate, false); |
84 EXPECT_TRUE(ok); | 84 EXPECT_TRUE(ok); |
85 | 85 |
86 SetEvent(event.Get()); | 86 SetEvent(event.Get()); |
87 | 87 |
88 // Let the background thread do its business | 88 // Let the background thread do its business |
89 Sleep(30); | 89 Sleep(30); |
90 | 90 |
91 watcher.StopWatching(); | 91 watcher.StopWatching(); |
92 | 92 |
93 RunLoop().RunUntilIdle(); | 93 RunLoop().RunUntilIdle(); |
94 | 94 |
95 // Our delegate should not have fired. | 95 // Our delegate should not have fired. |
96 EXPECT_EQ(1, counter); | 96 EXPECT_EQ(1, counter); |
97 } | 97 } |
98 | 98 |
99 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { | 99 void RunTest_SignalBeforeWatch(MessageLoop::Type message_loop_type) { |
100 MessageLoop message_loop(message_loop_type); | 100 MessageLoop message_loop(message_loop_type); |
101 | 101 |
102 ObjectWatcher watcher; | 102 ObjectWatcher watcher; |
103 | 103 |
104 // A manual-reset event that is signaled before we begin watching. | 104 // A manual-reset event that is signaled before we begin watching. |
105 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, TRUE, NULL)); | 105 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, TRUE, NULL)); |
106 | 106 |
107 QuitDelegate delegate; | 107 QuitDelegate delegate; |
108 bool ok = watcher.StartWatching(event.Get(), &delegate); | 108 bool ok = watcher.StartWatching(event.Get(), &delegate, false); |
109 EXPECT_TRUE(ok); | 109 EXPECT_TRUE(ok); |
110 | 110 |
111 MessageLoop::current()->Run(); | 111 MessageLoop::current()->Run(); |
112 | 112 |
113 EXPECT_FALSE(watcher.IsWatching()); | 113 EXPECT_FALSE(watcher.IsWatching()); |
114 } | 114 } |
115 | 115 |
116 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 116 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
117 // Simulate a MessageLoop that dies before an ObjectWatcher. This ordinarily | 117 // 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 | 118 // doesn't happen when people use the Thread class, but it can happen when |
119 // people use the Singleton pattern or atexit. | 119 // people use the Singleton pattern or atexit. |
120 // Note that |event| is not signaled | 120 // Note that |event| is not signaled |
121 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); | 121 base::win::ScopedHandle event(CreateEvent(NULL, TRUE, FALSE, NULL)); |
122 { | 122 { |
123 ObjectWatcher watcher; | 123 ObjectWatcher watcher; |
124 { | 124 { |
125 MessageLoop message_loop(message_loop_type); | 125 MessageLoop message_loop(message_loop_type); |
126 | 126 |
127 QuitDelegate delegate; | 127 QuitDelegate delegate; |
128 watcher.StartWatching(event.Get(), &delegate); | 128 watcher.StartWatching(event.Get(), &delegate, false); |
129 } | 129 } |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 } // namespace | 133 } // namespace |
134 | 134 |
135 //----------------------------------------------------------------------------- | 135 //----------------------------------------------------------------------------- |
136 | 136 |
137 TEST(ObjectWatcherTest, BasicSignal) { | 137 TEST(ObjectWatcherTest, BasicSignal) { |
138 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); | 138 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT); |
(...skipping 20 matching lines...) Expand all Loading... |
159 } | 159 } |
160 | 160 |
161 TEST(ObjectWatcherTest, OutlivesMessageLoop) { | 161 TEST(ObjectWatcherTest, OutlivesMessageLoop) { |
162 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); | 162 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT); |
163 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); | 163 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO); |
164 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); | 164 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI); |
165 } | 165 } |
166 | 166 |
167 } // namespace win | 167 } // namespace win |
168 } // namespace base | 168 } // namespace base |
OLD | NEW |