• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
6 #define MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
7 
8 #include <string>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/process/process.h"
13 #include "base/test/multiprocess_test.h"
14 #include "base/test/test_timeouts.h"
15 #include "mojo/edk/embedder/embedder.h"
16 #include "mojo/public/cpp/system/message_pipe.h"
17 #include "testing/multiprocess_func_list.h"
18 
19 namespace mojo {
20 
21 namespace edk {
22 class PlatformChannelPair;
23 
24 namespace test {
25 
26 class MultiprocessTestHelper {
27  public:
28   using HandlerCallback = base::Callback<void(ScopedMessagePipeHandle)>;
29 
30   MultiprocessTestHelper();
31   ~MultiprocessTestHelper();
32 
33   // Start a child process and run the "main" function "named" |test_child_name|
34   // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or
35   // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below).
36   ScopedMessagePipeHandle StartChild(const std::string& test_child_name);
37 
38   // 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
40   // command line.)
41   ScopedMessagePipeHandle StartChildWithExtraSwitch(
42       const std::string& test_child_name,
43       const std::string& switch_string,
44       const std::string& switch_value);
45 
set_process_error_callback(const ProcessErrorCallback & callback)46   void set_process_error_callback(const ProcessErrorCallback& callback) {
47     process_error_callback_ = callback;
48   }
49 
50   // Wait for the child process to terminate.
51   // 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
53   // 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
55   // 0-127 being preserved, and -1 being outside the range 0-127.
56   int WaitForChildShutdown();
57 
58   // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0)
59   // and false otherwise. You probably want to do something like
60   // |EXPECT_TRUE(WaitForChildTestShutdown());|.
61   bool WaitForChildTestShutdown();
62 
test_child()63   const base::Process& test_child() const { return test_child_; }
64 
65   // Used by macros in mojo/edk/test/mojo_test_base.h to support multiprocess
66   // test client initialization.
67   static void ChildSetup();
68   static int RunClientMain(const base::Callback<int(MojoHandle)>& main);
69   static int RunClientTestMain(const base::Callback<void(MojoHandle)>& main);
70 
71   // For use (and only valid) in the child process:
72   static std::string primordial_pipe_token;
73 
74  private:
75   // Valid after |StartChild()| and before |WaitForChildShutdown()|.
76   base::Process test_child_;
77 
78   ProcessErrorCallback process_error_callback_;
79 
80   DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper);
81 };
82 
83 }  // namespace test
84 }  // namespace edk
85 }  // namespace mojo
86 
87 #endif  // MOJO_EDK_TEST_MULTIPROCESS_TEST_HELPER_H_
88