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 <utility> |
| 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/callback.h" | 12 #include "base/callback.h" |
11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
15 #include "base/path_service.h" | 17 #include "base/path_service.h" |
16 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
17 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
18 #include "mojo/edk/embedder/embedder.h" | 20 #include "mojo/edk/embedder/embedder.h" |
19 #include "mojo/edk/embedder/process_delegate.h" | 21 #include "mojo/edk/embedder/process_delegate.h" |
20 #include "mojo/message_pump/message_pump_mojo.h" | 22 #include "mojo/message_pump/message_pump_mojo.h" |
21 #include "mojo/shell/runner/host/command_line_switch.h" | 23 #include "mojo/shell/native_runner_delegate.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
23 | 25 |
24 namespace mojo { | 26 namespace mojo { |
25 namespace shell { | 27 namespace shell { |
26 namespace { | 28 namespace { |
27 | 29 |
28 void ProcessReadyCallbackAdapater(const base::Closure& callback, | 30 void ProcessReadyCallbackAdapater(const base::Closure& callback, |
29 base::ProcessId process_id) { | 31 base::ProcessId process_id) { |
30 callback.Run(); | 32 callback.Run(); |
31 } | 33 } |
32 | 34 |
33 class ProcessDelegate : public edk::ProcessDelegate { | 35 class ProcessDelegate : public edk::ProcessDelegate { |
34 public: | 36 public: |
35 ProcessDelegate() {} | 37 ProcessDelegate() {} |
36 ~ProcessDelegate() override {} | 38 ~ProcessDelegate() override {} |
37 | 39 |
38 private: | 40 private: |
39 void OnShutdownComplete() override {} | 41 void OnShutdownComplete() override {} |
40 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); | 42 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); |
41 }; | 43 }; |
42 | 44 |
| 45 class NativeRunnerDelegateImpl : public NativeRunnerDelegate { |
| 46 public: |
| 47 NativeRunnerDelegateImpl() {} |
| 48 ~NativeRunnerDelegateImpl() override {} |
| 49 |
| 50 size_t get_and_clear_adjust_count() { |
| 51 size_t count = 0; |
| 52 std::swap(count, adjust_count_); |
| 53 return count; |
| 54 } |
| 55 |
| 56 private: |
| 57 // NativeRunnerDelegate: |
| 58 void AdjustCommandLineArgumentsForTarget( |
| 59 const Identity& target, |
| 60 base::CommandLine* command_line) override { |
| 61 adjust_count_++; |
| 62 } |
| 63 |
| 64 size_t adjust_count_ = 0; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); |
| 67 }; |
| 68 |
43 #if defined(OS_ANDROID) | 69 #if defined(OS_ANDROID) |
44 // TODO(qsr): Multiprocess shell tests are not supported on android. | 70 // TODO(qsr): Multiprocess shell tests are not supported on android. |
45 #define MAYBE_StartJoin DISABLED_StartJoin | 71 #define MAYBE_StartJoin DISABLED_StartJoin |
46 #else | 72 #else |
47 #define MAYBE_StartJoin StartJoin | 73 #define MAYBE_StartJoin StartJoin |
48 #endif // defined(OS_ANDROID) | 74 #endif // defined(OS_ANDROID) |
49 // Just tests starting the child process and joining it (without starting an | 75 // Just tests starting the child process and joining it (without starting an |
50 // app). | 76 // app). |
51 TEST(ChildProcessHostTest, MAYBE_StartJoin) { | 77 TEST(ChildProcessHostTest, MAYBE_StartJoin) { |
52 base::FilePath shell_dir; | 78 base::FilePath shell_dir; |
53 PathService::Get(base::DIR_MODULE, &shell_dir); | 79 PathService::Get(base::DIR_MODULE, &shell_dir); |
54 base::MessageLoop message_loop( | 80 base::MessageLoop message_loop( |
55 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); | 81 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); |
56 scoped_refptr<base::SequencedWorkerPool> blocking_pool( | 82 scoped_refptr<base::SequencedWorkerPool> blocking_pool( |
57 new base::SequencedWorkerPool(3, "blocking_pool")); | 83 new base::SequencedWorkerPool(3, "blocking_pool")); |
58 | 84 |
59 base::Thread io_thread("io_thread"); | 85 base::Thread io_thread("io_thread"); |
60 base::Thread::Options options; | 86 base::Thread::Options options; |
61 options.message_loop_type = base::MessageLoop::TYPE_IO; | 87 options.message_loop_type = base::MessageLoop::TYPE_IO; |
62 io_thread.StartWithOptions(options); | 88 io_thread.StartWithOptions(options); |
63 | 89 |
64 ProcessDelegate delegate; | 90 ProcessDelegate delegate; |
65 edk::InitIPCSupport(&delegate, io_thread.task_runner()); | 91 edk::InitIPCSupport(&delegate, io_thread.task_runner()); |
66 | 92 |
67 ChildProcessHost child_process_host(blocking_pool.get(), false, | 93 NativeRunnerDelegateImpl native_runner_delegate; |
68 base::FilePath(), | 94 ChildProcessHost child_process_host(blocking_pool.get(), |
69 std::vector<CommandLineSwitch>()); | 95 &native_runner_delegate, false, |
| 96 Identity(), base::FilePath()); |
70 base::RunLoop run_loop; | 97 base::RunLoop run_loop; |
71 child_process_host.Start( | 98 child_process_host.Start( |
72 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure())); | 99 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure())); |
73 run_loop.Run(); | 100 run_loop.Run(); |
74 | 101 |
75 child_process_host.ExitNow(123); | 102 child_process_host.ExitNow(123); |
76 int exit_code = child_process_host.Join(); | 103 int exit_code = child_process_host.Join(); |
77 VLOG(2) << "Joined child: exit_code = " << exit_code; | 104 VLOG(2) << "Joined child: exit_code = " << exit_code; |
78 EXPECT_EQ(123, exit_code); | 105 EXPECT_EQ(123, exit_code); |
79 blocking_pool->Shutdown(); | 106 blocking_pool->Shutdown(); |
80 edk::ShutdownIPCSupport(); | 107 edk::ShutdownIPCSupport(); |
| 108 EXPECT_EQ(1u, native_runner_delegate.get_and_clear_adjust_count()); |
81 } | 109 } |
82 | 110 |
83 } // namespace | 111 } // namespace |
84 } // namespace shell | 112 } // namespace shell |
85 } // namespace mojo | 113 } // namespace mojo |
OLD | NEW |