1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <tuple>
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/service_worker/embedded_worker_registry.h"
#include "content/browser/service_worker/embedded_worker_status.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_disk_cache.h"
#include "content/browser/service_worker/service_worker_job_coordinator.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_registration_status.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/common/service_worker/embedded_worker_messages.h"
#include "content/common/service_worker/service_worker_messages.h"
#include "content/common/service_worker/service_worker_utils.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "ipc/ipc_test_sink.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_response_headers.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::IOBuffer;
using net::TestCompletionCallback;
using net::WrappedIOBuffer;
// Unit tests for testing all job registration tasks.
namespace content {
namespace {
void SaveRegistrationCallback(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration_out,
ServiceWorkerStatusCode status,
const std::string& status_message,
ServiceWorkerRegistration* registration) {
EXPECT_EQ(expected_status, status);
*called = true;
*registration_out = registration;
}
void SaveFoundRegistrationCallback(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration,
ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> result) {
EXPECT_EQ(expected_status, status);
*called = true;
*registration = std::move(result);
}
// Creates a callback which both keeps track of if it's been called,
// as well as the resulting registration. Whent the callback is fired,
// it ensures that the resulting status matches the expectation.
// 'called' is useful for making sure a sychronous callback is or
// isn't called.
ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration) {
*called = false;
return base::Bind(
&SaveRegistrationCallback, expected_status, called, registration);
}
ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration(
ServiceWorkerStatusCode expected_status,
bool* called,
scoped_refptr<ServiceWorkerRegistration>* registration) {
*called = false;
return base::Bind(&SaveFoundRegistrationCallback,
expected_status,
called,
registration);
}
void SaveUnregistrationCallback(ServiceWorkerStatusCode expected_status,
bool* called,
int64_t registration_id,
ServiceWorkerStatusCode status) {
EXPECT_EQ(expected_status, status);
*called = true;
}
ServiceWorkerUnregisterJob::UnregistrationCallback SaveUnregistration(
ServiceWorkerStatusCode expected_status,
bool* called) {
*called = false;
return base::Bind(&SaveUnregistrationCallback, expected_status, called);
}
} // namespace
class ServiceWorkerJobTest : public testing::Test {
public:
ServiceWorkerJobTest()
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
void SetUp() override {
helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
}
void TearDown() override { helper_.reset(); }
ServiceWorkerContextCore* context() const { return helper_->context(); }
ServiceWorkerJobCoordinator* job_coordinator() const {
return context()->job_coordinator();
}
ServiceWorkerStorage* storage() const { return context()->storage(); }
protected:
scoped_refptr<ServiceWorkerRegistration> RunRegisterJob(
const GURL& pattern,
const GURL& script_url,
ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK);
void RunUnregisterJob(
const GURL& pattern,
ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK);
scoped_refptr<ServiceWorkerRegistration> FindRegistrationForPattern(
const GURL& pattern,
ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK);
std::unique_ptr<ServiceWorkerProviderHost> CreateControllee();
TestBrowserThreadBundle browser_thread_bundle_;
std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
};
scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob(
const GURL& pattern,
const GURL& script_url,
ServiceWorkerStatusCode expected_status) {
scoped_refptr<ServiceWorkerRegistration> registration;
bool called;
job_coordinator()->Register(
pattern, script_url, NULL,
SaveRegistration(expected_status, &called, ®istration));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
return registration;
}
void ServiceWorkerJobTest::RunUnregisterJob(
const GURL& pattern,
ServiceWorkerStatusCode expected_status) {
bool called;
job_coordinator()->Unregister(pattern,
SaveUnregistration(expected_status, &called));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
}
scoped_refptr<ServiceWorkerRegistration>
ServiceWorkerJobTest::FindRegistrationForPattern(
const GURL& pattern,
ServiceWorkerStatusCode expected_status) {
bool called;
scoped_refptr<ServiceWorkerRegistration> registration;
storage()->FindRegistrationForPattern(
pattern,
SaveFoundRegistration(expected_status, &called, ®istration));
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
return registration;
}
std::unique_ptr<ServiceWorkerProviderHost>
ServiceWorkerJobTest::CreateControllee() {
return std::unique_ptr<ServiceWorkerProviderHost>(
new ServiceWorkerProviderHost(
33 /* dummy render_process id */,
MSG_ROUTING_NONE /* render_frame_id */, 1 /* dummy provider_id */,
SERVICE_WORKER_PROVIDER_FOR_WINDOW,
ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
helper_->context()->AsWeakPtr(), NULL));
}
TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) {
scoped_refptr<ServiceWorkerRegistration> original_registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"));
bool called;
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration1));
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration2));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_TRUE(registration1.get());
ASSERT_EQ(registration1, original_registration);
ASSERT_EQ(registration1, registration2);
}
TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) {
bool called;
scoped_refptr<ServiceWorkerRegistration> original_registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"));
ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL),
original_registration.get());
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/one"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration1));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/two"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called, ®istration2));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_EQ(registration1, original_registration);
ASSERT_EQ(registration1, registration2);
}
TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) {
bool called1;
scoped_refptr<ServiceWorkerRegistration> original_registration1;
job_coordinator()->Register(
GURL("http://www.example.com/one/"),
GURL("http://www.example.com/service_worker.js"),
NULL,
SaveRegistration(SERVICE_WORKER_OK, &called1, &original_registration1));
bool called2;
scoped_refptr<ServiceWorkerRegistration> original_registration2;
job_coordinator()->Register(
GURL("http://www.example.com/two/"),
GURL("http://www.example.com/service_worker.js"),
NULL,
SaveRegistration(SERVICE_WORKER_OK, &called2, &original_registration2));
EXPECT_FALSE(called1);
EXPECT_FALSE(called2);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called2);
EXPECT_TRUE(called1);
scoped_refptr<ServiceWorkerRegistration> registration1;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/one/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called1, ®istration1));
scoped_refptr<ServiceWorkerRegistration> registration2;
storage()->FindRegistrationForDocument(
GURL("http://www.example.com/two/"),
SaveFoundRegistration(SERVICE_WORKER_OK, &called2, ®istration2));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called2);
EXPECT_TRUE(called1);
ASSERT_NE(registration1, registration2);
}
// Make sure basic registration is working.
TEST_F(ServiceWorkerJobTest, Register) {
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"));
ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
ServiceWorkerMsg_InstallEvent::ID));
}
// Make sure registrations are cleaned up when they are unregistered.
TEST_F(ServiceWorkerJobTest, Unregister) {
GURL pattern("http://www.example.com/");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js"));
RunUnregisterJob(pattern);
ASSERT_TRUE(registration->HasOneRef());
registration = FindRegistrationForPattern(pattern,
SERVICE_WORKER_ERROR_NOT_FOUND);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
}
TEST_F(ServiceWorkerJobTest, Unregister_NothingRegistered) {
GURL pattern("http://www.example.com/");
RunUnregisterJob(pattern, SERVICE_WORKER_ERROR_NOT_FOUND);
}
// Make sure registering a new script creates a new version and shares an
// existing registration.
TEST_F(ServiceWorkerJobTest, RegisterNewScript) {
GURL pattern("http://www.example.com/");
scoped_refptr<ServiceWorkerRegistration> old_registration =
RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js"));
scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern =
FindRegistrationForPattern(pattern);
ASSERT_EQ(old_registration, old_registration_by_pattern);
old_registration_by_pattern = NULL;
scoped_refptr<ServiceWorkerRegistration> new_registration =
RunRegisterJob(pattern,
GURL("http://www.example.com/service_worker_new.js"));
ASSERT_EQ(old_registration, new_registration);
scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern =
FindRegistrationForPattern(pattern);
ASSERT_EQ(new_registration, new_registration_by_pattern);
}
// Make sure that when registering a duplicate pattern+script_url
// combination, that the same registration is used.
TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
scoped_refptr<ServiceWorkerRegistration> old_registration =
RunRegisterJob(pattern, script_url);
scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern =
FindRegistrationForPattern(pattern);
ASSERT_TRUE(old_registration_by_pattern.get());
scoped_refptr<ServiceWorkerRegistration> new_registration =
RunRegisterJob(pattern, script_url);
ASSERT_EQ(old_registration, new_registration);
ASSERT_FALSE(old_registration->HasOneRef());
scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern =
FindRegistrationForPattern(pattern);
ASSERT_EQ(new_registration, old_registration);
}
class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper {
public:
FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
void OnStartWorker(
int embedded_worker_id,
int64_t service_worker_version_id,
const GURL& scope,
const GURL& script_url,
bool pause_after_download,
mojom::ServiceWorkerEventDispatcherRequest request) override {
EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
}
};
TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) {
helper_.reset(new FailToStartWorkerTestHelper);
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"),
SERVICE_WORKER_ERROR_START_WORKER_FAILED);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
}
// Register and then unregister the pattern, in parallel. Job coordinator should
// process jobs until the last job.
TEST_F(ServiceWorkerJobTest, ParallelRegUnreg) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
bool registration_called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
pattern,
script_url,
NULL,
SaveRegistration(SERVICE_WORKER_OK, ®istration_called, ®istration));
bool unregistration_called = false;
job_coordinator()->Unregister(
pattern,
SaveUnregistration(SERVICE_WORKER_OK, &unregistration_called));
ASSERT_FALSE(registration_called);
ASSERT_FALSE(unregistration_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called);
ASSERT_TRUE(unregistration_called);
registration = FindRegistrationForPattern(pattern,
SERVICE_WORKER_ERROR_NOT_FOUND);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
}
// Register conflicting scripts for the same pattern. The most recent
// registration should win, and the old registration should have been
// shutdown.
TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) {
GURL pattern("http://www.example.com/");
GURL script_url1("http://www.example.com/service_worker1.js");
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
pattern,
script_url1,
NULL,
SaveRegistration(
SERVICE_WORKER_OK, ®istration1_called, ®istration1));
GURL script_url2("http://www.example.com/service_worker2.js");
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
pattern,
script_url2,
NULL,
SaveRegistration(
SERVICE_WORKER_OK, ®istration2_called, ®istration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
scoped_refptr<ServiceWorkerRegistration> registration =
FindRegistrationForPattern(pattern);
ASSERT_EQ(registration2, registration);
}
// Register the exact same pattern + script. Requests should be
// coalesced such that both callers get the exact same registration
// object.
TEST_F(ServiceWorkerJobTest, ParallelRegSameScript) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker1.js");
bool registration1_called = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
pattern,
script_url,
NULL,
SaveRegistration(
SERVICE_WORKER_OK, ®istration1_called, ®istration1));
bool registration2_called = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
pattern,
script_url,
NULL,
SaveRegistration(
SERVICE_WORKER_OK, ®istration2_called, ®istration2));
ASSERT_FALSE(registration1_called);
ASSERT_FALSE(registration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration1_called);
ASSERT_TRUE(registration2_called);
ASSERT_EQ(registration1, registration2);
scoped_refptr<ServiceWorkerRegistration> registration =
FindRegistrationForPattern(pattern);
ASSERT_EQ(registration, registration1);
}
// Call simulataneous unregister calls.
TEST_F(ServiceWorkerJobTest, ParallelUnreg) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
bool unregistration1_called = false;
job_coordinator()->Unregister(
pattern,
SaveUnregistration(SERVICE_WORKER_ERROR_NOT_FOUND,
&unregistration1_called));
bool unregistration2_called = false;
job_coordinator()->Unregister(
pattern,
SaveUnregistration(SERVICE_WORKER_ERROR_NOT_FOUND,
&unregistration2_called));
ASSERT_FALSE(unregistration1_called);
ASSERT_FALSE(unregistration2_called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(unregistration1_called);
ASSERT_TRUE(unregistration2_called);
// There isn't really a way to test that they are being coalesced,
// but we can make sure they can exist simultaneously without
// crashing.
scoped_refptr<ServiceWorkerRegistration> registration =
FindRegistrationForPattern(pattern, SERVICE_WORKER_ERROR_NOT_FOUND);
ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
}
TEST_F(ServiceWorkerJobTest, AbortAll_Register) {
GURL pattern1("http://www1.example.com/");
GURL pattern2("http://www2.example.com/");
GURL script_url1("http://www1.example.com/service_worker.js");
GURL script_url2("http://www2.example.com/service_worker.js");
bool registration_called1 = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Register(
pattern1,
script_url1,
NULL,
SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
®istration_called1, ®istration1));
bool registration_called2 = false;
scoped_refptr<ServiceWorkerRegistration> registration2;
job_coordinator()->Register(
pattern2,
script_url2,
NULL,
SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
®istration_called2, ®istration2));
ASSERT_FALSE(registration_called1);
ASSERT_FALSE(registration_called2);
job_coordinator()->AbortAll();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called1);
ASSERT_TRUE(registration_called2);
bool find_called1 = false;
storage()->FindRegistrationForPattern(
pattern1,
SaveFoundRegistration(
SERVICE_WORKER_ERROR_NOT_FOUND, &find_called1, ®istration1));
bool find_called2 = false;
storage()->FindRegistrationForPattern(
pattern2,
SaveFoundRegistration(
SERVICE_WORKER_ERROR_NOT_FOUND, &find_called2, ®istration2));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(find_called1);
ASSERT_TRUE(find_called2);
EXPECT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration1);
EXPECT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration2);
}
TEST_F(ServiceWorkerJobTest, AbortAll_Unregister) {
GURL pattern1("http://www1.example.com/");
GURL pattern2("http://www2.example.com/");
bool unregistration_called1 = false;
scoped_refptr<ServiceWorkerRegistration> registration1;
job_coordinator()->Unregister(
pattern1,
SaveUnregistration(SERVICE_WORKER_ERROR_ABORT,
&unregistration_called1));
bool unregistration_called2 = false;
job_coordinator()->Unregister(
pattern2,
SaveUnregistration(SERVICE_WORKER_ERROR_ABORT,
&unregistration_called2));
ASSERT_FALSE(unregistration_called1);
ASSERT_FALSE(unregistration_called2);
job_coordinator()->AbortAll();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(unregistration_called1);
ASSERT_TRUE(unregistration_called2);
}
TEST_F(ServiceWorkerJobTest, AbortAll_RegUnreg) {
GURL pattern("http://www.example.com/");
GURL script_url("http://www.example.com/service_worker.js");
bool registration_called = false;
scoped_refptr<ServiceWorkerRegistration> registration;
job_coordinator()->Register(
pattern,
script_url,
NULL,
SaveRegistration(SERVICE_WORKER_ERROR_ABORT,
®istration_called, ®istration));
bool unregistration_called = false;
job_coordinator()->Unregister(
pattern,
SaveUnregistration(SERVICE_WORKER_ERROR_ABORT,
&unregistration_called));
ASSERT_FALSE(registration_called);
ASSERT_FALSE(unregistration_called);
job_coordinator()->AbortAll();
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(registration_called);
ASSERT_TRUE(unregistration_called);
registration = FindRegistrationForPattern(pattern,
SERVICE_WORKER_ERROR_NOT_FOUND);
EXPECT_EQ(scoped_refptr<ServiceWorkerRegistration>(), registration);
}
// Tests that the waiting worker enters the 'redundant' state upon
// unregistration.
TEST_F(ServiceWorkerJobTest, UnregisterWaitingSetsRedundant) {
GURL script_url("http://www.example.com/service_worker.js");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"), script_url);
ASSERT_TRUE(registration.get());
// Manually create the waiting worker since there is no way to become a
// waiting worker until Update is implemented.
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
registration.get(), script_url, 1L, helper_->context()->AsWeakPtr());
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
version->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
CreateReceiverOnCurrentThread(&status));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(SERVICE_WORKER_OK, status);
version->set_fetch_handler_existence(
ServiceWorkerVersion::FetchHandlerExistence::EXISTS);
version->SetStatus(ServiceWorkerVersion::INSTALLED);
registration->SetWaitingVersion(version);
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::INSTALLED, version->status());
RunUnregisterJob(GURL("http://www.example.com/"));
// The version should be stopped since there is no controllee after
// unregistration.
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version->status());
}
// Tests that the active worker enters the 'redundant' state upon
// unregistration.
TEST_F(ServiceWorkerJobTest, UnregisterActiveSetsRedundant) {
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"));
ASSERT_TRUE(registration.get());
scoped_refptr<ServiceWorkerVersion> version = registration->active_version();
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, version->status());
RunUnregisterJob(GURL("http://www.example.com/"));
// The version should be stopped since there is no controllee after
// unregistration.
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version->status());
}
// Tests that the active worker enters the 'redundant' state upon
// unregistration.
TEST_F(ServiceWorkerJobTest,
UnregisterActiveSetsRedundant_WaitForNoControllee) {
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/"),
GURL("http://www.example.com/service_worker.js"));
ASSERT_TRUE(registration.get());
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
registration->active_version()->AddControllee(host.get());
scoped_refptr<ServiceWorkerVersion> version = registration->active_version();
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, version->status());
RunUnregisterJob(GURL("http://www.example.com/"));
// The version should be running since there is still a controllee.
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, version->status());
registration->active_version()->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
// The version should be stopped since there is no controllee.
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version->status());
}
namespace { // Helpers for the update job tests.
const GURL kNoChangeOrigin("http://nochange/");
const GURL kNewVersionOrigin("http://newversion/");
const std::string kScope("scope/");
const std::string kScript("script.js");
void RunNestedUntilIdle() {
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
base::RunLoop().RunUntilIdle();
}
void OnIOComplete(int* rv_out, int rv) {
*rv_out = rv;
}
void WriteResponse(ServiceWorkerStorage* storage,
int64_t id,
const std::string& headers,
IOBuffer* body,
int length) {
std::unique_ptr<ServiceWorkerResponseWriter> writer =
storage->CreateResponseWriter(id);
std::unique_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo);
info->request_time = base::Time::Now();
info->response_time = base::Time::Now();
info->was_cached = false;
info->headers = new net::HttpResponseHeaders(headers);
scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
new HttpResponseInfoIOBuffer(info.release());
int rv = -1234;
writer->WriteInfo(info_buffer.get(), base::Bind(&OnIOComplete, &rv));
RunNestedUntilIdle();
EXPECT_LT(0, rv);
rv = -1234;
writer->WriteData(body, length,
base::Bind(&OnIOComplete, &rv));
RunNestedUntilIdle();
EXPECT_EQ(length, rv);
}
void WriteStringResponse(ServiceWorkerStorage* storage,
int64_t id,
const std::string& body) {
scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data()));
const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0\0";
std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
WriteResponse(storage, id, headers, body_buffer.get(), body.length());
}
class UpdateJobTestHelper
: public EmbeddedWorkerTestHelper,
public ServiceWorkerRegistration::Listener,
public ServiceWorkerVersion::Listener {
public:
struct AttributeChangeLogEntry {
int64_t registration_id;
ChangedVersionAttributesMask mask;
ServiceWorkerRegistrationInfo info;
};
struct StateChangeLogEntry {
int64_t version_id;
ServiceWorkerVersion::Status status;
};
UpdateJobTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {}
~UpdateJobTestHelper() override {
if (registration_.get())
registration_->RemoveListener(this);
}
ServiceWorkerStorage* storage() { return context()->storage(); }
ServiceWorkerJobCoordinator* job_coordinator() {
return context()->job_coordinator();
}
void set_force_start_worker_failure(bool force_start_worker_failure) {
force_start_worker_failure_ = force_start_worker_failure;
}
scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration(
const GURL& test_origin) {
scoped_refptr<ServiceWorkerRegistration> registration;
bool called = false;
job_coordinator()->Register(
test_origin.Resolve(kScope),
test_origin.Resolve(kScript),
NULL,
SaveRegistration(SERVICE_WORKER_OK, &called, ®istration));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
EXPECT_TRUE(registration.get());
EXPECT_TRUE(registration->active_version());
EXPECT_FALSE(registration->installing_version());
EXPECT_FALSE(registration->waiting_version());
registration_ = registration;
return registration;
}
// EmbeddedWorkerTestHelper overrides
void OnStartWorker(
int embedded_worker_id,
int64_t version_id,
const GURL& scope,
const GURL& script,
bool pause_after_download,
mojom::ServiceWorkerEventDispatcherRequest request) override {
const std::string kMockScriptBody = "mock_script";
const uint64_t kMockScriptSize = 19284;
ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
ServiceWorkerRegistration* registration =
context()->GetLiveRegistration(version->registration_id());
bool is_update = registration->active_version() &&
version != registration->active_version();
ASSERT_TRUE(version);
version->AddListener(this);
// Simulate network access.
base::TimeDelta time_since_last_check =
base::Time::Now() - registration->last_update_check();
if (!is_update || script.GetOrigin() != kNoChangeOrigin ||
time_since_last_check > base::TimeDelta::FromHours(
kServiceWorkerScriptMaxCacheAgeInHours)) {
version->embedded_worker()->OnNetworkAccessedForScriptLoad();
}
int64_t resource_id = storage()->NewResourceId();
version->script_cache_map()->NotifyStartedCaching(script, resource_id);
if (!is_update) {
// Spoof caching the script for the initial version.
WriteStringResponse(storage(), resource_id, kMockScriptBody);
version->script_cache_map()->NotifyFinishedCaching(
script, kMockScriptSize, net::OK, std::string());
version->SetMainScriptHttpResponseInfo(
EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
} else {
if (script.GetOrigin() == kNoChangeOrigin) {
// Simulate fetching the updated script and finding it's identical to
// the incumbent.
version->script_cache_map()->NotifyFinishedCaching(
script, kMockScriptSize, net::ERR_FILE_EXISTS, std::string());
version->SetMainScriptHttpResponseInfo(
EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
SimulateWorkerScriptLoaded(embedded_worker_id);
return;
}
// Spoof caching the script for the new version.
WriteStringResponse(storage(), resource_id, "mock_different_script");
version->script_cache_map()->NotifyFinishedCaching(
script, kMockScriptSize, net::OK, std::string());
version->SetMainScriptHttpResponseInfo(
EmbeddedWorkerTestHelper::CreateHttpResponseInfo());
}
EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id,
scope, script, pause_after_download,
std::move(request));
}
void OnResumeAfterDownload(int embedded_worker_id) override {
if (!force_start_worker_failure_) {
EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id);
} else {
SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id);
SimulateWorkerScriptEvaluated(embedded_worker_id, false);
}
}
// ServiceWorkerRegistration::Listener overrides
void OnVersionAttributesChanged(
ServiceWorkerRegistration* registration,
ChangedVersionAttributesMask changed_mask,
const ServiceWorkerRegistrationInfo& info) override {
AttributeChangeLogEntry entry;
entry.registration_id = registration->id();
entry.mask = changed_mask;
entry.info = info;
attribute_change_log_.push_back(entry);
}
void OnRegistrationFailed(ServiceWorkerRegistration* registration) override {
NOTREACHED();
}
void OnUpdateFound(ServiceWorkerRegistration* registration) override {
update_found_ = true;
}
// ServiceWorkerVersion::Listener overrides
void OnVersionStateChanged(ServiceWorkerVersion* version) override {
StateChangeLogEntry entry;
entry.version_id = version->version_id();
entry.status = version->status();
state_change_log_.push_back(entry);
}
scoped_refptr<ServiceWorkerRegistration> registration_;
std::vector<AttributeChangeLogEntry> attribute_change_log_;
std::vector<StateChangeLogEntry> state_change_log_;
bool update_found_ = false;
bool force_start_worker_failure_ = false;
};
// Helper class for update tests that evicts the active version when the update
// worker is about to be started.
class EvictIncumbentVersionHelper : public UpdateJobTestHelper {
public:
EvictIncumbentVersionHelper() {}
~EvictIncumbentVersionHelper() override {}
void OnStartWorker(
int embedded_worker_id,
int64_t version_id,
const GURL& scope,
const GURL& script,
bool pause_after_download,
mojom::ServiceWorkerEventDispatcherRequest request) override {
ServiceWorkerVersion* version = context()->GetLiveVersion(version_id);
ServiceWorkerRegistration* registration =
context()->GetLiveRegistration(version->registration_id());
bool is_update = registration->active_version() &&
version != registration->active_version();
if (is_update) {
// Evict the incumbent worker.
ASSERT_FALSE(registration->waiting_version());
registration->DeleteVersion(
make_scoped_refptr(registration->active_version()));
}
UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope,
script, pause_after_download,
std::move(request));
}
void OnRegistrationFailed(ServiceWorkerRegistration* registration) override {
registration_failed_ = true;
}
bool registration_failed_ = false;
};
} // namespace
TEST_F(ServiceWorkerJobTest, Update_NoChange) {
UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNoChangeOrigin);
ASSERT_TRUE(registration.get());
ASSERT_EQ(4u, update_helper->state_change_log_.size());
EXPECT_EQ(ServiceWorkerVersion::INSTALLING,
update_helper->state_change_log_[0].status);
EXPECT_EQ(ServiceWorkerVersion::INSTALLED,
update_helper->state_change_log_[1].status);
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING,
update_helper->state_change_log_[2].status);
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
update_helper->state_change_log_[3].status);
update_helper->state_change_log_.clear();
// Run the update job.
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
base::RunLoop().RunUntilIdle();
// Verify results.
ASSERT_TRUE(registration->active_version());
EXPECT_EQ(first_version.get(), registration->active_version());
EXPECT_FALSE(registration->installing_version());
EXPECT_FALSE(registration->waiting_version());
EXPECT_TRUE(update_helper->attribute_change_log_.empty());
ASSERT_EQ(1u, update_helper->state_change_log_.size());
EXPECT_NE(registration->active_version()->version_id(),
update_helper->state_change_log_[0].version_id);
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT,
update_helper->state_change_log_[0].status);
EXPECT_FALSE(update_helper->update_found_);
}
TEST_F(ServiceWorkerJobTest, Update_BumpLastUpdateCheckTime) {
const base::Time kToday = base::Time::Now();
const base::Time kYesterday =
kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1);
UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNoChangeOrigin);
ASSERT_TRUE(registration.get());
registration->AddListener(update_helper);
// Run an update where the script did not change and the network was not
// accessed. The check time should not be updated.
registration->set_last_update_check(kToday);
registration->active_version()->StartUpdate();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(kToday, registration->last_update_check());
EXPECT_FALSE(update_helper->update_found_);
// Run an update where the script did not change and the network was accessed.
// The check time should be updated.
registration->set_last_update_check(kYesterday);
registration->active_version()->StartUpdate();
base::RunLoop().RunUntilIdle();
EXPECT_LT(kYesterday, registration->last_update_check());
EXPECT_FALSE(update_helper->update_found_);
registration->RemoveListener(update_helper);
registration = update_helper->SetupInitialRegistration(kNewVersionOrigin);
ASSERT_TRUE(registration.get());
registration->AddListener(update_helper);
// Run an update where the script changed. The check time should be updated.
registration->set_last_update_check(kYesterday);
registration->active_version()->StartUpdate();
base::RunLoop().RunUntilIdle();
EXPECT_LT(kYesterday, registration->last_update_check());
// Run an update to a worker that loads successfully but fails to start up
// (script evaluation failure). The check time should be updated.
update_helper->set_force_start_worker_failure(true);
registration->set_last_update_check(kYesterday);
registration->active_version()->StartUpdate();
base::RunLoop().RunUntilIdle();
EXPECT_LT(kYesterday, registration->last_update_check());
}
TEST_F(ServiceWorkerJobTest, Update_NewVersion) {
UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNewVersionOrigin);
ASSERT_TRUE(registration.get());
update_helper->state_change_log_.clear();
// Run the update job.
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
base::RunLoop().RunUntilIdle();
// Verify results.
ASSERT_TRUE(registration->active_version());
EXPECT_NE(first_version.get(), registration->active_version());
EXPECT_FALSE(registration->installing_version());
EXPECT_FALSE(registration->waiting_version());
ASSERT_EQ(3u, update_helper->attribute_change_log_.size());
UpdateJobTestHelper::AttributeChangeLogEntry entry;
entry = update_helper->attribute_change_log_[0];
EXPECT_TRUE(entry.mask.installing_changed());
EXPECT_FALSE(entry.mask.waiting_changed());
EXPECT_FALSE(entry.mask.active_changed());
EXPECT_NE(entry.info.installing_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_EQ(entry.info.waiting_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_NE(entry.info.active_version.version_id,
kInvalidServiceWorkerVersionId);
entry = update_helper->attribute_change_log_[1];
EXPECT_TRUE(entry.mask.installing_changed());
EXPECT_TRUE(entry.mask.waiting_changed());
EXPECT_FALSE(entry.mask.active_changed());
EXPECT_EQ(entry.info.installing_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_NE(entry.info.waiting_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_NE(entry.info.active_version.version_id,
kInvalidServiceWorkerVersionId);
entry = update_helper->attribute_change_log_[2];
EXPECT_FALSE(entry.mask.installing_changed());
EXPECT_TRUE(entry.mask.waiting_changed());
EXPECT_TRUE(entry.mask.active_changed());
EXPECT_EQ(entry.info.installing_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_EQ(entry.info.waiting_version.version_id,
kInvalidServiceWorkerVersionId);
EXPECT_NE(entry.info.active_version.version_id,
kInvalidServiceWorkerVersionId);
// expected version state transitions:
// new.installing, new.installed,
// old.redundant,
// new.activating, new.activated
ASSERT_EQ(5u, update_helper->state_change_log_.size());
EXPECT_EQ(registration->active_version()->version_id(),
update_helper->state_change_log_[0].version_id);
EXPECT_EQ(ServiceWorkerVersion::INSTALLING,
update_helper->state_change_log_[0].status);
EXPECT_EQ(registration->active_version()->version_id(),
update_helper->state_change_log_[1].version_id);
EXPECT_EQ(ServiceWorkerVersion::INSTALLED,
update_helper->state_change_log_[1].status);
EXPECT_EQ(first_version->version_id(),
update_helper->state_change_log_[2].version_id);
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT,
update_helper->state_change_log_[2].status);
EXPECT_EQ(registration->active_version()->version_id(),
update_helper->state_change_log_[3].version_id);
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING,
update_helper->state_change_log_[3].status);
EXPECT_EQ(registration->active_version()->version_id(),
update_helper->state_change_log_[4].version_id);
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
update_helper->state_change_log_[4].status);
EXPECT_TRUE(update_helper->update_found_);
}
// Test that the update job uses the script URL of the newest worker when the
// job starts, rather than when it is scheduled.
TEST_F(ServiceWorkerJobTest, Update_ScriptUrlChanged) {
// Create a registration with an active version.
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/one/"),
GURL("http://www.example.com/service_worker.js"));
// Queue an Update. When this runs, it will use the waiting version's script.
job_coordinator()->Update(registration.get(), false);
// Add a waiting version with a new script.
GURL new_script("http://www.example.com/new_worker.js");
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
registration.get(), new_script, 2L /* dummy version id */,
helper_->context()->AsWeakPtr());
registration->SetWaitingVersion(version);
// Run the update job.
base::RunLoop().RunUntilIdle();
// The update job should have created a new version with the new script,
// and promoted it to the active version.
EXPECT_EQ(new_script, registration->active_version()->script_url());
EXPECT_EQ(nullptr, registration->waiting_version());
EXPECT_EQ(nullptr, registration->installing_version());
}
// Test that update succeeds if the incumbent worker was evicted
// during the update job (this can happen on disk cache failure).
TEST_F(ServiceWorkerJobTest, Update_EvictedIncumbent) {
EvictIncumbentVersionHelper* update_helper = new EvictIncumbentVersionHelper;
helper_.reset(update_helper);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNewVersionOrigin);
ASSERT_TRUE(registration.get());
update_helper->state_change_log_.clear();
// Run the update job.
registration->AddListener(update_helper);
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->StartUpdate();
base::RunLoop().RunUntilIdle();
// Verify results.
ASSERT_TRUE(registration->active_version());
EXPECT_NE(first_version.get(), registration->active_version());
EXPECT_FALSE(registration->installing_version());
EXPECT_FALSE(registration->waiting_version());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, first_version->status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED,
registration->active_version()->status());
ASSERT_EQ(4u, update_helper->attribute_change_log_.size());
EXPECT_TRUE(update_helper->update_found_);
EXPECT_TRUE(update_helper->registration_failed_);
EXPECT_FALSE(registration->is_uninstalled());
}
TEST_F(ServiceWorkerJobTest, Update_UninstallingRegistration) {
bool called;
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(GURL("http://www.example.com/one/"),
GURL("http://www.example.com/service_worker.js"));
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
ServiceWorkerVersion* active_version = registration->active_version();
active_version->AddControllee(host.get());
job_coordinator()->Unregister(GURL("http://www.example.com/one/"),
SaveUnregistration(SERVICE_WORKER_OK, &called));
// Update should abort after it starts and sees uninstalling.
job_coordinator()->Update(registration.get(), false);
EXPECT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
// Verify the registration was not modified by the Update.
EXPECT_TRUE(registration->is_uninstalling());
EXPECT_EQ(active_version, registration->active_version());
EXPECT_EQ(NULL, registration->waiting_version());
EXPECT_EQ(NULL, registration->installing_version());
}
TEST_F(ServiceWorkerJobTest, RegisterWhileUninstalling) {
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
registration->SetTaskRunnerForTest(runner);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
// Register another script.
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_EQ(old_version, registration->active_version());
scoped_refptr<ServiceWorkerVersion> new_version =
registration->waiting_version();
// Verify the new version is installed but not activated yet.
EXPECT_EQ(NULL, registration->installing_version());
EXPECT_TRUE(new_version);
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::INSTALLED, new_version->status());
old_version->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_FALSE(registration->is_uninstalled());
// Verify the new version is activated.
EXPECT_EQ(NULL, registration->installing_version());
EXPECT_EQ(NULL, registration->waiting_version());
EXPECT_EQ(new_version, registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
runner->RunUntilIdle();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status());
}
TEST_F(ServiceWorkerJobTest, RegisterAndUnregisterWhileUninstalling) {
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
EXPECT_EQ(registration, FindRegistrationForPattern(pattern));
scoped_refptr<ServiceWorkerVersion> new_version =
registration->waiting_version();
ASSERT_TRUE(new_version);
// Unregister the registration (but it's still live).
RunUnregisterJob(pattern);
// Now it's not found in the storage.
RunUnregisterJob(pattern, SERVICE_WORKER_ERROR_NOT_FOUND);
FindRegistrationForPattern(pattern, SERVICE_WORKER_ERROR_NOT_FOUND);
EXPECT_TRUE(registration->is_uninstalling());
EXPECT_EQ(old_version, registration->active_version());
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, old_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, old_version->status());
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::INSTALLED, new_version->status());
old_version->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_TRUE(registration->is_uninstalled());
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, old_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status());
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, new_version->status());
}
TEST_F(ServiceWorkerJobTest, RegisterSameScriptMultipleTimesWhileUninstalling) {
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
registration->SetTaskRunnerForTest(runner);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
scoped_refptr<ServiceWorkerVersion> new_version =
registration->waiting_version();
ASSERT_TRUE(new_version);
RunUnregisterJob(pattern);
EXPECT_TRUE(registration->is_uninstalling());
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_EQ(new_version, registration->waiting_version());
old_version->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_FALSE(registration->is_uninstalled());
// Verify the new version is activated.
EXPECT_EQ(NULL, registration->installing_version());
EXPECT_EQ(NULL, registration->waiting_version());
EXPECT_EQ(new_version, registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
runner->RunUntilIdle();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status());
}
TEST_F(ServiceWorkerJobTest, RegisterMultipleTimesWhileUninstalling) {
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js?first");
GURL script2("http://www.example.com/service_worker.js?second");
GURL script3("http://www.example.com/service_worker.js?third");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
registration->SetTaskRunnerForTest(runner);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->AddControllee(host.get());
RunUnregisterJob(pattern);
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
scoped_refptr<ServiceWorkerVersion> second_version =
registration->waiting_version();
ASSERT_TRUE(second_version);
RunUnregisterJob(pattern);
EXPECT_TRUE(registration->is_uninstalling());
EXPECT_EQ(registration, RunRegisterJob(pattern, script3));
scoped_refptr<ServiceWorkerVersion> third_version =
registration->waiting_version();
ASSERT_TRUE(third_version);
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, second_version->status());
first_version->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_FALSE(registration->is_uninstalled());
// Verify the new version is activated.
EXPECT_EQ(NULL, registration->installing_version());
EXPECT_EQ(NULL, registration->waiting_version());
EXPECT_EQ(third_version, registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, third_version->status());
runner->RunUntilIdle();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, third_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status());
}
class EventCallbackHelper : public EmbeddedWorkerTestHelper {
public:
EventCallbackHelper()
: EmbeddedWorkerTestHelper(base::FilePath()),
install_event_result_(blink::WebServiceWorkerEventResultCompleted),
activate_event_result_(blink::WebServiceWorkerEventResultCompleted) {}
void OnInstallEvent(int embedded_worker_id,
int request_id) override {
if (!install_callback_.is_null())
install_callback_.Run();
SimulateSend(new ServiceWorkerHostMsg_InstallEventFinished(
embedded_worker_id, request_id, install_event_result_,
has_fetch_handler_, base::Time::Now()));
}
void OnActivateEvent(int embedded_worker_id, int request_id) override {
SimulateSend(new ServiceWorkerHostMsg_ActivateEventFinished(
embedded_worker_id, request_id, activate_event_result_,
base::Time::Now()));
}
void set_install_callback(const base::Closure& callback) {
install_callback_ = callback;
}
void set_install_event_result(blink::WebServiceWorkerEventResult result) {
install_event_result_ = result;
}
void set_activate_event_result(blink::WebServiceWorkerEventResult result) {
activate_event_result_ = result;
}
void set_has_fetch_handler(bool has_fetch_handler) {
has_fetch_handler_ = has_fetch_handler;
}
private:
base::Closure install_callback_;
blink::WebServiceWorkerEventResult install_event_result_;
blink::WebServiceWorkerEventResult activate_event_result_;
bool has_fetch_handler_ = true;
};
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringInstall) {
EventCallbackHelper* helper = new EventCallbackHelper;
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
// Register another script. While installing, old_version loses controllee.
helper->set_install_callback(
base::Bind(&ServiceWorkerVersion::RemoveControllee,
old_version, host.get()));
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_FALSE(registration->is_uninstalled());
// Verify the new version is activated.
scoped_refptr<ServiceWorkerVersion> new_version =
registration->active_version();
EXPECT_NE(old_version, new_version);
EXPECT_EQ(NULL, registration->installing_version());
EXPECT_EQ(NULL, registration->waiting_version());
EXPECT_EQ(new_version, registration->active_version());
EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, new_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, new_version->status());
EXPECT_EQ(registration, FindRegistrationForPattern(pattern));
}
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringRejectedInstall) {
EventCallbackHelper* helper = new EventCallbackHelper;
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
// Register another script that fails to install. While installing,
// old_version loses controllee.
helper->set_install_callback(
base::Bind(&ServiceWorkerVersion::RemoveControllee,
old_version, host.get()));
helper->set_install_event_result(blink::WebServiceWorkerEventResultRejected);
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
// Verify the registration was uninstalled.
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_TRUE(registration->is_uninstalled());
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, old_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status());
FindRegistrationForPattern(pattern, SERVICE_WORKER_ERROR_NOT_FOUND);
}
TEST_F(ServiceWorkerJobTest, RemoveControlleeDuringInstall_RejectActivate) {
EventCallbackHelper* helper = new EventCallbackHelper;
helper_.reset(helper);
GURL pattern("http://www.example.com/one/");
GURL script1("http://www.example.com/service_worker.js");
GURL script2("http://www.example.com/service_worker.js?new");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script1);
// Add a controllee and queue an unregister to force the uninstalling state.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> old_version =
registration->active_version();
old_version->AddControllee(host.get());
RunUnregisterJob(pattern);
// Register another script that fails to activate. While installing,
// old_version loses controllee.
helper->set_install_callback(
base::Bind(&ServiceWorkerVersion::RemoveControllee,
old_version, host.get()));
helper->set_activate_event_result(blink::WebServiceWorkerEventResultRejected);
EXPECT_EQ(registration, RunRegisterJob(pattern, script2));
// Verify the registration remains.
EXPECT_FALSE(registration->is_uninstalling());
EXPECT_FALSE(registration->is_uninstalled());
EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, old_version->running_status());
EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, old_version->status());
FindRegistrationForPattern(pattern, SERVICE_WORKER_OK);
}
TEST_F(ServiceWorkerJobTest, HasFetchHandler) {
EventCallbackHelper* helper = new EventCallbackHelper;
helper_.reset(helper);
GURL pattern("http://www.example.com/");
GURL script("http://www.example.com/service_worker.js");
scoped_refptr<ServiceWorkerRegistration> registration;
helper->set_has_fetch_handler(true);
RunRegisterJob(pattern, script);
registration = FindRegistrationForPattern(pattern);
EXPECT_EQ(ServiceWorkerVersion::FetchHandlerExistence::EXISTS,
registration->active_version()->fetch_handler_existence());
RunUnregisterJob(pattern);
helper->set_has_fetch_handler(false);
RunRegisterJob(pattern, script);
registration = FindRegistrationForPattern(pattern);
EXPECT_EQ(ServiceWorkerVersion::FetchHandlerExistence::DOES_NOT_EXIST,
registration->active_version()->fetch_handler_existence());
RunUnregisterJob(pattern);
}
class CheckPauseAfterDownloadEmbeddedWorkerInstanceClient
: public EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient {
public:
explicit CheckPauseAfterDownloadEmbeddedWorkerInstanceClient(
base::WeakPtr<EmbeddedWorkerTestHelper> helper)
: EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {}
int num_of_startworker() const { return num_of_startworker_; }
void set_next_pause_after_download(bool expectation) {
next_pause_after_download_ = expectation;
}
protected:
void StartWorker(
const EmbeddedWorkerStartParams& params,
mojom::ServiceWorkerEventDispatcherRequest request) override {
ASSERT_TRUE(next_pause_after_download_.has_value());
EXPECT_EQ(next_pause_after_download_.value(), params.pause_after_download);
num_of_startworker_++;
EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker(
params, std::move(request));
}
private:
base::Optional<bool> next_pause_after_download_;
int num_of_startworker_ = 0;
DISALLOW_COPY_AND_ASSIGN(CheckPauseAfterDownloadEmbeddedWorkerInstanceClient);
};
TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) {
UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
helper_.reset(update_helper);
std::vector<CheckPauseAfterDownloadEmbeddedWorkerInstanceClient*> clients;
clients.push_back(helper_->CreateAndRegisterMockInstanceClient<
CheckPauseAfterDownloadEmbeddedWorkerInstanceClient>(
helper_->AsWeakPtr()));
clients.push_back(helper_->CreateAndRegisterMockInstanceClient<
CheckPauseAfterDownloadEmbeddedWorkerInstanceClient>(
helper_->AsWeakPtr()));
// The initial version should not pause after download.
clients[0]->set_next_pause_after_download(false);
scoped_refptr<ServiceWorkerRegistration> registration =
update_helper->SetupInitialRegistration(kNewVersionOrigin);
ASSERT_EQ(1, clients[0]->num_of_startworker());
// The updated version should pause after download.
clients[1]->set_next_pause_after_download(true);
registration->AddListener(update_helper);
registration->active_version()->StartUpdate();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1, clients[1]->num_of_startworker());
}
// Test that activation doesn't complete if it's triggered by removing a
// controllee and starting the worker failed due to shutdown.
TEST_F(ServiceWorkerJobTest, ActivateCancelsOnShutdown) {
UpdateJobTestHelper* update_helper = new UpdateJobTestHelper;
helper_.reset(update_helper);
GURL pattern("http://www.example.com/");
GURL script("http://www.example.com/service_worker.js");
scoped_refptr<ServiceWorkerRegistration> registration =
RunRegisterJob(pattern, script);
scoped_refptr<base::TestSimpleTaskRunner> runner(
new base::TestSimpleTaskRunner());
registration->SetTaskRunnerForTest(runner);
// Add a controllee.
std::unique_ptr<ServiceWorkerProviderHost> host = CreateControllee();
scoped_refptr<ServiceWorkerVersion> first_version =
registration->active_version();
first_version->AddControllee(host.get());
// Update. The new version should be waiting.
registration->AddListener(update_helper);
first_version->StartUpdate();
base::RunLoop().RunUntilIdle();
scoped_refptr<ServiceWorkerVersion> new_version =
registration->waiting_version();
ASSERT_TRUE(new_version);
EXPECT_EQ(ServiceWorkerVersion::INSTALLED, new_version->status());
// Stop the worker so that it must start again when activation is attempted.
// (This is not strictly necessary to exercise the codepath, but it makes it
// easy to cause a failure with set_force_start_worker_failure after
// shutdown is simulated. Otherwise our test helper often fails on
// DCHECK(context)).
new_version->StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
// Remove the controllee. The new version should be activating, and delayed
// until the runner runs again.
first_version->RemoveControllee(host.get());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(new_version.get(), registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
// Shutdown.
update_helper->context()->wrapper()->Shutdown();
update_helper->set_force_start_worker_failure(true);
// Allow the activation to continue. It will fail, and the worker
// should not be promoted to ACTIVATED because failure occur
// during shutdown.
runner->RunUntilIdle();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(new_version.get(), registration->active_version());
EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
registration->RemoveListener(update_helper);
}
} // namespace content
|