Commit 39ebdbb2 authored by msw@chromium.org's avatar msw@chromium.org

Add a Mojo example apptest that runs in mojo_shell.

Add mojo/example/apptest with a service and client.
Add example_apptest.cc with a GTest Mojo application.

These tests can be run from out/<Debug|Release>/ via:
./mojo_shell --origin=file://`pwd` --disable-cache mojo:mojo_example_apptests

BUG=392646
TEST=The new mojo_example_apptests passes.
R=ben@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#288250}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288250 0039d316-1c4b-4281-b951-d872f2087c98
parent 1e721e79
// 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/examples/apptest/example_client_impl.h"
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// TODO(msw): Remove this once we can get ApplicationImpl from TLS.
mojo::ApplicationImpl* g_application_impl_hack = NULL;
} // namespace
namespace mojo {
namespace {
class ExampleServiceTest : public testing::Test {
public:
ExampleServiceTest() {
g_application_impl_hack->ConnectToService("mojo:mojo_example_service",
&example_service_);
example_service_.set_client(&example_client_);
}
virtual ~ExampleServiceTest() MOJO_OVERRIDE {}
protected:
ExampleServicePtr example_service_;
ExampleClientImpl example_client_;
private:
MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest);
};
TEST_F(ExampleServiceTest, Ping) {
EXPECT_EQ(0, example_client_.last_pong_value());
example_service_->Ping(1);
mojo::RunLoop::current()->Run();
EXPECT_EQ(1, example_client_.last_pong_value());
}
template <typename T>
struct SetAndQuit : public Callback<void()>::Runnable {
SetAndQuit(T* val, T result) : val_(val), result_(result) {}
virtual ~SetAndQuit() {}
virtual void Run() const MOJO_OVERRIDE{
*val_ = result_;
mojo::RunLoop::current()->Quit();
}
T* val_;
T result_;
};
TEST_F(ExampleServiceTest, RunCallback) {
bool was_run = false;
example_service_->RunCallback(SetAndQuit<bool>(&was_run, true));
mojo::RunLoop::current()->Run();
EXPECT_TRUE(was_run);
}
} // namespace
} // namespace mojo
extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain(
MojoHandle shell_handle) {
mojo::Environment env;
mojo::RunLoop loop;
mojo::ApplicationDelegate* delegate = mojo::ApplicationDelegate::Create();
mojo::ApplicationImpl app(delegate);
app.BindShell(shell_handle);
g_application_impl_hack = &app;
// TODO(msw): Get actual commandline arguments.
int argc = 0;
char** argv = NULL;
testing::InitGoogleTest(&argc, argv);
mojo_ignore_result(RUN_ALL_TESTS());
delete delegate;
return MOJO_RESULT_OK;
}
// 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/examples/apptest/example_client_application.h"
#include "mojo/examples/apptest/example_client_impl.h"
namespace mojo {
ExampleClientApplication::ExampleClientApplication() {}
ExampleClientApplication::~ExampleClientApplication() {}
void ExampleClientApplication::Initialize(ApplicationImpl* app) {}
// static
ApplicationDelegate* ApplicationDelegate::Create() {
return new ExampleClientApplication();
}
} // 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.
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_APPLICATION_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_APPLICATION_H_
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/interface_factory.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
class ExampleClientApplication : public ApplicationDelegate {
public:
ExampleClientApplication();
virtual ~ExampleClientApplication();
private:
// ApplicationDelegate implementation.
virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE;
MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientApplication);
};
} // namespace mojo
#endif // MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_APPLICATION_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/examples/apptest/example_client_impl.h"
#include "mojo/public/cpp/utility/run_loop.h"
namespace mojo {
ExampleClientImpl::ExampleClientImpl() : last_pong_value_(0) {}
ExampleClientImpl::~ExampleClientImpl() {}
void ExampleClientImpl::Pong(uint16_t pong_value) {
last_pong_value_ = pong_value;
mojo::RunLoop::current()->Quit();
}
} // 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.
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_IMPL_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_IMPL_H_
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
class ApplicationConnection;
class ExampleClientImpl : public InterfaceImpl<ExampleClient> {
public:
explicit ExampleClientImpl();
virtual ~ExampleClientImpl();
int16_t last_pong_value() const { return last_pong_value_; }
private:
// InterfaceImpl<ExampleClient> overrides.
virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE;
int16_t last_pong_value_;
MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl);
};
} // namespace mojo
#endif // MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_IMPL_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.
module mojo {
// A simple service used to exemplify application testing within mojo_shell.
[Client=ExampleClient]
interface ExampleService {
// Calls ExampleClient::Pong with |ping_value|.
Ping(uint16 ping_value);
// Runs the argument callback.
RunCallback() => ();
};
interface ExampleClient {
Pong(uint16 pong_value);
};
}
// 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/examples/apptest/example_service_application.h"
#include "mojo/public/cpp/application/application_connection.h"
namespace mojo {
ExampleServiceApplication::ExampleServiceApplication() {}
ExampleServiceApplication::~ExampleServiceApplication() {}
bool ExampleServiceApplication::ConfigureIncomingConnection(
ApplicationConnection* connection) {
connection->AddService(&example_service_factory_);
return true;
}
// static
ApplicationDelegate* ApplicationDelegate::Create() {
return new ExampleServiceApplication();
}
} // 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.
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_APPLICATION_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_APPLICATION_H_
#include "mojo/examples/apptest/example_service_impl.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/interface_factory_impl.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
class ApplicationConnection;
class ExampleServiceApplication : public ApplicationDelegate {
public:
ExampleServiceApplication();
virtual ~ExampleServiceApplication();
private:
// ApplicationDelegate implementation.
virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
MOJO_OVERRIDE;
InterfaceFactoryImpl<ExampleServiceImpl> example_service_factory_;
MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceApplication);
};
} // namespace mojo
#endif // MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_APPLICATION_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/examples/apptest/example_service_impl.h"
namespace mojo {
ExampleServiceImpl::ExampleServiceImpl() {}
ExampleServiceImpl::~ExampleServiceImpl() {}
void ExampleServiceImpl::Ping(uint16_t ping_value) {
client()->Pong(ping_value);
}
void ExampleServiceImpl::RunCallback(const Callback<void()>& callback) {
callback.Run();
}
} // 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.
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_IMPL_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_IMPL_H_
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
class ApplicationConnection;
class ExampleServiceImpl : public InterfaceImpl<ExampleService> {
public:
ExampleServiceImpl();
virtual ~ExampleServiceImpl();
private:
// InterfaceImpl<ExampleService> overrides.
virtual void Ping(uint16_t ping_value) MOJO_OVERRIDE;
virtual void RunCallback(const Callback<void()>& callback) MOJO_OVERRIDE;
MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceImpl);
};
} // namespace mojo
#endif // MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_IMPL_H_
......@@ -29,6 +29,8 @@
'mojo_compositor_app',
'mojo_echo_client',
'mojo_echo_service',
'mojo_example_apptests',
'mojo_example_service',
'mojo_geometry_lib',
'mojo_html_viewer',
'mojo_js',
......
......@@ -96,6 +96,66 @@
},
'includes': [ 'build/package_app.gypi' ],
},
{
'target_name': 'mojo_example_service_bindings',
'type': 'static_library',
'sources': [
'examples/apptest/example_service.mojom',
],
'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ],
'export_dependent_settings': [
'mojo_base.gyp:mojo_cpp_bindings',
],
'dependencies': [
'mojo_base.gyp:mojo_cpp_bindings',
],
},
{
'target_name': 'mojo_example_service',
'type': 'loadable_module',
'dependencies': [
'mojo_base.gyp:mojo_application_standalone', # For ApplicationDelegate.
'mojo_base.gyp:mojo_cpp_bindings', # For *.mojom.h
'mojo_base.gyp:mojo_environment_standalone', # For Environment.
'mojo_example_service_bindings',
'mojo_base.gyp:mojo_utility', # For RunLoop.
'<(mojo_system_for_loadable_module)',
],
'sources': [
'examples/apptest/example_service_application.cc',
'examples/apptest/example_service_application.h',
'examples/apptest/example_service_impl.cc',
'examples/apptest/example_service_impl.h',
'public/cpp/application/lib/mojo_main_standalone.cc',
],
},
{
'target_name': 'mojo_example_apptests',
'type': 'loadable_module',
'dependencies': [
'../testing/gtest.gyp:gtest',
'mojo_base.gyp:mojo_application_standalone', # For ApplicationDelegate.
'mojo_base.gyp:mojo_environment_standalone', # For Environment.
'mojo_example_service',
'mojo_example_service_bindings',
'mojo_base.gyp:mojo_utility', # For RunLoop.
'<(mojo_system_for_loadable_module)',
],
'sources': [
'examples/apptest/example_apptest.cc',
'examples/apptest/example_client_application.cc',
'examples/apptest/example_client_application.h',
'examples/apptest/example_client_impl.cc',
'examples/apptest/example_client_impl.h',
],
},
{
'target_name': 'package_mojo_example_apptests',
'variables': {
'app_name': 'mojo_example_apptests',
},
'includes': [ 'build/package_app.gypi' ],
},
{
'target_name': 'mojo_compositor_app',
'type': 'loadable_module',
......
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