Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: chrome/browser/printing/cloud_print/cloud_print_proxy_service_unittest.cc

Issue 8438020: Cloud print connector policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments, initialization fix for Windows. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ::testing::Assign;
21 using ::testing::AtMost;
22 using ::testing::Invoke;
23 using ::testing::Property;
24 using ::testing::ReturnPointee;
25 using ::testing::WithArgs;
26 using ::testing::_;
27
28 class MockServiceProcessControl : public ServiceProcessControl {
29 public:
30 static std::string EnabledUserId();
31
32 MockServiceProcessControl() : connected_(false) { }
33
34 MOCK_CONST_METHOD0(is_connected, bool());
35
36 MOCK_METHOD2(Launch, void(const base::Closure&, const base::Closure&));
37 MOCK_METHOD0(Disconnect, void());
38
39 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message&));
40 MOCK_METHOD1(OnChannelConnected, void(int32 peer_pid));
41 MOCK_METHOD0(OnChannelError, void());
42
43 MOCK_METHOD1(Send, bool(IPC::Message*));
44
45 typedef enum {
46 kServiceStateDisabled,
47 kServiceStateEnabled,
48 kServiceStateNone
49 } ServiceState;
50
51 void SetConnectSuccessMockExpectations(ServiceState state);
52
53 void SetServiceEnabledExpectations();
54 void SetServiceDisabledExpectations();
55 void SetWillBeEnabledExpectations();
56 void SetWillBeDisabledExpectations();
57
58 bool SendEnabledInfo();
59 bool SendDisabledInfo();
60
61 private:
62 bool connected_;
63 cloud_print::CloudPrintProxyInfo info_;
64 };
65
66 // static
67 std::string MockServiceProcessControl::EnabledUserId() {
68 return std::string("dorothy@somewhere.otr");
69 }
70
71 void CallTask(const base::Closure& task) {
72 if (!task.is_null())
73 task.Run();
74 }
75
76 void MockServiceProcessControl::SetConnectSuccessMockExpectations(
77 ServiceState service_state) {
78 EXPECT_CALL(*this, is_connected()).WillRepeatedly(ReturnPointee(&connected_));
79
80 EXPECT_CALL(*this, Launch(_, _))
81 .WillRepeatedly(
82 DoAll(Assign(&connected_, true), WithArgs<0>(Invoke(CallTask))));
83 EXPECT_CALL(*this, Disconnect()).Times(AtMost(1))
84 .WillRepeatedly(Assign(&connected_, false));
85
86 EXPECT_CALL(*this, Send(_)).Times(0);
87
88 if (service_state == kServiceStateEnabled)
89 SetServiceEnabledExpectations();
90 else if (service_state == kServiceStateDisabled)
91 SetServiceDisabledExpectations();
92 }
93
94 void MockServiceProcessControl::SetServiceEnabledExpectations() {
95 EXPECT_CALL(
96 *this,
97 Send(Property(&IPC::Message::type,
98 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID))))
99 .Times(1).WillOnce(
100 WithoutArgs(
101 Invoke(this, &MockServiceProcessControl::SendEnabledInfo)));
102 }
103
104 void MockServiceProcessControl::SetServiceDisabledExpectations() {
105 EXPECT_CALL(
106 *this,
107 Send(Property(&IPC::Message::type,
108 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID))))
109 .Times(1).WillOnce(
110 WithoutArgs(
111 Invoke(this, &MockServiceProcessControl::SendDisabledInfo)));
112 }
113
114 void MockServiceProcessControl::SetWillBeEnabledExpectations() {
115 EXPECT_CALL(
116 *this,
117 Send(Property(&IPC::Message::type,
118 static_cast<int32>(ServiceMsg_EnableCloudPrintProxy::ID))))
119 .Times(1);
120 }
121
122 void MockServiceProcessControl::SetWillBeDisabledExpectations() {
123 EXPECT_CALL(
124 *this,
125 Send(Property(&IPC::Message::type,
126 static_cast<int32>(ServiceMsg_DisableCloudPrintProxy::ID))))
127 .Times(1);
128 }
129
130 bool MockServiceProcessControl::SendEnabledInfo() {
131 info_.enabled = true;
132 info_.email = EnabledUserId();
133 OnCloudPrintProxyInfo(info_);
134 return true;
135 }
136
137 bool MockServiceProcessControl::SendDisabledInfo() {
138 info_.enabled = false;
139 info_.email = std::string();
140 OnCloudPrintProxyInfo(info_);
141 return true;
142 }
143
144 class TestCloudPrintProxyService : public CloudPrintProxyService {
145 public:
146 explicit TestCloudPrintProxyService(Profile* profile)
147 : CloudPrintProxyService(profile) { }
148
149 virtual ServiceProcessControl* GetServiceProcessControl() {
150 return &process_control_;
151 }
152 MockServiceProcessControl* GetMockServiceProcessControl() {
153 return &process_control_;
154 }
155
156 private:
157 MockServiceProcessControl process_control_;
158 };
159
160 class CloudPrintProxyPolicyTest : public ::testing::Test {
161 public:
162 CloudPrintProxyPolicyTest()
163 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
164 }
165
166 protected:
167 MessageLoopForUI message_loop_;
168 content::TestBrowserThread ui_thread_;
169 TestingProfile profile_;
170 };
171
172 TEST_F(CloudPrintProxyPolicyTest, VerifyExpectations) {
173 MockServiceProcessControl mock_control;
174 mock_control.SetConnectSuccessMockExpectations(
175 MockServiceProcessControl::kServiceStateNone);
176
177 EXPECT_FALSE(mock_control.is_connected());
178 mock_control.Launch(base::Closure(), base::Closure());
179 EXPECT_TRUE(mock_control.is_connected());
180 mock_control.Launch(base::Closure(), base::Closure());
181 EXPECT_TRUE(mock_control.is_connected());
182 mock_control.Disconnect();
183 EXPECT_FALSE(mock_control.is_connected());
184 }
185
186 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabled) {
187 TestCloudPrintProxyService service(&profile_);
188
189 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
190 MockServiceProcessControl::kServiceStateDisabled);
191
192 TestingPrefService* prefs = profile_.GetTestingPrefService();
193 prefs->SetUserPref(prefs::kCloudPrintEmail,
194 Value::CreateStringValue(
195 MockServiceProcessControl::EnabledUserId()));
196
197 service.Initialize();
198
199 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
200 }
201
202 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabled) {
203 TestCloudPrintProxyService service(&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 TestCloudPrintProxyService service(&profile_);
221
222 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
223 MockServiceProcessControl::kServiceStateDisabled);
224
225 TestingPrefService* prefs = profile_.GetTestingPrefService();
226 prefs->SetUserPref(prefs::kCloudPrintEmail,
227 Value::CreateStringValue(std::string()));
228 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
229 Value::CreateBooleanValue(false));
230
231 service.Initialize();
232
233 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
234 }
235
236 TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyEnabled) {
237 TestCloudPrintProxyService service(&profile_);
238
239 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
240 MockServiceProcessControl::kServiceStateEnabled);
241 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
242
243 TestingPrefService* prefs = profile_.GetTestingPrefService();
244 prefs->SetUserPref(prefs::kCloudPrintEmail,
245 Value::CreateStringValue(std::string()));
246 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
247 Value::CreateBooleanValue(false));
248
249 service.Initialize();
250
251 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
252 }
253
254 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenSetPolicy) {
255 TestCloudPrintProxyService service(&profile_);
256
257 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
258 MockServiceProcessControl::kServiceStateDisabled);
259
260 TestingPrefService* prefs = profile_.GetTestingPrefService();
261 prefs->SetUserPref(prefs::kCloudPrintEmail,
262 Value::CreateStringValue(
263 MockServiceProcessControl::EnabledUserId()));
264
265 service.Initialize();
266
267 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
268
269 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
270 Value::CreateBooleanValue(false));
271
272 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
273 }
274
275 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabledThenSetPolicy) {
276 TestCloudPrintProxyService service(&profile_);
277
278 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
279 MockServiceProcessControl::kServiceStateEnabled);
280
281 TestingPrefService* prefs = profile_.GetTestingPrefService();
282 prefs->SetUserPref(prefs::kCloudPrintEmail,
283 Value::CreateStringValue(std::string()));
284
285 service.Initialize();
286 service.RefreshStatusFromService();
287
288 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
289 prefs->GetString(prefs::kCloudPrintEmail));
290
291 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
292 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
293 Value::CreateBooleanValue(false));
294
295 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
296 }
297
298 TEST_F(CloudPrintProxyPolicyTest,
299 StartWithPolicySetProxyDisabledThenClearPolicy) {
300 TestCloudPrintProxyService service(&profile_);
301
302 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
303 MockServiceProcessControl::kServiceStateDisabled);
304
305 TestingPrefService* prefs = profile_.GetTestingPrefService();
306 prefs->SetUserPref(prefs::kCloudPrintEmail,
307 Value::CreateStringValue(std::string()));
308 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
309 Value::CreateBooleanValue(false));
310
311 service.Initialize();
312
313 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
314 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
315 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
316 }
317
318 TEST_F(CloudPrintProxyPolicyTest,
319 StartWithPolicySetProxyEnabledThenClearPolicy) {
320 TestCloudPrintProxyService service(&profile_);
321
322 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
323 MockServiceProcessControl::kServiceStateEnabled);
324 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
325
326 TestingPrefService* prefs = profile_.GetTestingPrefService();
327 prefs->SetUserPref(prefs::kCloudPrintEmail,
328 Value::CreateStringValue(std::string()));
329 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
330 Value::CreateBooleanValue(false));
331
332 service.Initialize();
333
334 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
335 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
336 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
337 }
338
339 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenEnable) {
340 TestCloudPrintProxyService service(&profile_);
341
342 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
343 MockServiceProcessControl::kServiceStateDisabled);
344
345 TestingPrefService* prefs = profile_.GetTestingPrefService();
346 prefs->SetUserPref(prefs::kCloudPrintEmail,
347 Value::CreateStringValue(
348 MockServiceProcessControl::EnabledUserId()));
349
350 service.Initialize();
351 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
352
353 service.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
354 service.EnableForUser(std::string(),
355 MockServiceProcessControl::EnabledUserId());
356
357 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
358 prefs->GetString(prefs::kCloudPrintEmail));
359 }
360
361 TEST_F(CloudPrintProxyPolicyTest,
362 StartWithPolicySetProxyEnabledThenClearPolicyAndEnable) {
363 TestCloudPrintProxyService service(&profile_);
364
365 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
366 MockServiceProcessControl::kServiceStateEnabled);
367 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
368
369 TestingPrefService* prefs = profile_.GetTestingPrefService();
370 prefs->SetUserPref(prefs::kCloudPrintEmail,
371 Value::CreateStringValue(std::string()));
372 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
373 Value::CreateBooleanValue(false));
374
375 service.Initialize();
376
377 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
378 service.EnableForUser(std::string(),
379 MockServiceProcessControl::EnabledUserId());
380 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
381
382 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
383 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
384
385 service.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
386 service.EnableForUser(std::string(),
387 MockServiceProcessControl::EnabledUserId());
388
389 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
390 prefs->GetString(prefs::kCloudPrintEmail));
391 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698