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

Side by Side Diff: mojo/shell/shell_test_base_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698