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