| 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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.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 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 class TestCallback : public WorkerThreadTicker::Callback { | 13 class TestCallback : public WorkerThreadTicker::Callback { |
| 14 public: | 14 public: |
| 15 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {} | 15 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {} |
| 16 | 16 |
| 17 virtual void OnTick() OVERRIDE { | 17 virtual void OnTick() override { |
| 18 counter_++; | 18 counter_++; |
| 19 | 19 |
| 20 // Finish the test faster. | 20 // Finish the test faster. |
| 21 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 21 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 22 } | 22 } |
| 23 | 23 |
| 24 int counter() const { return counter_; } | 24 int counter() const { return counter_; } |
| 25 | 25 |
| 26 private: | 26 private: |
| 27 int counter_; | 27 int counter_; |
| 28 base::MessageLoop* message_loop_; | 28 base::MessageLoop* message_loop_; |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 class LongCallback : public WorkerThreadTicker::Callback { | 31 class LongCallback : public WorkerThreadTicker::Callback { |
| 32 public: | 32 public: |
| 33 virtual void OnTick() OVERRIDE { | 33 virtual void OnTick() override { |
| 34 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500)); | 34 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500)); |
| 35 } | 35 } |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 void RunMessageLoopForAWhile() { | 38 void RunMessageLoopForAWhile() { |
| 39 base::MessageLoop::current()->PostDelayedTask( | 39 base::MessageLoop::current()->PostDelayedTask( |
| 40 FROM_HERE, | 40 FROM_HERE, |
| 41 base::MessageLoop::QuitClosure(), | 41 base::MessageLoop::QuitClosure(), |
| 42 base::TimeDelta::FromMilliseconds(500)); | 42 base::TimeDelta::FromMilliseconds(500)); |
| 43 base::MessageLoop::current()->Run(); | 43 base::MessageLoop::current()->Run(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 TEST(WorkerThreadTickerTest, LongCallback) { | 100 TEST(WorkerThreadTickerTest, LongCallback) { |
| 101 base::MessageLoop loop; | 101 base::MessageLoop loop; |
| 102 | 102 |
| 103 LongCallback callback; | 103 LongCallback callback; |
| 104 WorkerThreadTicker ticker(50); | 104 WorkerThreadTicker ticker(50); |
| 105 ASSERT_TRUE(ticker.RegisterTickHandler(&callback)); | 105 ASSERT_TRUE(ticker.RegisterTickHandler(&callback)); |
| 106 | 106 |
| 107 ASSERT_TRUE(ticker.Start()); | 107 ASSERT_TRUE(ticker.Start()); |
| 108 RunMessageLoopForAWhile(); | 108 RunMessageLoopForAWhile(); |
| 109 } | 109 } |
| OLD | NEW |