Fix translation extraction failure.
[chromium-blink-merge.git] / chrome / common / worker_thread_ticker_unittest.cc
blobdbb2e01180299d50aaacd7953c7fd2fa2daafc88
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/location.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"
11 #include "base/threading/platform_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
16 class TestCallback : public WorkerThreadTicker::Callback {
17 public:
18 TestCallback() : counter_(0), message_loop_(base::MessageLoop::current()) {}
20 void OnTick() override {
21 counter_++;
23 // Finish the test faster.
24 message_loop_->task_runner()->PostTask(FROM_HERE,
25 base::MessageLoop::QuitClosure());
28 int counter() const { return counter_; }
30 private:
31 int counter_;
32 base::MessageLoop* message_loop_;
35 class LongCallback : public WorkerThreadTicker::Callback {
36 public:
37 void OnTick() override {
38 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1500));
42 void RunMessageLoopForAWhile() {
43 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
44 FROM_HERE, base::MessageLoop::QuitClosure(),
45 base::TimeDelta::FromMilliseconds(500));
46 base::MessageLoop::current()->Run();
49 } // namespace
51 TEST(WorkerThreadTickerTest, Basic) {
52 base::MessageLoop loop;
54 TestCallback callback;
55 WorkerThreadTicker ticker(50);
56 EXPECT_FALSE(ticker.IsRunning());
57 EXPECT_TRUE(ticker.RegisterTickHandler(&callback));
58 EXPECT_TRUE(ticker.UnregisterTickHandler(&callback));
59 EXPECT_TRUE(ticker.Start());
60 EXPECT_FALSE(ticker.RegisterTickHandler(&callback));
61 EXPECT_FALSE(ticker.UnregisterTickHandler(&callback));
62 EXPECT_TRUE(ticker.IsRunning());
63 EXPECT_FALSE(ticker.Start()); // Can't start when it is running.
64 EXPECT_TRUE(ticker.Stop());
65 EXPECT_FALSE(ticker.IsRunning());
66 EXPECT_FALSE(ticker.Stop()); // Can't stop when it isn't running.
69 TEST(WorkerThreadTickerTest, Callback) {
70 base::MessageLoop loop;
72 TestCallback callback;
73 WorkerThreadTicker ticker(50);
74 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
76 ASSERT_TRUE(ticker.Start());
77 RunMessageLoopForAWhile();
78 EXPECT_TRUE(callback.counter() > 0);
80 ASSERT_TRUE(ticker.Stop());
81 const int counter_value = callback.counter();
82 RunMessageLoopForAWhile();
83 EXPECT_EQ(counter_value, callback.counter());
86 TEST(WorkerThreadTickerTest, OutOfScope) {
87 base::MessageLoop loop;
89 TestCallback callback;
91 WorkerThreadTicker ticker(50);
92 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
94 ASSERT_TRUE(ticker.Start());
95 RunMessageLoopForAWhile();
96 EXPECT_TRUE(callback.counter() > 0);
98 const int counter_value = callback.counter();
99 RunMessageLoopForAWhile();
100 EXPECT_EQ(counter_value, callback.counter());
103 TEST(WorkerThreadTickerTest, LongCallback) {
104 base::MessageLoop loop;
106 LongCallback callback;
107 WorkerThreadTicker ticker(50);
108 ASSERT_TRUE(ticker.RegisterTickHandler(&callback));
110 ASSERT_TRUE(ticker.Start());
111 RunMessageLoopForAWhile();