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/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
13 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
14 #include "chrome/browser/service_process/service_process_control.h"
15 #include "chrome/browser/ui/startup/startup_browser_creator.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/cloud_print/cloud_print_proxy_info.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/service_messages.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chrome/test/base/testing_profile_manager.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using ::testing::Assign
;
30 using ::testing::AtMost
;
31 using ::testing::DeleteArg
;
32 using ::testing::DoAll
;
33 using ::testing::Invoke
;
34 using ::testing::Property
;
35 using ::testing::Return
;
36 using ::testing::ReturnPointee
;
37 using ::testing::WithArgs
;
38 using ::testing::WithoutArgs
;
41 class MockServiceProcessControl
: public ServiceProcessControl
{
43 static std::string
EnabledUserId();
45 MockServiceProcessControl() : connected_(false) { }
47 MOCK_CONST_METHOD0(IsConnected
, bool());
49 MOCK_METHOD2(Launch
, void(const base::Closure
&, const base::Closure
&));
50 MOCK_METHOD0(Disconnect
, void());
52 MOCK_METHOD1(OnMessageReceived
, bool(const IPC::Message
&));
53 MOCK_METHOD1(OnChannelConnected
, void(int32 peer_pid
));
54 MOCK_METHOD0(OnChannelError
, void());
56 MOCK_METHOD1(Send
, bool(IPC::Message
*));
59 kServiceStateDisabled
,
64 void SetConnectSuccessMockExpectations(ServiceState state
, bool post_task
);
66 void SetServiceEnabledExpectations();
67 void SetServiceDisabledExpectations();
68 void SetWillBeEnabledExpectations();
69 void SetWillBeDisabledExpectations();
71 bool SendEnabledInfo();
72 bool SendDisabledInfo();
76 cloud_print::CloudPrintProxyInfo info_
;
80 std::string
MockServiceProcessControl::EnabledUserId() {
81 return std::string("dorothy@somewhere.otr");
84 void CallTask(const base::Closure
& task
) {
89 void PostTask(const base::Closure
& task
) {
91 base::MessageLoop::current()->PostTask(FROM_HERE
, task
);
94 void MockServiceProcessControl::SetConnectSuccessMockExpectations(
95 ServiceState service_state
,
97 EXPECT_CALL(*this, IsConnected()).WillRepeatedly(ReturnPointee(&connected_
));
99 EXPECT_CALL(*this, Launch(_
, _
))
101 DoAll(Assign(&connected_
, true),
102 WithArgs
<0>(Invoke(post_task
? PostTask
: CallTask
))));
104 EXPECT_CALL(*this, Disconnect()).Times(AtMost(1))
105 .WillRepeatedly(Assign(&connected_
, false));
107 EXPECT_CALL(*this, Send(_
)).Times(0);
109 if (service_state
== kServiceStateEnabled
)
110 SetServiceEnabledExpectations();
111 else if (service_state
== kServiceStateDisabled
)
112 SetServiceDisabledExpectations();
115 void MockServiceProcessControl::SetServiceEnabledExpectations() {
118 Send(Property(&IPC::Message::type
,
119 static_cast<int32
>(ServiceMsg_GetCloudPrintProxyInfo::ID
))))
124 Invoke(this, &MockServiceProcessControl::SendEnabledInfo
))));
127 void MockServiceProcessControl::SetServiceDisabledExpectations() {
130 Send(Property(&IPC::Message::type
,
131 static_cast<int32
>(ServiceMsg_GetCloudPrintProxyInfo::ID
))))
136 Invoke(this, &MockServiceProcessControl::SendDisabledInfo
))));
139 void MockServiceProcessControl::SetWillBeEnabledExpectations() {
140 int32 message_id
= ServiceMsg_EnableCloudPrintProxyWithRobot::ID
;
143 Send(Property(&IPC::Message::type
, message_id
)))
144 .Times(1).WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
147 void MockServiceProcessControl::SetWillBeDisabledExpectations() {
150 Send(Property(&IPC::Message::type
,
151 static_cast<int32
>(ServiceMsg_DisableCloudPrintProxy::ID
))))
152 .Times(1).WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
155 bool MockServiceProcessControl::SendEnabledInfo() {
156 info_
.enabled
= true;
157 info_
.email
= EnabledUserId();
158 PostTask(base::Bind(&MockServiceProcessControl::OnCloudPrintProxyInfo
,
159 base::Unretained(this), info_
));
163 bool MockServiceProcessControl::SendDisabledInfo() {
164 info_
.enabled
= false;
165 info_
.email
= std::string();
166 PostTask(base::Bind(&MockServiceProcessControl::OnCloudPrintProxyInfo
,
167 base::Unretained(this), info_
));
171 class TestCloudPrintProxyService
: public CloudPrintProxyService
{
173 explicit TestCloudPrintProxyService(Profile
* profile
)
174 : CloudPrintProxyService(profile
) { }
177 CloudPrintProxyService::Initialize();
178 base::RunLoop().RunUntilIdle();
181 void RefreshStatusFromService() {
182 CloudPrintProxyService::RefreshStatusFromService();
183 base::RunLoop().RunUntilIdle();
186 ServiceProcessControl
* GetServiceProcessControl() override
{
187 return &process_control_
;
189 MockServiceProcessControl
* GetMockServiceProcessControl() {
190 return &process_control_
;
193 void EnableForUser() {
194 EnableForUserWithRobot("123", "123@gmail.com",
195 MockServiceProcessControl::EnabledUserId(),
196 base::DictionaryValue());
200 MockServiceProcessControl process_control_
;
203 class CloudPrintProxyPolicyTest
: public ::testing::Test
{
205 CloudPrintProxyPolicyTest()
206 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
209 bool LaunchBrowser(const base::CommandLine
& command_line
, Profile
* profile
) {
210 StartupBrowserCreator browser_creator
;
211 return StartupBrowserCreator::ProcessCmdLineImpl(
212 command_line
, base::FilePath(), false, profile
,
213 StartupBrowserCreator::Profiles(), &browser_creator
);
217 base::MessageLoopForUI message_loop_
;
218 content::TestBrowserThread ui_thread_
;
219 TestingProfile profile_
;
222 TEST_F(CloudPrintProxyPolicyTest
, VerifyExpectations
) {
223 MockServiceProcessControl mock_control
;
224 mock_control
.SetConnectSuccessMockExpectations(
225 MockServiceProcessControl::kServiceStateNone
, false);
227 EXPECT_FALSE(mock_control
.IsConnected());
228 mock_control
.Launch(base::Closure(), base::Closure());
229 EXPECT_TRUE(mock_control
.IsConnected());
230 mock_control
.Launch(base::Closure(), base::Closure());
231 EXPECT_TRUE(mock_control
.IsConnected());
232 mock_control
.Disconnect();
233 EXPECT_FALSE(mock_control
.IsConnected());
236 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabled
) {
237 TestCloudPrintProxyService
service(&profile_
);
239 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
240 MockServiceProcessControl::kServiceStateDisabled
, false);
242 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
244 prefs::kCloudPrintEmail
,
245 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
247 service
.Initialize();
249 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
252 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabled
) {
253 TestCloudPrintProxyService
service(&profile_
);
255 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
256 MockServiceProcessControl::kServiceStateEnabled
, false);
258 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
259 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
260 new base::StringValue(std::string()));
262 service
.Initialize();
263 service
.RefreshStatusFromService();
265 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
266 prefs
->GetString(prefs::kCloudPrintEmail
));
269 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyDisabled
) {
270 TestCloudPrintProxyService
service(&profile_
);
272 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
273 MockServiceProcessControl::kServiceStateDisabled
, false);
275 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
276 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
277 new base::StringValue(std::string()));
278 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
279 new base::FundamentalValue(false));
281 service
.Initialize();
283 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
286 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyEnabled
) {
287 TestCloudPrintProxyService
service(&profile_
);
289 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
290 MockServiceProcessControl::kServiceStateEnabled
, false);
291 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
293 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
294 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
295 new base::StringValue(std::string()));
296 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
297 new base::FundamentalValue(false));
299 service
.Initialize();
301 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
304 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenSetPolicy
) {
305 TestCloudPrintProxyService
service(&profile_
);
307 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
308 MockServiceProcessControl::kServiceStateDisabled
, false);
310 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
312 prefs::kCloudPrintEmail
,
313 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
315 service
.Initialize();
317 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
319 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
320 new base::FundamentalValue(false));
322 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
325 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabledThenSetPolicy
) {
326 TestCloudPrintProxyService
service(&profile_
);
328 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
329 MockServiceProcessControl::kServiceStateEnabled
, false);
331 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
332 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
333 new base::StringValue(std::string()));
335 service
.Initialize();
336 service
.RefreshStatusFromService();
338 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
339 prefs
->GetString(prefs::kCloudPrintEmail
));
341 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
342 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
343 new base::FundamentalValue(false));
345 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
348 TEST_F(CloudPrintProxyPolicyTest
,
349 StartWithPolicySetProxyDisabledThenClearPolicy
) {
350 TestCloudPrintProxyService
service(&profile_
);
352 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
353 MockServiceProcessControl::kServiceStateDisabled
, false);
355 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
356 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
357 new base::StringValue(std::string()));
358 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
359 new base::FundamentalValue(false));
361 service
.Initialize();
363 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
364 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
365 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
368 TEST_F(CloudPrintProxyPolicyTest
,
369 StartWithPolicySetProxyEnabledThenClearPolicy
) {
370 TestCloudPrintProxyService
service(&profile_
);
372 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
373 MockServiceProcessControl::kServiceStateEnabled
, false);
374 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
376 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
377 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
378 new base::StringValue(std::string()));
379 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
380 new base::FundamentalValue(false));
382 service
.Initialize();
384 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
385 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
386 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
389 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenEnable
) {
390 TestCloudPrintProxyService
service(&profile_
);
392 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
393 MockServiceProcessControl::kServiceStateDisabled
, false);
395 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
397 prefs::kCloudPrintEmail
,
398 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
400 service
.Initialize();
401 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
403 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
404 service
.EnableForUser();
406 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
407 prefs
->GetString(prefs::kCloudPrintEmail
));
410 TEST_F(CloudPrintProxyPolicyTest
,
411 StartWithPolicySetProxyEnabledThenClearPolicyAndEnable
) {
412 TestCloudPrintProxyService
service(&profile_
);
414 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
415 MockServiceProcessControl::kServiceStateEnabled
, false);
416 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
418 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
419 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
420 new base::StringValue(std::string()));
421 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
422 new base::FundamentalValue(false));
424 service
.Initialize();
426 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
427 service
.EnableForUser();
428 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
430 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
431 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
433 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
434 service
.EnableForUser();
436 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
437 prefs
->GetString(prefs::kCloudPrintEmail
));
440 KeyedService
* TestCloudPrintProxyServiceFactory(
441 content::BrowserContext
* profile
) {
442 TestCloudPrintProxyService
* service
=
443 new TestCloudPrintProxyService(static_cast<Profile
*>(profile
));
445 service
->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
446 MockServiceProcessControl::kServiceStateEnabled
, true);
447 service
->GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
449 service
->Initialize();
453 TEST_F(CloudPrintProxyPolicyTest
, StartupBrowserCreatorWithCommandLine
) {
454 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
455 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
456 new base::StringValue(std::string()));
457 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
458 new base::FundamentalValue(false));
460 CloudPrintProxyServiceFactory::GetInstance()->
461 SetTestingFactory(&profile_
, TestCloudPrintProxyServiceFactory
);
463 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
464 command_line
.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy
);
466 EXPECT_FALSE(LaunchBrowser(command_line
, &profile_
));
467 base::RunLoop().RunUntilIdle();