OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ | 5 #ifndef MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ |
6 #define MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ | 6 #define MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
13 #include "base/test/multiprocess_test.h" | 13 #include "base/test/multiprocess_test.h" |
14 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
15 #include "mojo/edk/embedder/embedder.h" | 15 #include "mojo/edk/embedder/embedder.h" |
16 #include "mojo/public/cpp/system/message_pipe.h" | 16 #include "mojo/public/cpp/system/message_pipe.h" |
17 #include "testing/multiprocess_func_list.h" | 17 #include "testing/multiprocess_func_list.h" |
18 | 18 |
19 namespace mojo { | 19 namespace mojo { |
20 | 20 |
21 namespace edk { | 21 namespace edk { |
22 class PlatformChannelPair; | 22 class PlatformChannelPair; |
23 | 23 |
24 namespace test { | 24 namespace test { |
25 | 25 |
26 class MultiprocessTestHelper { | 26 class MultiprocessTestHelper { |
27 public: | 27 public: |
28 using HandlerCallback = base::Callback<void(ScopedMessagePipeHandle)>; | 28 using HandlerCallback = base::Callback<void(ScopedMessagePipeHandle)>; |
29 | 29 |
| 30 enum class LaunchType { |
| 31 // Launch the child process as a child in the mojo system. |
| 32 CHILD, |
| 33 |
| 34 // Launch the child process as an unrelated peer process in the mojo system. |
| 35 PEER, |
| 36 }; |
| 37 |
30 MultiprocessTestHelper(); | 38 MultiprocessTestHelper(); |
31 ~MultiprocessTestHelper(); | 39 ~MultiprocessTestHelper(); |
32 | 40 |
33 // Start a child process and run the "main" function "named" |test_child_name| | 41 // Start a child process and run the "main" function "named" |test_child_name| |
34 // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or | 42 // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or |
35 // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below). | 43 // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below). |
36 ScopedMessagePipeHandle StartChild(const std::string& test_child_name); | 44 ScopedMessagePipeHandle StartChild( |
| 45 const std::string& test_child_name, |
| 46 LaunchType launch_type = LaunchType::CHILD); |
37 | 47 |
38 // Like |StartChild()|, but appends an extra switch (with ASCII value) to the | 48 // Like |StartChild()|, but appends an extra switch (with ASCII value) to the |
39 // command line. (The switch must not already be present in the default | 49 // command line. (The switch must not already be present in the default |
40 // command line.) | 50 // command line.) |
41 ScopedMessagePipeHandle StartChildWithExtraSwitch( | 51 ScopedMessagePipeHandle StartChildWithExtraSwitch( |
42 const std::string& test_child_name, | 52 const std::string& test_child_name, |
43 const std::string& switch_string, | 53 const std::string& switch_string, |
44 const std::string& switch_value); | 54 const std::string& switch_value, |
| 55 LaunchType launch_type); |
45 | 56 |
46 void set_process_error_callback(const ProcessErrorCallback& callback) { | 57 void set_process_error_callback(const ProcessErrorCallback& callback) { |
47 process_error_callback_ = callback; | 58 process_error_callback_ = callback; |
48 } | 59 } |
49 | 60 |
50 // Wait for the child process to terminate. | 61 // Wait for the child process to terminate. |
51 // Returns the exit code of the child process. Note that, though it's declared | 62 // Returns the exit code of the child process. Note that, though it's declared |
52 // to be an |int|, the exit code is subject to mangling by the OS. E.g., we | 63 // to be an |int|, the exit code is subject to mangling by the OS. E.g., we |
53 // usually return -1 on error in the child (e.g., if |test_child_name| was not | 64 // usually return -1 on error in the child (e.g., if |test_child_name| was not |
54 // found), but this is mangled to 255 on Linux. You should only rely on codes | 65 // found), but this is mangled to 255 on Linux. You should only rely on codes |
55 // 0-127 being preserved, and -1 being outside the range 0-127. | 66 // 0-127 being preserved, and -1 being outside the range 0-127. |
56 int WaitForChildShutdown(); | 67 int WaitForChildShutdown(); |
57 | 68 |
58 // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0) | 69 // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0) |
59 // and false otherwise. You probably want to do something like | 70 // and false otherwise. You probably want to do something like |
60 // |EXPECT_TRUE(WaitForChildTestShutdown());|. | 71 // |EXPECT_TRUE(WaitForChildTestShutdown());|. |
61 bool WaitForChildTestShutdown(); | 72 bool WaitForChildTestShutdown(); |
62 | 73 |
63 const base::Process& test_child() const { return test_child_; } | 74 const base::Process& test_child() const { return test_child_; } |
64 | 75 |
65 // Used by macros in mojo/edk/test/mojo_test_base.h to support multiprocess | 76 // Used by macros in mojo/edk/test/mojo_test_base.h to support multiprocess |
66 // test client initialization. | 77 // test client initialization. |
67 static void ChildSetup(); | 78 static void ChildSetup(); |
68 static int RunClientMain(const base::Callback<int(MojoHandle)>& main); | 79 static int RunClientMain(const base::Callback<int(MojoHandle)>& main); |
69 static int RunClientTestMain(const base::Callback<void(MojoHandle)>& main); | 80 static int RunClientTestMain(const base::Callback<void(MojoHandle)>& main); |
70 | 81 |
71 // For use (and only valid) in the child process: | 82 // For use (and only valid) in the child process: |
72 static std::string primordial_pipe_token; | 83 static mojo::ScopedMessagePipeHandle primordial_pipe; |
73 | 84 |
74 private: | 85 private: |
75 // Valid after |StartChild()| and before |WaitForChildShutdown()|. | 86 // Valid after |StartChild()| and before |WaitForChildShutdown()|. |
76 base::Process test_child_; | 87 base::Process test_child_; |
77 | 88 |
78 ProcessErrorCallback process_error_callback_; | 89 ProcessErrorCallback process_error_callback_; |
79 | 90 |
80 DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper); | 91 DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper); |
81 }; | 92 }; |
82 | 93 |
83 } // namespace test | 94 } // namespace test |
84 } // namespace edk | 95 } // namespace edk |
85 } // namespace mojo | 96 } // namespace mojo |
86 | 97 |
87 #endif // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ | 98 #endif // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ |
OLD | NEW |