Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / printing / cloud_print / cloud_print_proxy_service_unittest.cc
blobed618e289834134afa42cc54c0850b37cb2010e8
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.
5 #include <string>
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_pref_service_syncable.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/testing_profile_manager.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;
42 using ::testing::_;
44 class MockServiceProcessControl : public ServiceProcessControl {
45 public:
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*));
61 typedef enum {
62 kServiceStateDisabled,
63 kServiceStateEnabled,
64 kServiceStateNone
65 } ServiceState;
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();
77 private:
78 bool connected_;
79 cloud_print::CloudPrintProxyInfo info_;
82 // static
83 std::string MockServiceProcessControl::EnabledUserId() {
84 return std::string("dorothy@somewhere.otr");
87 void CallTask(const base::Closure& task) {
88 if (!task.is_null())
89 task.Run();
92 void PostTask(const base::Closure& task) {
93 if (!task.is_null())
94 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
97 void MockServiceProcessControl::SetConnectSuccessMockExpectations(
98 ServiceState service_state,
99 bool post_task) {
100 EXPECT_CALL(*this, IsConnected()).WillRepeatedly(ReturnPointee(&connected_));
102 EXPECT_CALL(*this, Launch(_, _))
103 .WillRepeatedly(
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() {
119 EXPECT_CALL(
120 *this,
121 Send(Property(&IPC::Message::type,
122 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID))))
123 .Times(1).WillOnce(
124 DoAll(
125 DeleteArg<0>(),
126 WithoutArgs(
127 Invoke(this, &MockServiceProcessControl::SendEnabledInfo))));
130 void MockServiceProcessControl::SetServiceDisabledExpectations() {
131 EXPECT_CALL(
132 *this,
133 Send(Property(&IPC::Message::type,
134 static_cast<int32>(ServiceMsg_GetCloudPrintProxyInfo::ID))))
135 .Times(1).WillOnce(
136 DoAll(
137 DeleteArg<0>(),
138 WithoutArgs(
139 Invoke(this, &MockServiceProcessControl::SendDisabledInfo))));
142 void MockServiceProcessControl::SetWillBeEnabledExpectations() {
143 int32 message_id = ServiceMsg_EnableCloudPrintProxyWithRobot::ID;
144 EXPECT_CALL(
145 *this,
146 Send(Property(&IPC::Message::type, message_id)))
147 .Times(1).WillOnce(DoAll(DeleteArg<0>(), Return(true)));
150 void MockServiceProcessControl::SetWillBeDisabledExpectations() {
151 EXPECT_CALL(
152 *this,
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_));
163 return true;
166 bool MockServiceProcessControl::SendDisabledInfo() {
167 info_.enabled = false;
168 info_.email = std::string();
169 PostTask(base::Bind(&MockServiceProcessControl::OnCloudPrintProxyInfo,
170 base::Unretained(this), info_));
171 return true;
174 class TestCloudPrintProxyService : public CloudPrintProxyService {
175 public:
176 explicit TestCloudPrintProxyService(Profile* profile)
177 : CloudPrintProxyService(profile) { }
179 void Initialize() {
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());
202 private:
203 MockServiceProcessControl process_control_;
206 class CloudPrintProxyPolicyTest : public ::testing::Test {
207 public:
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);
219 protected:
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 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
246 prefs->SetUserPref(
247 prefs::kCloudPrintEmail,
248 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
250 service.Initialize();
252 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
255 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabled) {
256 TestCloudPrintProxyService service(&profile_);
258 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
259 MockServiceProcessControl::kServiceStateEnabled, false);
261 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
262 prefs->SetUserPref(prefs::kCloudPrintEmail,
263 new base::StringValue(std::string()));
265 service.Initialize();
266 service.RefreshStatusFromService();
268 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
269 prefs->GetString(prefs::kCloudPrintEmail));
272 TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyDisabled) {
273 TestCloudPrintProxyService service(&profile_);
275 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
276 MockServiceProcessControl::kServiceStateDisabled, false);
278 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
279 prefs->SetUserPref(prefs::kCloudPrintEmail,
280 new base::StringValue(std::string()));
281 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
282 new base::FundamentalValue(false));
284 service.Initialize();
286 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
289 TEST_F(CloudPrintProxyPolicyTest, StartWithPolicySetProxyEnabled) {
290 TestCloudPrintProxyService service(&profile_);
292 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
293 MockServiceProcessControl::kServiceStateEnabled, false);
294 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
296 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
297 prefs->SetUserPref(prefs::kCloudPrintEmail,
298 new base::StringValue(std::string()));
299 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
300 new base::FundamentalValue(false));
302 service.Initialize();
304 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
307 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenSetPolicy) {
308 TestCloudPrintProxyService service(&profile_);
310 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
311 MockServiceProcessControl::kServiceStateDisabled, false);
313 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
314 prefs->SetUserPref(
315 prefs::kCloudPrintEmail,
316 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
318 service.Initialize();
320 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
322 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
323 new base::FundamentalValue(false));
325 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
328 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyEnabledThenSetPolicy) {
329 TestCloudPrintProxyService service(&profile_);
331 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
332 MockServiceProcessControl::kServiceStateEnabled, false);
334 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
335 prefs->SetUserPref(prefs::kCloudPrintEmail,
336 new base::StringValue(std::string()));
338 service.Initialize();
339 service.RefreshStatusFromService();
341 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
342 prefs->GetString(prefs::kCloudPrintEmail));
344 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
345 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
346 new base::FundamentalValue(false));
348 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
351 TEST_F(CloudPrintProxyPolicyTest,
352 StartWithPolicySetProxyDisabledThenClearPolicy) {
353 TestCloudPrintProxyService service(&profile_);
355 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
356 MockServiceProcessControl::kServiceStateDisabled, false);
358 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
359 prefs->SetUserPref(prefs::kCloudPrintEmail,
360 new base::StringValue(std::string()));
361 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
362 new base::FundamentalValue(false));
364 service.Initialize();
366 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
367 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
368 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
371 TEST_F(CloudPrintProxyPolicyTest,
372 StartWithPolicySetProxyEnabledThenClearPolicy) {
373 TestCloudPrintProxyService service(&profile_);
375 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
376 MockServiceProcessControl::kServiceStateEnabled, false);
377 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
379 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
380 prefs->SetUserPref(prefs::kCloudPrintEmail,
381 new base::StringValue(std::string()));
382 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
383 new base::FundamentalValue(false));
385 service.Initialize();
387 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
388 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
389 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
392 TEST_F(CloudPrintProxyPolicyTest, StartWithNoPolicyProxyDisabledThenEnable) {
393 TestCloudPrintProxyService service(&profile_);
395 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
396 MockServiceProcessControl::kServiceStateDisabled, false);
398 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
399 prefs->SetUserPref(
400 prefs::kCloudPrintEmail,
401 new base::StringValue(MockServiceProcessControl::EnabledUserId()));
403 service.Initialize();
404 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
406 service.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
407 service.EnableForUser();
409 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
410 prefs->GetString(prefs::kCloudPrintEmail));
413 TEST_F(CloudPrintProxyPolicyTest,
414 StartWithPolicySetProxyEnabledThenClearPolicyAndEnable) {
415 TestCloudPrintProxyService service(&profile_);
417 service.GetMockServiceProcessControl()->SetConnectSuccessMockExpectations(
418 MockServiceProcessControl::kServiceStateEnabled, false);
419 service.GetMockServiceProcessControl()->SetWillBeDisabledExpectations();
421 TestingPrefServiceSyncable* prefs = profile_.GetTestingPrefService();
422 prefs->SetUserPref(prefs::kCloudPrintEmail,
423 new base::StringValue(std::string()));
424 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled,
425 new base::FundamentalValue(false));
427 service.Initialize();
429 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
430 service.EnableForUser();
431 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
433 prefs->RemoveManagedPref(prefs::kCloudPrintProxyEnabled);
434 EXPECT_EQ(std::string(), prefs->GetString(prefs::kCloudPrintEmail));
436 service.GetMockServiceProcessControl()->SetWillBeEnabledExpectations();
437 service.EnableForUser();
439 EXPECT_EQ(MockServiceProcessControl::EnabledUserId(),
440 prefs->GetString(prefs::kCloudPrintEmail));