roll skia to 4057
[chromium-blink-merge.git] / sync / notifier / chrome_system_resources_unittest.cc
bloba61497f00fa55f03c96d3b8c0056d466461586d3
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 "sync/notifier/chrome_system_resources.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/message_loop.h"
14 #include "google/cacheinvalidation/include/types.h"
15 #include "sync/notifier/state_writer.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace sync_notifier {
20 namespace {
22 using ::testing::_;
23 using ::testing::SaveArg;
25 class MockStateWriter : public StateWriter {
26 public:
27 MOCK_METHOD1(WriteState, void(const std::string&));
30 class MockClosure {
31 public:
32 MOCK_CONST_METHOD0(Run, void(void));
33 base::Closure* CreateClosure() {
34 return new base::Closure(
35 base::Bind(&MockClosure::Run, base::Unretained(this)));
39 class MockStorageCallback {
40 public:
41 MOCK_CONST_METHOD1(Run, void(invalidation::Status));
42 base::Callback<void(invalidation::Status)>* CreateCallback() {
43 return new base::Callback<void(invalidation::Status)>(
44 base::Bind(&MockStorageCallback::Run, base::Unretained(this)));
48 class ChromeSystemResourcesTest : public testing::Test {
49 protected:
50 ChromeSystemResourcesTest()
51 : chrome_system_resources_(&mock_state_writer_) {}
53 virtual ~ChromeSystemResourcesTest() {}
55 void ScheduleShouldNotRun() {
57 // Owned by ScheduleImmediately.
58 MockClosure mock_closure;
59 base::Closure* should_not_run = mock_closure.CreateClosure();
60 EXPECT_CALL(mock_closure, Run()).Times(0);
61 chrome_system_resources_.internal_scheduler()->Schedule(
62 invalidation::Scheduler::NoDelay(), should_not_run);
65 // Owned by ScheduleOnListenerThread.
66 MockClosure mock_closure;
67 base::Closure* should_not_run = mock_closure.CreateClosure();
68 EXPECT_CALL(mock_closure, Run()).Times(0);
69 chrome_system_resources_.listener_scheduler()->Schedule(
70 invalidation::Scheduler::NoDelay(), should_not_run);
73 // Owned by ScheduleWithDelay.
74 MockClosure mock_closure;
75 base::Closure* should_not_run = mock_closure.CreateClosure();
76 EXPECT_CALL(mock_closure, Run()).Times(0);
77 chrome_system_resources_.internal_scheduler()->Schedule(
78 invalidation::TimeDelta::FromSeconds(0), should_not_run);
82 // Needed by |chrome_system_resources_|.
83 MessageLoop message_loop_;
84 MockStateWriter mock_state_writer_;
85 ChromeSystemResources chrome_system_resources_;
87 private:
88 DISALLOW_COPY_AND_ASSIGN(ChromeSystemResourcesTest);
91 // Make sure current_time() doesn't crash or leak.
92 TEST_F(ChromeSystemResourcesTest, CurrentTime) {
93 invalidation::Time current_time =
94 chrome_system_resources_.internal_scheduler()->GetCurrentTime();
95 DVLOG(1) << "current_time returned: " << current_time.ToInternalValue();
98 // Make sure Log() doesn't crash or leak.
99 TEST_F(ChromeSystemResourcesTest, Log) {
100 chrome_system_resources_.logger()->Log(ChromeLogger::INFO_LEVEL,
101 __FILE__, __LINE__, "%s %d",
102 "test string", 5);
105 TEST_F(ChromeSystemResourcesTest, ScheduleBeforeStart) {
106 ScheduleShouldNotRun();
107 chrome_system_resources_.Start();
110 TEST_F(ChromeSystemResourcesTest, ScheduleAfterStop) {
111 chrome_system_resources_.Start();
112 chrome_system_resources_.Stop();
113 ScheduleShouldNotRun();
116 TEST_F(ChromeSystemResourcesTest, ScheduleAndStop) {
117 chrome_system_resources_.Start();
118 ScheduleShouldNotRun();
119 chrome_system_resources_.Stop();
122 TEST_F(ChromeSystemResourcesTest, ScheduleAndDestroy) {
123 chrome_system_resources_.Start();
124 ScheduleShouldNotRun();
127 TEST_F(ChromeSystemResourcesTest, ScheduleImmediately) {
128 chrome_system_resources_.Start();
129 MockClosure mock_closure;
130 EXPECT_CALL(mock_closure, Run());
131 chrome_system_resources_.internal_scheduler()->Schedule(
132 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure());
133 message_loop_.RunAllPending();
136 TEST_F(ChromeSystemResourcesTest, ScheduleOnListenerThread) {
137 chrome_system_resources_.Start();
138 MockClosure mock_closure;
139 EXPECT_CALL(mock_closure, Run());
140 chrome_system_resources_.listener_scheduler()->Schedule(
141 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure());
142 EXPECT_TRUE(
143 chrome_system_resources_.internal_scheduler()->IsRunningOnThread());
144 message_loop_.RunAllPending();
147 TEST_F(ChromeSystemResourcesTest, ScheduleWithZeroDelay) {
148 chrome_system_resources_.Start();
149 MockClosure mock_closure;
150 EXPECT_CALL(mock_closure, Run());
151 chrome_system_resources_.internal_scheduler()->Schedule(
152 invalidation::TimeDelta::FromSeconds(0), mock_closure.CreateClosure());
153 message_loop_.RunAllPending();
156 // TODO(akalin): Figure out how to test with a non-zero delay.
158 TEST_F(ChromeSystemResourcesTest, WriteState) {
159 chrome_system_resources_.Start();
160 EXPECT_CALL(mock_state_writer_, WriteState(_));
161 // Owned by WriteState.
162 MockStorageCallback mock_storage_callback;
163 invalidation::Status results(invalidation::Status::PERMANENT_FAILURE,
164 "fake-failure");
165 EXPECT_CALL(mock_storage_callback, Run(_))
166 .WillOnce(SaveArg<0>(&results));
167 chrome_system_resources_.storage()->WriteKey(
168 "", "state", mock_storage_callback.CreateCallback());
169 message_loop_.RunAllPending();
170 EXPECT_EQ(invalidation::Status(invalidation::Status::SUCCESS, ""), results);
173 } // namespace
174 } // namespace notifier