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
3 // found in the LICENSE file.
4
5 #include "chrome/common/worker_thread_ticker.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 class TestCallback : public WorkerThreadTicker::Callback {
14 public:
TestCallback()15 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {}
16
OnTick()17 virtual void OnTick() OVERRIDE {
18 counter_++;
19
20 // Finish the test faster.
21 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
22 }
23
counter() const24 int counter() const { return counter_; }
25
26 private:
27 int counter_;
28 base::MessageLoop* message_loop_;
29 };
30
31 class LongCallback : public WorkerThreadTicker::Callback {
32 public:
OnTick()33 virtual void OnTick() OVERRIDE {
34 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500));
35 }
36 };
37
RunMessageLoopForAWhile()38 void RunMessageLoopForAWhile() {
39 base::MessageLoop::current()->PostDelayedTask(
40 FROM_HERE,
41 base::MessageLoop::QuitClosure(),
42 base::TimeDelta::FromMilliseconds(500));
43 base::MessageLoop::current()->Run();
44 }
45
46 } // namespace
47
TEST(WorkerThreadTickerTest,Basic)48 TEST(WorkerThreadTickerTest, Basic) {
49 base::MessageLoop loop;
50
51 TestCallback callback;
52 WorkerThreadTicker ticker(50);
53 EXPECT_FALSE(ticker.IsRunning());
54 EXPECT_TRUE(ticker.RegisterTickHandler(&callback));
55 EXPECT_TRUE(ticker.UnregisterTickHandler(&callback));
56 EXPECT_TRUE(ticker.Start());
57 EXPECT_FALSE(ticker.RegisterTickHandler(&callback));
58 EXPECT_FALSE(ticker.UnregisterTickHandler(&callback));
59 EXPECT_TRUE(ticker.IsRunning());
60 EXPECT_FALSE(ticker.Start()); // Can't start when it is running.
61 EXPECT_TRUE(ticker.Stop());
62 EXPECT_FALSE(ticker.IsRunning());
63 EXPECT_FALSE(ticker.Stop()); // Can't stop when it isn't running.
64 }
65
TEST(WorkerThreadTickerTest,Callback)66 TEST(WorkerThreadTickerTest, Callback) {
67 base::MessageLoop loop;
68
69 TestCallback callback;
70 WorkerThreadTicker ticker(50);
71 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
72
73 ASSERT_TRUE(ticker.Start());
74 RunMessageLoopForAWhile();
75 EXPECT_TRUE(callback.counter() > 0);
76
77 ASSERT_TRUE(ticker.Stop());
78 const int counter_value = callback.counter();
79 RunMessageLoopForAWhile();
80 EXPECT_EQ(counter_value, callback.counter());
81 }
82
TEST(WorkerThreadTickerTest,OutOfScope)83 TEST(WorkerThreadTickerTest, OutOfScope) {
84 base::MessageLoop loop;
85
86 TestCallback callback;
87 {
88 WorkerThreadTicker ticker(50);
89 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
90
91 ASSERT_TRUE(ticker.Start());
92 RunMessageLoopForAWhile();
93 EXPECT_TRUE(callback.counter() > 0);
94 }
95 const int counter_value = callback.counter();
96 RunMessageLoopForAWhile();
97 EXPECT_EQ(counter_value, callback.counter());
98 }
99
TEST(WorkerThreadTickerTest,LongCallback)100 TEST(WorkerThreadTickerTest, LongCallback) {
101 base::MessageLoop loop;
102
103 LongCallback callback;
104 WorkerThreadTicker ticker(50);
105 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
106
107 ASSERT_TRUE(ticker.Start());
108 RunMessageLoopForAWhile();
109 }
110