Home | History | Annotate | Download | only in test
      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_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
      6 #define MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/process/process_handle.h"
     13 #include "base/test/multiprocess_test.h"
     14 #include "base/test/test_timeouts.h"
     15 #include "mojo/embedder/scoped_platform_handle.h"
     16 #include "testing/multiprocess_func_list.h"
     17 
     18 namespace mojo {
     19 
     20 namespace embedder {
     21 class PlatformChannelPair;
     22 }
     23 
     24 namespace test {
     25 
     26 class MultiprocessTestHelper {
     27  public:
     28   MultiprocessTestHelper();
     29   ~MultiprocessTestHelper();
     30 
     31   // Start a child process and run the "main" function "named" |test_child_name|
     32   // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or
     33   // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below).
     34   void StartChild(const std::string& test_child_name);
     35   // Wait for the child process to terminate.
     36   // Returns the exit code of the child process. Note that, though it's declared
     37   // to be an |int|, the exit code is subject to mangling by the OS. E.g., we
     38   // usually return -1 on error in the child (e.g., if |test_child_name| was not
     39   // found), but this is mangled to 255 on Linux. You should only rely on codes
     40   // 0-127 being preserved, and -1 being outside the range 0-127.
     41   int WaitForChildShutdown();
     42 
     43   // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0)
     44   // and false otherwise. You probably want to do something like
     45   // |EXPECT_TRUE(WaitForChildTestShutdown());|. Mainly for use with
     46   // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()|.
     47   bool WaitForChildTestShutdown();
     48 
     49   // For use by |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| only:
     50   static void ChildSetup();
     51 
     52   // For use in the main process:
     53   embedder::ScopedPlatformHandle server_platform_handle;
     54 
     55   // For use (and only valid) in the child process:
     56   static embedder::ScopedPlatformHandle client_platform_handle;
     57 
     58  private:
     59   scoped_ptr<embedder::PlatformChannelPair> platform_channel_pair_;
     60 
     61   // Valid after |StartChild()| and before |WaitForChildShutdown()|.
     62   base::ProcessHandle test_child_handle_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(MultiprocessTestHelper);
     65 };
     66 
     67 // Use this to declare the child process's "main()" function for tests using
     68 // |MultiprocessTestHelper|. It returns an |int|, which will be the process's
     69 // exit code (but see the comment about |WaitForChildShutdown()|).
     70 #define MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) \
     71     MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
     72         test_child_name ## TestChildMain, \
     73         ::mojo::test::MultiprocessTestHelper::ChildSetup)
     74 
     75 // Use this (and |WaitForChildTestShutdown()|) for the child process's "main()",
     76 // if you want to use |EXPECT_...()| or |ASSERT_...()|; it has a |void| return
     77 // type. (Note that while an |ASSERT_...()| failure will abort the test in the
     78 // child, it will not abort the test in the parent.)
     79 #define MOJO_MULTIPROCESS_TEST_CHILD_TEST(test_child_name) \
     80     void test_child_name ## TestChildTest(); \
     81     MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) { \
     82       test_child_name ## TestChildTest(); \
     83       return (::testing::Test::HasFatalFailure() || \
     84               ::testing::Test::HasNonfatalFailure()) ? 1 : 0; \
     85     } \
     86     void test_child_name ## TestChildTest()
     87 
     88 }  // namespace test
     89 }  // namespace mojo
     90 
     91 #endif  // MOJO_COMMON_TEST_MULTIPROCESS_TEST_HELPER_H_
     92