OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sync/notifier/chrome_system_resources.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/callback.h" | |
12 #include "base/message_loop.h" | |
13 | |
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" | |
19 | |
20 namespace syncer { | |
21 namespace { | |
22 | |
23 using ::testing::_; | |
24 using ::testing::SaveArg; | |
25 | |
26 class MockStateWriter : public StateWriter { | |
27 public: | |
28 MOCK_METHOD1(WriteState, void(const std::string&)); | |
29 }; | |
30 | |
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))); | |
37 } | |
38 }; | |
39 | |
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))); | |
46 } | |
47 }; | |
48 | |
49 class ChromeSystemResourcesTest : public testing::Test { | |
50 protected: | |
51 ChromeSystemResourcesTest() | |
52 : chrome_system_resources_( | |
53 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | |
54 &mock_state_writer_) {} | |
55 | |
56 virtual ~ChromeSystemResourcesTest() {} | |
57 | |
58 void ScheduleShouldNotRun() { | |
59 { | |
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 chrome_system_resources_.internal_scheduler()->Schedule( | |
65 invalidation::Scheduler::NoDelay(), should_not_run); | |
66 } | |
67 { | |
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 chrome_system_resources_.listener_scheduler()->Schedule( | |
73 invalidation::Scheduler::NoDelay(), should_not_run); | |
74 } | |
75 { | |
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 chrome_system_resources_.internal_scheduler()->Schedule( | |
81 invalidation::TimeDelta::FromSeconds(0), should_not_run); | |
82 } | |
83 } | |
84 | |
85 // Needed by |chrome_system_resources_|. | |
86 MessageLoop message_loop_; | |
87 MockStateWriter mock_state_writer_; | |
88 ChromeSystemResources chrome_system_resources_; | |
89 | |
90 private: | |
91 DISALLOW_COPY_AND_ASSIGN(ChromeSystemResourcesTest); | |
92 }; | |
93 | |
94 // Make sure current_time() doesn't crash or leak. | |
95 TEST_F(ChromeSystemResourcesTest, CurrentTime) { | |
96 invalidation::Time current_time = | |
97 chrome_system_resources_.internal_scheduler()->GetCurrentTime(); | |
98 DVLOG(1) << "current_time returned: " << current_time.ToInternalValue(); | |
99 } | |
100 | |
101 // Make sure Log() doesn't crash or leak. | |
102 TEST_F(ChromeSystemResourcesTest, Log) { | |
103 chrome_system_resources_.logger()->Log(ChromeLogger::INFO_LEVEL, | |
104 __FILE__, __LINE__, "%s %d", | |
105 "test string", 5); | |
106 } | |
107 | |
108 TEST_F(ChromeSystemResourcesTest, ScheduleBeforeStart) { | |
109 ScheduleShouldNotRun(); | |
110 chrome_system_resources_.Start(); | |
111 } | |
112 | |
113 TEST_F(ChromeSystemResourcesTest, ScheduleAfterStop) { | |
114 chrome_system_resources_.Start(); | |
115 chrome_system_resources_.Stop(); | |
116 ScheduleShouldNotRun(); | |
117 } | |
118 | |
119 TEST_F(ChromeSystemResourcesTest, ScheduleAndStop) { | |
120 chrome_system_resources_.Start(); | |
121 ScheduleShouldNotRun(); | |
122 chrome_system_resources_.Stop(); | |
123 } | |
124 | |
125 TEST_F(ChromeSystemResourcesTest, ScheduleAndDestroy) { | |
126 chrome_system_resources_.Start(); | |
127 ScheduleShouldNotRun(); | |
128 } | |
129 | |
130 TEST_F(ChromeSystemResourcesTest, ScheduleImmediately) { | |
131 chrome_system_resources_.Start(); | |
132 MockClosure mock_closure; | |
133 EXPECT_CALL(mock_closure, Run()); | |
134 chrome_system_resources_.internal_scheduler()->Schedule( | |
135 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure()); | |
136 message_loop_.RunAllPending(); | |
137 } | |
138 | |
139 TEST_F(ChromeSystemResourcesTest, ScheduleOnListenerThread) { | |
140 chrome_system_resources_.Start(); | |
141 MockClosure mock_closure; | |
142 EXPECT_CALL(mock_closure, Run()); | |
143 chrome_system_resources_.listener_scheduler()->Schedule( | |
144 invalidation::Scheduler::NoDelay(), mock_closure.CreateClosure()); | |
145 EXPECT_TRUE( | |
146 chrome_system_resources_.internal_scheduler()->IsRunningOnThread()); | |
147 message_loop_.RunAllPending(); | |
148 } | |
149 | |
150 TEST_F(ChromeSystemResourcesTest, ScheduleWithZeroDelay) { | |
151 chrome_system_resources_.Start(); | |
152 MockClosure mock_closure; | |
153 EXPECT_CALL(mock_closure, Run()); | |
154 chrome_system_resources_.internal_scheduler()->Schedule( | |
155 invalidation::TimeDelta::FromSeconds(0), mock_closure.CreateClosure()); | |
156 message_loop_.RunAllPending(); | |
157 } | |
158 | |
159 // TODO(akalin): Figure out how to test with a non-zero delay. | |
160 | |
161 TEST_F(ChromeSystemResourcesTest, WriteState) { | |
162 chrome_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 chrome_system_resources_.storage()->WriteKey( | |
171 "", "state", mock_storage_callback.CreateCallback()); | |
172 message_loop_.RunAllPending(); | |
173 EXPECT_EQ(invalidation::Status(invalidation::Status::SUCCESS, ""), results); | |
174 } | |
175 | |
176 } // namespace | |
177 } // namespace syncer | |
OLD | NEW |