Mojo: Add a trivial end-to-end (in-process) test of Mojo shell.

Loads a test service and pings it (and then quits).

R=sky@chromium.org

Review URL: https://codereview.chromium.org/314293002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275829 0039d316-1c4b-4281-b951-d872f2087c98
parent 242a963f
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
'mojo_system', 'mojo_system',
'mojo_system_impl', 'mojo_system_impl',
'mojo_system_unittests', 'mojo_system_unittests',
'mojo_test_service',
'mojo_utility', 'mojo_utility',
'mojo_view_manager_lib', 'mojo_view_manager_lib',
'mojo_view_manager_lib_unittests', 'mojo_view_manager_lib_unittests',
...@@ -594,11 +595,13 @@ ...@@ -594,11 +595,13 @@
'mojo_service_manager', 'mojo_service_manager',
'mojo_shell_lib', 'mojo_shell_lib',
'mojo_system_impl', 'mojo_system_impl',
'mojo_test_service_bindings',
], ],
'sources': [ 'sources': [
'shell/child_process_host_unittest.cc', 'shell/child_process_host_unittest.cc',
'shell/shell_test_base.cc', 'shell/shell_test_base.cc',
'shell/shell_test_base.h', 'shell/shell_test_base.h',
'shell/shell_test_base_unittest.cc',
'shell/shell_test_main.cc', 'shell/shell_test_main.cc',
], ],
}, },
......
...@@ -318,6 +318,39 @@ ...@@ -318,6 +318,39 @@
}] }]
], ],
}, },
{
'target_name': 'mojo_test_service_bindings',
'type': 'static_library',
'sources': [
'services/test_service/test_service.mojom',
],
'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ],
'export_dependent_settings': [
'mojo_cpp_bindings',
],
'dependencies': [
'mojo_cpp_bindings',
],
},
{
'target_name': 'mojo_test_service',
'type': 'shared_library',
'dependencies': [
'../base/base.gyp:base',
'mojo_application',
'mojo_common_lib',
'mojo_environment_standalone',
'mojo_test_service_bindings',
'mojo_system_impl',
'mojo_utility',
],
'sources': [
'public/cpp/application/lib/mojo_main_standalone.cc',
'services/test_service/test_service_application.cc',
'services/test_service/test_service_impl.cc',
'services/test_service/test_service_impl.h',
],
},
], ],
'conditions': [ 'conditions': [
['use_aura==1', { ['use_aura==1', {
......
// Copyright 2014 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.
module mojo.test {
interface ITestService {
Ping() => ();
};
} // module mojo.test
// Copyright 2014 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/public/cpp/application/application.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/test_service/test_service_impl.h"
namespace mojo {
namespace test {
namespace {
class TestServiceApplication : public Application {
public:
TestServiceApplication() {}
virtual ~TestServiceApplication() {}
virtual void Initialize() MOJO_OVERRIDE {
AddService<TestServiceImpl>();
}
private:
MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceApplication);
};
} // namespace
} // namespace test
// static
Application* Application::Create() {
return new mojo::test::TestServiceApplication();
}
} // namespace mojo
// Copyright 2014 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/services/test_service/test_service_impl.h"
namespace mojo {
namespace test {
TestServiceImpl::TestServiceImpl() {
}
TestServiceImpl::~TestServiceImpl() {
}
void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) {
callback.Run();
}
} // namespace test
} // namespace mojo
// Copyright 2014 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/public/cpp/application/application.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/test_service/test_service.mojom.h"
namespace mojo {
namespace test {
class TestServiceImpl : public InterfaceImpl<ITestService> {
public:
TestServiceImpl();
virtual ~TestServiceImpl();
// |ITestService| methods:
virtual void Ping(const mojo::Callback<void()>& callback) MOJO_OVERRIDE;
private:
MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceImpl);
};
} // namespace test
} // namespace mojo
...@@ -4,6 +4,15 @@ ...@@ -4,6 +4,15 @@
#include "mojo/shell/shell_test_base.h" #include "mojo/shell/shell_test_base.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "mojo/shell/context.h"
#include "net/base/filename_util.h"
#include "url/gurl.h"
namespace mojo { namespace mojo {
namespace shell { namespace shell {
namespace test { namespace test {
...@@ -14,6 +23,35 @@ ShellTestBase::ShellTestBase() { ...@@ -14,6 +23,35 @@ ShellTestBase::ShellTestBase() {
ShellTestBase::~ShellTestBase() { ShellTestBase::~ShellTestBase() {
} }
void ShellTestBase::InitMojo() {
DCHECK(!message_loop_);
DCHECK(!shell_context_);
message_loop_.reset(new base::MessageLoop());
shell_context_.reset(new Context());
}
void ShellTestBase::LaunchServiceInProcess(
const GURL& service_url,
const std::string& service_name,
ScopedMessagePipeHandle client_handle) {
DCHECK(message_loop_);
DCHECK(shell_context_);
base::FilePath base_dir = base::MakeAbsoluteFilePath(
base::CommandLine::ForCurrentProcess()->GetProgram().DirName());
// On Mac and Windows, libraries are dumped beside the executables.
#if defined(OS_MACOSX) || defined(OS_WIN)
base::FilePath service_dir(base_dir);
#else
// On Linux, they're under lib/.
base::FilePath service_dir(base_dir.AppendASCII("lib"));
#endif
shell_context_->set_mojo_origin(net::FilePathToFileURL(service_dir).spec());
shell_context_->service_manager()->ConnectToService(
service_url, service_name, client_handle.Pass(), GURL());
}
} // namespace test } // namespace test
} // namespace shell } // namespace shell
} // namespace mojo } // namespace mojo
...@@ -5,13 +5,25 @@ ...@@ -5,13 +5,25 @@
#ifndef MOJO_SHELL_SHELL_TEST_BASE_H_ #ifndef MOJO_SHELL_SHELL_TEST_BASE_H_
#define MOJO_SHELL_SHELL_TEST_BASE_H_ #define MOJO_SHELL_SHELL_TEST_BASE_H_
#include "base/at_exit.h" #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/public/cpp/environment/environment.h" #include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/system/core.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
class GURL;
namespace base {
class MessageLoop;
}
namespace mojo { namespace mojo {
namespace shell { namespace shell {
class Context;
namespace test { namespace test {
class ShellTestBase : public testing::Test { class ShellTestBase : public testing::Test {
...@@ -19,15 +31,31 @@ class ShellTestBase : public testing::Test { ...@@ -19,15 +31,31 @@ class ShellTestBase : public testing::Test {
ShellTestBase(); ShellTestBase();
virtual ~ShellTestBase(); virtual ~ShellTestBase();
// Should be called before any of the methods below are called.
void InitMojo();
// Launches the given service in-process; |service_url| should typically be a
// mojo: URL (the origin will be set to an "appropriate" file: URL).
void LaunchServiceInProcess(const GURL& service_url,
const std::string& service_name,
ScopedMessagePipeHandle client_handle);
base::MessageLoop* message_loop() { return message_loop_.get(); }
Context* shell_context() { return shell_context_.get(); }
private: private:
base::ShadowingAtExitManager at_exit_manager_;
Environment environment_; Environment environment_;
// Only set if/when |InitMojo()| is called.
scoped_ptr<base::MessageLoop> message_loop_;
scoped_ptr<Context> shell_context_;
DISALLOW_COPY_AND_ASSIGN(ShellTestBase); DISALLOW_COPY_AND_ASSIGN(ShellTestBase);
}; };
} // namespace test } // namespace test
} // namespace shell } // namespace shell
} // namespace mojo } // namespace mojo
#endif // MOJO_SYSTEM_SHELL_SHELLASE_H_ #endif // MOJO_SHELL_SHELL_TEST_BASE_H_
// Copyright 2014 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/shell/shell_test_base.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/services/test_service/test_service.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace mojo {
namespace shell {
namespace test {
namespace {
typedef ShellTestBase ShellTestBaseTest;
void PingCallback(base::MessageLoop* message_loop) {
VLOG(2) << "Ping callback";
message_loop->QuitWhenIdle();
}
TEST_F(ShellTestBaseTest, LaunchServiceInProcess) {
InitMojo();
InterfacePtr<mojo::test::ITestService> test_service;
{
MessagePipe mp;
test_service.Bind(mp.handle0.Pass());
LaunchServiceInProcess(GURL("mojo:mojo_test_service"),
mojo::test::ITestService::Name_,
mp.handle1.Pass());
}
test_service->Ping(base::Bind(&PingCallback,
base::Unretained(message_loop())));
message_loop()->Run();
test_service.reset();
// This will run until the test service has actually quit (which it will,
// since we killed the only connection to it).
message_loop()->Run();
}
} // namespace
} // namespace test
} // namespace shell
} // namespace mojo
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment