OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // run on the IO thread. | 155 // run on the IO thread. |
156 void SetOnline(bool online); | 156 void SetOnline(bool online); |
157 void SetOnlineOnIOThread( | 157 void SetOnlineOnIOThread( |
158 const scoped_refptr<BackgroundSyncContext>& sync_context, | 158 const scoped_refptr<BackgroundSyncContext>& sync_context, |
159 bool online); | 159 bool online); |
160 | 160 |
161 // Returns true if the one-shot sync with tag is currently pending. Fails | 161 // Returns true if the one-shot sync with tag is currently pending. Fails |
162 // (assertion failure) if the tag isn't registered. | 162 // (assertion failure) if the tag isn't registered. |
163 bool OneShotPending(const std::string& tag); | 163 bool OneShotPending(const std::string& tag); |
164 | 164 |
| 165 std::string PopConsoleString(); |
165 bool PopConsole(const std::string& expected_msg); | 166 bool PopConsole(const std::string& expected_msg); |
166 bool RegisterServiceWorker(); | 167 bool RegisterServiceWorker(); |
167 bool RegisterOneShot(const std::string& tag); | 168 bool RegisterOneShot(const std::string& tag); |
| 169 bool RegisterOneShotFromServiceWorker(const std::string& tag); |
168 bool UnregisterOneShot(const std::string& tag); | 170 bool UnregisterOneShot(const std::string& tag); |
169 bool UnregisterOneShotTwice(const std::string& tag); | 171 bool UnregisterOneShotTwice(const std::string& tag); |
170 bool GetRegistrationOneShot(const std::string& tag); | 172 bool GetRegistrationOneShot(const std::string& tag); |
| 173 bool GetRegistrationOneShotFromServiceWorker(const std::string& tag); |
| 174 bool MatchRegistrations(const std::string& script_result, |
| 175 const std::vector<std::string>& expected_tags); |
171 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); | 176 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); |
| 177 bool GetRegistrationsOneShotFromServiceWorker( |
| 178 const std::vector<std::string>& expected_tags); |
172 bool CompleteDelayedOneShot(); | 179 bool CompleteDelayedOneShot(); |
173 bool RejectDelayedOneShot(); | 180 bool RejectDelayedOneShot(); |
174 bool NotifyWhenDoneOneShot(const std::string& tag); | 181 bool NotifyWhenDoneOneShot(const std::string& tag); |
175 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); | 182 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); |
176 bool StoreRegistrationOneShot(const std::string& tag); | 183 bool StoreRegistrationOneShot(const std::string& tag); |
177 | 184 |
178 private: | 185 private: |
179 scoped_ptr<net::SpawnedTestServer> https_server_; | 186 scoped_ptr<net::SpawnedTestServer> https_server_; |
180 Shell* shell_ = nullptr; | 187 Shell* shell_ = nullptr; |
181 | 188 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 BrowserThread::IO, FROM_HERE, | 233 BrowserThread::IO, FROM_HERE, |
227 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), | 234 base::Bind(&OneShotPendingOnIOThread, make_scoped_refptr(sync_context), |
228 make_scoped_refptr(service_worker_context), tag, | 235 make_scoped_refptr(service_worker_context), tag, |
229 https_server_->GetURL(kDefaultTestURL), callback)); | 236 https_server_->GetURL(kDefaultTestURL), callback)); |
230 | 237 |
231 run_loop.Run(); | 238 run_loop.Run(); |
232 | 239 |
233 return is_pending; | 240 return is_pending; |
234 } | 241 } |
235 | 242 |
236 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { | 243 std::string BackgroundSyncBrowserTest::PopConsoleString() { |
237 std::string script_result; | 244 std::string script_result; |
238 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 245 EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 246 return script_result; |
| 247 } |
| 248 |
| 249 bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) { |
| 250 std::string script_result = PopConsoleString(); |
239 return script_result == expected_msg; | 251 return script_result == expected_msg; |
240 } | 252 } |
241 | 253 |
242 bool BackgroundSyncBrowserTest::RegisterServiceWorker() { | 254 bool BackgroundSyncBrowserTest::RegisterServiceWorker() { |
243 std::string script_result; | 255 std::string script_result; |
244 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); | 256 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result)); |
245 return script_result == BuildExpectedResult("service worker", "registered"); | 257 return script_result == BuildExpectedResult("service worker", "registered"); |
246 } | 258 } |
247 | 259 |
248 bool BackgroundSyncBrowserTest::RegisterOneShot(const std::string& tag) { | 260 bool BackgroundSyncBrowserTest::RegisterOneShot(const std::string& tag) { |
249 std::string script_result; | 261 std::string script_result; |
250 EXPECT_TRUE( | 262 EXPECT_TRUE( |
251 RunScript(BuildScriptString("registerOneShot", tag), &script_result)); | 263 RunScript(BuildScriptString("registerOneShot", tag), &script_result)); |
252 return script_result == BuildExpectedResult(tag, "registered"); | 264 return script_result == BuildExpectedResult(tag, "registered"); |
253 } | 265 } |
254 | 266 |
| 267 bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker( |
| 268 const std::string& tag) { |
| 269 std::string script_result; |
| 270 EXPECT_TRUE( |
| 271 RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag), |
| 272 &script_result)); |
| 273 return script_result == BuildExpectedResult(tag, "register sent to SW"); |
| 274 } |
| 275 |
255 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { | 276 bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) { |
256 std::string script_result; | 277 std::string script_result; |
257 EXPECT_TRUE( | 278 EXPECT_TRUE( |
258 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); | 279 RunScript(BuildScriptString("unregisterOneShot", tag), &script_result)); |
259 return script_result == BuildExpectedResult(tag, "unregistered"); | 280 return script_result == BuildExpectedResult(tag, "unregistered"); |
260 } | 281 } |
261 | 282 |
262 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { | 283 bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) { |
263 std::string script_result; | 284 std::string script_result; |
264 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), | 285 EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag), |
265 &script_result)); | 286 &script_result)); |
266 return script_result == | 287 return script_result == |
267 BuildExpectedResult(tag, "failed to unregister twice"); | 288 BuildExpectedResult(tag, "failed to unregister twice"); |
268 } | 289 } |
269 | 290 |
270 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { | 291 bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) { |
271 std::string script_result; | 292 std::string script_result; |
272 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), | 293 EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag), |
273 &script_result)); | 294 &script_result)); |
274 return script_result == BuildExpectedResult(tag, "found"); | 295 return script_result == BuildExpectedResult(tag, "found"); |
275 } | 296 } |
276 | 297 |
| 298 bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker( |
| 299 const std::string& tag) { |
| 300 std::string script_result; |
| 301 EXPECT_TRUE(RunScript( |
| 302 BuildScriptString("getRegistrationOneShotFromServiceWorker", tag), |
| 303 &script_result)); |
| 304 EXPECT_TRUE(script_result == "ok - getRegistration sent to SW"); |
| 305 |
| 306 return PopConsole(BuildExpectedResult(tag, "found")); |
| 307 } |
| 308 |
| 309 bool BackgroundSyncBrowserTest::MatchRegistrations( |
| 310 const std::string& script_result, |
| 311 const std::vector<std::string>& expected_tags) { |
| 312 EXPECT_TRUE(base::StartsWith(script_result, kSuccessfulOperationPrefix, |
| 313 base::CompareCase::INSENSITIVE_ASCII)); |
| 314 std::string tag_string = |
| 315 script_result.substr(strlen(kSuccessfulOperationPrefix)); |
| 316 std::vector<std::string> result_tags = base::SplitString( |
| 317 tag_string, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 318 |
| 319 return std::set<std::string>(expected_tags.begin(), expected_tags.end()) == |
| 320 std::set<std::string>(result_tags.begin(), result_tags.end()); |
| 321 } |
| 322 |
277 bool BackgroundSyncBrowserTest::GetRegistrationsOneShot( | 323 bool BackgroundSyncBrowserTest::GetRegistrationsOneShot( |
278 const std::vector<std::string>& expected_tags) { | 324 const std::vector<std::string>& expected_tags) { |
279 std::string script_result; | 325 std::string script_result; |
280 EXPECT_TRUE(RunScript("getRegistrationsOneShot()", &script_result)); | 326 EXPECT_TRUE(RunScript("getRegistrationsOneShot()", &script_result)); |
281 | 327 |
282 EXPECT_TRUE(base::StartsWith(script_result, kSuccessfulOperationPrefix, | 328 return MatchRegistrations(script_result, expected_tags); |
283 base::CompareCase::INSENSITIVE_ASCII)); | 329 } |
284 script_result = script_result.substr(strlen(kSuccessfulOperationPrefix)); | |
285 std::vector<std::string> result_tags = base::SplitString( | |
286 script_result, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | |
287 | 330 |
288 return std::set<std::string>(expected_tags.begin(), expected_tags.end()) == | 331 bool BackgroundSyncBrowserTest::GetRegistrationsOneShotFromServiceWorker( |
289 std::set<std::string>(result_tags.begin(), result_tags.end()); | 332 const std::vector<std::string>& expected_tags) { |
| 333 std::string script_result; |
| 334 EXPECT_TRUE( |
| 335 RunScript("getRegistrationsOneShotFromServiceWorker()", &script_result)); |
| 336 EXPECT_TRUE(script_result == "ok - getRegistrations sent to SW"); |
| 337 |
| 338 return MatchRegistrations(PopConsoleString(), expected_tags); |
290 } | 339 } |
291 | 340 |
292 bool BackgroundSyncBrowserTest::CompleteDelayedOneShot() { | 341 bool BackgroundSyncBrowserTest::CompleteDelayedOneShot() { |
293 std::string script_result; | 342 std::string script_result; |
294 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); | 343 EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result)); |
295 return script_result == BuildExpectedResult("delay", "completing"); | 344 return script_result == BuildExpectedResult("delay", "completing"); |
296 } | 345 } |
297 | 346 |
298 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { | 347 bool BackgroundSyncBrowserTest::RejectDelayedOneShot() { |
299 std::string script_result; | 348 std::string script_result; |
(...skipping 24 matching lines...) Expand all Loading... |
324 | 373 |
325 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { | 374 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) { |
326 EXPECT_TRUE(RegisterServiceWorker()); | 375 EXPECT_TRUE(RegisterServiceWorker()); |
327 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 376 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
328 | 377 |
329 EXPECT_TRUE(RegisterOneShot("foo")); | 378 EXPECT_TRUE(RegisterOneShot("foo")); |
330 EXPECT_TRUE(PopConsole("foo fired")); | 379 EXPECT_TRUE(PopConsole("foo fired")); |
331 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 380 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
332 } | 381 } |
333 | 382 |
| 383 // Verify that Register works in a service worker |
| 384 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 385 OneShotFromServiceWorkerFires) { |
| 386 EXPECT_TRUE(RegisterServiceWorker()); |
| 387 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 388 |
| 389 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 390 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 391 EXPECT_TRUE(PopConsole("foo_sw fired")); |
| 392 EXPECT_FALSE(GetRegistrationOneShot("foo_sw")); |
| 393 } |
| 394 |
334 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { | 395 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) { |
335 EXPECT_TRUE(RegisterServiceWorker()); | 396 EXPECT_TRUE(RegisterServiceWorker()); |
336 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 397 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
337 | 398 |
338 // Prevent firing by going offline. | 399 // Prevent firing by going offline. |
339 SetOnline(false); | 400 SetOnline(false); |
340 EXPECT_TRUE(RegisterOneShot("foo")); | 401 EXPECT_TRUE(RegisterOneShot("foo")); |
341 EXPECT_TRUE(GetRegistrationOneShot("foo")); | 402 EXPECT_TRUE(GetRegistrationOneShot("foo")); |
342 EXPECT_TRUE(OneShotPending("foo")); | 403 EXPECT_TRUE(OneShotPending("foo")); |
343 | 404 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 SetOnline(false); | 489 SetOnline(false); |
429 registered_tags.push_back("foo"); | 490 registered_tags.push_back("foo"); |
430 registered_tags.push_back("bar"); | 491 registered_tags.push_back("bar"); |
431 | 492 |
432 for (const std::string& tag : registered_tags) | 493 for (const std::string& tag : registered_tags) |
433 EXPECT_TRUE(RegisterOneShot(tag)); | 494 EXPECT_TRUE(RegisterOneShot(tag)); |
434 | 495 |
435 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); | 496 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
436 } | 497 } |
437 | 498 |
| 499 // Verify that GetRegistrations works in a service worker |
| 500 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 501 GetRegistrationsFromServiceWorker) { |
| 502 EXPECT_TRUE(RegisterServiceWorker()); |
| 503 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 504 |
| 505 std::vector<std::string> registered_tags; |
| 506 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 507 |
| 508 SetOnline(false); |
| 509 registered_tags.push_back("foo_sw"); |
| 510 registered_tags.push_back("bar_sw"); |
| 511 |
| 512 for (const std::string& tag : registered_tags) { |
| 513 EXPECT_TRUE(RegisterOneShotFromServiceWorker(tag)); |
| 514 EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW"))); |
| 515 } |
| 516 |
| 517 EXPECT_TRUE(GetRegistrationsOneShotFromServiceWorker(registered_tags)); |
| 518 } |
| 519 |
| 520 // Verify that GetRegistration works in a service worker |
| 521 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, |
| 522 GetRegistrationFromServiceWorker) { |
| 523 EXPECT_TRUE(RegisterServiceWorker()); |
| 524 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
| 525 |
| 526 std::vector<std::string> registered_tags; |
| 527 EXPECT_TRUE(GetRegistrationsOneShot(registered_tags)); |
| 528 |
| 529 SetOnline(false); |
| 530 |
| 531 EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw")); |
| 532 EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW")); |
| 533 EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw")); |
| 534 } |
| 535 |
438 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { | 536 IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) { |
439 EXPECT_TRUE(RegisterServiceWorker()); | 537 EXPECT_TRUE(RegisterServiceWorker()); |
440 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. | 538 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. |
441 | 539 |
442 SetOnline(false); | 540 SetOnline(false); |
443 EXPECT_TRUE(RegisterOneShot("foo")); | 541 EXPECT_TRUE(RegisterOneShot("foo")); |
444 EXPECT_TRUE(UnregisterOneShot("foo")); | 542 EXPECT_TRUE(UnregisterOneShot("foo")); |
445 EXPECT_FALSE(GetRegistrationOneShot("foo")); | 543 EXPECT_FALSE(GetRegistrationOneShot("foo")); |
446 } | 544 } |
447 | 545 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 EXPECT_TRUE(RegisterOneShot("delay")); | 627 EXPECT_TRUE(RegisterOneShot("delay")); |
530 EXPECT_FALSE(OneShotPending("delay")); | 628 EXPECT_FALSE(OneShotPending("delay")); |
531 EXPECT_TRUE(StoreRegistrationOneShot("delay")); | 629 EXPECT_TRUE(StoreRegistrationOneShot("delay")); |
532 | 630 |
533 EXPECT_TRUE(RejectDelayedOneShot()); | 631 EXPECT_TRUE(RejectDelayedOneShot()); |
534 EXPECT_TRUE(PopConsole("ok - delay rejected")); | 632 EXPECT_TRUE(PopConsole("ok - delay rejected")); |
535 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); | 633 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); |
536 } | 634 } |
537 | 635 |
538 } // namespace content | 636 } // namespace content |
OLD | NEW |