Get foreground tab on Android
[chromium-blink-merge.git] / sync / notifier / sync_system_resources_unittest.cc
blobe8ee38c5c3e9ccd926084df60af60a7680047223
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/sync_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/message_loop.h"
14 #include "google/cacheinvalidation/include/types.h"
15 #include "jingle/notifier/listener/fake_push_client.h"
16 #include "sync/notifier/state_writer.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace syncer {
21 namespace {
23 using ::testing::_;
24 using ::testing::SaveArg;
26 class MockStateWriter : public StateWriter {
27 public:
28 MOCK_METHOD1(WriteState, void(const std::string&));
31 class MockClosure {
32 public:
33 MOCK_CONST_METHOD0(Run, void(void));
34 base::Closure* CreateClosure() {
35 return new base::Closure(
36 base::Bind(&MockClosure::Run, base::Unretained(this)));
40 class MockStorageCallback {
41 public:
42 MOCK_CONST_METHOD1(Run, void(invalidation::Status));
43 base::Callback<void(invalidation::Status)>* CreateCallback() {
44 return new base::Callback<void(invalidation::Status)>(
45 base::Bind(&MockStorageCallback::Run, base::Unretained(this)));
49 class SyncSystemResourcesTest : public testing::Test {
50 protected:
51 SyncSystemResourcesTest()
52 : sync_system_resources_(
53 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()),
54 &mock_state_writer_) {}
56 virtual ~SyncSystemResourcesTest() {}
58 void ScheduleShouldNotRun() {
60 // Owned by ScheduleImmediately.
61 MockClosure mock_closure;
62 base::Closure* should_not_run = mock_closure.CreateClosure();
63 EXPECT_CALL(mock_closure, Run()).Times(0);
64 sync_system_resources_.internal_scheduler()->Schedule(
65 invalidation::Scheduler::NoDelay(), should_not_run);
68 // Owned by ScheduleOnListenerThread.
69 MockClosure mock_closure;
70 base::Closure* should_not_run = mock_closure.CreateClosure();
71 EXPECT_CALL(mock_closure, Run()).Times(0);
72 sync_system_resources_.listener_scheduler()->Schedule(
73 invalidation::Scheduler::NoDelay(), should_not_run);
76 // Owned by ScheduleWithDelay.
77 MockClosure mock_closure;
78 base::Closure* should_not_run = mock_closure.CreateClosure();
79 EXPECT_CALL(mock_closure, Run()).Times(0);
80 sync_system_resources_.internal_scheduler()->Schedule(
81 invalidation::TimeDelta::FromSeconds(0), should_not_run);
85 // Needed by |sync_system_resources_|.
86 base::MessageLoop message_loop_;
87 MockStateWriter mock_state_writer_;
88 SyncSystemResources sync_system_resources_;
90 private:
91 DISALLOW_COPY_AND_ASSIGN(SyncSystemResourcesTest);
94 // Make sure current_time() doesn't crash or leak.
95 TEST_F(SyncSystemResourcesTest, CurrentTime) {
96 invalidation::Time current_time =
97 sync_system_resources_.internal_scheduler()->GetCurrentTime();
98 DVLOG(1) << "current_time returned: " << current_time.ToInternalValue();
101 // Make sure Log() doesn't crash or leak.
102 TEST_F(SyncSystemResourcesTest, Log) {
103 sync_system_resources_.logger()->Log(SyncLogger::INFO_LEVEL,
104 __FILE__, __LINE__, "%s %d",
105 "test string", 5);
108 TEST_F(SyncSystemResourcesTest, ScheduleBeforeStart) {
109 ScheduleShouldNotRun();
110 sync_system_resources_.Start();
113 TEST_F(SyncSystemResourcesTest, ScheduleAfterStop) {
114 sync_system_resources_.Start();
115 sync_system_resources_.Stop();
116 ScheduleShouldNotRun();
119 TEST_F(SyncSystemResourcesTest, ScheduleAndStop) {
120 sync_system_resources_.Start();
121 ScheduleShouldNotRun();
122 sync_system_resources_.Stop();
125 TEST_F(SyncSystemResourcesTest, ScheduleAndDestroy) {
126 sync_system_resources_.Start();
127 ScheduleShouldNotRun();
130 TEST_F(SyncSystemResourcesTest, ScheduleImmediately) {
131 sync_system_resources_.Start();
132 MockClosure mock_closure;
133 EXPECT_CALL(mock_closure, Run());
134 sync_system_resources_.internal_scheduler()->Schedule(
135 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure());
136 message_loop_.RunUntilIdle();
139 TEST_F(SyncSystemResourcesTest, ScheduleOnListenerThread) {
140 sync_system_resources_.Start();
141 MockClosure mock_closure;
142 EXPECT_CALL(mock_closure, Run());
143 sync_system_resources_.listener_scheduler()->Schedule(
144 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure());
145 EXPECT_TRUE(
146 sync_system_resources_.internal_scheduler()->IsRunningOnThread());
147 message_loop_.RunUntilIdle();
150 TEST_F(SyncSystemResourcesTest, ScheduleWithZeroDelay) {
151 sync_system_resources_.Start();
152 MockClosure mock_closure;
153 EXPECT_CALL(mock_closure, Run());
154 sync_system_resources_.internal_scheduler()->Schedule(
155 invalidation::TimeDelta::FromSeconds(0), mock_closure.CreateClosure());
156 message_loop_.RunUntilIdle();
159 // TODO(akalin): Figure out how to test with a non-zero delay.
161 TEST_F(SyncSystemResourcesTest, WriteState) {
162 sync_system_resources_.Start();
163 EXPECT_CALL(mock_state_writer_, WriteState(_));
164 // Owned by WriteState.
165 MockStorageCallback mock_storage_callback;
166 invalidation::Status results(invalidation::Status::PERMANENT_FAILURE,
167 "fake-failure");
168 EXPECT_CALL(mock_storage_callback, Run(_))
169 .WillOnce(SaveArg<0>(&results));
170 sync_system_resources_.storage()->WriteKey(
171 std::string(), "state", mock_storage_callback.CreateCallback());
172 message_loop_.RunUntilIdle();
173 EXPECT_EQ(invalidation::Status(invalidation::Status::SUCCESS, std::string()),
174 results);
177 } // namespace
178 } // namespace syncer