storage.https.html (4625B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="../service-workers/service-worker/resources/test-helpers.sub.js"></script> 7 <script src="support/test_utils.sub.js"></script> 8 </head> 9 10 <body> 11 <script> 12 /** @property{Datatype} The storage datatype. */ 13 var storage = TestUtils.DATATYPES.filter(function(datatype) { 14 return datatype.name == "storage"; 15 })[0]; 16 17 var serviceWorkerTestPageIFrame; 18 function fetchFromIFrame() { 19 return serviceWorkerTestPageIFrame.contentWindow 20 .fetch('controlled-endpoint.py') 21 .then((result) => { 22 return result.text(); 23 }); 24 } 25 26 // The tests are set up asynchronously. 27 setup({"explicit_done": true}); 28 29 // There must be at least one test added synchronously, otherwise 30 // testharness will complain. 31 // TODO(@msramek): Find a way to avoid this dummy test. 32 test(function() {}, "Populate backends."); 33 34 TestUtils.populateStorage() 35 .then(() => { 36 return new Promise(function(resolve, reject) { 37 promise_test(function(t) { 38 return navigator.serviceWorker.getRegistration("support/page_using_service_worker.html").then(function(reg) { 39 return wait_for_state(t, reg.installing || reg.waiting || reg.active, 'activated'); 40 }).then(resolve, reject); 41 }); 42 }); 43 }) 44 .then(() => { 45 return new Promise(function (resolve) { 46 // Create iFrame in the service worker's scope. This page will make a request 47 // for another page that is only served by the service worker 48 serviceWorkerTestPageIFrame = document.createElement("iframe"); 49 serviceWorkerTestPageIFrame.src = "support/page_using_service_worker.html"; 50 serviceWorkerTestPageIFrame.onload = function() { resolve(); }; 51 document.body.appendChild(serviceWorkerTestPageIFrame); 52 }); 53 }) 54 .then(() => { 55 const serviceWorkerResponseBody = fetchFromIFrame(); 56 57 promise_test(function() { 58 return serviceWorkerResponseBody.then(function(body) { 59 assert_equals(body, "FROM_SERVICE_WORKER", "Response should be from service worker"); 60 }); 61 }, "Baseline: Service worker responds to request"); 62 63 return serviceWorkerResponseBody; 64 }) 65 .then(function() { 66 const waitForControllerChange = new Promise(function(resolve) { 67 serviceWorkerTestPageIFrame.contentWindow 68 .navigator.serviceWorker.addEventListener("controllerchange", resolve); 69 }); 70 // Navigate to a resource with a Clear-Site-Data header in 71 // an iframe, then verify that all backends of the "storage" 72 // datatype have been deleted. 73 return new Promise(function(resolve, reject) { 74 window.addEventListener("message", resolve); 75 var iframe = document.createElement("iframe"); 76 iframe.src = TestUtils.getClearSiteDataUrl([storage]); 77 document.body.appendChild(iframe); 78 }).then(function() { 79 TestUtils.STORAGE.forEach(function(backend) { 80 var test_name = 81 "Clear backend when 'storage' is deleted: " + backend.name; 82 83 promise_test(function() { 84 return backend.isEmpty().then(function(isEmpty) { 85 assert_true( 86 isEmpty, 87 backend.name + " should have been cleared."); 88 }); 89 }, test_name); 90 }); 91 92 promise_test(function() { 93 return fetchFromIFrame().then(function(body) { 94 assert_equals(body, "FROM_NETWORK", "Response should be from network and not from the service worker"); 95 }); 96 }, "Service worker no longer responds to requests"); 97 98 promise_test(function() { 99 return waitForControllerChange.then(function() { 100 assert_false(!!serviceWorkerTestPageIFrame.contentWindow.navigator.serviceWorker.controller, 101 "Client should not have a controller"); 102 }); 103 }, "controllerchange event fires and client no longer has controller"); 104 105 done(); 106 }); 107 }); 108 </script> 109 </body> 110 </html>