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