| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 205 |
| 206 typedef base::Callback<void(MockServiceIPCServer* server)> | 206 typedef base::Callback<void(MockServiceIPCServer* server)> |
| 207 SetExpectationsCallback; | 207 SetExpectationsCallback; |
| 208 | 208 |
| 209 // The return value from this routine is used as the exit code for the mock | 209 // The return value from this routine is used as the exit code for the mock |
| 210 // service process. Any non-zero return value will be printed out and can help | 210 // service process. Any non-zero return value will be printed out and can help |
| 211 // determine the failure. | 211 // determine the failure. |
| 212 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { | 212 int CloudPrintMockService_Main(SetExpectationsCallback set_expectations) { |
| 213 base::MessageLoopForUI main_message_loop; | 213 base::MessageLoopForUI main_message_loop; |
| 214 main_message_loop.set_thread_name("Main Thread"); | 214 main_message_loop.set_thread_name("Main Thread"); |
| 215 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 215 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 216 content::RegisterPathProvider(); | 216 content::RegisterPathProvider(); |
| 217 | 217 |
| 218 #if defined(OS_MACOSX) | 218 #if defined(OS_MACOSX) |
| 219 if (!command_line->HasSwitch(kTestExecutablePath)) | 219 if (!command_line->HasSwitch(kTestExecutablePath)) |
| 220 return kMissingSwitch; | 220 return kMissingSwitch; |
| 221 base::FilePath executable_path = | 221 base::FilePath executable_path = |
| 222 command_line->GetSwitchValuePath(kTestExecutablePath); | 222 command_line->GetSwitchValuePath(kTestExecutablePath); |
| 223 EXPECT_FALSE(executable_path.empty()); | 223 EXPECT_FALSE(executable_path.empty()); |
| 224 MockLaunchd mock_launchd(executable_path, &main_message_loop, true, true); | 224 MockLaunchd mock_launchd(executable_path, &main_message_loop, true, true); |
| 225 Launchd::ScopedInstance use_mock(&mock_launchd); | 225 Launchd::ScopedInstance use_mock(&mock_launchd); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 251 EXPECT_TRUE(server.Init()); | 251 EXPECT_TRUE(server.Init()); |
| 252 EXPECT_TRUE(state->SignalReady(service_process.IOMessageLoopProxy(), | 252 EXPECT_TRUE(state->SignalReady(service_process.IOMessageLoopProxy(), |
| 253 base::Bind(&ShutdownTask))); | 253 base::Bind(&ShutdownTask))); |
| 254 #if defined(OS_MACOSX) | 254 #if defined(OS_MACOSX) |
| 255 mock_launchd.SignalReady(); | 255 mock_launchd.SignalReady(); |
| 256 #endif | 256 #endif |
| 257 | 257 |
| 258 // Connect up the parent/child IPC channel to signal that the test can | 258 // Connect up the parent/child IPC channel to signal that the test can |
| 259 // continue. | 259 // continue. |
| 260 TestStartupClientChannelListener listener; | 260 TestStartupClientChannelListener listener; |
| 261 EXPECT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch( | 261 EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 262 switches::kProcessChannelID)); | 262 switches::kProcessChannelID)); |
| 263 std::string startup_channel_name = | 263 std::string startup_channel_name = |
| 264 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 264 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 265 switches::kProcessChannelID); | 265 switches::kProcessChannelID); |
| 266 scoped_ptr<IPC::ChannelProxy> startup_channel; | 266 scoped_ptr<IPC::ChannelProxy> startup_channel; |
| 267 startup_channel = | 267 startup_channel = |
| 268 IPC::ChannelProxy::Create(startup_channel_name, | 268 IPC::ChannelProxy::Create(startup_channel_name, |
| 269 IPC::Channel::MODE_CLIENT, | 269 IPC::Channel::MODE_CLIENT, |
| 270 &listener, | 270 &listener, |
| 271 service_process.IOMessageLoopProxy()); | 271 service_process.IOMessageLoopProxy()); |
| 272 | 272 |
| 273 main_message_loop.Run(); | 273 main_message_loop.Run(); |
| 274 if (!Mock::VerifyAndClearExpectations(&server)) | 274 if (!Mock::VerifyAndClearExpectations(&server)) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 base::ProcessHandle Launch(const std::string& name); | 311 base::ProcessHandle Launch(const std::string& name); |
| 312 void WaitForConnect(); | 312 void WaitForConnect(); |
| 313 bool Send(IPC::Message* message); | 313 bool Send(IPC::Message* message); |
| 314 void ShutdownAndWaitForExitWithTimeout(base::ProcessHandle handle); | 314 void ShutdownAndWaitForExitWithTimeout(base::ProcessHandle handle); |
| 315 | 315 |
| 316 // IPC::Listener implementation | 316 // IPC::Listener implementation |
| 317 bool OnMessageReceived(const IPC::Message& message) override { return false; } | 317 bool OnMessageReceived(const IPC::Message& message) override { return false; } |
| 318 void OnChannelConnected(int32 peer_pid) override; | 318 void OnChannelConnected(int32 peer_pid) override; |
| 319 | 319 |
| 320 // MultiProcessTest implementation. | 320 // MultiProcessTest implementation. |
| 321 CommandLine MakeCmdLine(const std::string& procname) override; | 321 base::CommandLine MakeCmdLine(const std::string& procname) override; |
| 322 | 322 |
| 323 bool LaunchBrowser(const CommandLine& command_line, Profile* profile) { | 323 bool LaunchBrowser(const base::CommandLine& command_line, Profile* profile) { |
| 324 int return_code = 0; | 324 int return_code = 0; |
| 325 StartupBrowserCreator browser_creator; | 325 StartupBrowserCreator browser_creator; |
| 326 return StartupBrowserCreator::ProcessCmdLineImpl( | 326 return StartupBrowserCreator::ProcessCmdLineImpl( |
| 327 command_line, base::FilePath(), false, profile, | 327 command_line, base::FilePath(), false, profile, |
| 328 StartupBrowserCreator::Profiles(), &return_code, &browser_creator); | 328 StartupBrowserCreator::Profiles(), &return_code, &browser_creator); |
| 329 } | 329 } |
| 330 | 330 |
| 331 protected: | 331 protected: |
| 332 content::TestBrowserThreadBundle thread_bundle_; | 332 content::TestBrowserThreadBundle thread_bundle_; |
| 333 base::ScopedTempDir temp_user_data_dir_; | 333 base::ScopedTempDir temp_user_data_dir_; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 base::MessageLoopForUI::current(), | 400 base::MessageLoopForUI::current(), |
| 401 true, false)); | 401 true, false)); |
| 402 scoped_launchd_instance_.reset( | 402 scoped_launchd_instance_.reset( |
| 403 new Launchd::ScopedInstance(mock_launchd_.get())); | 403 new Launchd::ScopedInstance(mock_launchd_.get())); |
| 404 #endif | 404 #endif |
| 405 | 405 |
| 406 // Ensure test does not use the standard profile directory. This is copied | 406 // Ensure test does not use the standard profile directory. This is copied |
| 407 // from InProcessBrowserTest::SetUp(). These tests require a more complex | 407 // from InProcessBrowserTest::SetUp(). These tests require a more complex |
| 408 // process startup so they are unable to just inherit from | 408 // process startup so they are unable to just inherit from |
| 409 // InProcessBrowserTest. | 409 // InProcessBrowserTest. |
| 410 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 410 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 411 base::FilePath user_data_dir = | 411 base::FilePath user_data_dir = |
| 412 command_line->GetSwitchValuePath(switches::kUserDataDir); | 412 command_line->GetSwitchValuePath(switches::kUserDataDir); |
| 413 if (user_data_dir.empty()) { | 413 if (user_data_dir.empty()) { |
| 414 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir() && | 414 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir() && |
| 415 temp_user_data_dir_.IsValid()) | 415 temp_user_data_dir_.IsValid()) |
| 416 << "Could not create temporary user data directory \"" | 416 << "Could not create temporary user data directory \"" |
| 417 << temp_user_data_dir_.path().value() << "\"."; | 417 << temp_user_data_dir_.path().value() << "\"."; |
| 418 | 418 |
| 419 user_data_dir = temp_user_data_dir_.path(); | 419 user_data_dir = temp_user_data_dir_.path(); |
| 420 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir); | 420 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 TestTimeouts::action_timeout()); | 483 TestTimeouts::action_timeout()); |
| 484 EXPECT_TRUE(exited); | 484 EXPECT_TRUE(exited); |
| 485 EXPECT_EQ(exit_code, 0); | 485 EXPECT_EQ(exit_code, 0); |
| 486 base::CloseProcessHandle(handle); | 486 base::CloseProcessHandle(handle); |
| 487 } | 487 } |
| 488 | 488 |
| 489 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32 peer_pid) { | 489 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32 peer_pid) { |
| 490 observer_.Notify(); | 490 observer_.Notify(); |
| 491 } | 491 } |
| 492 | 492 |
| 493 CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( | 493 base::CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( |
| 494 const std::string& procname) { | 494 const std::string& procname) { |
| 495 CommandLine cl = MultiProcessTest::MakeCmdLine(procname); | 495 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); |
| 496 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_); | 496 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_); |
| 497 #if defined(OS_MACOSX) | 497 #if defined(OS_MACOSX) |
| 498 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); | 498 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); |
| 499 #endif | 499 #endif |
| 500 return cl; | 500 return cl; |
| 501 } | 501 } |
| 502 | 502 |
| 503 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { | 503 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { |
| 504 TestingBrowserProcess* browser_process = | 504 TestingBrowserProcess* browser_process = |
| 505 TestingBrowserProcess::GetGlobal(); | 505 TestingBrowserProcess::GetGlobal(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 TestingProfile* profile = | 545 TestingProfile* profile = |
| 546 profile_manager.CreateTestingProfile("StartBrowserWithoutPolicy"); | 546 profile_manager.CreateTestingProfile("StartBrowserWithoutPolicy"); |
| 547 CloudPrintProxyServiceFactory::GetInstance()-> | 547 CloudPrintProxyServiceFactory::GetInstance()-> |
| 548 SetTestingFactory(profile, CloudPrintProxyServiceFactoryForPolicyTest); | 548 SetTestingFactory(profile, CloudPrintProxyServiceFactoryForPolicyTest); |
| 549 | 549 |
| 550 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); | 550 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); |
| 551 prefs->SetUserPref( | 551 prefs->SetUserPref( |
| 552 prefs::kCloudPrintEmail, | 552 prefs::kCloudPrintEmail, |
| 553 new base::StringValue(MockServiceIPCServer::EnabledUserId())); | 553 new base::StringValue(MockServiceIPCServer::EnabledUserId())); |
| 554 | 554 |
| 555 CommandLine command_line(CommandLine::NO_PROGRAM); | 555 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 556 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); | 556 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); |
| 557 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); | 557 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); |
| 558 | 558 |
| 559 WaitForConnect(); | 559 WaitForConnect(); |
| 560 base::RunLoop run_loop; | 560 base::RunLoop run_loop; |
| 561 base::MessageLoop::current()->PostDelayedTask( | 561 base::MessageLoop::current()->PostDelayedTask( |
| 562 FROM_HERE, | 562 FROM_HERE, |
| 563 run_loop.QuitClosure(), | 563 run_loop.QuitClosure(), |
| 564 TestTimeouts::action_timeout()); | 564 TestTimeouts::action_timeout()); |
| 565 | 565 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 595 CloudPrintProxyServiceFactory::GetInstance()-> | 595 CloudPrintProxyServiceFactory::GetInstance()-> |
| 596 SetTestingFactory(profile, CloudPrintProxyServiceFactoryForPolicyTest); | 596 SetTestingFactory(profile, CloudPrintProxyServiceFactoryForPolicyTest); |
| 597 | 597 |
| 598 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); | 598 TestingPrefServiceSyncable* prefs = profile->GetTestingPrefService(); |
| 599 prefs->SetUserPref( | 599 prefs->SetUserPref( |
| 600 prefs::kCloudPrintEmail, | 600 prefs::kCloudPrintEmail, |
| 601 new base::StringValue(MockServiceIPCServer::EnabledUserId())); | 601 new base::StringValue(MockServiceIPCServer::EnabledUserId())); |
| 602 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, | 602 prefs->SetManagedPref(prefs::kCloudPrintProxyEnabled, |
| 603 new base::FundamentalValue(false)); | 603 new base::FundamentalValue(false)); |
| 604 | 604 |
| 605 CommandLine command_line(CommandLine::NO_PROGRAM); | 605 base::CommandLine command_line(base::CommandLine::NO_PROGRAM); |
| 606 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); | 606 command_line.AppendSwitch(switches::kCheckCloudPrintConnectorPolicy); |
| 607 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); | 607 test_launcher_utils::PrepareBrowserCommandLineForTests(&command_line); |
| 608 | 608 |
| 609 WaitForConnect(); | 609 WaitForConnect(); |
| 610 base::RunLoop run_loop; | 610 base::RunLoop run_loop; |
| 611 base::MessageLoop::current()->PostDelayedTask( | 611 base::MessageLoop::current()->PostDelayedTask( |
| 612 FROM_HERE, | 612 FROM_HERE, |
| 613 run_loop.QuitClosure(), | 613 run_loop.QuitClosure(), |
| 614 TestTimeouts::action_timeout()); | 614 TestTimeouts::action_timeout()); |
| 615 | 615 |
| 616 bool should_run_loop = LaunchBrowser(command_line, profile); | 616 bool should_run_loop = LaunchBrowser(command_line, profile); |
| 617 | 617 |
| 618 // No expectations on run_loop being true here; that would be a race | 618 // No expectations on run_loop being true here; that would be a race |
| 619 // condition. | 619 // condition. |
| 620 if (should_run_loop) | 620 if (should_run_loop) |
| 621 run_loop.Run(); | 621 run_loop.Run(); |
| 622 | 622 |
| 623 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail)); | 623 EXPECT_EQ("", prefs->GetString(prefs::kCloudPrintEmail)); |
| 624 | 624 |
| 625 ShutdownAndWaitForExitWithTimeout(handle); | 625 ShutdownAndWaitForExitWithTimeout(handle); |
| 626 content::RunAllPendingInMessageLoop(); | 626 content::RunAllPendingInMessageLoop(); |
| 627 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy"); | 627 profile_manager.DeleteTestingProfile("StartBrowserWithPolicy"); |
| 628 } | 628 } |
| OLD | NEW |