OLD | NEW |
| (Empty) |
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/shell/shell_test_base.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/i18n/time_formatting.h" | |
9 #include "base/macros.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "mojo/public/cpp/bindings/error_handler.h" | |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
14 #include "mojo/public/cpp/system/core.h" | |
15 #include "services/test_service/test_request_tracker.mojom.h" | |
16 #include "services/test_service/test_service.mojom.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "url/gurl.h" | |
19 | |
20 using mojo::test::ServiceReport; | |
21 using mojo::test::ServiceReportPtr; | |
22 using mojo::test::TestService; | |
23 using mojo::test::TestTimeService; | |
24 using mojo::test::TestServicePtr; | |
25 using mojo::test::TestTimeServicePtr; | |
26 using mojo::test::TestTrackedRequestService; | |
27 using mojo::test::TestTrackedRequestServicePtr; | |
28 | |
29 namespace mojo { | |
30 namespace shell { | |
31 namespace test { | |
32 namespace { | |
33 | |
34 void GetReportCallback(base::MessageLoop* loop, | |
35 std::vector<ServiceReport>* reports_out, | |
36 mojo::Array<ServiceReportPtr> report) { | |
37 for (size_t i = 0; i < report.size(); i++) | |
38 reports_out->push_back(*report[i]); | |
39 loop->QuitWhenIdle(); | |
40 } | |
41 | |
42 class ShellTestBaseTest : public ShellTestBase { | |
43 public: | |
44 // Convenience helpers for use as callbacks in tests. | |
45 template <typename T> | |
46 base::Callback<void()> SetAndQuit(T* val, T result) { | |
47 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>, | |
48 base::Unretained(this), val, result); | |
49 } | |
50 template <typename T> | |
51 base::Callback<void(T result)> SetAndQuit(T* val) { | |
52 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl<T>, | |
53 base::Unretained(this), val); | |
54 } | |
55 static GURL test_app_url() { return GURL("mojo:test_app"); } | |
56 | |
57 void GetReport(std::vector<ServiceReport>* report) { | |
58 ConnectToService(GURL("mojo:test_request_tracker_app"), &request_tracking_); | |
59 request_tracking_->GetReport(base::Bind(&GetReportCallback, | |
60 base::Unretained(message_loop()), | |
61 base::Unretained(report))); | |
62 message_loop()->Run(); | |
63 } | |
64 | |
65 private: | |
66 template<typename T> | |
67 void SetAndQuitImpl(T* val, T result) { | |
68 *val = result; | |
69 message_loop()->QuitWhenIdle(); | |
70 } | |
71 TestTrackedRequestServicePtr request_tracking_; | |
72 }; | |
73 | |
74 class QuitMessageLoopErrorHandler : public ErrorHandler { | |
75 public: | |
76 QuitMessageLoopErrorHandler() {} | |
77 ~QuitMessageLoopErrorHandler() override {} | |
78 | |
79 // |ErrorHandler| implementation: | |
80 void OnConnectionError() override { | |
81 base::MessageLoop::current()->QuitWhenIdle(); | |
82 } | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); | |
86 }; | |
87 | |
88 // Tests that we can connect to a single service within a single app. | |
89 TEST_F(ShellTestBaseTest, ConnectBasic) { | |
90 InterfacePtr<TestService> service; | |
91 ConnectToService(test_app_url(), &service); | |
92 | |
93 bool was_run = false; | |
94 service->Ping(SetAndQuit<bool>(&was_run, true)); | |
95 message_loop()->Run(); | |
96 EXPECT_TRUE(was_run); | |
97 EXPECT_FALSE(service.encountered_error()); | |
98 | |
99 service.reset(); | |
100 | |
101 // This will run until the test app has actually quit (which it will, | |
102 // since we killed the only connection to it). | |
103 message_loop()->Run(); | |
104 } | |
105 | |
106 // Tests that trying to connect to a service fails properly if the service | |
107 // doesn't exist. Implicit in this test is verification that the shell | |
108 // terminates if no services are running. | |
109 TEST_F(ShellTestBaseTest, ConnectInvalidService) { | |
110 InterfacePtr<TestService> test_service; | |
111 ConnectToService(GURL("mojo:non_existent_service"), &test_service); | |
112 | |
113 bool was_run = false; | |
114 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | |
115 | |
116 // This will quit because there's nothing running. | |
117 message_loop()->Run(); | |
118 EXPECT_FALSE(was_run); | |
119 | |
120 // It may have quit before an error was processed. | |
121 if (!test_service.encountered_error()) { | |
122 QuitMessageLoopErrorHandler quitter; | |
123 test_service.set_error_handler(&quitter); | |
124 message_loop()->Run(); | |
125 EXPECT_TRUE(test_service.encountered_error()); | |
126 } | |
127 | |
128 test_service.reset(); | |
129 } | |
130 | |
131 // Tests that we can connect to a single service within a single app using | |
132 // a network based loader instead of local files. | |
133 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | |
134 // subsequent tests can't init properly. | |
135 TEST_F(ShellTestBaseTest, DISABLED_ConnectBasicNetwork) { | |
136 InterfacePtr<TestService> service; | |
137 ConnectToService(test_app_url(), &service); | |
138 | |
139 bool was_run = false; | |
140 service->Ping(SetAndQuit<bool>(&was_run, true)); | |
141 message_loop()->Run(); | |
142 EXPECT_TRUE(was_run); | |
143 EXPECT_FALSE(service.encountered_error()); | |
144 | |
145 // Note that use of the network service is implicit in this test. | |
146 // Since TestService is not the only service in use, the shell won't auto | |
147 // magically exit when TestService is destroyed (unlike ConnectBasic). | |
148 // Tearing down the shell context will kill connections. The shell loop will | |
149 // exit as soon as no more apps are connected. | |
150 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | |
151 // necessary once the shell terminates if the primordial app exits, which | |
152 // we could enforce here by resetting |service|. | |
153 shell_context()->application_manager()->TerminateShellConnections(); | |
154 message_loop()->Run(); // Waits for all connections to die. | |
155 } | |
156 | |
157 // Tests that trying to connect to a service over network fails preoprly | |
158 // if the service doesn't exist. | |
159 // TODO(tim): Disabled because network service leaks NSS at exit, meaning | |
160 // subsequent tests can't init properly. | |
161 TEST_F(ShellTestBaseTest, DISABLED_ConnectInvalidServiceNetwork) { | |
162 InterfacePtr<TestService> test_service; | |
163 ConnectToServiceViaNetwork(GURL("mojo:non_existent_service"), &test_service); | |
164 QuitMessageLoopErrorHandler quitter; | |
165 test_service.set_error_handler(&quitter); | |
166 bool was_run = false; | |
167 test_service->Ping(SetAndQuit<bool>(&was_run, true)); | |
168 message_loop()->Run(); | |
169 EXPECT_TRUE(test_service.encountered_error()); | |
170 | |
171 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be | |
172 // necessary once the shell terminates if the primordial app exits, which | |
173 // we could enforce here by resetting |service|. | |
174 shell_context()->application_manager()->TerminateShellConnections(); | |
175 message_loop()->Run(); // Waits for all connections to die. | |
176 } | |
177 | |
178 // Similar to ConnectBasic, but causes the app to instantiate multiple | |
179 // service implementation objects and verifies the shell can reach both. | |
180 TEST_F(ShellTestBaseTest, ConnectMultipleInstancesPerApp) { | |
181 { | |
182 TestServicePtr service1, service2; | |
183 ConnectToService(test_app_url(), &service1); | |
184 ConnectToService(test_app_url(), &service2); | |
185 | |
186 bool was_run1 = false; | |
187 bool was_run2 = false; | |
188 service1->Ping(SetAndQuit<bool>(&was_run1, true)); | |
189 message_loop()->Run(); | |
190 service2->Ping(SetAndQuit<bool>(&was_run2, true)); | |
191 message_loop()->Run(); | |
192 EXPECT_TRUE(was_run1); | |
193 EXPECT_TRUE(was_run2); | |
194 EXPECT_FALSE(service1.encountered_error()); | |
195 EXPECT_FALSE(service2.encountered_error()); | |
196 } | |
197 message_loop()->Run(); | |
198 } | |
199 | |
200 // Tests that service A and service B, both in App 1, can talk to each other | |
201 // and parameters are passed around properly. | |
202 TEST_F(ShellTestBaseTest, ConnectDifferentServicesInSingleApp) { | |
203 // Have a TestService GetPartyTime on a TestTimeService in the same app. | |
204 int64 time_message; | |
205 TestServicePtr service; | |
206 ConnectToService(test_app_url(), &service); | |
207 service->ConnectToAppAndGetTime(test_app_url().spec(), | |
208 SetAndQuit<int64>(&time_message)); | |
209 message_loop()->Run(); | |
210 | |
211 // Verify by hitting the TimeService directly. | |
212 TestTimeServicePtr time_service; | |
213 ConnectToService(test_app_url(), &time_service); | |
214 int64 party_time; | |
215 time_service->GetPartyTime(SetAndQuit<int64>(&party_time)); | |
216 message_loop()->Run(); | |
217 | |
218 EXPECT_EQ(time_message, party_time); | |
219 } | |
220 | |
221 // Tests that a service A in App 1 can talk to service B in App 2 and | |
222 // parameters are passed around properly. | |
223 TEST_F(ShellTestBaseTest, ConnectDifferentServicesInDifferentApps) { | |
224 int64 time_message; | |
225 TestServicePtr service; | |
226 ConnectToService(test_app_url(), &service); | |
227 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", | |
228 SetAndQuit<int64>(&time_message)); | |
229 message_loop()->Run(); | |
230 | |
231 // Verify by hitting the TimeService in the request tracker app directly. | |
232 TestTimeServicePtr time_service; | |
233 ConnectToService(GURL("mojo:test_request_tracker_app"), &time_service); | |
234 int64 party_time; | |
235 time_service->GetPartyTime(SetAndQuit<int64>(&party_time)); | |
236 message_loop()->Run(); | |
237 | |
238 EXPECT_EQ(time_message, party_time); | |
239 } | |
240 | |
241 // Tests that service A in App 1 can be a client of service B in App 2. | |
242 TEST_F(ShellTestBaseTest, ConnectServiceAsClientOfSeparateApp) { | |
243 TestServicePtr service; | |
244 ConnectToService(test_app_url(), &service); | |
245 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
246 service->Ping(mojo::Callback<void()>()); | |
247 message_loop()->Run(); | |
248 | |
249 for (int i = 0; i < 8; i++) | |
250 service->Ping(mojo::Callback<void()>()); | |
251 service->Ping(message_loop()->QuitWhenIdleClosure()); | |
252 message_loop()->Run(); | |
253 | |
254 // If everything worked properly, the tracking service should report | |
255 // 10 pings to TestService. | |
256 std::vector<ServiceReport> reports; | |
257 GetReport(&reports); | |
258 ASSERT_EQ(1U, reports.size()); | |
259 EXPECT_EQ(TestService::Name_, reports[0].service_name); | |
260 EXPECT_EQ(10U, reports[0].total_requests); | |
261 } | |
262 | |
263 // Connect several services together and use the tracking service to verify | |
264 // communication. | |
265 TEST_F(ShellTestBaseTest, ConnectManyClientsAndServices) { | |
266 TestServicePtr service; | |
267 TestTimeServicePtr time_service; | |
268 | |
269 // Make a request to the TestService and have it contact TimeService in the | |
270 // tracking app. Do all this with tracking enabled, meaning both services | |
271 // are connected as clients of the TrackedRequestService. | |
272 ConnectToService(test_app_url(), &service); | |
273 service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
274 message_loop()->Run(); | |
275 for (int i = 0; i < 5; i++) | |
276 service->Ping(mojo::Callback<void()>()); | |
277 int64 time_result; | |
278 service->ConnectToAppAndGetTime("mojo:test_request_tracker_app", | |
279 SetAndQuit<int64>(&time_result)); | |
280 message_loop()->Run(); | |
281 | |
282 // Also make a few requests to the TimeService in the test_app. | |
283 ConnectToService(test_app_url(), &time_service); | |
284 time_service->StartTrackingRequests(message_loop()->QuitWhenIdleClosure()); | |
285 time_service->GetPartyTime(mojo::Callback<void(uint64_t)>()); | |
286 message_loop()->Run(); | |
287 for (int i = 0; i < 18; i++) | |
288 time_service->GetPartyTime(mojo::Callback<void(uint64_t)>()); | |
289 // Flush the tasks with one more to quit. | |
290 int64 party_time = 0; | |
291 time_service->GetPartyTime(SetAndQuit<int64>(&party_time)); | |
292 message_loop()->Run(); | |
293 | |
294 std::vector<ServiceReport> reports; | |
295 GetReport(&reports); | |
296 ASSERT_EQ(3U, reports.size()); | |
297 EXPECT_EQ(TestService::Name_, reports[0].service_name); | |
298 EXPECT_EQ(6U, reports[0].total_requests); | |
299 EXPECT_EQ(TestTimeService::Name_, reports[1].service_name); | |
300 EXPECT_EQ(1U, reports[1].total_requests); | |
301 EXPECT_EQ(TestTimeService::Name_, reports[2].service_name); | |
302 EXPECT_EQ(20U, reports[2].total_requests); | |
303 } | |
304 | |
305 } // namespace | |
306 } // namespace test | |
307 } // namespace shell | |
308 } // namespace mojo | |
OLD | NEW |