ipc_test_base.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef IPC_IPC_TEST_BASE_H_
  5. #define IPC_IPC_TEST_BASE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/process/process.h"
  9. #include "base/test/multiprocess_test.h"
  10. #include "base/test/task_environment.h"
  11. #include "build/build_config.h"
  12. #include "ipc/ipc_channel.h"
  13. #include "ipc/ipc_channel_factory.h"
  14. #include "ipc/ipc_channel_proxy.h"
  15. #include "mojo/core/test/mojo_test_base.h"
  16. #include "mojo/core/test/multiprocess_test_helper.h"
  17. class IPCChannelMojoTestBase : public testing::Test {
  18. public:
  19. IPCChannelMojoTestBase();
  20. IPCChannelMojoTestBase(const IPCChannelMojoTestBase&) = delete;
  21. IPCChannelMojoTestBase& operator=(const IPCChannelMojoTestBase&) = delete;
  22. ~IPCChannelMojoTestBase() override;
  23. void Init(const std::string& test_client_name);
  24. bool WaitForClientShutdown();
  25. void TearDown() override;
  26. void CreateChannel(IPC::Listener* listener);
  27. bool ConnectChannel();
  28. void DestroyChannel();
  29. IPC::Sender* sender() { return channel(); }
  30. IPC::Channel* channel() { return channel_.get(); }
  31. const base::Process& client_process() const { return helper_.test_child(); }
  32. protected:
  33. mojo::ScopedMessagePipeHandle TakeHandle();
  34. private:
  35. base::test::SingleThreadTaskEnvironment task_environment_;
  36. mojo::ScopedMessagePipeHandle handle_;
  37. mojo::core::test::MultiprocessTestHelper helper_;
  38. std::unique_ptr<IPC::Channel> channel_;
  39. };
  40. class IpcChannelMojoTestClient {
  41. public:
  42. IpcChannelMojoTestClient();
  43. ~IpcChannelMojoTestClient();
  44. void Init(mojo::ScopedMessagePipeHandle handle);
  45. void Connect(IPC::Listener* listener);
  46. void Close();
  47. IPC::Channel* channel() const { return channel_.get(); }
  48. private:
  49. base::test::SingleThreadTaskEnvironment task_environment_{
  50. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  51. mojo::ScopedMessagePipeHandle handle_;
  52. std::unique_ptr<IPC::Channel> channel_;
  53. };
  54. // Use this to declare the client side for tests using IPCChannelMojoTestBase
  55. // when a custom test fixture class is required in the client. |test_base| must
  56. // be derived from IpcChannelMojoTestClient.
  57. #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(client_name, \
  58. test_base) \
  59. class client_name##_MainFixture : public test_base { \
  60. public: \
  61. void Main(); \
  62. }; \
  63. MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
  64. client_name##TestChildMain, \
  65. ::mojo::core::test::MultiprocessTestHelper::ChildSetup) { \
  66. client_name##_MainFixture test; \
  67. test.Init( \
  68. std::move(mojo::core::test::MultiprocessTestHelper::primordial_pipe)); \
  69. test.Main(); \
  70. return (::testing::Test::HasFatalFailure() || \
  71. ::testing::Test::HasNonfatalFailure()) \
  72. ? 1 \
  73. : 0; \
  74. } \
  75. void client_name##_MainFixture::Main()
  76. // Use this to declare the client side for tests using IPCChannelMojoTestBase.
  77. #define DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT(client_name) \
  78. DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE( \
  79. client_name, IpcChannelMojoTestClient)
  80. #endif // IPC_IPC_TEST_BASE_H_