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