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/cancelable_callback.h" | 5 #include "base/cancelable_callback.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 base::Unretained(&count))); | 152 base::Unretained(&count))); |
153 EXPECT_FALSE(cancelable.IsCancelled()); | 153 EXPECT_FALSE(cancelable.IsCancelled()); |
154 | 154 |
155 cancelable.Cancel(); | 155 cancelable.Cancel(); |
156 EXPECT_TRUE(cancelable.IsCancelled()); | 156 EXPECT_TRUE(cancelable.IsCancelled()); |
157 } | 157 } |
158 | 158 |
159 // CancelableCallback posted to a MessageLoop with PostTask. | 159 // CancelableCallback posted to a MessageLoop with PostTask. |
160 // - Callbacks posted to a MessageLoop can be cancelled. | 160 // - Callbacks posted to a MessageLoop can be cancelled. |
161 TEST(CancelableCallbackTest, PostTask) { | 161 TEST(CancelableCallbackTest, PostTask) { |
162 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 162 MessageLoop loop; |
163 | 163 |
164 int count = 0; | 164 int count = 0; |
165 CancelableClosure cancelable(base::Bind(&Increment, | 165 CancelableClosure cancelable(base::Bind(&Increment, |
166 base::Unretained(&count))); | 166 base::Unretained(&count))); |
167 | 167 |
168 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback()); | 168 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback()); |
169 RunLoop().RunUntilIdle(); | 169 RunLoop().RunUntilIdle(); |
170 | 170 |
171 EXPECT_EQ(1, count); | 171 EXPECT_EQ(1, count); |
172 | 172 |
173 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback()); | 173 MessageLoop::current()->PostTask(FROM_HERE, cancelable.callback()); |
174 | 174 |
175 // Cancel before running the message loop. | 175 // Cancel before running the message loop. |
176 cancelable.Cancel(); | 176 cancelable.Cancel(); |
177 RunLoop().RunUntilIdle(); | 177 RunLoop().RunUntilIdle(); |
178 | 178 |
179 // Callback never ran due to cancellation; count is the same. | 179 // Callback never ran due to cancellation; count is the same. |
180 EXPECT_EQ(1, count); | 180 EXPECT_EQ(1, count); |
181 } | 181 } |
182 | 182 |
183 } // namespace | 183 } // namespace |
184 } // namespace base | 184 } // namespace base |
OLD | NEW |