tuple: update to make use of C++11
[chromium-blink-merge.git] / mojo / edk / test / multiprocess_test_helper_unittest.cc
blob93496fb68dbbe97fb2857a1bbe25d20fa8882f40
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.
5 #include "mojo/edk/test/multiprocess_test_helper.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "mojo/edk/embedder/scoped_platform_handle.h"
10 #include "mojo/edk/test/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 #if defined(OS_POSIX)
14 #include <fcntl.h>
15 #endif
17 namespace mojo {
18 namespace test {
19 namespace {
21 bool IsNonBlocking(const embedder::PlatformHandle& handle) {
22 #if defined(OS_WIN)
23 // Haven't figured out a way to query whether a HANDLE was created with
24 // FILE_FLAG_OVERLAPPED.
25 return true;
26 #else
27 return fcntl(handle.fd, F_GETFL) & O_NONBLOCK;
28 #endif
31 bool WriteByte(const embedder::PlatformHandle& handle, char c) {
32 size_t bytes_written = 0;
33 BlockingWrite(handle, &c, 1, &bytes_written);
34 return bytes_written == 1;
37 bool ReadByte(const embedder::PlatformHandle& handle, char* c) {
38 size_t bytes_read = 0;
39 BlockingRead(handle, c, 1, &bytes_read);
40 return bytes_read == 1;
43 typedef testing::Test MultiprocessTestHelperTest;
45 #if defined(OS_ANDROID)
46 // Android multi-process tests are not executing the new process. This is flaky.
47 #define MAYBE_RunChild DISABLED_RunChild
48 #else
49 #define MAYBE_RunChild RunChild
50 #endif // defined(OS_ANDROID)
51 TEST_F(MultiprocessTestHelperTest, MAYBE_RunChild) {
52 MultiprocessTestHelper helper;
53 EXPECT_TRUE(helper.server_platform_handle.is_valid());
55 helper.StartChild("RunChild");
56 EXPECT_EQ(123, helper.WaitForChildShutdown());
59 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) {
60 CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
61 return 123;
64 #if defined(OS_ANDROID)
65 // Android multi-process tests are not executing the new process. This is flaky.
66 #define MAYBE_TestChildMainNotFound DISABLED_TestChildMainNotFound
67 #else
68 #define MAYBE_TestChildMainNotFound TestChildMainNotFound
69 #endif // defined(OS_ANDROID)
70 TEST_F(MultiprocessTestHelperTest, MAYBE_TestChildMainNotFound) {
71 MultiprocessTestHelper helper;
72 helper.StartChild("NoSuchTestChildMain");
73 int result = helper.WaitForChildShutdown();
74 EXPECT_FALSE(result >= 0 && result <= 127);
77 #if defined(OS_ANDROID)
78 // Android multi-process tests are not executing the new process. This is flaky.
79 #define MAYBE_PassedChannel DISABLED_PassedChannel
80 #else
81 #define MAYBE_PassedChannel PassedChannel
82 #endif // defined(OS_ANDROID)
83 TEST_F(MultiprocessTestHelperTest, MAYBE_PassedChannel) {
84 MultiprocessTestHelper helper;
85 EXPECT_TRUE(helper.server_platform_handle.is_valid());
86 helper.StartChild("PassedChannel");
88 // Take ownership of the handle.
89 embedder::ScopedPlatformHandle handle = helper.server_platform_handle.Pass();
91 // The handle should be non-blocking.
92 EXPECT_TRUE(IsNonBlocking(handle.get()));
94 // Write a byte.
95 const char c = 'X';
96 EXPECT_TRUE(WriteByte(handle.get(), c));
98 // It'll echo it back to us, incremented.
99 char d = 0;
100 EXPECT_TRUE(ReadByte(handle.get(), &d));
101 EXPECT_EQ(c + 1, d);
103 // And return it, incremented again.
104 EXPECT_EQ(c + 2, helper.WaitForChildShutdown());
107 MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) {
108 CHECK(MultiprocessTestHelper::client_platform_handle.is_valid());
110 // Take ownership of the handle.
111 embedder::ScopedPlatformHandle handle =
112 MultiprocessTestHelper::client_platform_handle.Pass();
114 // The handle should be non-blocking.
115 EXPECT_TRUE(IsNonBlocking(handle.get()));
117 // Read a byte.
118 char c = 0;
119 EXPECT_TRUE(ReadByte(handle.get(), &c));
121 // Write it back, incremented.
122 c++;
123 EXPECT_TRUE(WriteByte(handle.get(), c));
125 // And return it, incremented again.
126 c++;
127 return static_cast<int>(c);
130 #if defined(OS_ANDROID)
131 // Android multi-process tests are not executing the new process. This is flaky.
132 #define MAYBE_ChildTestPasses DISABLED_ChildTestPasses
133 #else
134 #define MAYBE_ChildTestPasses ChildTestPasses
135 #endif // defined(OS_ANDROID)
136 TEST_F(MultiprocessTestHelperTest, MAYBE_ChildTestPasses) {
137 MultiprocessTestHelper helper;
138 EXPECT_TRUE(helper.server_platform_handle.is_valid());
139 helper.StartChild("ChildTestPasses");
140 EXPECT_TRUE(helper.WaitForChildTestShutdown());
143 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses) {
144 ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle.is_valid());
145 EXPECT_TRUE(
146 IsNonBlocking(MultiprocessTestHelper::client_platform_handle.get()));
149 #if defined(OS_ANDROID)
150 // Android multi-process tests are not executing the new process. This is flaky.
151 #define MAYBE_ChildTestFailsAssert DISABLED_ChildTestFailsAssert
152 #else
153 #define MAYBE_ChildTestFailsAssert ChildTestFailsAssert
154 #endif // defined(OS_ANDROID)
155 TEST_F(MultiprocessTestHelperTest, MAYBE_ChildTestFailsAssert) {
156 MultiprocessTestHelper helper;
157 EXPECT_TRUE(helper.server_platform_handle.is_valid());
158 helper.StartChild("ChildTestFailsAssert");
159 EXPECT_FALSE(helper.WaitForChildTestShutdown());
162 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert) {
163 ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid())
164 << "DISREGARD: Expected failure in child process";
165 ASSERT_FALSE(
166 IsNonBlocking(MultiprocessTestHelper::client_platform_handle.get()))
167 << "Not reached";
168 CHECK(false) << "Not reached";
171 #if defined(OS_ANDROID)
172 // Android multi-process tests are not executing the new process. This is flaky.
173 #define MAYBE_ChildTestFailsExpect DISABLED_ChildTestFailsExpect
174 #else
175 #define MAYBE_ChildTestFailsExpect ChildTestFailsExpect
176 #endif // defined(OS_ANDROID)
177 TEST_F(MultiprocessTestHelperTest, MAYBE_ChildTestFailsExpect) {
178 MultiprocessTestHelper helper;
179 EXPECT_TRUE(helper.server_platform_handle.is_valid());
180 helper.StartChild("ChildTestFailsExpect");
181 EXPECT_FALSE(helper.WaitForChildTestShutdown());
184 MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect) {
185 EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid())
186 << "DISREGARD: Expected failure #1 in child process";
187 EXPECT_FALSE(
188 IsNonBlocking(MultiprocessTestHelper::client_platform_handle.get()))
189 << "DISREGARD: Expected failure #2 in child process";
192 } // namespace
193 } // namespace test
194 } // namespace mojo