| 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 "chrome/common/worker_thread_ticker.h" | 5 #include "chrome/common/worker_thread_ticker.h" |
| 6 | 6 |
| 7 #include "base/location.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 8 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace { | 14 namespace { |
| 12 | 15 |
| 13 class TestCallback : public WorkerThreadTicker::Callback { | 16 class TestCallback : public WorkerThreadTicker::Callback { |
| 14 public: | 17 public: |
| 15 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {} | 18 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {} |
| 16 | 19 |
| 17 void OnTick() override { | 20 void OnTick() override { |
| 18 counter_++; | 21 counter_++; |
| 19 | 22 |
| 20 // Finish the test faster. | 23 // Finish the test faster. |
| 21 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 24 message_loop_->task_runner()->PostTask(FROM_HERE, |
| 25 base::MessageLoop::QuitClosure()); |
| 22 } | 26 } |
| 23 | 27 |
| 24 int counter() const { return counter_; } | 28 int counter() const { return counter_; } |
| 25 | 29 |
| 26 private: | 30 private: |
| 27 int counter_; | 31 int counter_; |
| 28 base::MessageLoop* message_loop_; | 32 base::MessageLoop* message_loop_; |
| 29 }; | 33 }; |
| 30 | 34 |
| 31 class LongCallback : public WorkerThreadTicker::Callback { | 35 class LongCallback : public WorkerThreadTicker::Callback { |
| 32 public: | 36 public: |
| 33 void OnTick() override { | 37 void OnTick() override { |
| 34 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500)); | 38 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500)); |
| 35 } | 39 } |
| 36 }; | 40 }; |
| 37 | 41 |
| 38 void RunMessageLoopForAWhile() { | 42 void RunMessageLoopForAWhile() { |
| 39 base::MessageLoop::current()->PostDelayedTask( | 43 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 40 FROM_HERE, | 44 FROM_HERE, base::MessageLoop::QuitClosure(), |
| 41 base::MessageLoop::QuitClosure(), | |
| 42 base::TimeDelta::FromMilliseconds(500)); | 45 base::TimeDelta::FromMilliseconds(500)); |
| 43 base::MessageLoop::current()->Run(); | 46 base::MessageLoop::current()->Run(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 } // namespace | 49 } // namespace |
| 47 | 50 |
| 48 TEST(WorkerThreadTickerTest, Basic) { | 51 TEST(WorkerThreadTickerTest, Basic) { |
| 49 base::MessageLoop loop; | 52 base::MessageLoop loop; |
| 50 | 53 |
| 51 TestCallback callback; | 54 TestCallback callback; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 TEST(WorkerThreadTickerTest, LongCallback) { | 103 TEST(WorkerThreadTickerTest, LongCallback) { |
| 101 base::MessageLoop loop; | 104 base::MessageLoop loop; |
| 102 | 105 |
| 103 LongCallback callback; | 106 LongCallback callback; |
| 104 WorkerThreadTicker ticker(50); | 107 WorkerThreadTicker ticker(50); |
| 105 ASSERT_TRUE(ticker.RegisterTickHandler(&callback)); | 108 ASSERT_TRUE(ticker.RegisterTickHandler(&callback)); |
| 106 | 109 |
| 107 ASSERT_TRUE(ticker.Start()); | 110 ASSERT_TRUE(ticker.Start()); |
| 108 RunMessageLoopForAWhile(); | 111 RunMessageLoopForAWhile(); |
| 109 } | 112 } |
| OLD | NEW |