Home | History | Annotate | Download | only in notifier
      1 // Copyright (c) 2010 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/browser/sync/notifier/chrome_system_resources.h"
      6 
      7 #include <string>
      8 
      9 #include "base/callback.h"
     10 #include "base/message_loop.h"
     11 #include "base/tuple.h"
     12 #include "google/cacheinvalidation/invalidation-client.h"
     13 #include "chrome/browser/sync/notifier/state_writer.h"
     14 #include "testing/gmock/include/gmock/gmock.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace sync_notifier {
     18 namespace {
     19 
     20 using ::testing::_;
     21 using ::testing::SaveArg;
     22 
     23 class MockStateWriter : public StateWriter {
     24  public:
     25   MOCK_METHOD1(WriteState, void(const std::string&));
     26 };
     27 
     28 class MockClosure : public Callback0::Type {
     29  public:
     30   MOCK_METHOD1(RunWithParams, void(const Tuple0&));
     31 };
     32 
     33 class MockStorageCallback : public Callback1<bool>::Type {
     34  public:
     35   MOCK_METHOD1(RunWithParams, void(const Tuple1<bool>&));
     36 };
     37 
     38 class ChromeSystemResourcesTest : public testing::Test {
     39  protected:
     40   ChromeSystemResourcesTest()
     41       : chrome_system_resources_(&mock_state_writer_) {}
     42 
     43   virtual ~ChromeSystemResourcesTest() {}
     44 
     45   void ScheduleShouldNotRun() {
     46     {
     47       // Owned by ScheduleImmediately.
     48       MockClosure* should_not_run = new MockClosure();
     49       EXPECT_CALL(*should_not_run, RunWithParams(_)).Times(0);
     50       chrome_system_resources_.ScheduleImmediately(should_not_run);
     51     }
     52     {
     53       // Owned by ScheduleOnListenerThread.
     54       MockClosure* should_not_run = new MockClosure();
     55       EXPECT_CALL(*should_not_run, RunWithParams(_)).Times(0);
     56       chrome_system_resources_.ScheduleOnListenerThread(should_not_run);
     57     }
     58     {
     59       // Owned by ScheduleWithDelay.
     60       MockClosure* should_not_run = new MockClosure();
     61       EXPECT_CALL(*should_not_run, RunWithParams(_)).Times(0);
     62       chrome_system_resources_.ScheduleWithDelay(
     63           invalidation::TimeDelta::FromSeconds(0), should_not_run);
     64     }
     65   }
     66 
     67   // Needed by |chrome_system_resources_|.
     68   MessageLoop message_loop_;
     69   MockStateWriter mock_state_writer_;
     70   ChromeSystemResources chrome_system_resources_;
     71 
     72  private:
     73   DISALLOW_COPY_AND_ASSIGN(ChromeSystemResourcesTest);
     74 };
     75 
     76 // Make sure current_time() doesn't crash or leak.
     77 TEST_F(ChromeSystemResourcesTest, CurrentTime) {
     78   invalidation::Time current_time =
     79       chrome_system_resources_.current_time();
     80   VLOG(1) << "current_time returned: " << current_time.ToInternalValue();
     81 }
     82 
     83 // Make sure Log() doesn't crash or leak.
     84 TEST_F(ChromeSystemResourcesTest, Log) {
     85   chrome_system_resources_.Log(ChromeSystemResources::INFO_LEVEL,
     86                                __FILE__, __LINE__, "%s %d",
     87                                "test string", 5);
     88 }
     89 
     90 TEST_F(ChromeSystemResourcesTest, ScheduleBeforeStart) {
     91   ScheduleShouldNotRun();
     92   chrome_system_resources_.StartScheduler();
     93 }
     94 
     95 TEST_F(ChromeSystemResourcesTest, ScheduleAfterStop) {
     96   chrome_system_resources_.StartScheduler();
     97   chrome_system_resources_.StopScheduler();
     98   ScheduleShouldNotRun();
     99 }
    100 
    101 TEST_F(ChromeSystemResourcesTest, ScheduleAndStop) {
    102   chrome_system_resources_.StartScheduler();
    103   ScheduleShouldNotRun();
    104   chrome_system_resources_.StopScheduler();
    105 }
    106 
    107 TEST_F(ChromeSystemResourcesTest, ScheduleAndDestroy) {
    108   chrome_system_resources_.StartScheduler();
    109   ScheduleShouldNotRun();
    110 }
    111 
    112 TEST_F(ChromeSystemResourcesTest, ScheduleImmediately) {
    113   chrome_system_resources_.StartScheduler();
    114   // Owned by ScheduleImmediately.
    115   MockClosure* mock_closure = new MockClosure();
    116   EXPECT_CALL(*mock_closure, RunWithParams(_));
    117   chrome_system_resources_.ScheduleImmediately(mock_closure);
    118   message_loop_.RunAllPending();
    119 }
    120 
    121 TEST_F(ChromeSystemResourcesTest, ScheduleOnListenerThread) {
    122   chrome_system_resources_.StartScheduler();
    123   // Owned by ScheduleOnListenerThread.
    124   MockClosure* mock_closure = new MockClosure();
    125   EXPECT_CALL(*mock_closure, RunWithParams(_));
    126   chrome_system_resources_.ScheduleOnListenerThread(mock_closure);
    127   EXPECT_FALSE(chrome_system_resources_.IsRunningOnInternalThread());
    128   message_loop_.RunAllPending();
    129 }
    130 
    131 TEST_F(ChromeSystemResourcesTest, ScheduleWithZeroDelay) {
    132   chrome_system_resources_.StartScheduler();
    133   // Owned by ScheduleWithDelay.
    134   MockClosure* mock_closure = new MockClosure();
    135   EXPECT_CALL(*mock_closure, RunWithParams(_));
    136   chrome_system_resources_.ScheduleWithDelay(
    137       invalidation::TimeDelta::FromSeconds(0), mock_closure);
    138   message_loop_.RunAllPending();
    139 }
    140 
    141 // TODO(akalin): Figure out how to test with a non-zero delay.
    142 
    143 TEST_F(ChromeSystemResourcesTest, WriteState) {
    144   chrome_system_resources_.StartScheduler();
    145   EXPECT_CALL(mock_state_writer_, WriteState(_));
    146   // Owned by WriteState.
    147   MockStorageCallback* mock_storage_callback = new MockStorageCallback();
    148   Tuple1<bool> results(false);
    149   EXPECT_CALL(*mock_storage_callback, RunWithParams(_))
    150       .WillOnce(SaveArg<0>(&results));
    151   chrome_system_resources_.WriteState("state", mock_storage_callback);
    152   message_loop_.RunAllPending();
    153   EXPECT_TRUE(results.a);
    154 }
    155 
    156 }  // namespace
    157 }  // namespace notifier
    158