Chromium Code Reviews| 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 startup_channel = IPC::ChannelProxy::Create( |
|
Lei Zhang
2016/10/12 07:34:54
Can this be combined with the previous line?
Sam McNally
2016/10/12 08:25:48
Done.
| |
| 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, service_process.io_task_runner()); | |
| 302 | 298 |
| 303 base::RunLoop().Run(); | 299 base::RunLoop().Run(); |
| 304 if (!Mock::VerifyAndClearExpectations(&server)) | 300 if (!Mock::VerifyAndClearExpectations(&server)) |
| 305 return kExpectationsNotMet; | 301 return kExpectationsNotMet; |
| 306 if (!g_good_shutdown) | 302 if (!g_good_shutdown) |
| 307 return kShutdownNotGood; | 303 return kShutdownNotGood; |
| 308 return 0; | 304 return 0; |
| 309 } | 305 } |
| 310 | 306 |
| 311 void SetServiceEnabledExpectations(MockServiceIPCServer* server) { | 307 void SetServiceEnabledExpectations(MockServiceIPCServer* server) { |
| 312 server->SetServiceEnabledExpectations(); | 308 server->SetServiceEnabledExpectations(); |
| 313 } | 309 } |
| 314 | 310 |
| 315 MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { | 311 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledWaitForQuit) { |
| 316 return CloudPrintMockService_Main( | 312 return CloudPrintMockService_Main( |
| 317 base::Bind(&SetServiceEnabledExpectations)); | 313 base::Bind(&SetServiceEnabledExpectations)); |
| 318 } | 314 } |
| 319 | 315 |
| 320 void SetServiceWillBeDisabledExpectations(MockServiceIPCServer* server) { | 316 void SetServiceWillBeDisabledExpectations(MockServiceIPCServer* server) { |
| 321 server->SetWillBeDisabledExpectations(); | 317 server->SetWillBeDisabledExpectations(); |
| 322 } | 318 } |
| 323 | 319 |
| 324 MULTIPROCESS_IPC_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { | 320 MULTIPROCESS_TEST_MAIN(CloudPrintMockService_StartEnabledExpectDisabled) { |
| 325 return CloudPrintMockService_Main( | 321 return CloudPrintMockService_Main( |
| 326 base::Bind(&SetServiceWillBeDisabledExpectations)); | 322 base::Bind(&SetServiceWillBeDisabledExpectations)); |
| 327 } | 323 } |
| 328 | 324 |
| 329 class CloudPrintProxyPolicyStartupTest : public base::MultiProcessTest, | 325 class CloudPrintProxyPolicyStartupTest : public base::MultiProcessTest, |
| 330 public IPC::Listener { | 326 public IPC::Listener { |
| 331 public: | 327 public: |
| 332 CloudPrintProxyPolicyStartupTest(); | 328 CloudPrintProxyPolicyStartupTest(); |
| 333 ~CloudPrintProxyPolicyStartupTest() override; | 329 ~CloudPrintProxyPolicyStartupTest() override; |
| 334 | 330 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 354 StartupBrowserCreator browser_creator; | 350 StartupBrowserCreator browser_creator; |
| 355 return browser_creator.ProcessCmdLineImpl( | 351 return browser_creator.ProcessCmdLineImpl( |
| 356 command_line, base::FilePath(), false, profile, | 352 command_line, base::FilePath(), false, profile, |
| 357 StartupBrowserCreator::Profiles()); | 353 StartupBrowserCreator::Profiles()); |
| 358 } | 354 } |
| 359 | 355 |
| 360 protected: | 356 protected: |
| 361 content::TestBrowserThreadBundle thread_bundle_; | 357 content::TestBrowserThreadBundle thread_bundle_; |
| 362 base::ScopedTempDir temp_user_data_dir_; | 358 base::ScopedTempDir temp_user_data_dir_; |
| 363 | 359 |
| 364 std::string startup_channel_id_; | 360 mojo::edk::NamedPlatformHandle startup_channel_handle_; |
| 365 std::unique_ptr<IPC::ChannelProxy> startup_channel_; | 361 std::unique_ptr<IPC::ChannelProxy> startup_channel_; |
| 366 std::unique_ptr<ChromeContentClient> content_client_; | 362 std::unique_ptr<ChromeContentClient> content_client_; |
| 367 std::unique_ptr<ChromeContentBrowserClient> browser_content_client_; | 363 std::unique_ptr<ChromeContentBrowserClient> browser_content_client_; |
| 368 | 364 |
| 369 #if defined(OS_MACOSX) | 365 #if defined(OS_MACOSX) |
| 370 base::ScopedTempDir temp_dir_; | 366 base::ScopedTempDir temp_dir_; |
| 371 base::FilePath executable_path_, bundle_path_; | 367 base::FilePath executable_path_, bundle_path_; |
| 372 std::unique_ptr<MockLaunchd> mock_launchd_; | 368 std::unique_ptr<MockLaunchd> mock_launchd_; |
| 373 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; | 369 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; |
| 374 #endif | 370 #endif |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 455 content_client_.reset(); | 451 content_client_.reset(); |
| 456 content::SetContentClient(NULL); | 452 content::SetContentClient(NULL); |
| 457 | 453 |
| 458 TestingBrowserProcess::DeleteInstance(); | 454 TestingBrowserProcess::DeleteInstance(); |
| 459 } | 455 } |
| 460 | 456 |
| 461 base::Process CloudPrintProxyPolicyStartupTest::Launch( | 457 base::Process CloudPrintProxyPolicyStartupTest::Launch( |
| 462 const std::string& name) { | 458 const std::string& name) { |
| 463 EXPECT_FALSE(CheckServiceProcessReady()); | 459 EXPECT_FALSE(CheckServiceProcessReady()); |
| 464 | 460 |
| 465 startup_channel_id_ = | 461 startup_channel_handle_ = mojo::edk::NamedPlatformHandle( |
| 466 base::StringPrintf("%d.%p.%d", | 462 base::StringPrintf("%d.%p.%d", base::GetCurrentProcId(), this, |
| 467 base::GetCurrentProcId(), this, | 463 base::RandInt(0, std::numeric_limits<int>::max()))); |
| 468 base::RandInt(0, std::numeric_limits<int>::max())); | 464 startup_channel_ = IPC::ChannelProxy::Create( |
| 469 startup_channel_ = IPC::ChannelProxy::Create(startup_channel_id_, | 465 mojo::edk::ConnectToPeerProcess( |
| 470 IPC::Channel::MODE_SERVER, | 466 mojo::edk::CreateServerHandle(startup_channel_handle_, false)) |
| 471 this, | 467 .release(), |
| 472 IOTaskRunner()); | 468 IPC::Channel::MODE_SERVER, this, IOTaskRunner()); |
| 473 | 469 |
| 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); | 470 base::Process process = SpawnChild(name); |
| 484 #endif | |
| 485 EXPECT_TRUE(process.IsValid()); | 471 EXPECT_TRUE(process.IsValid()); |
| 486 return process; | 472 return process; |
| 487 } | 473 } |
| 488 | 474 |
| 489 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { | 475 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { |
| 490 observer_.Wait(); | 476 observer_.Wait(); |
| 491 EXPECT_TRUE(CheckServiceProcessReady()); | 477 EXPECT_TRUE(CheckServiceProcessReady()); |
| 492 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); | 478 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); |
| 493 | 479 |
| 494 mojo::MessagePipe pipe; | 480 mojo::MessagePipe pipe; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 517 EXPECT_EQ(exit_code, 0); | 503 EXPECT_EQ(exit_code, 0); |
| 518 } | 504 } |
| 519 | 505 |
| 520 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32_t peer_pid) { | 506 void CloudPrintProxyPolicyStartupTest::OnChannelConnected(int32_t peer_pid) { |
| 521 observer_.Notify(); | 507 observer_.Notify(); |
| 522 } | 508 } |
| 523 | 509 |
| 524 base::CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( | 510 base::CommandLine CloudPrintProxyPolicyStartupTest::MakeCmdLine( |
| 525 const std::string& procname) { | 511 const std::string& procname) { |
| 526 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); | 512 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); |
| 527 cl.AppendSwitchASCII(kProcessChannelID, startup_channel_id_); | 513 cl.AppendSwitchNative(kProcessChannelID, startup_channel_handle_.name); |
| 528 #if defined(OS_MACOSX) | 514 #if defined(OS_MACOSX) |
| 529 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); | 515 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); |
| 530 #endif | 516 #endif |
| 531 return cl; | 517 return cl; |
| 532 } | 518 } |
| 533 | 519 |
| 534 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { | 520 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { |
| 535 mojo::edk::Init(); | 521 mojo::edk::Init(); |
| 536 mojo::edk::ScopedIPCSupport ipc_support( | 522 mojo::edk::ScopedIPCSupport ipc_support( |
| 537 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) | 523 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) |
| 538 ->task_runner()); | 524 ->task_runner()); |
| 539 | 525 |
| 540 TestingBrowserProcess* browser_process = | 526 TestingBrowserProcess* browser_process = |
| 541 TestingBrowserProcess::GetGlobal(); | 527 TestingBrowserProcess::GetGlobal(); |
| 542 TestingProfileManager profile_manager(browser_process); | 528 TestingProfileManager profile_manager(browser_process); |
| 543 ASSERT_TRUE(profile_manager.SetUp()); | 529 ASSERT_TRUE(profile_manager.SetUp()); |
| 544 | 530 |
| 545 // Must be created after the TestingProfileManager since that creates the | 531 // Must be created after the TestingProfileManager since that creates the |
| 546 // LocalState for the BrowserProcess. Must be created before profiles are | 532 // LocalState for the BrowserProcess. Must be created before profiles are |
| 547 // constructed. | 533 // constructed. |
| 548 chrome::TestingIOThreadState testing_io_thread_state; | 534 chrome::TestingIOThreadState testing_io_thread_state; |
| 549 | 535 |
| 550 base::Process process = | 536 base::Process process = |
| 551 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); | 537 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); |
| 552 WaitForConnect(); | 538 WaitForConnect(); |
| 553 ShutdownAndWaitForExitWithTimeout(std::move(process)); | 539 ShutdownAndWaitForExitWithTimeout(std::move(process)); |
| 554 ServiceProcessControl::GetInstance()->Disconnect(); | 540 ServiceProcessControl::GetInstance()->Disconnect(); |
| 555 content::RunAllPendingInMessageLoop(); | 541 content::RunAllPendingInMessageLoop(); |
| 556 } | 542 } |
| OLD | NEW |