| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/timer/timer.h" | 5 #include "base/timer/timer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 13 #include "base/test/test_simple_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using base::TimeDelta; | 18 using base::TimeDelta; |
| 18 using base::SingleThreadTaskRunner; | 19 using base::SingleThreadTaskRunner; |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // The message loops on which each timer should be tested. | 23 // The message loops on which each timer should be tested. |
| 23 const base::MessageLoop::Type testing_message_loops[] = { | 24 const base::MessageLoop::Type testing_message_loops[] = { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 EXPECT_TRUE(did_run); | 121 EXPECT_TRUE(did_run); |
| 121 } | 122 } |
| 122 | 123 |
| 123 void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) { | 124 void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) { |
| 124 base::MessageLoop loop(message_loop_type); | 125 base::MessageLoop loop(message_loop_type); |
| 125 | 126 |
| 126 bool did_run_a = false; | 127 bool did_run_a = false; |
| 127 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); | 128 OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); |
| 128 | 129 |
| 129 // This should run before the timer expires. | 130 // This should run before the timer expires. |
| 130 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 131 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
| 131 | 132 |
| 132 // Now start the timer. | 133 // Now start the timer. |
| 133 a->Start(); | 134 a->Start(); |
| 134 | 135 |
| 135 bool did_run_b = false; | 136 bool did_run_b = false; |
| 136 OneShotTimerTester b(&did_run_b); | 137 OneShotTimerTester b(&did_run_b); |
| 137 b.Start(); | 138 b.Start(); |
| 138 | 139 |
| 139 base::MessageLoop::current()->Run(); | 140 base::MessageLoop::current()->Run(); |
| 140 | 141 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 169 } | 170 } |
| 170 | 171 |
| 171 void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type, | 172 void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type, |
| 172 const TimeDelta& delay) { | 173 const TimeDelta& delay) { |
| 173 base::MessageLoop loop(message_loop_type); | 174 base::MessageLoop loop(message_loop_type); |
| 174 | 175 |
| 175 bool did_run_a = false; | 176 bool did_run_a = false; |
| 176 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay); | 177 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay); |
| 177 | 178 |
| 178 // This should run before the timer expires. | 179 // This should run before the timer expires. |
| 179 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); | 180 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, a); |
| 180 | 181 |
| 181 // Now start the timer. | 182 // Now start the timer. |
| 182 a->Start(); | 183 a->Start(); |
| 183 | 184 |
| 184 bool did_run_b = false; | 185 bool did_run_b = false; |
| 185 RepeatingTimerTester b(&did_run_b, delay); | 186 RepeatingTimerTester b(&did_run_b, delay); |
| 186 b.Start(); | 187 b.Start(); |
| 187 | 188 |
| 188 base::MessageLoop::current()->Run(); | 189 base::MessageLoop::current()->Run(); |
| 189 | 190 |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 base::Bind(&SetCallbackHappened1)); | 529 base::Bind(&SetCallbackHappened1)); |
| 529 timer.Reset(); | 530 timer.Reset(); |
| 530 // Since Reset happened before task ran, the user_task must not be cleared: | 531 // Since Reset happened before task ran, the user_task must not be cleared: |
| 531 ASSERT_FALSE(timer.user_task().is_null()); | 532 ASSERT_FALSE(timer.user_task().is_null()); |
| 532 base::MessageLoop::current()->Run(); | 533 base::MessageLoop::current()->Run(); |
| 533 EXPECT_TRUE(g_callback_happened1); | 534 EXPECT_TRUE(g_callback_happened1); |
| 534 } | 535 } |
| 535 } | 536 } |
| 536 | 537 |
| 537 } // namespace | 538 } // namespace |
| OLD | NEW |