| 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 <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "chrome/test/base/testing_browser_process.h" | 43 #include "chrome/test/base/testing_browser_process.h" |
| 44 #include "chrome/test/base/testing_io_thread_state.h" | 44 #include "chrome/test/base/testing_io_thread_state.h" |
| 45 #include "chrome/test/base/testing_profile.h" | 45 #include "chrome/test/base/testing_profile.h" |
| 46 #include "chrome/test/base/testing_profile_manager.h" | 46 #include "chrome/test/base/testing_profile_manager.h" |
| 47 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 47 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 48 #include "content/public/browser/notification_service.h" | 48 #include "content/public/browser/notification_service.h" |
| 49 #include "content/public/common/content_paths.h" | 49 #include "content/public/common/content_paths.h" |
| 50 #include "content/public/test/test_browser_thread_bundle.h" | 50 #include "content/public/test/test_browser_thread_bundle.h" |
| 51 #include "content/public/test/test_utils.h" | 51 #include "content/public/test/test_utils.h" |
| 52 #include "ipc/ipc_channel_mojo.h" | 52 #include "ipc/ipc_channel_mojo.h" |
| 53 #include "ipc/ipc_descriptors.h" | |
| 54 #include "ipc/ipc_multiprocess_test.h" | |
| 55 #include "mojo/edk/embedder/embedder.h" | 53 #include "mojo/edk/embedder/embedder.h" |
| 56 #include "mojo/edk/embedder/named_platform_handle.h" | 54 #include "mojo/edk/embedder/named_platform_handle.h" |
| 57 #include "mojo/edk/embedder/named_platform_handle_utils.h" | 55 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 58 #include "mojo/edk/embedder/scoped_ipc_support.h" | 56 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 59 #include "testing/gmock/include/gmock/gmock.h" | 57 #include "testing/gmock/include/gmock/gmock.h" |
| 60 #include "testing/gtest/include/gtest/gtest.h" | 58 #include "testing/gtest/include/gtest/gtest.h" |
| 61 #include "testing/multiprocess_func_list.h" | 59 #include "testing/multiprocess_func_list.h" |
| 62 | 60 |
| 63 #if defined(OS_MACOSX) | 61 #if defined(OS_MACOSX) |
| 64 #include "chrome/common/mac/mock_launchd.h" | 62 #include "chrome/common/mac/mock_launchd.h" |
| 65 #endif | 63 #endif |
| 66 #if defined(OS_POSIX) | |
| 67 #include "base/posix/global_descriptors.h" | |
| 68 #endif | |
| 69 | 64 |
| 70 using ::testing::AnyNumber; | 65 using ::testing::AnyNumber; |
| 71 using ::testing::Assign; | 66 using ::testing::Assign; |
| 72 using ::testing::AtLeast; | 67 using ::testing::AtLeast; |
| 73 using ::testing::DoAll; | 68 using ::testing::DoAll; |
| 74 using ::testing::Invoke; | 69 using ::testing::Invoke; |
| 75 using ::testing::Mock; | 70 using ::testing::Mock; |
| 76 using ::testing::Property; | 71 using ::testing::Property; |
| 77 using ::testing::Return; | 72 using ::testing::Return; |
| 78 using ::testing::WithoutArgs; | 73 using ::testing::WithoutArgs; |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 #endif | 281 #endif |
| 287 | 282 |
| 288 // Connect up the parent/child IPC channel to signal that the test can | 283 // Connect up the parent/child IPC channel to signal that the test can |
| 289 // continue. | 284 // continue. |
| 290 TestStartupClientChannelListener listener; | 285 TestStartupClientChannelListener listener; |
| 291 EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch( | 286 EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 292 kProcessChannelID)); | 287 kProcessChannelID)); |
| 293 std::string startup_channel_name = | 288 std::string startup_channel_name = |
| 294 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 289 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 295 kProcessChannelID); | 290 kProcessChannelID); |
| 296 std::unique_ptr<IPC::ChannelProxy> startup_channel; | 291 std::unique_ptr<IPC::ChannelProxy> startup_channel = |
| 297 startup_channel = | 292 IPC::ChannelProxy::Create( |
| 298 IPC::ChannelProxy::Create(startup_channel_name, | 293 mojo::edk::ConnectToPeerProcess( |
| 299 IPC::Channel::MODE_CLIENT, | 294 mojo::edk::CreateClientHandle( |
| 300 &listener, | 295 mojo::edk::NamedPlatformHandle(startup_channel_name))) |
| 301 service_process.io_task_runner()); | 296 .release(), |
| 297 IPC::Channel::MODE_CLIENT, &listener, |
| 298 service_process.io_task_runner()); |
| 302 | 299 |
| 303 base::RunLoop().Run(); | 300 base::RunLoop().Run(); |
| 304 if (!Mock::VerifyAndClearExpectations(&server)) | 301 if (!Mock::VerifyAndClearExpectations(&server)) |
| 305 return kExpectationsNotMet; | 302 return kExpectationsNotMet; |
| 306 if (!g_good_shutdown) | 303 if (!g_good_shutdown) |
| 307 return kShutdownNotGood; | 304 return kShutdownNotGood; |
| 308 return 0; | 305 return 0; |
| 309 } | 306 } |
| 310 | 307 |
| 311 void SetServiceEnabledExpectations(MockServiceIPCServer* server) { | 308 void SetServiceEnabledExpectations(MockServiceIPCServer* server) { |
| 312 server->SetServiceEnabledExpectations(); | 309 server->SetServiceEnabledExpectations(); |
| 313 } | 310 } |
| 314 | 311 |
| 315 MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { | 312 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { |
| 316 return CloudPrintMockService_Main( | 313 return CloudPrintMockService_Main( |
| 317 base::Bind(&SetServiceEnabledExpectations)); | 314 base::Bind(&SetServiceEnabledExpectations)); |
| 318 } | 315 } |
| 319 | 316 |
| 320 void SetServiceWillBeDisabledExpectations(MockServiceIPCServer* server) { | 317 void SetServiceWillBeDisabledExpectations(MockServiceIPCServer* server) { |
| 321 server->SetWillBeDisabledExpectations(); | 318 server->SetWillBeDisabledExpectations(); |
| 322 } | 319 } |
| 323 | 320 |
| 324 MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { | 321 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { |
| 325 return CloudPrintMockService_Main( | 322 return CloudPrintMockService_Main( |
| 326 base::Bind(&SetServiceWillBeDisabledExpectations)); | 323 base::Bind(&SetServiceWillBeDisabledExpectations)); |
| 327 } | 324 } |
| 328 | 325 |
| 329 class CloudPrintProxyPolicyStartupTest : public base::MultiProcessTest, | 326 class CloudPrintProxyPolicyStartupTest : public base::MultiProcessTest, |
| 330 public IPC::Listener { | 327 public IPC::Listener { |
| 331 public: | 328 public: |
| 332 CloudPrintProxyPolicyStartupTest(); | 329 CloudPrintProxyPolicyStartupTest(); |
| 333 ~CloudPrintProxyPolicyStartupTest() override; | 330 ~CloudPrintProxyPolicyStartupTest() override; |
| 334 | 331 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 354 StartupBrowserCreator browser_creator; | 351 StartupBrowserCreator browser_creator; |
| 355 return browser_creator.ProcessCmdLineImpl( | 352 return browser_creator.ProcessCmdLineImpl( |
| 356 command_line, base::FilePath(), false, profile, | 353 command_line, base::FilePath(), false, profile, |
| 357 StartupBrowserCreator::Profiles()); | 354 StartupBrowserCreator::Profiles()); |
| 358 } | 355 } |
| 359 | 356 |
| 360 protected: | 357 protected: |
| 361 content::TestBrowserThreadBundle thread_bundle_; | 358 content::TestBrowserThreadBundle thread_bundle_; |
| 362 base::ScopedTempDir temp_user_data_dir_; | 359 base::ScopedTempDir temp_user_data_dir_; |
| 363 | 360 |
| 364 std::string startup_channel_id_; | 361 mojo::edk::NamedPlatformHandle startup_channel_handle_; |
| 365 std::unique_ptr<IPC::ChannelProxy> startup_channel_; | 362 std::unique_ptr<IPC::ChannelProxy> startup_channel_; |
| 366 std::unique_ptr<ChromeContentClient> content_client_; | 363 std::unique_ptr<ChromeContentClient> content_client_; |
| 367 std::unique_ptr<ChromeContentBrowserClient> browser_content_client_; | 364 std::unique_ptr<ChromeContentBrowserClient> browser_content_client_; |
| 368 | 365 |
| 369 #if defined(OS_MACOSX) | 366 #if defined(OS_MACOSX) |
| 370 base::ScopedTempDir temp_dir_; | 367 base::ScopedTempDir temp_dir_; |
| 371 base::FilePath executable_path_, bundle_path_; | 368 base::FilePath executable_path_, bundle_path_; |
| 372 std::unique_ptr<MockLaunchd> mock_launchd_; | 369 std::unique_ptr<MockLaunchd> mock_launchd_; |
| 373 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; | 370 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; |
| 374 #endif | 371 #endif |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 content_client_.reset(); | 452 content_client_.reset(); |
| 456 content::SetContentClient(NULL); | 453 content::SetContentClient(NULL); |
| 457 | 454 |
| 458 TestingBrowserProcess::DeleteInstance(); | 455 TestingBrowserProcess::DeleteInstance(); |
| 459 } | 456 } |
| 460 | 457 |
| 461 base::Process CloudPrintProxyPolicyStartupTest::Launch( | 458 base::Process CloudPrintProxyPolicyStartupTest::Launch( |
| 462 const std::string& name) { | 459 const std::string& name) { |
| 463 EXPECT_FALSE(CheckServiceProcessReady()); | 460 EXPECT_FALSE(CheckServiceProcessReady()); |
| 464 | 461 |
| 465 startup_channel_id_ = | 462 startup_channel_handle_ = mojo::edk::NamedPlatformHandle( |
| 466 base::StringPrintf("%d.%p.%d", | 463 base::StringPrintf("%d.%p.%d", base::GetCurrentProcId(), this, |
| 467 base::GetCurrentProcId(), this, | 464 base::RandInt(0, std::numeric_limits<int>::max()))); |
| 468 base::RandInt(0, std::numeric_limits<int>::max())); | 465 startup_channel_ = IPC::ChannelProxy::Create( |
| 469 startup_channel_ = IPC::ChannelProxy::Create(startup_channel_id_, | 466 mojo::edk::ConnectToPeerProcess( |
| 470 IPC::Channel::MODE_SERVER, | 467 mojo::edk::CreateServerHandle(startup_channel_handle_, false)) |
| 471 this, | 468 .release(), |
| 472 IOTaskRunner()); | 469 IPC::Channel::MODE_SERVER, this, IOTaskRunner()); |
| 473 | 470 |
| 474 #if defined(OS_POSIX) | |
| 475 base::FileHandleMappingVector ipc_file_list; | |
| 476 ipc_file_list.push_back(std::make_pair( | |
| 477 startup_channel_->TakeClientFileDescriptor().release(), | |
| 478 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | |
| 479 base::LaunchOptions options; | |
| 480 options.fds_to_remap = &ipc_file_list; | |
| 481 base::Process process = SpawnChildWithOptions(name, options); | |
| 482 #else | |
| 483 base::Process process = SpawnChild(name); | 471 base::Process process = SpawnChild(name); |
| 484 #endif | |
| 485 EXPECT_TRUE(process.IsValid()); | 472 EXPECT_TRUE(process.IsValid()); |
| 486 return process; | 473 return process; |
| 487 } | 474 } |
| 488 | 475 |
| 489 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { | 476 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { |
| 490 observer_.Wait(); | 477 observer_.Wait(); |
| 491 EXPECT_TRUE(CheckServiceProcessReady()); | 478 EXPECT_TRUE(CheckServiceProcessReady()); |
| 492 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); | 479 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); |
| 493 | 480 |
| 494 mojo::MessagePipe pipe; | 481 mojo::MessagePipe pipe; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 517 EXPECT_EQ(exit_code, 0); | 504 EXPECT_EQ(exit_code, 0); |
| 518 } | 505 } |
| 519 | 506 |
| 520 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32_t peer_pid) { | 507 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32_t peer_pid) { |
| 521 observer_.Notify(); | 508 observer_.Notify(); |
| 522 } | 509 } |
| 523 | 510 |
| 524 base::CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( | 511 base::CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( |
| 525 const std::string& procname) { | 512 const std::string& procname) { |
| 526 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); | 513 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); |
| 527 cl.AppendSwitchASCII(kProcessChannelID, startup_channel_id_); | 514 cl.AppendSwitchNative(kProcessChannelID, startup_channel_handle_.name); |
| 528 #if defined(OS_MACOSX) | 515 #if defined(OS_MACOSX) |
| 529 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); | 516 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); |
| 530 #endif | 517 #endif |
| 531 return cl; | 518 return cl; |
| 532 } | 519 } |
| 533 | 520 |
| 534 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { | 521 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { |
| 535 mojo::edk::Init(); | 522 mojo::edk::Init(); |
| 536 mojo::edk::ScopedIPCSupport ipc_support( | 523 mojo::edk::ScopedIPCSupport ipc_support( |
| 537 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) | 524 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) |
| 538 ->task_runner()); | 525 ->task_runner()); |
| 539 | 526 |
| 540 TestingBrowserProcess* browser_process = | 527 TestingBrowserProcess* browser_process = |
| 541 TestingBrowserProcess::GetGlobal(); | 528 TestingBrowserProcess::GetGlobal(); |
| 542 TestingProfileManager profile_manager(browser_process); | 529 TestingProfileManager profile_manager(browser_process); |
| 543 ASSERT_TRUE(profile_manager.SetUp()); | 530 ASSERT_TRUE(profile_manager.SetUp()); |
| 544 | 531 |
| 545 // Must be created after the TestingProfileManager since that creates the | 532 // Must be created after the TestingProfileManager since that creates the |
| 546 // LocalState for the BrowserProcess. Must be created before profiles are | 533 // LocalState for the BrowserProcess. Must be created before profiles are |
| 547 // constructed. | 534 // constructed. |
| 548 chrome::TestingIOThreadState testing_io_thread_state; | 535 chrome::TestingIOThreadState testing_io_thread_state; |
| 549 | 536 |
| 550 base::Process process = | 537 base::Process process = |
| 551 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); | 538 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); |
| 552 WaitForConnect(); | 539 WaitForConnect(); |
| 553 ShutdownAndWaitForExitWithTimeout(std::move(process)); | 540 ShutdownAndWaitForExitWithTimeout(std::move(process)); |
| 554 ServiceProcessControl::GetInstance()->Disconnect(); | 541 ServiceProcessControl::GetInstance()->Disconnect(); |
| 555 content::RunAllPendingInMessageLoop(); | 542 content::RunAllPendingInMessageLoop(); |
| 556 } | 543 } |
| OLD | NEW |