| 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/process/process.h" | 11 #include "base/process/process.h" |
| 11 #include "base/test/multiprocess_test.h" | 12 #include "base/test/multiprocess_test.h" |
| 12 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
| 13 #include "mojo/edk/embedder/scoped_platform_handle.h" | |
| 14 #include "mojo/public/cpp/system/macros.h" | 14 #include "mojo/public/cpp/system/macros.h" |
| 15 #include "mojo/public/cpp/system/message_pipe.h" |
| 15 #include "testing/multiprocess_func_list.h" | 16 #include "testing/multiprocess_func_list.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 | 19 |
| 19 namespace edk { | 20 namespace edk { |
| 20 class PlatformChannelPair; | 21 class PlatformChannelPair; |
| 21 | 22 |
| 22 namespace test { | 23 namespace test { |
| 23 | 24 |
| 24 extern const char kBrokerHandleSwitch[]; | |
| 25 | |
| 26 class MultiprocessTestHelper { | 25 class MultiprocessTestHelper { |
| 27 public: | 26 public: |
| 27 using HandlerCallback = base::Callback<void(ScopedMessagePipeHandle)>; |
| 28 |
| 28 MultiprocessTestHelper(); | 29 MultiprocessTestHelper(); |
| 29 ~MultiprocessTestHelper(); | 30 ~MultiprocessTestHelper(); |
| 30 | 31 |
| 31 // Start a child process and run the "main" function "named" |test_child_name| | 32 // Start a child process and run the "main" function "named" |test_child_name| |
| 32 // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or | 33 // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or |
| 33 // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below). | 34 // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below). |
| 34 void StartChild(const std::string& test_child_name); | 35 void StartChild(const std::string& test_child_name, |
| 36 const HandlerCallback& callback); |
| 37 |
| 35 // Like |StartChild()|, but appends an extra switch (with ASCII value) to the | 38 // Like |StartChild()|, but appends an extra switch (with ASCII value) to the |
| 36 // command line. (The switch must not already be present in the default | 39 // command line. (The switch must not already be present in the default |
| 37 // command line.) | 40 // command line.) |
| 38 void StartChildWithExtraSwitch(const std::string& test_child_name, | 41 void StartChildWithExtraSwitch(const std::string& test_child_name, |
| 39 const std::string& switch_string, | 42 const std::string& switch_string, |
| 40 const std::string& switch_value); | 43 const std::string& switch_value, |
| 44 const HandlerCallback& callback); |
| 45 |
| 41 // Wait for the child process to terminate. | 46 // Wait for the child process to terminate. |
| 42 // Returns the exit code of the child process. Note that, though it's declared | 47 // Returns the exit code of the child process. Note that, though it's declared |
| 43 // to be an |int|, the exit code is subject to mangling by the OS. E.g., we | 48 // to be an |int|, the exit code is subject to mangling by the OS. E.g., we |
| 44 // usually return -1 on error in the child (e.g., if |test_child_name| was not | 49 // usually return -1 on error in the child (e.g., if |test_child_name| was not |
| 45 // found), but this is mangled to 255 on Linux. You should only rely on codes | 50 // found), but this is mangled to 255 on Linux. You should only rely on codes |
| 46 // 0-127 being preserved, and -1 being outside the range 0-127. | 51 // 0-127 being preserved, and -1 being outside the range 0-127. |
| 47 int WaitForChildShutdown(); | 52 int WaitForChildShutdown(); |
| 48 | 53 |
| 49 // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0) | 54 // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0) |
| 50 // and false otherwise. You probably want to do something like | 55 // and false otherwise. You probably want to do something like |
| 51 // |EXPECT_TRUE(WaitForChildTestShutdown());|. Mainly for use with | 56 // |EXPECT_TRUE(WaitForChildTestShutdown());|. |
| 52 // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()|. | |
| 53 bool WaitForChildTestShutdown(); | 57 bool WaitForChildTestShutdown(); |
| 54 | 58 |
| 55 // For use by |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| only: | 59 // Used by macros in mojo/edk/test/mojo_test_base.h to support multiprocess |
| 60 // test client initialization. |
| 56 static void ChildSetup(); | 61 static void ChildSetup(); |
| 57 | 62 static int RunClientMain(const base::Callback<int(MojoHandle)>& main); |
| 58 // For use in the main process: | 63 static int RunClientTestMain(const base::Callback<void(MojoHandle)>& main); |
| 59 ScopedPlatformHandle server_platform_handle; | |
| 60 | 64 |
| 61 // For use (and only valid) in the child process: | 65 // For use (and only valid) in the child process: |
| 62 static ScopedPlatformHandle client_platform_handle; | 66 static std::string primordial_pipe_token; |
| 63 | 67 |
| 64 private: | 68 private: |
| 65 // Used differently depending on the test. | |
| 66 scoped_ptr<PlatformChannelPair> platform_channel_pair_; | |
| 67 | |
| 68 // Used by the broker. | |
| 69 scoped_ptr<PlatformChannelPair> broker_platform_channel_pair_; | |
| 70 | |
| 71 // Valid after |StartChild()| and before |WaitForChildShutdown()|. | 69 // Valid after |StartChild()| and before |WaitForChildShutdown()|. |
| 72 base::Process test_child_; | 70 base::Process test_child_; |
| 73 | 71 |
| 74 MOJO_DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper); | 72 MOJO_DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper); |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 // Use this to declare the child process's "main()" function for tests using | |
| 78 // |MultiprocessTestHelper|. It returns an |int|, which will be the process's | |
| 79 // exit code (but see the comment about |WaitForChildShutdown()|). | |
| 80 #define MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) \ | |
| 81 MULTIPROCESS_TEST_MAIN_WITH_SETUP( \ | |
| 82 test_child_name##TestChildMain, \ | |
| 83 test::MultiprocessTestHelper::ChildSetup) | |
| 84 | |
| 85 // Use this (and |WaitForChildTestShutdown()|) for the child process's "main()", | |
| 86 // if you want to use |EXPECT_...()| or |ASSERT_...()|; it has a |void| return | |
| 87 // type. (Note that while an |ASSERT_...()| failure will abort the test in the | |
| 88 // child, it will not abort the test in the parent.) | |
| 89 #define MOJO_MULTIPROCESS_TEST_CHILD_TEST(test_child_name) \ | |
| 90 void test_child_name##TestChildTest(); \ | |
| 91 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) { \ | |
| 92 test_child_name##TestChildTest(); \ | |
| 93 return (::testing::Test::HasFatalFailure() || \ | |
| 94 ::testing::Test::HasNonfatalFailure()) \ | |
| 95 ? 1 \ | |
| 96 : 0; \ | |
| 97 } \ | |
| 98 void test_child_name##TestChildTest() | |
| 99 | |
| 100 } // namespace test | 75 } // namespace test |
| 101 } // namespace edk | 76 } // namespace edk |
| 102 } // namespace mojo | 77 } // namespace mojo |
| 103 | 78 |
| 104 #endif // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ | 79 #endif // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_ |
| OLD | NEW |