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/runner/host/child_process_host.h" | 7 #include "mojo/runner/host/child_process_host.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "mojo/message_pump/message_pump_mojo.h" | 15 #include "mojo/message_pump/message_pump_mojo.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
18 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h" | |
19 | 17 |
20 namespace mojo { | 18 namespace mojo { |
21 namespace runner { | 19 namespace runner { |
22 namespace { | 20 namespace { |
23 | 21 |
24 using PidAvailableCallback = base::Callback<void(base::ProcessId)>; | 22 using PidAvailableCallback = base::Callback<void(base::ProcessId)>; |
25 | 23 |
26 void EmptyCallback(base::ProcessId pid) {} | 24 void EmptyCallback(base::ProcessId pid) {} |
27 | 25 |
28 // Subclass just so we can observe |DidStart()|. | 26 // Subclass just so we can observe |DidStart()|. |
29 class TestChildProcessHost : public ChildProcessHost { | 27 class TestChildProcessHost : public ChildProcessHost { |
30 public: | 28 public: |
31 explicit TestChildProcessHost(base::TaskRunner* launch_process_runner) | 29 explicit TestChildProcessHost(base::TaskRunner* launch_process_runner) |
32 : ChildProcessHost(launch_process_runner, false, base::FilePath()) {} | 30 : ChildProcessHost(launch_process_runner, false, base::FilePath()) {} |
33 ~TestChildProcessHost() override {} | 31 ~TestChildProcessHost() override {} |
34 | 32 |
35 void DidStart(const PidAvailableCallback& pid_available_callback) override { | 33 void DidStart(const PidAvailableCallback& pid_available_callback) override { |
36 ChildProcessHost::DidStart(pid_available_callback); | 34 ChildProcessHost::DidStart(pid_available_callback); |
37 base::MessageLoop::current()->QuitWhenIdle(); | 35 base::MessageLoop::current()->QuitWhenIdle(); |
38 } | 36 } |
39 | 37 |
40 private: | 38 private: |
41 DISALLOW_COPY_AND_ASSIGN(TestChildProcessHost); | 39 DISALLOW_COPY_AND_ASSIGN(TestChildProcessHost); |
42 }; | 40 }; |
43 | 41 |
44 class ProcessDelegate : public embedder::ProcessDelegate { | |
45 public: | |
46 ProcessDelegate() {} | |
47 ~ProcessDelegate() override {} | |
48 | |
49 private: | |
50 void OnShutdownComplete() override {} | |
51 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); | |
52 }; | |
53 | |
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) { |
63 base::FilePath shell_dir; | 51 base::FilePath shell_dir; |
64 PathService::Get(base::DIR_MODULE, &shell_dir); | 52 PathService::Get(base::DIR_MODULE, &shell_dir); |
65 base::MessageLoop message_loop( | 53 base::MessageLoop message_loop( |
66 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); | 54 scoped_ptr<base::MessagePump>(new common::MessagePumpMojo())); |
67 scoped_refptr<base::SequencedWorkerPool> blocking_pool( | 55 scoped_refptr<base::SequencedWorkerPool> blocking_pool( |
68 new base::SequencedWorkerPool(3, "blocking_pool")); | 56 new base::SequencedWorkerPool(3, "blocking_pool")); |
69 | 57 |
70 base::Thread io_thread("io_thread"); | |
71 base::Thread::Options options; | |
72 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
73 io_thread.StartWithOptions(options); | |
74 | |
75 ProcessDelegate delegate; | |
76 embedder::InitIPCSupport( | |
77 embedder::ProcessType::NONE, &delegate, io_thread.task_runner(), | |
78 embedder::ScopedPlatformHandle()); | |
79 | |
80 TestChildProcessHost child_process_host(blocking_pool.get()); | 58 TestChildProcessHost child_process_host(blocking_pool.get()); |
81 child_process_host.Start(base::Bind(&EmptyCallback)); | 59 child_process_host.Start(base::Bind(&EmptyCallback)); |
82 message_loop.Run(); | 60 message_loop.Run(); |
83 child_process_host.ExitNow(123); | 61 child_process_host.ExitNow(123); |
84 int exit_code = child_process_host.Join(); | 62 int exit_code = child_process_host.Join(); |
85 VLOG(2) << "Joined child: exit_code = " << exit_code; | 63 VLOG(2) << "Joined child: exit_code = " << exit_code; |
86 EXPECT_EQ(123, exit_code); | 64 EXPECT_EQ(123, exit_code); |
87 blocking_pool->Shutdown(); | 65 blocking_pool->Shutdown(); |
88 embedder::ShutdownIPCSupport(); | |
89 } | 66 } |
90 | 67 |
91 } // namespace | 68 } // namespace |
92 } // namespace runner | 69 } // namespace runner |
93 } // namespace mojo | 70 } // namespace mojo |
OLD | NEW |