Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: mojo/examples/apptest/example_apptest.cc

Issue 654103002: Further refine mojo_example_apptests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix MojoClose result check. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/application/application_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits.h> 5 #include <limits.h>
6 6
7 #include "mojo/examples/apptest/example_client_application.h" 7 #include "mojo/examples/apptest/example_client_application.h"
8 #include "mojo/examples/apptest/example_client_impl.h" 8 #include "mojo/examples/apptest/example_client_impl.h"
9 #include "mojo/examples/apptest/example_service.mojom.h" 9 #include "mojo/examples/apptest/example_service.mojom.h"
10 #include "mojo/public/c/system/main.h" 10 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_impl.h" 11 #include "mojo/public/cpp/application/application_impl.h"
12 #include "mojo/public/cpp/bindings/array.h" 12 #include "mojo/public/cpp/bindings/array.h"
13 #include "mojo/public/cpp/bindings/callback.h" 13 #include "mojo/public/cpp/bindings/callback.h"
14 #include "mojo/public/cpp/bindings/string.h" 14 #include "mojo/public/cpp/bindings/string.h"
15 #include "mojo/public/cpp/environment/environment.h" 15 #include "mojo/public/cpp/environment/environment.h"
16 #include "mojo/public/cpp/environment/logging.h" 16 #include "mojo/public/cpp/environment/logging.h"
17 #include "mojo/public/cpp/system/macros.h" 17 #include "mojo/public/cpp/system/macros.h"
18 #include "mojo/public/cpp/utility/run_loop.h" 18 #include "mojo/public/cpp/utility/run_loop.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace {
22
23 // TODO(msw): Remove this once we can get ApplicationImpl from TLS.
24 mojo::ApplicationImpl* g_application_impl_hack = NULL;
25
26 } // namespace
27
28 namespace mojo { 21 namespace mojo {
29 22
30 namespace { 23 namespace {
31 24
32 class ExampleApptest : public testing::Test { 25 // This global shell handle is needed for repeated use by test applications.
26 MessagePipeHandle g_shell_message_pipe_handle_hack;
27
28 // Apptest is a GTEST base class for application testing executed in mojo_shell.
29 class Apptest : public testing::Test {
33 public: 30 public:
34 ExampleApptest() { 31 explicit Apptest(Array<String> args)
35 g_application_impl_hack->ConnectToService("mojo:mojo_example_service", 32 : args_(args.Pass()),
36 &example_service_); 33 application_impl_(nullptr) {
34 }
35 virtual ~Apptest() override {}
36
37 protected:
38 ApplicationImpl* application_impl() { return application_impl_; }
39
40 // Get the ApplicationDelegate for the application to be tested.
41 virtual ApplicationDelegate* GetApplicationDelegate() = 0;
42
43 // testing::Test:
44 virtual void SetUp() override {
45 // New applications are constructed for each test to avoid persisting state.
46 MOJO_CHECK(g_shell_message_pipe_handle_hack.is_valid());
47 application_impl_ = new ApplicationImpl(
48 GetApplicationDelegate(),
49 MakeScopedHandle(g_shell_message_pipe_handle_hack));
50
51 // Fake application initialization with the given command line arguments.
52 application_impl_->Initialize(args_.Clone());
53 }
54 virtual void TearDown() override {
55 g_shell_message_pipe_handle_hack =
56 application_impl_->UnbindShell().release();
57 delete application_impl_;
58 }
59
60 private:
61 // The command line arguments supplied to each test application instance.
62 Array<String> args_;
63
64 // The application implementation instance, reconstructed for each test.
65 ApplicationImpl* application_impl_;
66
67 // A run loop is needed for ApplicationImpl initialization and communication.
68 RunLoop run_loop_;
69
70 MOJO_DISALLOW_COPY_AND_ASSIGN(Apptest);
71 };
72
73 // ExampleApptest exemplifies Apptest's application testing pattern.
74 class ExampleApptest : public Apptest {
75 public:
76 // TODO(msw): Exemplify the use of actual command line arguments.
77 ExampleApptest() : Apptest(Array<String>()) {}
78 virtual ~ExampleApptest() override {}
79
80 protected:
81 // Apptest:
82 virtual ApplicationDelegate* GetApplicationDelegate() override {
83 return &example_client_application_;
84 }
85 virtual void SetUp() override {
86 Apptest::SetUp();
87 application_impl()->ConnectToService("mojo:mojo_example_service",
88 &example_service_);
37 example_service_.set_client(&example_client_); 89 example_service_.set_client(&example_client_);
38 } 90 }
39 91
40 virtual ~ExampleApptest() override {}
41
42 protected:
43 ExampleServicePtr example_service_; 92 ExampleServicePtr example_service_;
44 ExampleClientImpl example_client_; 93 ExampleClientImpl example_client_;
45 94
46 private: 95 private:
96 ExampleClientApplication example_client_application_;
97
47 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest); 98 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest);
48 }; 99 };
49 100
50 TEST_F(ExampleApptest, PongClientDirectly) { 101 TEST_F(ExampleApptest, PongClientDirectly) {
51 // Test very basic standalone ExampleClient functionality. 102 // Test very basic standalone ExampleClient functionality.
52 ExampleClientImpl example_client; 103 ExampleClientImpl example_client;
53 EXPECT_EQ(0, example_client.last_pong_value()); 104 EXPECT_EQ(0, example_client.last_pong_value());
54 example_client.Pong(1); 105 example_client.Pong(1);
55 EXPECT_EQ(1, example_client.last_pong_value()); 106 EXPECT_EQ(1, example_client.last_pong_value());
56 } 107 }
57 108
58 TEST_F(ExampleApptest, PingServiceToPongClient) { 109 TEST_F(ExampleApptest, PingServiceToPongClient) {
59 // Test ExampleClient and ExampleService interaction. 110 // Test ExampleClient and ExampleService interaction.
60 EXPECT_EQ(0, example_client_.last_pong_value()); 111 EXPECT_EQ(0, example_client_.last_pong_value());
61 example_service_->Ping(1); 112 example_service_->Ping(1);
62 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); 113 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall());
63 EXPECT_EQ(1, example_client_.last_pong_value()); 114 EXPECT_EQ(1, example_client_.last_pong_value());
64 } 115 }
65 116
66 template <typename T> 117 template <typename T>
67 struct SetAndQuit : public Callback<void()>::Runnable { 118 struct SetCallback : public Callback<void()>::Runnable {
68 SetAndQuit(T* val, T result) : val_(val), result_(result) {} 119 SetCallback(T* val, T result) : val_(val), result_(result) {}
69 virtual ~SetAndQuit() {} 120 virtual ~SetCallback() {}
70 virtual void Run() const override { *val_ = result_; } 121 virtual void Run() const override { *val_ = result_; }
71 T* val_; 122 T* val_;
72 T result_; 123 T result_;
73 }; 124 };
74 125
75 TEST_F(ExampleApptest, RunCallbackViaService) { 126 TEST_F(ExampleApptest, RunCallbackViaService) {
76 // Test ExampleService callback functionality. 127 // Test ExampleService callback functionality.
77 bool was_run = false; 128 bool was_run = false;
78 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); 129 example_service_->RunCallback(SetCallback<bool>(&was_run, true));
79 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); 130 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall());
80 EXPECT_TRUE(was_run); 131 EXPECT_TRUE(was_run);
81 } 132 }
82 133
83 } // namespace 134 } // namespace
84 135
85 } // namespace mojo 136 } // namespace mojo
86 137
87 MojoResult MojoMain(MojoHandle shell_handle) { 138 MojoResult MojoMain(MojoHandle shell_handle) {
88 mojo::Environment env; 139 mojo::Environment environment;
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());
96 140
97 { 141 {
142 // This RunLoop is used for init, and then destroyed before running tests.
143 mojo::RunLoop run_loop;
144
145 // Construct an ApplicationImpl just for the GTEST commandline arguments.
146 // GTEST command line arguments are supported amid application arguments:
147 // mojo_shell 'mojo:mojo_example_apptest arg1 --gtest_filter=foo arg2'
148 mojo::ApplicationDelegate dummy_application_delegate;
149 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle);
150 MOJO_CHECK(app.WaitForInitialize());
151
98 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. 152 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL.
99 // It also removes GTEST arguments from |argv| and updates the |argc| count. 153 // It also removes GTEST arguments from |argv| and updates the |argc| count.
154 // TODO(msw): Provide tests access to these actual command line arguments.
100 const std::vector<std::string>& args = app.args(); 155 const std::vector<std::string>& args = app.args();
101 MOJO_CHECK(args.size() < INT_MAX); 156 MOJO_CHECK(args.size() < INT_MAX);
102 int argc = static_cast<int>(args.size()); 157 int argc = static_cast<int>(args.size());
103 std::vector<char*> argv(argc + 1); 158 std::vector<char*> argv(argc + 1);
104 for (int i = 0; i < argc; ++i) 159 for (int i = 0; i < argc; ++i)
105 argv[i] = const_cast<char*>(args[i].data()); 160 argv[i] = const_cast<char*>(args[i].data());
106 argv[argc] = NULL; 161 argv[argc] = NULL;
107 testing::InitGoogleTest(&argc, &argv[0]); 162 testing::InitGoogleTest(&argc, &argv[0]);
163 mojo::g_shell_message_pipe_handle_hack = app.UnbindShell().release();
108 } 164 }
109 165
110 mojo_ignore_result(RUN_ALL_TESTS()); 166 mojo_ignore_result(RUN_ALL_TESTS());
111 167
112 delete delegate; 168 MojoResult result = MojoClose(mojo::g_shell_message_pipe_handle_hack.value());
169 MOJO_ALLOW_UNUSED_LOCAL(result);
170 assert(result == MOJO_RESULT_OK);
171
113 return MOJO_RESULT_OK; 172 return MOJO_RESULT_OK;
114 } 173 }
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/application/application_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698