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.
7 #include "mojo/examples/apptest/example_client_application.h"
8 #include "mojo/examples/apptest/example_client_impl.h"
9 #include "mojo/examples/apptest/example_service.mojom.h"
10 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_impl.h"
12 #include "mojo/public/cpp/bindings/array.h"
13 #include "mojo/public/cpp/bindings/callback.h"
14 #include "mojo/public/cpp/bindings/string.h"
15 #include "mojo/public/cpp/environment/environment.h"
16 #include "mojo/public/cpp/environment/logging.h"
17 #include "mojo/public/cpp/system/macros.h"
18 #include "mojo/public/cpp/utility/run_loop.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 // TODO(msw): Remove this once we can get ApplicationImpl from TLS.
24 mojo::ApplicationImpl
* g_application_impl_hack
= NULL
;
32 class ExampleApptest
: public testing::Test
{
35 g_application_impl_hack
->ConnectToService("mojo:mojo_example_service",
37 example_service_
.set_client(&example_client_
);
40 virtual ~ExampleApptest() override
{}
43 ExampleServicePtr example_service_
;
44 ExampleClientImpl example_client_
;
47 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest
);
50 TEST_F(ExampleApptest
, PongClientDirectly
) {
51 // Test very basic standalone ExampleClient functionality.
52 ExampleClientImpl example_client
;
53 EXPECT_EQ(0, example_client
.last_pong_value());
54 example_client
.Pong(1);
55 EXPECT_EQ(1, example_client
.last_pong_value());
58 TEST_F(ExampleApptest
, PingServiceToPongClient
) {
59 // Test ExampleClient and ExampleService interaction.
60 EXPECT_EQ(0, example_client_
.last_pong_value());
61 example_service_
->Ping(1);
62 EXPECT_TRUE(example_service_
.WaitForIncomingMethodCall());
63 EXPECT_EQ(1, example_client_
.last_pong_value());
67 struct SetAndQuit
: public Callback
<void()>::Runnable
{
68 SetAndQuit(T
* val
, T result
) : val_(val
), result_(result
) {}
69 virtual ~SetAndQuit() {}
70 virtual void Run() const override
{ *val_
= result_
; }
75 TEST_F(ExampleApptest
, RunCallbackViaService
) {
76 // Test ExampleService callback functionality.
78 example_service_
->RunCallback(SetAndQuit
<bool>(&was_run
, true));
79 EXPECT_TRUE(example_service_
.WaitForIncomingMethodCall());
87 MojoResult
MojoMain(MojoHandle shell_handle
) {
88 mojo::Environment env
;
89 // TODO(msw): Destroy this ambient RunLoop before running tests.
90 // Need to CancelWait() / PassMessagePipe() from the ShellPtr?
92 mojo::ApplicationDelegate
* delegate
= new mojo::ExampleClientApplication();
93 mojo::ApplicationImpl
app(delegate
, shell_handle
);
94 g_application_impl_hack
= &app
;
95 MOJO_CHECK(app
.WaitForInitialize());
98 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL.
99 // It also removes GTEST arguments from |argv| and updates the |argc| count.
100 const std::vector
<std::string
>& args
= app
.args();
101 MOJO_CHECK(args
.size() < INT_MAX
);
102 int argc
= static_cast<int>(args
.size());
103 std::vector
<char*> argv(argc
+ 1);
104 for (int i
= 0; i
< argc
; ++i
)
105 argv
[i
] = const_cast<char*>(args
[i
].data());
107 testing::InitGoogleTest(&argc
, &argv
[0]);
110 mojo_ignore_result(RUN_ALL_TESTS());
113 return MOJO_RESULT_OK
;