diff --git a/BUILD.gn b/BUILD.gn index d76ad31..1de8fc0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -7,6 +7,6 @@ group("root") { deps = [ "//mojo/public", - "//sample_apps" + "//mojo/examples" ] } diff --git a/build/install-build-deps.sh b/build/install-build-deps.sh index 2bb75ba..e1156e8 100755 --- a/build/install-build-deps.sh +++ b/build/install-build-deps.sh @@ -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 diff --git a/mojo/examples/BUILD.gn b/mojo/examples/BUILD.gn new file mode 100644 index 0000000..45964ff --- /dev/null +++ b/mojo/examples/BUILD.gn @@ -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", +# ] +# } +} diff --git a/sample_apps/apptest/BUILD.gn b/mojo/examples/apptest/BUILD.gn similarity index 87% rename from sample_apps/apptest/BUILD.gn rename to mojo/examples/apptest/BUILD.gn index 321b765..fd9651e 100644 --- a/sample_apps/apptest/BUILD.gn +++ b/mojo/examples/apptest/BUILD.gn @@ -14,7 +14,7 @@ group("apptest") { } shared_library("service") { - output_name = "example_service" + output_name = "mojo_example_service" sources = [ "example_service_application.cc", @@ -32,7 +32,7 @@ shared_library("service") { } shared_library("apptests") { - output_name = "example_apptests" + output_name = "mojo_example_apptests" testonly = true @@ -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") { diff --git a/mojo/examples/apptest/example_apptest.cc b/mojo/examples/apptest/example_apptest.cc new file mode 100644 index 0000000..f5dccab --- /dev/null +++ b/mojo/examples/apptest/example_apptest.cc @@ -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 + +#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 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 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()) {} + ~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 +struct SetCallback : public Callback::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(&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& args = app.args(); + MOJO_CHECK(args.size() < INT_MAX); + int argc = static_cast(args.size()); + std::vector argv(argc + 1); + for (int i = 0; i < argc; ++i) + argv[i] = const_cast(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; +} diff --git a/sample_apps/apptest/example_client_application.cc b/mojo/examples/apptest/example_client_application.cc similarity index 86% rename from sample_apps/apptest/example_client_application.cc rename to mojo/examples/apptest/example_client_application.cc index 132cd8c..ca59eff 100644 --- a/sample_apps/apptest/example_client_application.cc +++ b/mojo/examples/apptest/example_client_application.cc @@ -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 { diff --git a/sample_apps/apptest/example_client_application.h b/mojo/examples/apptest/example_client_application.h similarity index 100% rename from sample_apps/apptest/example_client_application.h rename to mojo/examples/apptest/example_client_application.h diff --git a/sample_apps/apptest/example_client_impl.cc b/mojo/examples/apptest/example_client_impl.cc similarity index 89% rename from sample_apps/apptest/example_client_impl.cc rename to mojo/examples/apptest/example_client_impl.cc index 1bf61e1..d794117 100644 --- a/sample_apps/apptest/example_client_impl.cc +++ b/mojo/examples/apptest/example_client_impl.cc @@ -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 { diff --git a/sample_apps/apptest/example_client_impl.h b/mojo/examples/apptest/example_client_impl.h similarity index 93% rename from sample_apps/apptest/example_client_impl.h rename to mojo/examples/apptest/example_client_impl.h index fd6bc1f..ee835ca 100644 --- a/sample_apps/apptest/example_client_impl.h +++ b/mojo/examples/apptest/example_client_impl.h @@ -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 { diff --git a/sample_apps/apptest/example_service.mojom b/mojo/examples/apptest/example_service.mojom similarity index 100% rename from sample_apps/apptest/example_service.mojom rename to mojo/examples/apptest/example_service.mojom diff --git a/sample_apps/apptest/example_service_application.cc b/mojo/examples/apptest/example_service_application.cc similarity index 92% rename from sample_apps/apptest/example_service_application.cc rename to mojo/examples/apptest/example_service_application.cc index f74e4c3..a62a510 100644 --- a/sample_apps/apptest/example_service_application.cc +++ b/mojo/examples/apptest/example_service_application.cc @@ -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" diff --git a/sample_apps/apptest/example_service_application.h b/mojo/examples/apptest/example_service_application.h similarity index 94% rename from sample_apps/apptest/example_service_application.h rename to mojo/examples/apptest/example_service_application.h index 7c3ba49..96843cc 100644 --- a/sample_apps/apptest/example_service_application.h +++ b/mojo/examples/apptest/example_service_application.h @@ -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" diff --git a/sample_apps/apptest/example_service_impl.cc b/mojo/examples/apptest/example_service_impl.cc similarity index 90% rename from sample_apps/apptest/example_service_impl.cc rename to mojo/examples/apptest/example_service_impl.cc index 602584d..b8e53d5 100644 --- a/sample_apps/apptest/example_service_impl.cc +++ b/mojo/examples/apptest/example_service_impl.cc @@ -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" diff --git a/sample_apps/apptest/example_service_impl.h b/mojo/examples/apptest/example_service_impl.h similarity index 93% rename from sample_apps/apptest/example_service_impl.h rename to mojo/examples/apptest/example_service_impl.h index 835ad37..18537fa 100644 --- a/sample_apps/apptest/example_service_impl.h +++ b/mojo/examples/apptest/example_service_impl.h @@ -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 { diff --git a/sample_apps/echo/BUILD.gn b/mojo/examples/echo/BUILD.gn similarity index 92% rename from sample_apps/echo/BUILD.gn rename to mojo/examples/echo/BUILD.gn index b78a4e8..6c390c9 100644 --- a/sample_apps/echo/BUILD.gn +++ b/mojo/examples/echo/BUILD.gn @@ -12,7 +12,7 @@ group("echo") { } shared_library("client") { - output_name = "echo_client" + output_name = "mojo_echo_client" deps = [ ":bindings", @@ -26,7 +26,7 @@ shared_library("client") { } shared_library("service") { - output_name = "echo_service" + output_name = "mojo_echo_service" deps = [ ":bindings", diff --git a/sample_apps/echo/echo_client.cc b/mojo/examples/echo/echo_client.cc similarity index 95% rename from sample_apps/echo/echo_client.cc rename to mojo/examples/echo/echo_client.cc index c4bdcce..1d44d08 100644 --- a/sample_apps/echo/echo_client.cc +++ b/mojo/examples/echo/echo_client.cc @@ -4,7 +4,7 @@ #include -#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" diff --git a/sample_apps/echo/echo_service.cc b/mojo/examples/echo/echo_service.cc similarity index 96% rename from sample_apps/echo/echo_service.cc rename to mojo/examples/echo/echo_service.cc index 1c2ffd1..5df36ad 100644 --- a/sample_apps/echo/echo_service.cc +++ b/mojo/examples/echo/echo_service.cc @@ -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" diff --git a/sample_apps/echo/echo_service.mojom b/mojo/examples/echo/echo_service.mojom similarity index 100% rename from sample_apps/echo/echo_service.mojom rename to mojo/examples/echo/echo_service.mojom diff --git a/sample_apps/BUILD.gn b/sample_apps/BUILD.gn deleted file mode 100644 index ca8760d..0000000 --- a/sample_apps/BUILD.gn +++ /dev/null @@ -1,12 +0,0 @@ -# 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. - -group("sample_apps") { - testonly = true - - deps = [ - "//sample_apps/apptest", - "//sample_apps/echo", - ] -} diff --git a/sample_apps/apptest/example_apptest.cc b/sample_apps/apptest/example_apptest.cc deleted file mode 100644 index fc03f5d..0000000 --- a/sample_apps/apptest/example_apptest.cc +++ /dev/null @@ -1,79 +0,0 @@ -// 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 "sample_apps/apptest/example_client_application.h" -#include "sample_apps/apptest/example_client_impl.h" -#include "sample_apps/apptest/example_service.mojom.h" -#include "mojo/public/cpp/application/application_impl.h" -#include "mojo/public/cpp/application/application_test_base.h" -#include "mojo/public/cpp/bindings/callback.h" -#include "mojo/public/cpp/environment/logging.h" -#include "mojo/public/cpp/system/macros.h" - -namespace mojo { -namespace { - -// Exemplifies ApplicationTestBase's application testing pattern. -class ExampleApplicationTest : public test::ApplicationTestBase { - public: - // TODO(msw): Exemplify the use of actual command line arguments. - ExampleApplicationTest() : ApplicationTestBase(Array()) {} - ~ExampleApplicationTest() override {} - - protected: - // ApplicationTestBase: - ApplicationDelegate* GetApplicationDelegate() override { - return &example_client_application_; - } - void SetUp() override { - ApplicationTestBase::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(ExampleApplicationTest); -}; - -TEST_F(ExampleApplicationTest, 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(ExampleApplicationTest, 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 -struct SetCallback : public Callback::Runnable { - SetCallback(T* val, T result) : val_(val), result_(result) {} - ~SetCallback() override {} - void Run() const override { *val_ = result_; } - T* val_; - T result_; -}; - -TEST_F(ExampleApplicationTest, RunCallbackViaService) { - // Test ExampleService callback functionality. - bool was_run = false; - example_service_->RunCallback(SetCallback(&was_run, true)); - EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); - EXPECT_TRUE(was_run); -} - -} // namespace -} // namespace mojo