OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Create a service process that uses a Mock to respond to the browser in order | 5 // Create a service process that uses a Mock to respond to the browser in order |
6 // to test launching the browser using the cloud print policy check command | 6 // to test launching the browser using the cloud print policy check command |
7 // line switch. | 7 // line switch. |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 85 } |
86 }; | 86 }; |
87 | 87 |
88 } // namespace | 88 } // namespace |
89 | 89 |
90 class TestServiceProcess : public ServiceProcess { | 90 class TestServiceProcess : public ServiceProcess { |
91 public: | 91 public: |
92 TestServiceProcess() { } | 92 TestServiceProcess() { } |
93 virtual ~TestServiceProcess() { } | 93 virtual ~TestServiceProcess() { } |
94 | 94 |
95 bool Initialize(MessageLoopForUI* message_loop, ServiceProcessState* state); | 95 bool Initialize(base::MessageLoopForUI* message_loop, |
| 96 ServiceProcessState* state); |
96 | 97 |
97 base::MessageLoopProxy* IOMessageLoopProxy() { | 98 base::MessageLoopProxy* IOMessageLoopProxy() { |
98 return io_thread_->message_loop_proxy(); | 99 return io_thread_->message_loop_proxy(); |
99 } | 100 } |
100 }; | 101 }; |
101 | 102 |
102 bool TestServiceProcess::Initialize(MessageLoopForUI* message_loop, | 103 bool TestServiceProcess::Initialize(base::MessageLoopForUI* message_loop, |
103 ServiceProcessState* state) { | 104 ServiceProcessState* state) { |
104 main_message_loop_ = message_loop; | 105 main_message_loop_ = message_loop; |
105 | 106 |
106 service_process_state_.reset(state); | 107 service_process_state_.reset(state); |
107 | 108 |
108 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 109 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
109 io_thread_.reset(new base::Thread("TestServiceProcess_IO")); | 110 io_thread_.reset(new base::Thread("TestServiceProcess_IO")); |
110 return io_thread_->StartWithOptions(options); | 111 return io_thread_->StartWithOptions(options); |
111 } | 112 } |
112 | 113 |
113 // This mocks the service side IPC message handler, allowing us to have a | 114 // This mocks the service side IPC message handler, allowing us to have a |
114 // minimal service process. | 115 // minimal service process. |
115 class MockServiceIPCServer : public ServiceIPCServer { | 116 class MockServiceIPCServer : public ServiceIPCServer { |
116 public: | 117 public: |
117 static std::string EnabledUserId(); | 118 static std::string EnabledUserId(); |
118 | 119 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return true; | 196 return true; |
196 } | 197 } |
197 | 198 |
198 typedef base::Callback<void(MockServiceIPCServer* server)> | 199 typedef base::Callback<void(MockServiceIPCServer* server)> |
199 SetExpectationsCallback; | 200 SetExpectationsCallback; |
200 | 201 |
201 // The return value from this routine is used as the exit code for the mock | 202 // The return value from this routine is used as the exit code for the mock |
202 // service process. Any non-zero return value will be printed out and can help | 203 // service process. Any non-zero return value will be printed out and can help |
203 // determine the failure. | 204 // determine the failure. |
204 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { | 205 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { |
205 MessageLoopForUI main_message_loop; | 206 base::MessageLoopForUI main_message_loop; |
206 main_message_loop.set_thread_name("Main Thread"); | 207 main_message_loop.set_thread_name("Main Thread"); |
207 | 208 |
208 #if defined(OS_MACOSX) | 209 #if defined(OS_MACOSX) |
209 CommandLine* cl = CommandLine::ForCurrentProcess(); | 210 CommandLine* cl = CommandLine::ForCurrentProcess(); |
210 if (!cl->HasSwitch(kTestExecutablePath)) | 211 if (!cl->HasSwitch(kTestExecutablePath)) |
211 return kMissingSwitch; | 212 return kMissingSwitch; |
212 base::FilePath executable_path = cl->GetSwitchValuePath(kTestExecutablePath); | 213 base::FilePath executable_path = cl->GetSwitchValuePath(kTestExecutablePath); |
213 EXPECT_FALSE(executable_path.empty()); | 214 EXPECT_FALSE(executable_path.empty()); |
214 MockLaunchd mock_launchd(executable_path, &main_message_loop, true, true); | 215 MockLaunchd mock_launchd(executable_path, &main_message_loop, true, true); |
215 Launchd::ScopedInstance use_mock(&mock_launchd); | 216 Launchd::ScopedInstance use_mock(&mock_launchd); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 309 |
309 bool LaunchBrowser(const CommandLine& command_line, Profile* profile) { | 310 bool LaunchBrowser(const CommandLine& command_line, Profile* profile) { |
310 int return_code = 0; | 311 int return_code = 0; |
311 StartupBrowserCreator browser_creator; | 312 StartupBrowserCreator browser_creator; |
312 return StartupBrowserCreator::ProcessCmdLineImpl( | 313 return StartupBrowserCreator::ProcessCmdLineImpl( |
313 command_line, base::FilePath(), false, profile, | 314 command_line, base::FilePath(), false, profile, |
314 StartupBrowserCreator::Profiles(), &return_code, &browser_creator); | 315 StartupBrowserCreator::Profiles(), &return_code, &browser_creator); |
315 } | 316 } |
316 | 317 |
317 protected: | 318 protected: |
318 MessageLoopForUI message_loop_; | 319 base::MessageLoopForUI message_loop_; |
319 content::TestBrowserThread ui_thread_; | 320 content::TestBrowserThread ui_thread_; |
320 base::Thread io_thread_; | 321 base::Thread io_thread_; |
321 | 322 |
322 std::string startup_channel_id_; | 323 std::string startup_channel_id_; |
323 scoped_ptr<IPC::ChannelProxy> startup_channel_; | 324 scoped_ptr<IPC::ChannelProxy> startup_channel_; |
324 | 325 |
325 #if defined(OS_MACOSX) | 326 #if defined(OS_MACOSX) |
326 base::ScopedTempDir temp_dir_; | 327 base::ScopedTempDir temp_dir_; |
327 base::FilePath executable_path_, bundle_path_; | 328 base::FilePath executable_path_, bundle_path_; |
328 scoped_ptr<MockLaunchd> mock_launchd_; | 329 scoped_ptr<MockLaunchd> mock_launchd_; |
(...skipping 10 matching lines...) Expand all Loading... |
339 void Wait() { | 340 void Wait() { |
340 if (seen_) | 341 if (seen_) |
341 return; | 342 return; |
342 running_ = true; | 343 running_ = true; |
343 content::RunMessageLoop(); | 344 content::RunMessageLoop(); |
344 } | 345 } |
345 | 346 |
346 void Notify() { | 347 void Notify() { |
347 seen_ = true; | 348 seen_ = true; |
348 if (running_) | 349 if (running_) |
349 MessageLoopForUI::current()->Quit(); | 350 base::MessageLoopForUI::current()->Quit(); |
350 } | 351 } |
351 | 352 |
352 private: | 353 private: |
353 bool seen_; | 354 bool seen_; |
354 bool running_; | 355 bool running_; |
355 }; | 356 }; |
356 | 357 |
357 WindowedChannelConnectionObserver observer_; | 358 WindowedChannelConnectionObserver observer_; |
358 }; | 359 }; |
359 | 360 |
360 CloudPrintProxyPolicyStartupTest::CloudPrintProxyPolicyStartupTest() | 361 CloudPrintProxyPolicyStartupTest::CloudPrintProxyPolicyStartupTest() |
361 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 362 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
362 io_thread_("CloudPrintProxyPolicyTestThread") { | 363 io_thread_("CloudPrintProxyPolicyTestThread") { |
363 } | 364 } |
364 | 365 |
365 CloudPrintProxyPolicyStartupTest::~CloudPrintProxyPolicyStartupTest() { | 366 CloudPrintProxyPolicyStartupTest::~CloudPrintProxyPolicyStartupTest() { |
366 } | 367 } |
367 | 368 |
368 void CloudPrintProxyPolicyStartupTest::SetUp() { | 369 void CloudPrintProxyPolicyStartupTest::SetUp() { |
369 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 370 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
370 ASSERT_TRUE(io_thread_.StartWithOptions(options)); | 371 ASSERT_TRUE(io_thread_.StartWithOptions(options)); |
371 | 372 |
372 #if defined(OS_MACOSX) | 373 #if defined(OS_MACOSX) |
373 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 374 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
374 EXPECT_TRUE(MockLaunchd::MakeABundle(temp_dir_.path(), | 375 EXPECT_TRUE(MockLaunchd::MakeABundle(temp_dir_.path(), |
375 "CloudPrintProxyTest", | 376 "CloudPrintProxyTest", |
376 &bundle_path_, | 377 &bundle_path_, |
377 &executable_path_)); | 378 &executable_path_)); |
378 mock_launchd_.reset(new MockLaunchd(executable_path_, &message_loop_, | 379 mock_launchd_.reset(new MockLaunchd(executable_path_, &message_loop_, |
379 true, false)); | 380 true, false)); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); | 481 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); |
481 prefs->SetUserPref(prefs::kCloudPrintEmail, | 482 prefs->SetUserPref(prefs::kCloudPrintEmail, |
482 Value::CreateStringValue( | 483 Value::CreateStringValue( |
483 MockServiceIPCServer::EnabledUserId())); | 484 MockServiceIPCServer::EnabledUserId())); |
484 | 485 |
485 CommandLine command_line(CommandLine::NO_PROGRAM); | 486 CommandLine command_line(CommandLine::NO_PROGRAM); |
486 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); | 487 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); |
487 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); | 488 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); |
488 | 489 |
489 WaitForConnect(); | 490 WaitForConnect(); |
490 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 491 base::MessageLoop::current()->PostDelayedTask( |
491 MessageLoop::QuitClosure(), | 492 FROM_HERE, |
492 TestTimeouts::action_timeout()); | 493 base::MessageLoop::QuitClosure(), |
| 494 TestTimeouts::action_timeout()); |
493 | 495 |
494 bool run_loop = LaunchBrowser(command_line, profile); | 496 bool run_loop = LaunchBrowser(command_line, profile); |
495 EXPECT_FALSE(run_loop); | 497 EXPECT_FALSE(run_loop); |
496 if (run_loop) | 498 if (run_loop) |
497 MessageLoop::current()->Run(); | 499 base::MessageLoop::current()->Run(); |
498 | 500 |
499 EXPECT_EQ(MockServiceIPCServer::EnabledUserId(), | 501 EXPECT_EQ(MockServiceIPCServer::EnabledUserId(), |
500 prefs->GetString(prefs::kCloudPrintEmail)); | 502 prefs->GetString(prefs::kCloudPrintEmail)); |
501 | 503 |
502 ShutdownAndWaitForExitWithTimeout(handle); | 504 ShutdownAndWaitForExitWithTimeout(handle); |
503 profile_manager.DeleteTestingProfile("StartBrowserWithoutPolicy"); | 505 profile_manager.DeleteTestingProfile("StartBrowserWithoutPolicy"); |
504 } | 506 } |
505 | 507 |
506 TEST_F(CloudPrintProxyPolicyStartupTest, StartBrowserWithPolicy) { | 508 TEST_F(CloudPrintProxyPolicyStartupTest, StartBrowserWithPolicy) { |
507 base::ProcessHandle handle = | 509 base::ProcessHandle handle = |
(...skipping 14 matching lines...) Expand all Loading... |
522 Value::CreateStringValue( | 524 Value::CreateStringValue( |
523 MockServiceIPCServer::EnabledUserId())); | 525 MockServiceIPCServer::EnabledUserId())); |
524 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | 526 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
525 Value::CreateBooleanValue(false)); | 527 Value::CreateBooleanValue(false)); |
526 | 528 |
527 CommandLine command_line(CommandLine::NO_PROGRAM); | 529 CommandLine command_line(CommandLine::NO_PROGRAM); |
528 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); | 530 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); |
529 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); | 531 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); |
530 | 532 |
531 WaitForConnect(); | 533 WaitForConnect(); |
532 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 534 base::MessageLoop::current()->PostDelayedTask( |
533 MessageLoop::QuitClosure(), | 535 FROM_HERE, |
534 TestTimeouts::action_timeout()); | 536 base::MessageLoop::QuitClosure(), |
| 537 TestTimeouts::action_timeout()); |
535 | 538 |
536 bool run_loop = LaunchBrowser(command_line, profile); | 539 bool run_loop = LaunchBrowser(command_line, profile); |
537 | 540 |
538 // No expectations on run_loop being true here; that would be a race | 541 // No expectations on run_loop being true here; that would be a race |
539 // condition. | 542 // condition. |
540 if (run_loop) | 543 if (run_loop) |
541 MessageLoop::current()->Run(); | 544 base::MessageLoop::current()->Run(); |
542 | 545 |
543 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail)); | 546 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail)); |
544 | 547 |
545 ShutdownAndWaitForExitWithTimeout(handle); | 548 ShutdownAndWaitForExitWithTimeout(handle); |
546 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy"); | 549 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy"); |
547 } | 550 } |
OLD | NEW |