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 "mojo/runner/shell_test_base.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 "mojo/services/test_service/test_request_tracker.mojom.h"
16 #include "mojo/services/test_service/test_service.mojom.h"
17 #include "testing/gtest/include/gtest/gtest.h"
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
;
34 void GetReportCallback(base::MessageLoop
* loop
,
35 std::vector
<ServiceReport
>* reports_out
,
36 Array
<ServiceReportPtr
> report
) {
37 for (size_t i
= 0; i
< report
.size(); i
++)
38 reports_out
->push_back(*report
[i
]);
42 class ShellTestBaseTest
: public ShellTestBase
{
44 // Convenience helpers for use as callbacks in tests.
46 base::Callback
<void()> SetAndQuit(T
* val
, T result
) {
47 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl
<T
>,
48 base::Unretained(this), val
, result
);
51 base::Callback
<void(T result
)> SetAndQuit(T
* val
) {
52 return base::Bind(&ShellTestBaseTest::SetAndQuitImpl
<T
>,
53 base::Unretained(this), val
);
55 static GURL
test_app_url() { return GURL("mojo:test_app"); }
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();
67 void SetAndQuitImpl(T
* val
, T result
) {
69 message_loop()->QuitWhenIdle();
71 TestTrackedRequestServicePtr request_tracking_
;
74 class QuitMessageLoopErrorHandler
: public ErrorHandler
{
76 QuitMessageLoopErrorHandler() {}
77 ~QuitMessageLoopErrorHandler() override
{}
79 // |ErrorHandler| implementation:
80 void OnConnectionError() override
{
81 base::MessageLoop::current()->QuitWhenIdle();
85 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler
);
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
);
94 service
->Ping(SetAndQuit
<bool>(&was_run
, true));
95 message_loop()->Run();
97 EXPECT_FALSE(service
.encountered_error());
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();
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
);
113 bool was_run
= false;
114 test_service
->Ping(SetAndQuit
<bool>(&was_run
, true));
116 // This will quit because there's nothing running.
117 message_loop()->Run();
118 EXPECT_FALSE(was_run
);
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());
128 test_service
.reset();
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
);
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());
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.
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 ConnectToService(GURL("http://example.com/non_existent_service"),
165 QuitMessageLoopErrorHandler quitter
;
166 test_service
.set_error_handler(&quitter
);
167 bool was_run
= false;
168 test_service
->Ping(SetAndQuit
<bool>(&was_run
, true));
169 message_loop()->Run();
170 EXPECT_TRUE(test_service
.encountered_error());
172 // TODO(tim): crbug.com/392685. Calling this explicitly shouldn't be
173 // necessary once the shell terminates if the primordial app exits, which
174 // we could enforce here by resetting |service|.
175 shell_context()->application_manager()->TerminateShellConnections();
176 message_loop()->Run(); // Waits for all connections to die.
179 // Similar to ConnectBasic, but causes the app to instantiate multiple
180 // service implementation objects and verifies the shell can reach both.
181 TEST_F(ShellTestBaseTest
, ConnectMultipleInstancesPerApp
) {
183 TestServicePtr service1
, service2
;
184 ConnectToService(test_app_url(), &service1
);
185 ConnectToService(test_app_url(), &service2
);
187 bool was_run1
= false;
188 bool was_run2
= false;
189 service1
->Ping(SetAndQuit
<bool>(&was_run1
, true));
190 message_loop()->Run();
191 service2
->Ping(SetAndQuit
<bool>(&was_run2
, true));
192 message_loop()->Run();
193 EXPECT_TRUE(was_run1
);
194 EXPECT_TRUE(was_run2
);
195 EXPECT_FALSE(service1
.encountered_error());
196 EXPECT_FALSE(service2
.encountered_error());
198 message_loop()->Run();
201 // Tests that service A and service B, both in App 1, can talk to each other
202 // and parameters are passed around properly.
203 TEST_F(ShellTestBaseTest
, ConnectDifferentServicesInSingleApp
) {
204 // Have a TestService GetPartyTime on a TestTimeService in the same app.
206 TestServicePtr service
;
207 ConnectToService(test_app_url(), &service
);
208 service
->ConnectToAppAndGetTime(test_app_url().spec(),
209 SetAndQuit
<int64
>(&time_message
));
210 message_loop()->Run();
212 // Verify by hitting the TimeService directly.
213 TestTimeServicePtr time_service
;
214 ConnectToService(test_app_url(), &time_service
);
216 time_service
->GetPartyTime(SetAndQuit
<int64
>(&party_time
));
217 message_loop()->Run();
219 EXPECT_EQ(time_message
, party_time
);
222 // Tests that a service A in App 1 can talk to service B in App 2 and
223 // parameters are passed around properly.
224 TEST_F(ShellTestBaseTest
, ConnectDifferentServicesInDifferentApps
) {
226 TestServicePtr service
;
227 ConnectToService(test_app_url(), &service
);
228 service
->ConnectToAppAndGetTime("mojo:test_request_tracker_app",
229 SetAndQuit
<int64
>(&time_message
));
230 message_loop()->Run();
232 // Verify by hitting the TimeService in the request tracker app directly.
233 TestTimeServicePtr time_service
;
234 ConnectToService(GURL("mojo:test_request_tracker_app"), &time_service
);
236 time_service
->GetPartyTime(SetAndQuit
<int64
>(&party_time
));
237 message_loop()->Run();
239 EXPECT_EQ(time_message
, party_time
);
242 // Tests that service A in App 1 can be a client of service B in App 2.
243 TEST_F(ShellTestBaseTest
, ConnectServiceAsClientOfSeparateApp
) {
244 TestServicePtr service
;
245 ConnectToService(test_app_url(), &service
);
246 service
->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
247 service
->Ping(Callback
<void()>());
248 message_loop()->Run();
250 for (int i
= 0; i
< 8; i
++)
251 service
->Ping(Callback
<void()>());
252 service
->Ping(message_loop()->QuitWhenIdleClosure());
253 message_loop()->Run();
255 // If everything worked properly, the tracking service should report
256 // 10 pings to TestService.
257 std::vector
<ServiceReport
> reports
;
259 ASSERT_EQ(1U, reports
.size());
260 EXPECT_EQ(TestService::Name_
, reports
[0].service_name
);
261 EXPECT_EQ(10U, reports
[0].total_requests
);
264 // Connect several services together and use the tracking service to verify
266 TEST_F(ShellTestBaseTest
, ConnectManyClientsAndServices
) {
267 TestServicePtr service
;
268 TestTimeServicePtr time_service
;
270 // Make a request to the TestService and have it contact TimeService in the
271 // tracking app. Do all this with tracking enabled, meaning both services
272 // are connected as clients of the TrackedRequestService.
273 ConnectToService(test_app_url(), &service
);
274 service
->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
275 message_loop()->Run();
276 for (int i
= 0; i
< 5; i
++)
277 service
->Ping(Callback
<void()>());
279 service
->ConnectToAppAndGetTime("mojo:test_request_tracker_app",
280 SetAndQuit
<int64
>(&time_result
));
281 message_loop()->Run();
283 // Also make a few requests to the TimeService in the test_app.
284 ConnectToService(test_app_url(), &time_service
);
285 time_service
->StartTrackingRequests(message_loop()->QuitWhenIdleClosure());
286 time_service
->GetPartyTime(Callback
<void(uint64_t)>());
287 message_loop()->Run();
288 for (int i
= 0; i
< 18; i
++)
289 time_service
->GetPartyTime(Callback
<void(uint64_t)>());
290 // Flush the tasks with one more to quit.
291 int64 party_time
= 0;
292 time_service
->GetPartyTime(SetAndQuit
<int64
>(&party_time
));
293 message_loop()->Run();
295 std::vector
<ServiceReport
> reports
;
297 ASSERT_EQ(3U, reports
.size());
298 EXPECT_EQ(TestService::Name_
, reports
[0].service_name
);
299 EXPECT_EQ(6U, reports
[0].total_requests
);
300 EXPECT_EQ(TestTimeService::Name_
, reports
[1].service_name
);
301 EXPECT_EQ(1U, reports
[1].total_requests
);
302 EXPECT_EQ(TestTimeService::Name_
, reports
[2].service_name
);
303 EXPECT_EQ(20U, reports
[2].total_requests
);
308 } // namespace runner