| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Note: This file also tests child_process.*. | 5 // Note: This file also tests child_process.*. |
| 6 | 6 |
| 7 #include "mojo/shell/runner/host/child_process_host.h" | 7 #include "mojo/shell/runner/host/child_process_host.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/run_loop.h" |
| 14 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 15 #include "mojo/message_pump/message_pump_mojo.h" | 18 #include "mojo/message_pump/message_pump_mojo.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | 20 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 18 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h" | 21 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h" |
| 19 | 22 |
| 20 namespace mojo { | 23 namespace mojo { |
| 21 namespace shell { | 24 namespace shell { |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 using PidAvailableCallback = base::Callback<void(base::ProcessId)>; | 27 void ProcessReadyCallbackAdapater(const base::Closure& callback, |
| 25 | 28 base::ProcessId process_id) { |
| 26 void EmptyCallback(base::ProcessId pid) {} | 29 callback.Run(); |
| 27 | 30 } |
| 28 // Subclass just so we can observe |DidStart()|. | |
| 29 class TestChildProcessHost : public ChildProcessHost { | |
| 30 public: | |
| 31 explicit TestChildProcessHost(base::TaskRunner* launch_process_runner) | |
| 32 : ChildProcessHost(launch_process_runner, false, base::FilePath()) {} | |
| 33 ~TestChildProcessHost() override {} | |
| 34 | |
| 35 void DidStart(const PidAvailableCallback& pid_available_callback) override { | |
| 36 ChildProcessHost::DidStart(pid_available_callback); | |
| 37 base::MessageLoop::current()->QuitWhenIdle(); | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 DISALLOW_COPY_AND_ASSIGN(TestChildProcessHost); | |
| 42 }; | |
| 43 | 31 |
| 44 class ProcessDelegate : public embedder::ProcessDelegate { | 32 class ProcessDelegate : public embedder::ProcessDelegate { |
| 45 public: | 33 public: |
| 46 ProcessDelegate() {} | 34 ProcessDelegate() {} |
| 47 ~ProcessDelegate() override {} | 35 ~ProcessDelegate() override {} |
| 48 | 36 |
| 49 private: | 37 private: |
| 50 void OnShutdownComplete() override {} | 38 void OnShutdownComplete() override {} |
| 51 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); | 39 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); |
| 52 }; | 40 }; |
| 53 | 41 |
| 54 #if defined(OS_ANDROID) | 42 #if defined(OS_ANDROID) |
| 55 // TODO(qsr): Multiprocess shell tests are not supported on android. | 43 // TODO(qsr): Multiprocess shell tests are not supported on android. |
| 56 #define MAYBE_StartJoin DISABLED_StartJoin | 44 #define MAYBE_StartJoin DISABLED_StartJoin |
| 57 #else | 45 #else |
| 58 #define MAYBE_StartJoin StartJoin | 46 #define MAYBE_StartJoin StartJoin |
| 59 #endif // defined(OS_ANDROID) | 47 #endif // defined(OS_ANDROID) |
| 60 // Just tests starting the child process and joining it (without starting an | 48 // Just tests starting the child process and joining it (without starting an |
| 61 // app). | 49 // app). |
| 62 TEST(ChildProcessHostTest, MAYBE_StartJoin) { | 50 TEST(ChildProcessHostTest, MAYBE_StartJoin) { |
| 51 base::CommandLine::ForCurrentProcess()->AppendSwitch("use-new-edk"); |
| 52 |
| 63 base::FilePath shell_dir; | 53 base::FilePath shell_dir; |
| 64 PathService::Get(base::DIR_MODULE, &shell_dir); | 54 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 65 base::MessageLoop message_loop( | 55 base::MessageLoop message_loop( |
| 66 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); | 56 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); |
| 67 scoped_refptr<base::SequencedWorkerPool> blocking_pool( | 57 scoped_refptr<base::SequencedWorkerPool> blocking_pool( |
| 68 new base::SequencedWorkerPool(3, "blocking_pool")); | 58 new base::SequencedWorkerPool(3, "blocking_pool")); |
| 69 | 59 |
| 70 base::Thread io_thread("io_thread"); | 60 base::Thread io_thread("io_thread"); |
| 71 base::Thread::Options options; | 61 base::Thread::Options options; |
| 72 options.message_loop_type = base::MessageLoop::TYPE_IO; | 62 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 73 io_thread.StartWithOptions(options); | 63 io_thread.StartWithOptions(options); |
| 74 | 64 |
| 75 ProcessDelegate delegate; | 65 ProcessDelegate delegate; |
| 76 embedder::InitIPCSupport( | 66 embedder::InitIPCSupport( |
| 77 embedder::ProcessType::NONE, &delegate, io_thread.task_runner(), | 67 embedder::ProcessType::NONE, &delegate, io_thread.task_runner(), |
| 78 embedder::ScopedPlatformHandle()); | 68 embedder::ScopedPlatformHandle()); |
| 79 | 69 |
| 80 TestChildProcessHost child_process_host(blocking_pool.get()); | 70 ChildProcessHost child_process_host(blocking_pool.get(), false, |
| 81 child_process_host.Start(base::Bind(&EmptyCallback)); | 71 base::FilePath()); |
| 82 message_loop.Run(); | 72 base::RunLoop run_loop; |
| 73 child_process_host.Start( |
| 74 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure())); |
| 75 run_loop.Run(); |
| 76 |
| 83 child_process_host.ExitNow(123); | 77 child_process_host.ExitNow(123); |
| 84 int exit_code = child_process_host.Join(); | 78 int exit_code = child_process_host.Join(); |
| 85 VLOG(2) << "Joined child: exit_code = " << exit_code; | 79 VLOG(2) << "Joined child: exit_code = " << exit_code; |
| 86 EXPECT_EQ(123, exit_code); | 80 EXPECT_EQ(123, exit_code); |
| 87 blocking_pool->Shutdown(); | 81 blocking_pool->Shutdown(); |
| 88 embedder::ShutdownIPCSupport(); | 82 embedder::ShutdownIPCSupport(); |
| 89 } | 83 } |
| 90 | 84 |
| 91 } // namespace | 85 } // namespace |
| 92 } // namespace shell | 86 } // namespace shell |
| 93 } // namespace mojo | 87 } // namespace mojo |
| OLD | NEW |