Transfer Password sync to use new sync API.
[chromium-blink-merge.git] / chrome / common / worker_thread_ticker_unittest.cc
bloba0aa5ada64da639ff371afa357e0fe5f67eaa2a3
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.
5 #include "chrome/common/worker_thread_ticker.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 class TestCallback : public WorkerThreadTicker::Callback {
14 public:
15 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {}
17 virtual void OnTick() OVERRIDE {
18 counter_++;
20 // Finish the test faster.
21 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
24 int counter() const { return counter_; }
26 private:
27 int counter_;
28 base::MessageLoop* message_loop_;
31 class LongCallback : public WorkerThreadTicker::Callback {
32 public:
33 virtual void OnTick() OVERRIDE {
34 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500));
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();
46 } // namespace
48 TEST(WorkerThreadTickerTest, Basic) {
49 base::MessageLoop loop;
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.
66 TEST(WorkerThreadTickerTest, Callback) {
67 base::MessageLoop loop;
69 TestCallback callback;
70 WorkerThreadTicker ticker(50);
71 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
73 ASSERT_TRUE(ticker.Start());
74 RunMessageLoopForAWhile();
75 EXPECT_TRUE(callback.counter() > 0);
77 ASSERT_TRUE(ticker.Stop());
78 const int counter_value = callback.counter();
79 RunMessageLoopForAWhile();
80 EXPECT_EQ(counter_value, callback.counter());
83 TEST(WorkerThreadTickerTest, OutOfScope) {
84 base::MessageLoop loop;
86 TestCallback callback;
88 WorkerThreadTicker ticker(50);
89 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
91 ASSERT_TRUE(ticker.Start());
92 RunMessageLoopForAWhile();
93 EXPECT_TRUE(callback.counter() > 0);
95 const int counter_value = callback.counter();
96 RunMessageLoopForAWhile();
97 EXPECT_EQ(counter_value, callback.counter());
100 TEST(WorkerThreadTickerTest, LongCallback) {
101 base::MessageLoop loop;
103 LongCallback callback;
104 WorkerThreadTicker ticker(50);
105 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
107 ASSERT_TRUE(ticker.Start());
108 RunMessageLoopForAWhile();