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 virtual 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 CommandLine
& command_line
, Profile
* profile
) {
211 StartupBrowserCreator browser_creator
;
212 return StartupBrowserCreator::ProcessCmdLineImpl(
213 command_line
, base::FilePath(), false, profile
,
214 StartupBrowserCreator::Profiles(), &return_code
, &browser_creator
);
218 base::MessageLoopForUI message_loop_
;
219 content::TestBrowserThread ui_thread_
;
220 TestingProfile profile_
;
223 TEST_F(CloudPrintProxyPolicyTest
, VerifyExpectations
) {
224 MockServiceProcessControl mock_control
;
225 mock_control
.SetConnectSuccessMockExpectations(
226 MockServiceProcessControl::kServiceStateNone
, false);
228 EXPECT_FALSE(mock_control
.IsConnected());
229 mock_control
.Launch(base::Closure(), base::Closure());
230 EXPECT_TRUE(mock_control
.IsConnected());
231 mock_control
.Launch(base::Closure(), base::Closure());
232 EXPECT_TRUE(mock_control
.IsConnected());
233 mock_control
.Disconnect();
234 EXPECT_FALSE(mock_control
.IsConnected());
237 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabled
) {
238 TestCloudPrintProxyService
service(&profile_
);
240 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
241 MockServiceProcessControl::kServiceStateDisabled
, false);
243 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
245 prefs::kCloudPrintEmail
,
246 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
248 service
.Initialize();
250 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
253 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabled
) {
254 TestCloudPrintProxyService
service(&profile_
);
256 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
257 MockServiceProcessControl::kServiceStateEnabled
, false);
259 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
260 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
261 new base::StringValue(std::string()));
263 service
.Initialize();
264 service
.RefreshStatusFromService();
266 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
267 prefs
->GetString(prefs::kCloudPrintEmail
));
270 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyDisabled
) {
271 TestCloudPrintProxyService
service(&profile_
);
273 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
274 MockServiceProcessControl::kServiceStateDisabled
, false);
276 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
277 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
278 new base::StringValue(std::string()));
279 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
280 new base::FundamentalValue(false));
282 service
.Initialize();
284 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
287 TEST_F(CloudPrintProxyPolicyTest
, StartWithPolicySetProxyEnabled
) {
288 TestCloudPrintProxyService
service(&profile_
);
290 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
291 MockServiceProcessControl::kServiceStateEnabled
, false);
292 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
294 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
295 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
296 new base::StringValue(std::string()));
297 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
298 new base::FundamentalValue(false));
300 service
.Initialize();
302 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
305 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenSetPolicy
) {
306 TestCloudPrintProxyService
service(&profile_
);
308 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
309 MockServiceProcessControl::kServiceStateDisabled
, false);
311 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
313 prefs::kCloudPrintEmail
,
314 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
316 service
.Initialize();
318 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
320 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
321 new base::FundamentalValue(false));
323 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
326 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyEnabledThenSetPolicy
) {
327 TestCloudPrintProxyService
service(&profile_
);
329 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
330 MockServiceProcessControl::kServiceStateEnabled
, false);
332 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
333 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
334 new base::StringValue(std::string()));
336 service
.Initialize();
337 service
.RefreshStatusFromService();
339 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
340 prefs
->GetString(prefs::kCloudPrintEmail
));
342 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
343 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
344 new base::FundamentalValue(false));
346 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
349 TEST_F(CloudPrintProxyPolicyTest
,
350 StartWithPolicySetProxyDisabledThenClearPolicy
) {
351 TestCloudPrintProxyService
service(&profile_
);
353 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
354 MockServiceProcessControl::kServiceStateDisabled
, false);
356 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
357 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
358 new base::StringValue(std::string()));
359 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
360 new base::FundamentalValue(false));
362 service
.Initialize();
364 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
365 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
366 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
369 TEST_F(CloudPrintProxyPolicyTest
,
370 StartWithPolicySetProxyEnabledThenClearPolicy
) {
371 TestCloudPrintProxyService
service(&profile_
);
373 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
374 MockServiceProcessControl::kServiceStateEnabled
, false);
375 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
377 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
378 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
379 new base::StringValue(std::string()));
380 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
381 new base::FundamentalValue(false));
383 service
.Initialize();
385 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
386 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
387 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
390 TEST_F(CloudPrintProxyPolicyTest
, StartWithNoPolicyProxyDisabledThenEnable
) {
391 TestCloudPrintProxyService
service(&profile_
);
393 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
394 MockServiceProcessControl::kServiceStateDisabled
, false);
396 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
398 prefs::kCloudPrintEmail
,
399 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
401 service
.Initialize();
402 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
404 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
405 service
.EnableForUser();
407 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
408 prefs
->GetString(prefs::kCloudPrintEmail
));
411 TEST_F(CloudPrintProxyPolicyTest
,
412 StartWithPolicySetProxyEnabledThenClearPolicyAndEnable
) {
413 TestCloudPrintProxyService
service(&profile_
);
415 service
.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
416 MockServiceProcessControl::kServiceStateEnabled
, false);
417 service
.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
419 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
420 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
421 new base::StringValue(std::string()));
422 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
423 new base::FundamentalValue(false));
425 service
.Initialize();
427 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
428 service
.EnableForUser();
429 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
431 prefs
->RemoveManagedPref(prefs::kCloudPrintProxyEnabled
);
432 EXPECT_EQ(std::string(), prefs
->GetString(prefs::kCloudPrintEmail
));
434 service
.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
435 service
.EnableForUser();
437 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
438 prefs
->GetString(prefs::kCloudPrintEmail
));
441 KeyedService
* TestCloudPrintProxyServiceFactory(
442 content::BrowserContext
* profile
) {
443 TestCloudPrintProxyService
* service
=
444 new TestCloudPrintProxyService(static_cast<Profile
*>(profile
));
446 service
->GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
447 MockServiceProcessControl::kServiceStateEnabled
, true);
448 service
->GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
450 service
->Initialize();
454 TEST_F(CloudPrintProxyPolicyTest
, StartupBrowserCreatorWithCommandLine
) {
455 TestingPrefServiceSyncable
* prefs
= profile_
.GetTestingPrefService();
456 prefs
->SetUserPref(prefs::kCloudPrintEmail
,
457 new base::StringValue(std::string()));
458 prefs
->SetManagedPref(prefs::kCloudPrintProxyEnabled
,
459 new base::FundamentalValue(false));
461 CloudPrintProxyServiceFactory::GetInstance()->
462 SetTestingFactory(&profile_
, TestCloudPrintProxyServiceFactory
);
464 CommandLine
command_line(CommandLine::NO_PROGRAM
);
465 command_line
.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy
);
467 EXPECT_FALSE(LaunchBrowser(command_line
, &profile_
));
468 base::RunLoop().RunUntilIdle();