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.
4
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"
16
17namespace {
18
19// TODO(msw): Remove this once we can get ApplicationImpl from TLS.
20mojo::ApplicationImpl* g_application_impl_hack = NULL;
21
22}  // namespace
23
24namespace mojo {
25
26namespace {
27
28class ExampleServiceTest : public testing::Test {
29 public:
30  ExampleServiceTest() {
31    g_application_impl_hack->ConnectToService("mojo:mojo_example_service",
32                                              &example_service_);
33    example_service_.set_client(&example_client_);
34  }
35
36  virtual ~ExampleServiceTest() MOJO_OVERRIDE {}
37
38 protected:
39  ExampleServicePtr example_service_;
40  ExampleClientImpl example_client_;
41
42 private:
43  MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest);
44};
45
46TEST_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());
51}
52
53template <typename T>
54struct 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{
58    *val_ = result_;
59    RunLoop::current()->Quit();
60  }
61  T* val_;
62  T result_;
63};
64
65TEST_F(ExampleServiceTest, RunCallback) {
66  bool was_run = false;
67  example_service_->RunCallback(SetAndQuit<bool>(&was_run, true));
68  RunLoop::current()->Run();
69  EXPECT_TRUE(was_run);
70}
71
72}  // namespace
73
74}  // namespace mojo
75
76MojoResult MojoMain(MojoHandle shell_handle) {
77  mojo::Environment env;
78  mojo::RunLoop loop;
79
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;
85
86  // TODO(msw): Get actual commandline arguments.
87  int argc = 0;
88  char** argv = NULL;
89  testing::InitGoogleTest(&argc, argv);
90  mojo_ignore_result(RUN_ALL_TESTS());
91
92  delete delegate;
93  return MOJO_RESULT_OK;
94}
95