1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/examples/apptest/example_client_application.h"
6 #include "mojo/examples/apptest/example_client_impl.h"
7 #include "mojo/examples/apptest/example_service.mojom.h"
8 #include "mojo/public/c/system/main.h"
9 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/public/cpp/bindings/callback.h"
12 #include "mojo/public/cpp/environment/environment.h"
13 #include "mojo/public/cpp/system/macros.h"
14 #include "mojo/public/cpp/utility/run_loop.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // TODO(msw): Remove this once we can get ApplicationImpl from TLS.
20 mojo::ApplicationImpl* g_application_impl_hack = NULL;
28 class ExampleServiceTest : public testing::Test {
30 ExampleServiceTest() {
31 g_application_impl_hack->ConnectToService("mojo:mojo_example_service",
33 example_service_.set_client(&example_client_);
36 virtual ~ExampleServiceTest() MOJO_OVERRIDE {}
39 ExampleServicePtr example_service_;
40 ExampleClientImpl example_client_;
43 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest);
46 TEST_F(ExampleServiceTest, Ping) {
47 EXPECT_EQ(0, example_client_.last_pong_value());
48 example_service_->Ping(1);
49 RunLoop::current()->Run();
50 EXPECT_EQ(1, example_client_.last_pong_value());
54 struct SetAndQuit : public Callback<void()>::Runnable {
55 SetAndQuit(T* val, T result) : val_(val), result_(result) {}
56 virtual ~SetAndQuit() {}
57 virtual void Run() const MOJO_OVERRIDE{
59 RunLoop::current()->Quit();
65 TEST_F(ExampleServiceTest, RunCallback) {
67 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true));
68 RunLoop::current()->Run();
76 MojoResult MojoMain(MojoHandle shell_handle) {
77 mojo::Environment env;
80 // TODO(tim): Perhaps the delegate should be the thing that provides
81 // the ExampleServiceTest with the ApplicationImpl somehow.
82 mojo::ApplicationDelegate* delegate = new mojo::ExampleClientApplication();
83 mojo::ApplicationImpl app(delegate, shell_handle);
84 g_application_impl_hack = &app;
86 // TODO(msw): Get actual commandline arguments.
89 testing::InitGoogleTest(&argc, argv);
90 mojo_ignore_result(RUN_ALL_TESTS());
93 return MOJO_RESULT_OK;