diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 00:49:35 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 00:49:35 +0000 |
commit | 705d965566d9906601ddc3420f30dc84c9a9499e (patch) | |
tree | 9f6ea25da000d7e20bedc34b30ea8bb815dfc87d /mojo/common | |
parent | d7d434738efafbd212ae7a9b8b0e3f8148632b01 (diff) | |
download | chromium_src-705d965566d9906601ddc3420f30dc84c9a9499e.zip chromium_src-705d965566d9906601ddc3420f30dc84c9a9499e.tar.gz chromium_src-705d965566d9906601ddc3420f30dc84c9a9499e.tar.bz2 |
Mojo: MultiprocessTestHelper: Allow {ASSERT,EXPECT}_...() to be used in child.
Add a new macro, MOJO_MULTIPROCESS_TEST_CHILD_TEST() to define the test
body in the child process, where ASSERT_...() and EXPECT_...() can be
used as expected. Add a new method, WaitForChildTestShutdown(), to
MultiprocessTestHelper, which returns true if everything passed in the
child. (Keep the old macro and old WaitForChildShutdown() around, since
sometimes we want to use the child's exit code to report other
information.)
R=yzshen@chromium.org
Review URL: https://codereview.chromium.org/194593007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256373 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r-- | mojo/common/test/multiprocess_test_helper.cc | 4 | ||||
-rw-r--r-- | mojo/common/test/multiprocess_test_helper.h | 22 | ||||
-rw-r--r-- | mojo/common/test/multiprocess_test_helper_unittest.cc | 68 |
3 files changed, 68 insertions, 26 deletions
diff --git a/mojo/common/test/multiprocess_test_helper.cc b/mojo/common/test/multiprocess_test_helper.cc index 90f7250..a59a25b 100644 --- a/mojo/common/test/multiprocess_test_helper.cc +++ b/mojo/common/test/multiprocess_test_helper.cc @@ -66,6 +66,10 @@ int MultiprocessTestHelper::WaitForChildShutdown() { return rv; } +bool MultiprocessTestHelper::WaitForChildTestShutdown() { + return WaitForChildShutdown() == 0; +} + // static void MultiprocessTestHelper::ChildSetup() { CHECK(CommandLine::InitializedForCurrentProcess()); diff --git a/mojo/common/test/multiprocess_test_helper.h b/mojo/common/test/multiprocess_test_helper.h index 103dcf4..db5450c 100644 --- a/mojo/common/test/multiprocess_test_helper.h +++ b/mojo/common/test/multiprocess_test_helper.h @@ -29,7 +29,8 @@ class MultiprocessTestHelper { ~MultiprocessTestHelper(); // Start a child process and run the "main" function "named" |test_child_name| - // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| (below). + // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| or + // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()| (below). void StartChild(const std::string& test_child_name); // Wait for the child process to terminate. // Returns the exit code of the child process. Note that, though it's declared @@ -39,6 +40,12 @@ class MultiprocessTestHelper { // 0-127 being preserved, and -1 being outside the range 0-127. int WaitForChildShutdown(); + // Like |WaitForChildShutdown()|, but returns true on success (exit code of 0) + // and false otherwise. You probably want to do something like + // |EXPECT_TRUE(WaitForChildTestShutdown());|. Mainly for use with + // |MOJO_MULTIPROCESS_TEST_CHILD_TEST()|. + bool WaitForChildTestShutdown(); + // For use by |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| only: static void ChildSetup(); @@ -65,6 +72,19 @@ class MultiprocessTestHelper { test_child_name ## TestChildMain, \ ::mojo::test::MultiprocessTestHelper::ChildSetup) +// Use this (and |WaitForChildTestShutdown()|) for the child process's "main()", +// if you want to use |EXPECT_...()| or |ASSERT_...()|; it has a |void| return +// type. (Note that while an |ASSERT_...()| failure will abort the test in the +// child, it will not abort the test in the parent.) +#define MOJO_MULTIPROCESS_TEST_CHILD_TEST(test_child_name) \ + void test_child_name ## TestChildTest(); \ + MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) { \ + test_child_name ## TestChildTest(); \ + return (::testing::Test::HasFatalFailure() || \ + ::testing::Test::HasNonfatalFailure()) ? 1 : 0; \ + } \ + void test_child_name ## TestChildTest() + } // namespace test } // namespace mojo diff --git a/mojo/common/test/multiprocess_test_helper_unittest.cc b/mojo/common/test/multiprocess_test_helper_unittest.cc index a61ac22..6df1e07 100644 --- a/mojo/common/test/multiprocess_test_helper_unittest.cc +++ b/mojo/common/test/multiprocess_test_helper_unittest.cc @@ -14,26 +14,10 @@ #include <fcntl.h> #endif -#if defined(OS_WIN) -#include "base/win/windows_version.h" -#endif - namespace mojo { namespace test { namespace { -// Returns true and logs a warning on Windows prior to Vista. -bool SkipTest() { -#if defined(OS_WIN) - if (base::win::GetVersion() < base::win::VERSION_VISTA) { - LOG(WARNING) << "Test skipped: Vista or later needed."; - return true; - } -#endif - - return false; -} - bool IsNonBlocking(const embedder::PlatformHandle& handle) { #if defined(OS_WIN) // Haven't figured out a way to query whether a HANDLE was created with @@ -59,9 +43,6 @@ bool ReadByte(const embedder::PlatformHandle& handle, char* c) { typedef testing::Test MultiprocessTestHelperTest; TEST_F(MultiprocessTestHelperTest, RunChild) { - if (SkipTest()) - return; - MultiprocessTestHelper helper; EXPECT_TRUE(helper.server_platform_handle.is_valid()); @@ -75,9 +56,6 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) { } TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) { - if (SkipTest()) - return; - MultiprocessTestHelper helper; helper.StartChild("NoSuchTestChildMain"); int result = helper.WaitForChildShutdown(); @@ -85,9 +63,6 @@ TEST_F(MultiprocessTestHelperTest, TestChildMainNotFound) { } TEST_F(MultiprocessTestHelperTest, PassedChannel) { - if (SkipTest()) - return; - MultiprocessTestHelper helper; EXPECT_TRUE(helper.server_platform_handle.is_valid()); helper.StartChild("PassedChannel"); @@ -134,6 +109,49 @@ MOJO_MULTIPROCESS_TEST_CHILD_MAIN(PassedChannel) { return static_cast<int>(c); } +TEST_F(MultiprocessTestHelperTest, ChildTestPasses) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestPasses"); + EXPECT_TRUE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestPasses) { + ASSERT_TRUE(MultiprocessTestHelper::client_platform_handle.is_valid()); + EXPECT_TRUE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())); +} + +TEST_F(MultiprocessTestHelperTest, ChildTestFailsAssert) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestFailsAssert"); + EXPECT_FALSE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsAssert) { + ASSERT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) + << "DISREGARD: Expected failure in child process"; + ASSERT_FALSE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())) << "Not reached"; + CHECK(false) << "Not reached"; +} + +TEST_F(MultiprocessTestHelperTest, ChildTestFailsExpect) { + MultiprocessTestHelper helper; + EXPECT_TRUE(helper.server_platform_handle.is_valid()); + helper.StartChild("ChildTestFailsExpect"); + EXPECT_FALSE(helper.WaitForChildTestShutdown()); +} + +MOJO_MULTIPROCESS_TEST_CHILD_TEST(ChildTestFailsExpect) { + EXPECT_FALSE(MultiprocessTestHelper::client_platform_handle.is_valid()) + << "DISREGARD: Expected failure #1 in child process"; + EXPECT_FALSE(IsNonBlocking( + MultiprocessTestHelper::client_platform_handle.get())) + << "DISREGARD: Expected failure #2 in child process"; +} + } // namespace } // namespace test } // namespace mojo |