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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include "chrome/test/base/test_launcher_utils.h" | 42 #include "chrome/test/base/test_launcher_utils.h" |
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_descriptors.h" | 53 #include "ipc/ipc_descriptors.h" |
53 #include "ipc/ipc_multiprocess_test.h" | 54 #include "ipc/ipc_multiprocess_test.h" |
54 #include "ipc/ipc_switches.h" | 55 #include "ipc/ipc_switches.h" |
55 #include "mojo/edk/embedder/embedder.h" | 56 #include "mojo/edk/embedder/embedder.h" |
| 57 #include "mojo/edk/embedder/named_platform_handle.h" |
| 58 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 59 #include "mojo/edk/embedder/scoped_ipc_support.h" |
56 #include "testing/gmock/include/gmock/gmock.h" | 60 #include "testing/gmock/include/gmock/gmock.h" |
57 #include "testing/gtest/include/gtest/gtest.h" | 61 #include "testing/gtest/include/gtest/gtest.h" |
58 #include "testing/multiprocess_func_list.h" | 62 #include "testing/multiprocess_func_list.h" |
59 | 63 |
60 #if defined(OS_MACOSX) | 64 #if defined(OS_MACOSX) |
61 #include "chrome/common/mac/mock_launchd.h" | 65 #include "chrome/common/mac/mock_launchd.h" |
62 #endif | 66 #endif |
63 #if defined(OS_POSIX) | 67 #if defined(OS_POSIX) |
64 #include "base/posix/global_descriptors.h" | 68 #include "base/posix/global_descriptors.h" |
65 #endif | 69 #endif |
(...skipping 28 matching lines...) Expand all Loading... |
94 void ShutdownTask() { | 98 void ShutdownTask() { |
95 g_good_shutdown = true; | 99 g_good_shutdown = true; |
96 g_service_process->Shutdown(); | 100 g_service_process->Shutdown(); |
97 } | 101 } |
98 | 102 |
99 class TestStartupClientChannelListener : public IPC::Listener { | 103 class TestStartupClientChannelListener : public IPC::Listener { |
100 public: | 104 public: |
101 bool OnMessageReceived(const IPC::Message& message) override { return false; } | 105 bool OnMessageReceived(const IPC::Message& message) override { return false; } |
102 }; | 106 }; |
103 | 107 |
| 108 void ConnectOnBlockingPool(mojo::ScopedMessagePipeHandle handle, |
| 109 mojo::edk::NamedPlatformHandle os_pipe) { |
| 110 mojo::edk::ScopedPlatformHandle os_pipe_handle = |
| 111 mojo::edk::CreateClientHandle(os_pipe); |
| 112 if (!os_pipe_handle.is_valid()) |
| 113 return; |
| 114 |
| 115 mojo::FuseMessagePipes( |
| 116 mojo::edk::ConnectToPeerProcess(std::move(os_pipe_handle)), |
| 117 std::move(handle)); |
| 118 } |
| 119 |
104 } // namespace | 120 } // namespace |
105 | 121 |
106 class TestServiceProcess : public ServiceProcess { | 122 class TestServiceProcess : public ServiceProcess { |
107 public: | 123 public: |
108 TestServiceProcess() { } | 124 TestServiceProcess() { } |
109 ~TestServiceProcess() override {} | 125 ~TestServiceProcess() override {} |
110 | 126 |
111 bool Initialize(base::MessageLoopForUI* message_loop, | 127 bool Initialize(base::MessageLoopForUI* message_loop, |
112 ServiceProcessState* state); | 128 ServiceProcessState* state); |
113 }; | 129 }; |
(...skipping 11 matching lines...) Expand all Loading... |
125 | 141 |
126 // This mocks the service side IPC message handler, allowing us to have a | 142 // This mocks the service side IPC message handler, allowing us to have a |
127 // minimal service process. | 143 // minimal service process. |
128 class MockServiceIPCServer : public ServiceIPCServer { | 144 class MockServiceIPCServer : public ServiceIPCServer { |
129 public: | 145 public: |
130 static std::string EnabledUserId(); | 146 static std::string EnabledUserId(); |
131 | 147 |
132 MockServiceIPCServer( | 148 MockServiceIPCServer( |
133 ServiceIPCServer::Client* client, | 149 ServiceIPCServer::Client* client, |
134 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 150 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
135 const IPC::ChannelHandle& handle, | |
136 base::WaitableEvent* shutdown_event) | 151 base::WaitableEvent* shutdown_event) |
137 : ServiceIPCServer(client, io_task_runner, handle, shutdown_event), | 152 : ServiceIPCServer(client, io_task_runner, shutdown_event), |
138 enabled_(true) { } | 153 enabled_(true) {} |
139 | 154 |
140 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message& message)); | 155 MOCK_METHOD1(OnMessageReceived, bool(const IPC::Message& message)); |
141 MOCK_METHOD1(OnChannelConnected, void(int32_t peer_pid)); | 156 MOCK_METHOD1(OnChannelConnected, void(int32_t peer_pid)); |
142 MOCK_METHOD0(OnChannelError, void()); | 157 MOCK_METHOD0(OnChannelError, void()); |
143 | 158 |
144 void SetServiceEnabledExpectations(); | 159 void SetServiceEnabledExpectations(); |
145 void SetWillBeDisabledExpectations(); | 160 void SetWillBeDisabledExpectations(); |
146 | 161 |
147 void CallServiceOnChannelConnected(int32_t peer_pid) { | 162 void CallServiceOnChannelConnected(int32_t peer_pid) { |
148 ServiceIPCServer::OnChannelConnected(peer_pid); | 163 ServiceIPCServer::OnChannelConnected(peer_pid); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 261 |
247 TestServiceProcess service_process; | 262 TestServiceProcess service_process; |
248 EXPECT_EQ(&service_process, g_service_process); | 263 EXPECT_EQ(&service_process, g_service_process); |
249 | 264 |
250 // Takes ownership of the pointer, but we can use it since we have the same | 265 // Takes ownership of the pointer, but we can use it since we have the same |
251 // lifetime. | 266 // lifetime. |
252 EXPECT_TRUE(service_process.Initialize(&main_message_loop, state)); | 267 EXPECT_TRUE(service_process.Initialize(&main_message_loop, state)); |
253 | 268 |
254 // Needed for IPC. | 269 // Needed for IPC. |
255 mojo::edk::Init(); | 270 mojo::edk::Init(); |
| 271 mojo::edk::ScopedIPCSupport ipc_support(service_process.io_task_runner()); |
256 | 272 |
257 MockServiceIPCServer server(&service_process, | 273 MockServiceIPCServer server(&service_process, |
258 service_process.io_task_runner(), | 274 service_process.io_task_runner(), |
259 state->GetServiceProcessChannel(), | |
260 service_process.GetShutdownEventForTesting()); | 275 service_process.GetShutdownEventForTesting()); |
261 | 276 |
262 // Here is where the expectations/mock responses need to be set up. | 277 // Here is where the expectations/mock responses need to be set up. |
263 set_expectations.Run(&server); | 278 set_expectations.Run(&server); |
264 | 279 |
265 EXPECT_TRUE(server.Init()); | 280 EXPECT_TRUE(server.Init()); |
266 EXPECT_TRUE(state->SignalReady(service_process.io_task_runner().get(), | 281 EXPECT_TRUE(state->SignalReady(service_process.io_task_runner().get(), |
267 base::Bind(&ShutdownTask))); | 282 base::Bind(&ShutdownTask))); |
268 #if defined(OS_MACOSX) | 283 #if defined(OS_MACOSX) |
269 mock_launchd.SignalReady(); | 284 mock_launchd.SignalReady(); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 base::Process process = SpawnChild(name); | 483 base::Process process = SpawnChild(name); |
469 #endif | 484 #endif |
470 EXPECT_TRUE(process.IsValid()); | 485 EXPECT_TRUE(process.IsValid()); |
471 return process; | 486 return process; |
472 } | 487 } |
473 | 488 |
474 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { | 489 void CloudPrintProxyPolicyStartupTest::WaitForConnect() { |
475 observer_.Wait(); | 490 observer_.Wait(); |
476 EXPECT_TRUE(CheckServiceProcessReady()); | 491 EXPECT_TRUE(CheckServiceProcessReady()); |
477 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); | 492 EXPECT_TRUE(base::ThreadTaskRunnerHandle::Get().get()); |
| 493 |
| 494 mojo::MessagePipe pipe; |
| 495 BrowserThread::PostBlockingPoolTask( |
| 496 FROM_HERE, base::Bind(&ConnectOnBlockingPool, base::Passed(&pipe.handle1), |
| 497 mojo::edk::NamedPlatformHandle( |
| 498 GetServiceProcessChannel().name))); |
478 ServiceProcessControl::GetInstance()->SetChannel( | 499 ServiceProcessControl::GetInstance()->SetChannel( |
479 IPC::ChannelProxy::Create(GetServiceProcessChannel(), | 500 IPC::ChannelProxy::Create(IPC::ChannelMojo::CreateClientFactory( |
480 IPC::Channel::MODE_NAMED_CLIENT, | 501 std::move(pipe.handle0), IOTaskRunner()), |
481 ServiceProcessControl::GetInstance(), | 502 this, IOTaskRunner())); |
482 IOTaskRunner())); | |
483 } | 503 } |
484 | 504 |
485 bool CloudPrintProxyPolicyStartupTest::Send(IPC::Message* message) { | 505 bool CloudPrintProxyPolicyStartupTest::Send(IPC::Message* message) { |
486 return ServiceProcessControl::GetInstance()->Send(message); | 506 return ServiceProcessControl::GetInstance()->Send(message); |
487 } | 507 } |
488 | 508 |
489 void CloudPrintProxyPolicyStartupTest::ShutdownAndWaitForExitWithTimeout( | 509 void CloudPrintProxyPolicyStartupTest::ShutdownAndWaitForExitWithTimeout( |
490 base::Process process) { | 510 base::Process process) { |
491 ASSERT_TRUE(Send(new ServiceMsg_Shutdown())); | 511 ASSERT_TRUE(Send(new ServiceMsg_Shutdown())); |
492 | 512 |
(...skipping 12 matching lines...) Expand all Loading... |
505 const std::string& procname) { | 525 const std::string& procname) { |
506 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); | 526 base::CommandLine cl = MultiProcessTest::MakeCmdLine(procname); |
507 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_); | 527 cl.AppendSwitchASCII(switches::kProcessChannelID, startup_channel_id_); |
508 #if defined(OS_MACOSX) | 528 #if defined(OS_MACOSX) |
509 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); | 529 cl.AppendSwitchASCII(kTestExecutablePath, executable_path_.value()); |
510 #endif | 530 #endif |
511 return cl; | 531 return cl; |
512 } | 532 } |
513 | 533 |
514 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { | 534 TEST_F(CloudPrintProxyPolicyStartupTest, StartAndShutdown) { |
| 535 mojo::edk::Init(); |
| 536 mojo::edk::ScopedIPCSupport ipc_support( |
| 537 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) |
| 538 ->task_runner()); |
| 539 |
515 TestingBrowserProcess* browser_process = | 540 TestingBrowserProcess* browser_process = |
516 TestingBrowserProcess::GetGlobal(); | 541 TestingBrowserProcess::GetGlobal(); |
517 TestingProfileManager profile_manager(browser_process); | 542 TestingProfileManager profile_manager(browser_process); |
518 ASSERT_TRUE(profile_manager.SetUp()); | 543 ASSERT_TRUE(profile_manager.SetUp()); |
519 | 544 |
520 // Must be created after the TestingProfileManager since that creates the | 545 // Must be created after the TestingProfileManager since that creates the |
521 // LocalState for the BrowserProcess. Must be created before profiles are | 546 // LocalState for the BrowserProcess. Must be created before profiles are |
522 // constructed. | 547 // constructed. |
523 chrome::TestingIOThreadState testing_io_thread_state; | 548 chrome::TestingIOThreadState testing_io_thread_state; |
524 | 549 |
525 base::Process process = | 550 base::Process process = |
526 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); | 551 Launch("CloudPrintMockService_StartEnabledWaitForQuit"); |
527 WaitForConnect(); | 552 WaitForConnect(); |
528 ShutdownAndWaitForExitWithTimeout(std::move(process)); | 553 ShutdownAndWaitForExitWithTimeout(std::move(process)); |
| 554 ServiceProcessControl::GetInstance()->Disconnect(); |
529 content::RunAllPendingInMessageLoop(); | 555 content::RunAllPendingInMessageLoop(); |
530 } | 556 } |
OLD | NEW |