Skip to content

Commit

Permalink
Hold sample apps in their location in the Mojo repo
Browse files Browse the repository at this point in the history
  • Loading branch information
colinblundell committed Nov 4, 2014
1 parent bb91a10 commit a3f7c47
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 109 deletions.
2 changes: 1 addition & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ group("root") {

deps = [
"//mojo/public",
"//sample_apps"
"//mojo/examples"
]
}
3 changes: 2 additions & 1 deletion build/install-build-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ install_dep_from_tarfile $MARKUPSAFE_SRC_URL 'markupsafe'
mkdir -p $THIRD_PARTY_DIR/cython
cd $THIRD_PARTY_DIR/cython
curl --remote-name http://cython.org/release/Cython-0.20.2.zip
unzip Cython-0.20.2.zip -d src
unzip Cython-0.20.2.zip
rm -rf Cython-0.20.2.zip
mv Cython-0.20.2 src

# Install the Mojo shell
$BUILD_DIR/download_mojo_shell.py
33 changes: 33 additions & 0 deletions mojo/examples/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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.

import("//build/config/ui.gni")

group("examples") {
testonly = true

deps = [
"//mojo/examples/apptest",
#"//mojo/examples/content_handler_demo",
"//mojo/examples/echo",
#"//mojo/examples/http_server",
#"//mojo/examples/png_viewer",
#"//mojo/examples/sample_app",
#"//mojo/examples/surfaces_app",
#"//mojo/examples/wget",
]

# if (use_aura) {
# deps += [
# "//mojo/examples/aura_demo:mojo_aura_demo",
# "//mojo/examples/browser",
# "//mojo/examples/embedded_app",
# "//mojo/examples/media_viewer",
# "//mojo/examples/nesting_app",
# "//mojo/examples/keyboard",
# "//mojo/examples/window_manager",
# "//mojo/examples/wm_flow",
# ]
# }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ group("apptest") {
}

shared_library("service") {
output_name = "example_service"
output_name = "mojo_example_service"

sources = [
"example_service_application.cc",
Expand All @@ -32,7 +32,7 @@ shared_library("service") {
}

shared_library("apptests") {
output_name = "example_apptests"
output_name = "mojo_example_apptests"

testonly = true

Expand All @@ -46,12 +46,11 @@ shared_library("apptests") {

deps = [
":bindings",
"//testing/gtest",
"//mojo/public/c/system:for_shared_library",
"//mojo/public/cpp/application:standalone",
"//mojo/public/cpp/application:test_support",
"//mojo/public/cpp/utility",
]

datadeps = [ ":service", ]
}

mojom("bindings") {
Expand Down
173 changes: 173 additions & 0 deletions mojo/examples/apptest/example_apptest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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 <limits.h>

#include "mojo/examples/apptest/example_client_application.h"
#include "mojo/examples/apptest/example_client_impl.h"
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_impl.h"
#include "mojo/public/cpp/bindings/array.h"
#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/public/cpp/bindings/string.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/environment/logging.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {

namespace {

// This global shell handle is needed for repeated use by test applications.
MessagePipeHandle g_shell_message_pipe_handle_hack;

// Apptest is a GTEST base class for application testing executed in mojo_shell.
class Apptest : public testing::Test {
public:
explicit Apptest(Array<String> args)
: args_(args.Pass()),
application_impl_(nullptr) {
}
~Apptest() override {}

protected:
ApplicationImpl* application_impl() { return application_impl_; }

// Get the ApplicationDelegate for the application to be tested.
virtual ApplicationDelegate* GetApplicationDelegate() = 0;

// testing::Test:
void SetUp() override {
// New applications are constructed for each test to avoid persisting state.
MOJO_CHECK(g_shell_message_pipe_handle_hack.is_valid());
application_impl_ = new ApplicationImpl(
GetApplicationDelegate(),
MakeScopedHandle(g_shell_message_pipe_handle_hack));

// Fake application initialization with the given command line arguments.
application_impl_->Initialize(args_.Clone());
}
void TearDown() override {
g_shell_message_pipe_handle_hack =
application_impl_->UnbindShell().release();
delete application_impl_;
}

private:
// The command line arguments supplied to each test application instance.
Array<String> args_;

// The application implementation instance, reconstructed for each test.
ApplicationImpl* application_impl_;

// A run loop is needed for ApplicationImpl initialization and communication.
RunLoop run_loop_;

MOJO_DISALLOW_COPY_AND_ASSIGN(Apptest);
};

// ExampleApptest exemplifies Apptest's application testing pattern.
class ExampleApptest : public Apptest {
public:
// TODO(msw): Exemplify the use of actual command line arguments.
ExampleApptest() : Apptest(Array<String>()) {}
~ExampleApptest() override {}

protected:
// Apptest:
ApplicationDelegate* GetApplicationDelegate() override {
return &example_client_application_;
}
void SetUp() override {
Apptest::SetUp();
application_impl()->ConnectToService("mojo:example_service",
&example_service_);
example_service_.set_client(&example_client_);
}

ExampleServicePtr example_service_;
ExampleClientImpl example_client_;

private:
ExampleClientApplication example_client_application_;

MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest);
};

TEST_F(ExampleApptest, PongClientDirectly) {
// Test very basic standalone ExampleClient functionality.
ExampleClientImpl example_client;
EXPECT_EQ(0, example_client.last_pong_value());
example_client.Pong(1);
EXPECT_EQ(1, example_client.last_pong_value());
}

TEST_F(ExampleApptest, PingServiceToPongClient) {
// Test ExampleClient and ExampleService interaction.
EXPECT_EQ(0, example_client_.last_pong_value());
example_service_->Ping(1);
EXPECT_TRUE(example_service_.WaitForIncomingMethodCall());
EXPECT_EQ(1, example_client_.last_pong_value());
}

template <typename T>
struct SetCallback : public Callback<void()>::Runnable {
SetCallback(T* val, T result) : val_(val), result_(result) {}
~SetCallback() override {}
void Run() const override { *val_ = result_; }
T* val_;
T result_;
};

TEST_F(ExampleApptest, RunCallbackViaService) {
// Test ExampleService callback functionality.
bool was_run = false;
example_service_->RunCallback(SetCallback<bool>(&was_run, true));
EXPECT_TRUE(example_service_.WaitForIncomingMethodCall());
EXPECT_TRUE(was_run);
}

} // namespace

} // namespace mojo

MojoResult MojoMain(MojoHandle shell_handle) {
mojo::Environment environment;

{
// This RunLoop is used for init, and then destroyed before running tests.
mojo::RunLoop run_loop;

// Construct an ApplicationImpl just for the GTEST commandline arguments.
// GTEST command line arguments are supported amid application arguments:
// mojo_shell 'mojo:example_apptest arg1 --gtest_filter=foo arg2'
mojo::ApplicationDelegate dummy_application_delegate;
mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle);
MOJO_CHECK(app.WaitForInitialize());

// InitGoogleTest expects (argc + 1) elements, including a terminating NULL.
// It also removes GTEST arguments from |argv| and updates the |argc| count.
// TODO(msw): Provide tests access to these actual command line arguments.
const std::vector<std::string>& args = app.args();
MOJO_CHECK(args.size() < INT_MAX);
int argc = static_cast<int>(args.size());
std::vector<char*> argv(argc + 1);
for (int i = 0; i < argc; ++i)
argv[i] = const_cast<char*>(args[i].data());
argv[argc] = NULL;
testing::InitGoogleTest(&argc, &argv[0]);
mojo::g_shell_message_pipe_handle_hack = app.UnbindShell().release();
}

mojo_ignore_result(RUN_ALL_TESTS());

MojoResult result = MojoClose(mojo::g_shell_message_pipe_handle_hack.value());
MOJO_ALLOW_UNUSED_LOCAL(result);
assert(result == MOJO_RESULT_OK);

return MOJO_RESULT_OK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sample_apps/apptest/example_client_application.h"
#include "mojo/examples/apptest/example_client_application.h"

namespace mojo {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sample_apps/apptest/example_client_impl.h"
#include "mojo/examples/apptest/example_client_impl.h"
#include "mojo/public/cpp/utility/run_loop.h"

namespace mojo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_IMPL_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_CLIENT_IMPL_H_

#include "sample_apps/apptest/example_service.mojom.h"
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/cpp/system/macros.h"

namespace mojo {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sample_apps/apptest/example_service_application.h"
#include "mojo/examples/apptest/example_service_application.h"

#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_connection.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_APPLICATION_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_APPLICATION_H_

#include "sample_apps/apptest/example_service_impl.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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sample_apps/apptest/example_service_impl.h"
#include "mojo/examples/apptest/example_service_impl.h"

#include "mojo/public/cpp/utility/run_loop.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_IMPL_H_
#define MOJO_EXAMPLES_TEST_EXAMPLE_SERVICE_IMPL_H_

#include "sample_apps/apptest/example_service.mojom.h"
#include "mojo/examples/apptest/example_service.mojom.h"
#include "mojo/public/cpp/system/macros.h"

namespace mojo {
Expand Down
4 changes: 2 additions & 2 deletions sample_apps/echo/BUILD.gn → mojo/examples/echo/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group("echo") {
}

shared_library("client") {
output_name = "echo_client"
output_name = "mojo_echo_client"

deps = [
":bindings",
Expand All @@ -26,7 +26,7 @@ shared_library("client") {
}

shared_library("service") {
output_name = "echo_service"
output_name = "mojo_echo_service"

deps = [
":bindings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdio.h>

#include "sample_apps/echo/echo_service.mojom.h"
#include "mojo/examples/echo/echo_service.mojom.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sample_apps/echo/echo_service.mojom.h"
#include "mojo/examples/echo/echo_service.mojom.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/public/cpp/application/application_delegate.h"
Expand Down
File renamed without changes.
12 changes: 0 additions & 12 deletions sample_apps/BUILD.gn

This file was deleted.

Loading

0 comments on commit a3f7c47

Please sign in to comment.