Home | History | Annotate | Download | only in ipc
      1 // Copyright (c) 2011 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 IPC_IPC_TEST_BASE_H_
      6 #define IPC_IPC_TEST_BASE_H_
      7 
      8 #include <memory>
      9 #include <string>
     10 
     11 #include "base/macros.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/process/process.h"
     14 #include "base/test/multiprocess_test.h"
     15 #include "build/build_config.h"
     16 #include "ipc/ipc_channel.h"
     17 #include "ipc/ipc_channel_factory.h"
     18 #include "ipc/ipc_channel_proxy.h"
     19 #include "mojo/core/test/mojo_test_base.h"
     20 #include "mojo/core/test/multiprocess_test_helper.h"
     21 
     22 namespace base {
     23 class MessageLoop;
     24 }
     25 
     26 class IPCChannelMojoTestBase : public testing::Test {
     27  public:
     28   IPCChannelMojoTestBase();
     29   ~IPCChannelMojoTestBase() override;
     30 
     31   void Init(const std::string& test_client_name);
     32   void InitWithCustomMessageLoop(
     33       const std::string& test_client_name,
     34       std::unique_ptr<base::MessageLoop> message_loop);
     35 
     36   bool WaitForClientShutdown();
     37 
     38   void TearDown() override;
     39 
     40   void CreateChannel(IPC::Listener* listener);
     41 
     42   bool ConnectChannel();
     43 
     44   void DestroyChannel();
     45 
     46   IPC::Sender* sender() { return channel(); }
     47   IPC::Channel* channel() { return channel_.get(); }
     48   const base::Process& client_process() const { return helper_.test_child(); }
     49 
     50  protected:
     51   mojo::ScopedMessagePipeHandle TakeHandle();
     52 
     53  private:
     54   std::unique_ptr<base::MessageLoop> message_loop_;
     55 
     56   mojo::ScopedMessagePipeHandle handle_;
     57   mojo::core::test::MultiprocessTestHelper helper_;
     58 
     59   std::unique_ptr<IPC::Channel> channel_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(IPCChannelMojoTestBase);
     62 };
     63 
     64 class IpcChannelMojoTestClient {
     65  public:
     66   IpcChannelMojoTestClient();
     67   ~IpcChannelMojoTestClient();
     68 
     69   void Init(mojo::ScopedMessagePipeHandle handle);
     70 
     71   void Connect(IPC::Listener* listener);
     72 
     73   void Close();
     74 
     75   IPC::Channel* channel() const { return channel_.get(); }
     76 
     77  private:
     78   base::MessageLoopForIO main_message_loop_;
     79   mojo::ScopedMessagePipeHandle handle_;
     80   std::unique_ptr<IPC::Channel> channel_;
     81 };
     82 
     83 // Use this to declare the client side for tests using IPCChannelMojoTestBase
     84 // when a custom test fixture class is required in the client. |test_base| must
     85 // be derived from IpcChannelMojoTestClient.
     86 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name,   \
     87                                                                 test_base)     \
     88   class client_name##_MainFixture : public test_base {                         \
     89    public:                                                                     \
     90     void Main();                                                               \
     91   };                                                                           \
     92   MULTIPROCESS_TEST_MAIN_WITH_SETUP(                                           \
     93       client_name##TestChildMain,                                              \
     94       ::mojo::core::test::MultiprocessTestHelper::ChildSetup) {                \
     95     client_name##_MainFixture test;                                            \
     96     test.Init(                                                                 \
     97         std::move(mojo::core::test::MultiprocessTestHelper::primordial_pipe)); \
     98     test.Main();                                                               \
     99     return (::testing::Test::HasFatalFailure() ||                              \
    100             ::testing::Test::HasNonfatalFailure())                             \
    101                ? 1                                                             \
    102                : 0;                                                            \
    103   }                                                                            \
    104   void client_name##_MainFixture::Main()
    105 
    106 // Use this to declare the client side for tests using IPCChannelMojoTestBase.
    107 #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name)   \
    108   DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \
    109       client_name, IpcChannelMojoTestClient)
    110 
    111 #endif  // IPC_IPC_TEST_BASE_H_
    112