OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 <string> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | |
9 #include "chrome/browser/service/service_process_control.h" | |
10 #include "chrome/common/cloud_print/cloud_print_proxy_info.h" | |
11 #include "chrome/common/pref_names.h" | |
12 #include "chrome/common/service_messages.h" | |
13 #include "chrome/test/base/testing_pref_service.h" | |
14 #include "chrome/test/base/testing_profile.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/test/test_browser_thread.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using content::BrowserThread; | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
you could kill this declaration and just spell out
Scott Byer
2011/11/15 01:42:23
Done.
| |
21 | |
22 using ::testing::Assign; | |
23 using ::testing::AtMost; | |
24 using ::testing::Invoke; | |
25 using ::testing::Mock; | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
needed?
Scott Byer
2011/11/15 01:42:23
Nope. Done.
| |
26 using ::testing::Property; | |
27 using ::testing::ReturnPointee; | |
28 using ::testing::StrictMock; | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
not used.
Scott Byer
2011/11/15 01:42:23
Done.
| |
29 using ::testing::WithArgs; | |
30 using ::testing::_; | |
31 | |
32 class MockServiceProcessControl : public ServiceProcessControl { | |
33 public: | |
34 static std::string EnabledUserId(); | |
35 | |
36 MOCK_CONST_METHOD0(is_connected, bool()); | |
37 | |
38 MOCK_METHOD2(Launch, void(const base::Closure&, const base::Closure&)); | |
39 MOCK_METHOD0(Disconnect, void()); | |
40 | |
41 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message&)); | |
42 MOCK_METHOD1(OnChannelConnected, void(int32)); | |
43 MOCK_METHOD0(OnChannelError, void()); | |
44 | |
45 MOCK_METHOD1(Send, bool(IPC::Message*)); | |
46 | |
47 typedef enum { | |
48 kServiceStateDisabled, | |
49 kServiceStateEnabled, | |
50 kServiceStateNone | |
51 } ServiceState; | |
52 | |
53 void SetConnectSuccessMockExpectations(ServiceState state); | |
54 | |
55 void SetServiceEnabledExpectations(); | |
56 void SetServiceDisabledExpectations(); | |
57 void SetWillBeDisabledExpectations(); | |
58 | |
59 bool SendEnabledInfo(); | |
60 bool SendDisabledInfo(); | |
61 | |
62 private: | |
63 bool connected_; | |
64 cloud_print::CloudPrintProxyInfo info_; | |
65 }; | |
66 | |
67 // static | |
68 std::string MockServiceProcessControl::EnabledUserId() { | |
69 return std::string("dorothy@somewhere.otr"); | |
70 } | |
71 | |
72 void CallTask(const base::Closure& task) { | |
73 if (!task.is_null()) | |
74 task.Run(); | |
75 } | |
76 | |
77 void MockServiceProcessControl::SetConnectSuccessMockExpectations( | |
78 ServiceState service_state) { | |
79 EXPECT_CALL(*this, is_connected()).WillRepeatedly(ReturnPointee(&connected_)); | |
80 | |
81 EXPECT_CALL(*this, Launch(_, _)) | |
82 .WillRepeatedly( | |
83 DoAll(Assign(&connected_, true), WithArgs<0>(Invoke(CallTask)))); | |
84 EXPECT_CALL(*this, Disconnect()).Times(AtMost(1)) | |
85 .WillRepeatedly(Assign(&connected_, false)); | |
86 | |
87 EXPECT_CALL(*this, Send(_)).Times(0); | |
88 | |
89 if (service_state == kServiceStateEnabled) | |
90 SetServiceEnabledExpectations(); | |
91 else if (service_state == kServiceStateDisabled) | |
92 SetServiceDisabledExpectations(); | |
93 } | |
94 | |
95 void MockServiceProcessControl::SetServiceEnabledExpectations() { | |
96 EXPECT_CALL( | |
97 *this, | |
98 Send(Property(&IPC::Message::type, | |
99 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID)))) | |
100 .Times(1).WillOnce( | |
101 WithoutArgs( | |
102 Invoke(this, &MockServiceProcessControl::SendEnabledInfo))); | |
103 } | |
104 | |
105 void MockServiceProcessControl::SetServiceDisabledExpectations() { | |
106 EXPECT_CALL( | |
107 *this, | |
108 Send(Property(&IPC::Message::type, | |
109 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID)))) | |
110 .Times(1).WillOnce( | |
111 WithoutArgs( | |
112 Invoke(this, &MockServiceProcessControl::SendDisabledInfo))); | |
113 } | |
114 | |
115 void MockServiceProcessControl::SetWillBeDisabledExpectations() { | |
116 EXPECT_CALL( | |
117 *this, | |
118 Send(Property(&IPC::Message::type, | |
119 static_cast<int32>(ServiceMsg_DisableCloudPrintProxy::ID)))) | |
120 .Times(1); | |
121 } | |
122 | |
123 bool MockServiceProcessControl::SendEnabledInfo() { | |
124 info_.enabled = true; | |
125 info_.email = EnabledUserId(); | |
126 OnCloudPrintProxyInfo(info_); | |
127 return true; | |
128 } | |
129 | |
130 bool MockServiceProcessControl::SendDisabledInfo() { | |
131 info_.enabled = false; | |
132 info_.email = std::string(); | |
133 OnCloudPrintProxyInfo(info_); | |
134 return true; | |
135 } | |
136 | |
137 class TestCloudPrintProxyService : public CloudPrintProxyService { | |
138 public: | |
139 explicit TestCloudPrintProxyService(Profile* profile) | |
140 : CloudPrintProxyService(profile) { | |
141 process_control_ = new MockServiceProcessControl(); | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
how about just making this a regular class member
Scott Byer
2011/11/15 01:42:23
Done.
| |
142 } | |
143 virtual ~TestCloudPrintProxyService() { | |
144 delete process_control_; | |
145 } | |
146 | |
147 virtual ServiceProcessControl* GetServiceProcessControl() { | |
148 return process_control_; | |
149 } | |
150 MockServiceProcessControl* GetMockServiceProcessControl() { | |
151 return process_control_; | |
152 } | |
153 | |
154 private: | |
155 MockServiceProcessControl* process_control_; | |
156 }; | |
157 | |
158 class CloudPrintProxyPolicyTest : public ::testing::Test { | |
159 public: | |
160 CloudPrintProxyPolicyTest() | |
161 : ui_thread_(BrowserThread::UI, &message_loop_) { | |
162 } | |
163 | |
164 protected: | |
165 MessageLoopForUI message_loop_; | |
166 content::TestBrowserThread ui_thread_; | |
167 TestingProfile profile_; | |
168 }; | |
169 | |
170 TEST_F(CloudPrintProxyPolicyTest, VerifyExpectations) { | |
171 MockServiceProcessControl mock_control; | |
172 mock_control.SetConnectSuccessMockExpectations( | |
173 MockServiceProcessControl::kServiceStateNone); | |
174 | |
175 EXPECT_FALSE(mock_control.is_connected()); | |
176 mock_control.Launch(base::Closure(), base::Closure()); | |
177 EXPECT_TRUE(mock_control.is_connected()); | |
178 mock_control.Launch(base::Closure(), base::Closure()); | |
179 EXPECT_TRUE(mock_control.is_connected()); | |
180 mock_control.Disconnect(); | |
181 EXPECT_FALSE(mock_control.is_connected()); | |
182 } | |
183 | |
184 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabled) { | |
185 scoped_ptr<TestCloudPrintProxyService> service( | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
Any reason to wrap this in a scoped_ptr? Regular n
Scott Byer
2011/11/15 01:42:23
Done.
| |
186 new TestCloudPrintProxyService(&profile_)); | |
187 | |
188 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
189 MockServiceProcessControl::kServiceStateDisabled); | |
190 | |
191 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
192 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
193 Value::CreateStringValue( | |
194 MockServiceProcessControl::EnabledUserId())); | |
195 | |
196 service->Initialize(); | |
197 | |
198 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
199 } | |
200 | |
201 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabled) { | |
202 scoped_ptr<TestCloudPrintProxyService> service( | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
ditto (and also below).
Scott Byer
2011/11/15 01:42:23
Done.
| |
203 new TestCloudPrintProxyService(&profile_)); | |
204 | |
205 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
206 MockServiceProcessControl::kServiceStateEnabled); | |
207 | |
208 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
209 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
210 Value::CreateStringValue(std::string())); | |
211 | |
212 service->Initialize(); | |
213 service->RefreshStatusFromService(); | |
214 | |
215 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(), | |
216 prefs->GetString(prefs::kCloudPrintEmail)); | |
217 } | |
218 | |
219 TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyDisabled) { | |
220 scoped_ptr<TestCloudPrintProxyService> service( | |
221 new TestCloudPrintProxyService(&profile_)); | |
222 | |
223 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
224 MockServiceProcessControl::kServiceStateDisabled); | |
225 | |
226 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
227 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
228 Value::CreateStringValue(std::string())); | |
229 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
230 Value::CreateBooleanValue(false)); | |
231 | |
232 service->Initialize(); | |
233 | |
234 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
235 } | |
236 | |
237 TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyEnabled) { | |
238 scoped_ptr<TestCloudPrintProxyService> service( | |
239 new TestCloudPrintProxyService(&profile_)); | |
240 | |
241 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
242 MockServiceProcessControl::kServiceStateEnabled); | |
243 service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); | |
244 | |
245 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
246 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
247 Value::CreateStringValue(std::string())); | |
248 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
249 Value::CreateBooleanValue(false)); | |
250 | |
251 service->Initialize(); | |
252 | |
253 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
254 } | |
255 | |
256 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenSetPolicy) { | |
257 scoped_ptr<TestCloudPrintProxyService> service( | |
258 new TestCloudPrintProxyService(&profile_)); | |
259 | |
260 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
261 MockServiceProcessControl::kServiceStateDisabled); | |
262 | |
263 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
264 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
265 Value::CreateStringValue( | |
266 MockServiceProcessControl::EnabledUserId())); | |
267 | |
268 service->Initialize(); | |
269 | |
270 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
271 | |
272 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
273 Value::CreateBooleanValue(false)); | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
validate here that it's still disabled?
Scott Byer
2011/11/15 01:42:23
Done.
| |
274 } | |
275 | |
276 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabledThenSetPolicy) { | |
277 scoped_ptr<TestCloudPrintProxyService> service( | |
278 new TestCloudPrintProxyService(&profile_)); | |
279 | |
280 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
281 MockServiceProcessControl::kServiceStateEnabled); | |
282 | |
283 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
284 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
285 Value::CreateStringValue(std::string())); | |
286 | |
287 service->Initialize(); | |
288 service->RefreshStatusFromService(); | |
289 | |
290 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(), | |
291 prefs->GetString(prefs::kCloudPrintEmail)); | |
292 | |
293 service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); | |
294 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
295 Value::CreateBooleanValue(false)); | |
296 | |
297 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
298 } | |
299 | |
300 TEST_F(CloudPrintProxyPolicyTest, | |
301 StartWithPolicySetProxyDisabledThenClearPolicy) { | |
302 scoped_ptr<TestCloudPrintProxyService> service( | |
303 new TestCloudPrintProxyService(&profile_)); | |
304 | |
305 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
306 MockServiceProcessControl::kServiceStateDisabled); | |
307 | |
308 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
309 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
310 Value::CreateStringValue(std::string())); | |
311 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
312 Value::CreateBooleanValue(false)); | |
313 | |
314 service->Initialize(); | |
315 | |
316 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
317 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled); | |
318 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
319 } | |
320 | |
321 TEST_F(CloudPrintProxyPolicyTest, | |
322 StartWithPolicySetProxyEnabledThenClearPolicy) { | |
323 scoped_ptr<TestCloudPrintProxyService> service( | |
324 new TestCloudPrintProxyService(&profile_)); | |
325 | |
326 service->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations( | |
327 MockServiceProcessControl::kServiceStateEnabled); | |
328 service->GetMockServiceProcessControl()->SetWillBeDisabledExpectations(); | |
329 | |
330 TestingPrefService* prefs = profile_.GetTestingPrefService(); | |
331 prefs->SetUserPref(prefs::kCloudPrintEmail, | |
332 Value::CreateStringValue(std::string())); | |
333 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | |
334 Value::CreateBooleanValue(false)); | |
335 | |
336 service->Initialize(); | |
337 | |
338 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
339 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled); | |
340 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail)); | |
Mattias Nissler (ping if slow)
2011/11/14 08:33:06
I guess an additional test that first sets the pol
Scott Byer
2011/11/15 01:42:23
Thank you! At some point in the middle of the day
| |
341 } | |
OLD | NEW |