summaryrefslogtreecommitdiffstats
path: root/mojo/common
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-03 17:47:24 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-03 17:47:24 +0000
commitd16a5b4dd2c80719e1b0e1c05e8dea0a004b9b67 (patch)
tree1601f5dce7443e550038831e7152b61c46e2c302 /mojo/common
parentcece099151206e8e1c4d1166dad86c71d3092f1e (diff)
downloadchromium_src-d16a5b4dd2c80719e1b0e1c05e8dea0a004b9b67.zip
chromium_src-d16a5b4dd2c80719e1b0e1c05e8dea0a004b9b67.tar.gz
chromium_src-d16a5b4dd2c80719e1b0e1c05e8dea0a004b9b67.tar.bz2
Mojo: Add a Mojo-specific multiprocess test base class.
This sets up (most of) the infrastructure that'll be needed to set up a PlatformChannel between parent and child processes. R=darin@chromium.org, darin Review URL: https://codereview.chromium.org/99473007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238425 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r--mojo/common/test/multiprocess_test_base.cc104
-rw-r--r--mojo/common/test/multiprocess_test_base.h69
-rw-r--r--mojo/common/test/multiprocess_test_base_unittest.cc29
3 files changed, 202 insertions, 0 deletions
diff --git a/mojo/common/test/multiprocess_test_base.cc b/mojo/common/test/multiprocess_test_base.cc
new file mode 100644
index 0000000..85aaa76
--- /dev/null
+++ b/mojo/common/test/multiprocess_test_base.cc
@@ -0,0 +1,104 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/test/multiprocess_test_base.h"
+
+#include "base/command_line.h"
+#include "base/logging.h"
+#include "base/process/kill.h"
+#include "base/process/process_handle.h"
+#include "build/build_config.h"
+
+namespace mojo {
+namespace test {
+
+MultiprocessTestBase::MultiprocessTestBase()
+ : test_child_handle_(base::kNullProcessHandle) {
+}
+
+MultiprocessTestBase::~MultiprocessTestBase() {
+ CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
+}
+
+void MultiprocessTestBase::SetUp() {
+ CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
+
+ MultiProcessTest::SetUp();
+
+// TODO(vtl): Not implemented on Windows yet.
+#if defined(OS_POSIX)
+ platform_server_channel_ =
+ system::PlatformServerChannel::Create("TestChannel");
+#endif
+}
+
+void MultiprocessTestBase::TearDown() {
+ CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
+
+ platform_server_channel_.reset();
+
+ MultiProcessTest::TearDown();
+}
+
+void MultiprocessTestBase::StartChild(const std::string& test_child_name) {
+ CHECK(platform_server_channel_.get());
+ CHECK(!test_child_name.empty());
+ CHECK_EQ(test_child_handle_, base::kNullProcessHandle);
+
+ std::string test_child_main = test_child_name + "TestChildMain";
+
+#if defined(OS_POSIX)
+ CommandLine unused(CommandLine::NO_PROGRAM);
+ base::FileHandleMappingVector fds_to_map;
+ platform_server_channel_->GetDataNeededToPassClientChannelToChildProcess(
+ &unused, &fds_to_map);
+ test_child_handle_ = SpawnChild(test_child_main, fds_to_map, false);
+#elif defined(OS_WIN)
+ test_child_handle_ = SpawnChild(test_child_main, false);
+#else
+#error "Not supported yet."
+#endif
+// TODO(vtl): Not implemented on Windows yet.
+#if defined(OS_POSIX)
+ platform_server_channel_->ChildProcessLaunched();
+#endif
+
+ CHECK_NE(test_child_handle_, base::kNullProcessHandle);
+}
+
+int MultiprocessTestBase::WaitForChildShutdown() {
+ CHECK_NE(test_child_handle_, base::kNullProcessHandle);
+
+ static const int kTimeoutSeconds = 5;
+ int rv = -1;
+ CHECK(base::WaitForExitCodeWithTimeout(
+ test_child_handle_, &rv, base::TimeDelta::FromSeconds(kTimeoutSeconds)));
+ base::CloseProcessHandle(test_child_handle_);
+ test_child_handle_ = base::kNullProcessHandle;
+ return rv;
+}
+
+CommandLine MultiprocessTestBase::MakeCmdLine(const std::string& procname,
+ bool debug_on_start) {
+ CHECK(platform_server_channel_.get());
+
+ CommandLine command_line =
+ base::MultiProcessTest::MakeCmdLine(procname, debug_on_start);
+// TODO(vtl): Not implemented on Windows yet.
+#if defined(OS_POSIX)
+ base::FileHandleMappingVector unused;
+ platform_server_channel_->GetDataNeededToPassClientChannelToChildProcess(
+ &command_line, &unused);
+#endif
+ return command_line;
+}
+
+// static
+void MultiprocessTestBase::ChildSetup() {
+ // TODO(vtl)
+ NOTIMPLEMENTED();
+}
+
+} // namespace test
+} // namespace mojo
diff --git a/mojo/common/test/multiprocess_test_base.h b/mojo/common/test/multiprocess_test_base.h
new file mode 100644
index 0000000..08b66a3
--- /dev/null
+++ b/mojo/common/test/multiprocess_test_base.h
@@ -0,0 +1,69 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
+#define MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/process/process_handle.h"
+#include "base/test/multiprocess_test.h"
+#include "mojo/system/platform_channel.h"
+#include "testing/multiprocess_func_list.h"
+
+namespace mojo {
+namespace test {
+
+class MultiprocessTestBase : public base::MultiProcessTest {
+ public:
+ MultiprocessTestBase();
+ virtual ~MultiprocessTestBase();
+
+ virtual void SetUp() OVERRIDE;
+ virtual void TearDown() OVERRIDE;
+
+ // Start a child process and run the "main" function "named" |test_child_name|
+ // declared using |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| (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
+ // to be an |int|, the exit code is subject to mangling by the OS. E.g., we
+ // usually return -1 on error in the child (e.g., if |test_child_name| was not
+ // found), but this is mangled to 255 on Linux. You should only rely on codes
+ // 0-127 being preserved, and -1 being outside the range 0-127.
+ int WaitForChildShutdown();
+
+ // For use by |MOJO_MULTIPROCESS_TEST_CHILD_MAIN()| only:
+ static void ChildSetup();
+
+ system::PlatformServerChannel* platform_server_channel() {
+ return platform_server_channel_.get();
+ }
+
+ private:
+ virtual CommandLine MakeCmdLine(const std::string& procname,
+ bool debug_on_start) OVERRIDE;
+
+ // Valid after |StartChild()| and before |WaitForChildShutdown()|.
+ base::ProcessHandle test_child_handle_;
+
+ scoped_ptr<system::PlatformServerChannel> platform_server_channel_;
+
+ DISALLOW_COPY_AND_ASSIGN(MultiprocessTestBase);
+};
+
+// Use this to declare the child process's "main()" function for tests using
+// |MultiprocessTestBase|. It returns an |int|, which will be the process's exit
+// code (but see the comment about |WaitForChildShutdown()|).
+#define MOJO_MULTIPROCESS_TEST_CHILD_MAIN(test_child_name) \
+ MULTIPROCESS_TEST_MAIN_WITH_SETUP( \
+ test_child_name ## TestChildMain, \
+ ::mojo::test::MultiprocessTestBase::ChildSetup)
+
+} // namespace test
+} // namespace mojo
+
+#endif // MOJO_COMMON_TEST_MULTIPROCESS_TEST_BASE_H_
diff --git a/mojo/common/test/multiprocess_test_base_unittest.cc b/mojo/common/test/multiprocess_test_base_unittest.cc
new file mode 100644
index 0000000..681d8bc4
--- /dev/null
+++ b/mojo/common/test/multiprocess_test_base_unittest.cc
@@ -0,0 +1,29 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/test/multiprocess_test_base.h"
+
+namespace mojo {
+namespace {
+
+class MultiprocessTestBaseTest : public test::MultiprocessTestBase {
+};
+
+TEST_F(MultiprocessTestBaseTest, RunChild) {
+ StartChild("RunChild");
+ EXPECT_EQ(123, WaitForChildShutdown());
+}
+
+MOJO_MULTIPROCESS_TEST_CHILD_MAIN(RunChild) {
+ return 123;
+}
+
+TEST_F(MultiprocessTestBaseTest, TestChildMainNotFound) {
+ StartChild("NoSuchTestChildMain");
+ int result = WaitForChildShutdown();
+ EXPECT_FALSE(result >= 0 && result <= 127);
+}
+
+} // namespace
+} // namespace mojo