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.
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
16 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
17 #include "chrome/browser/service_process/service_process_control.h"
18 #include "chrome/browser/ui/startup/startup_browser_creator.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/cloud_print/cloud_print_proxy_info.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/service_messages.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/test/base/testing_profile_manager.h"
26 #include "components/syncable_prefs/testing_pref_service_syncable.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/test/test_browser_thread.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using ::testing::Assign
;
33 using ::testing::AtMost
;
34 using ::testing::DeleteArg
;
35 using ::testing::DoAll
;
36 using ::testing::Invoke
;
37 using ::testing::Property
;
38 using ::testing::Return
;
39 using ::testing::ReturnPointee
;
40 using ::testing::WithArgs
;
41 using ::testing::WithoutArgs
;
44 class MockServiceProcessControl
: public ServiceProcessControl
{
46 static std::string
EnabledUserId();
48 MockServiceProcessControl() : connected_(false) { }
50 MOCK_CONST_METHOD0(IsConnected
, bool());
52 MOCK_METHOD2(Launch
, void(const base::Closure
&, const base::Closure
&));
53 MOCK_METHOD0(Disconnect
, void());
55 MOCK_METHOD1(OnMessageReceived
, bool(const IPC::Message
&));
56 MOCK_METHOD1(OnChannelConnected
, void(int32 peer_pid
));
57 MOCK_METHOD0(OnChannelError
, void());
59 MOCK_METHOD1(Send
, bool(IPC::Message
*));
62 kServiceStateDisabled
,
67 void SetConnectSuccessMockExpectations(ServiceState state
, bool post_task
);
69 void SetServiceEnabledExpectations();
70 void SetServiceDisabledExpectations();
71 void SetWillBeEnabledExpectations();
72 void SetWillBeDisabledExpectations();
74 bool SendEnabledInfo();
75 bool SendDisabledInfo();
79 cloud_print::CloudPrintProxyInfo info_
;
83 std::string
MockServiceProcessControl::EnabledUserId() {
84 return std::string("dorothy@somewhere.otr");
87 void CallTask(const base::Closure
& task
) {
92 void PostTask(const base::Closure
& task
) {
94 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, task
);
97 void MockServiceProcessControl::SetConnectSuccessMockExpectations(
98 ServiceState service_state
,
100 EXPECT_CALL(*this, IsConnected()).WillRepeatedly(ReturnPointee(&connected_
));
102 EXPECT_CALL(*this, Launch(_
, _
))
104 DoAll(Assign(&connected_
, true),
105 WithArgs
<0>(Invoke(post_task
? PostTask
: CallTask
))));
107 EXPECT_CALL(*this, Disconnect()).Times(AtMost(1))
108 .WillRepeatedly(Assign(&connected_
, false));
110 EXPECT_CALL(*this, Send(_
)).Times(0);
112 if (service_state
== kServiceStateEnabled
)
113 SetServiceEnabledExpectations();
114 else if (service_state
== kServiceStateDisabled
)
115 SetServiceDisabledExpectations();
118 void MockServiceProcessControl::SetServiceEnabledExpectations() {
121 Send(Property(&IPC::Message::type
,
122 static_cast<int32
>(ServiceMsg_GetCloudPrintProxyInfo::ID
))))
127 Invoke(this, &MockServiceProcessControl::SendEnabledInfo
))));
130 void MockServiceProcessControl::SetServiceDisabledExpectations() {
133 Send(Property(&IPC::Message::type
,
134 static_cast<int32
>(ServiceMsg_GetCloudPrintProxyInfo::ID
))))
139 Invoke(this, &MockServiceProcessControl::SendDisabledInfo
))));
142 void MockServiceProcessControl::SetWillBeEnabledExpectations() {
143 int32 message_id
= ServiceMsg_EnableCloudPrintProxyWithRobot::ID
;
146 Send(Property(&IPC::Message::type
, message_id
)))
147 .Times(1).WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
150 void MockServiceProcessControl::SetWillBeDisabledExpectations() {
153 Send(Property(&IPC::Message::type
,
154 static_cast<int32
>(ServiceMsg_DisableCloudPrintProxy::ID
))))
155 .Times(1).WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
158 bool MockServiceProcessControl::SendEnabledInfo() {
159 info_
.enabled
= true;
160 info_
.email
= EnabledUserId();
161 PostTask(base::Bind(&MockServiceProcessControl::OnCloudPrintProxyInfo
,
162 base::Unretained(this), info_
));
166 bool MockServiceProcessControl::SendDisabledInfo() {
167 info_
.enabled
= false;
168 info_
.email
= std::string();
169 PostTask(base::Bind(&MockServiceProcessControl::OnCloudPrintProxyInfo
,
170 base::Unretained(this), info_
));
174 class TestCloudPrintProxyService
: public CloudPrintProxyService
{
176 explicit TestCloudPrintProxyService(Profile
* profile
)
177 : CloudPrintProxyService(profile
) { }
180 CloudPrintProxyService::Initialize();
181 base::RunLoop().RunUntilIdle();
184 void RefreshStatusFromService() {
185 CloudPrintProxyService::RefreshStatusFromService();
186 base::RunLoop().RunUntilIdle();
189 ServiceProcessControl
* GetServiceProcessControl() override
{
190 return &process_control_
;
192 MockServiceProcessControl
* GetMockServiceProcessControl() {
193 return &process_control_
;
196 void EnableForUser() {
197 EnableForUserWithRobot("123", "123@gmail.com",
198 MockServiceProcessControl::EnabledUserId(),
199 base::DictionaryValue());
203 MockServiceProcessControl process_control_
;
206 class CloudPrintProxyPolicyTest
: public ::testing::Test
{
208 CloudPrintProxyPolicyTest()
209 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
212 bool LaunchBrowser(const base::CommandLine
& command_line
, Profile
* profile
) {
213 StartupBrowserCreator browser_creator
;
214 return StartupBrowserCreator::ProcessCmdLineImpl(
215 command_line
, base::FilePath(), false, profile
,
216 StartupBrowserCreator::Profiles(), &browser_creator
);
220 base::MessageLoopForUI message_loop_
;
221 content::TestBrowserThread ui_thread_
;
222 TestingProfile profile_
;
225 TEST_F(CloudPrintProxyPolicyTest
, VerifyExpectations
) {
226 MockServiceProcessControl mock_control
;
227 mock_control
.SetConnectSuccessMockExpectations(
228 MockServiceProcessControl::kServiceStateNone
, false);
230 EXPECT_FALSE(mock_control
.IsConnected());
231 mock_control
.Launch(base::Closure(), base::Closure());
232 EXPECT_TRUE(mock_control
.IsConnected());
233 mock_control
.Launch(base::Closure(), base::Closure());
234 EXPECT_TRUE(mock_control
.IsConnected());
235 mock_control
.Disconnect();
236 EXPECT_FALSE(mock_control
.IsConnected());
239 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabled
) {
240 TestCloudPrintProxyService
service(&profile_
);
242 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
243 MockServiceProcessControl::kServiceStateDisabled
, false);
245 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
246 profile_
.GetTestingPrefService();
248 prefs::kCloudPrintEmail
,
249 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
251 service
.Initialize();
253 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
256 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabled
) {
257 TestCloudPrintProxyService
service(&profile_
);
259 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
260 MockServiceProcessControl::kServiceStateEnabled
, false);
262 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
263 profile_
.GetTestingPrefService();
264 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
265 new base::StringValue(std::string()));
267 service
.Initialize();
268 service
.RefreshStatusFromService();
270 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
271 prefs
->GetString(prefs::kCloudPrintEmail
));
274 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyDisabled
) {
275 TestCloudPrintProxyService
service(&profile_
);
277 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
278 MockServiceProcessControl::kServiceStateDisabled
, false);
280 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
281 profile_
.GetTestingPrefService();
282 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
283 new base::StringValue(std::string()));
284 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
285 new base::FundamentalValue(false));
287 service
.Initialize();
289 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
292 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyEnabled
) {
293 TestCloudPrintProxyService
service(&profile_
);
295 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
296 MockServiceProcessControl::kServiceStateEnabled
, false);
297 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
299 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
300 profile_
.GetTestingPrefService();
301 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
302 new base::StringValue(std::string()));
303 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
304 new base::FundamentalValue(false));
306 service
.Initialize();
308 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
311 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenSetPolicy
) {
312 TestCloudPrintProxyService
service(&profile_
);
314 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
315 MockServiceProcessControl::kServiceStateDisabled
, false);
317 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
318 profile_
.GetTestingPrefService();
320 prefs::kCloudPrintEmail
,
321 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
323 service
.Initialize();
325 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
327 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
328 new base::FundamentalValue(false));
330 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
333 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabledThenSetPolicy
) {
334 TestCloudPrintProxyService
service(&profile_
);
336 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
337 MockServiceProcessControl::kServiceStateEnabled
, false);
339 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
340 profile_
.GetTestingPrefService();
341 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
342 new base::StringValue(std::string()));
344 service
.Initialize();
345 service
.RefreshStatusFromService();
347 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
348 prefs
->GetString(prefs::kCloudPrintEmail
));
350 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
351 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
352 new base::FundamentalValue(false));
354 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
357 TEST_F(CloudPrintProxyPolicyTest
,
358 StartWithPolicySetProxyDisabledThenClearPolicy
) {
359 TestCloudPrintProxyService
service(&profile_
);
361 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
362 MockServiceProcessControl::kServiceStateDisabled
, false);
364 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
365 profile_
.GetTestingPrefService();
366 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
367 new base::StringValue(std::string()));
368 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
369 new base::FundamentalValue(false));
371 service
.Initialize();
373 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
374 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
375 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
378 TEST_F(CloudPrintProxyPolicyTest
,
379 StartWithPolicySetProxyEnabledThenClearPolicy
) {
380 TestCloudPrintProxyService
service(&profile_
);
382 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
383 MockServiceProcessControl::kServiceStateEnabled
, false);
384 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
386 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
387 profile_
.GetTestingPrefService();
388 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
389 new base::StringValue(std::string()));
390 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
391 new base::FundamentalValue(false));
393 service
.Initialize();
395 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
396 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
397 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
400 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenEnable
) {
401 TestCloudPrintProxyService
service(&profile_
);
403 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
404 MockServiceProcessControl::kServiceStateDisabled
, false);
406 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
407 profile_
.GetTestingPrefService();
409 prefs::kCloudPrintEmail
,
410 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
412 service
.Initialize();
413 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
415 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
416 service
.EnableForUser();
418 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
419 prefs
->GetString(prefs::kCloudPrintEmail
));
422 TEST_F(CloudPrintProxyPolicyTest
,
423 StartWithPolicySetProxyEnabledThenClearPolicyAndEnable
) {
424 TestCloudPrintProxyService
service(&profile_
);
426 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
427 MockServiceProcessControl::kServiceStateEnabled
, false);
428 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
430 syncable_prefs::TestingPrefServiceSyncable
* prefs
=
431 profile_
.GetTestingPrefService();
432 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
433 new base::StringValue(std::string()));
434 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
435 new base::FundamentalValue(false));
437 service
.Initialize();
439 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
440 service
.EnableForUser();
441 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
443 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
444 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
446 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
447 service
.EnableForUser();
449 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
450 prefs
->GetString(prefs::kCloudPrintEmail
));