Make ApplicationImpl::args() be a std::vector<std::string>
[chromium-blink-merge.git] / mojo / examples / apptest / example_apptest.cc
blob7288608edb3692bad0325e6beaf1a8bf5689fdab
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 <limits.h>
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"
21 namespace {
23 // TODO(msw): Remove this once we can get ApplicationImpl from TLS.
24 mojo::ApplicationImpl* g_application_impl_hack = NULL;
26 } // namespace
28 namespace mojo {
30 namespace {
32 class ExampleApptest : public testing::Test {
33 public:
34 ExampleApptest() {
35 g_application_impl_hack->ConnectToService("mojo:mojo_example_service",
36 &example_service_);
37 example_service_.set_client(&example_client_);
40 virtual ~ExampleApptest() override {}
42 protected:
43 ExampleServicePtr example_service_;
44 ExampleClientImpl example_client_;
46 private:
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());
66 template <typename T>
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_; }
71 T* val_;
72 T result_;
75 TEST_F(ExampleApptest, RunCallbackViaService) {
76 // Test ExampleService callback functionality.
77 bool was_run = false;
78 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true));
79 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall());
80 EXPECT_TRUE(was_run);
83 } // namespace
85 } // namespace mojo
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?
91 mojo::RunLoop loop;
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());
106 argv[argc] = NULL;
107 testing::InitGoogleTest(&argc, &argv[0]);
110 mojo_ignore_result(RUN_ALL_TESTS());
112 delete delegate;
113 return MOJO_RESULT_OK;