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