| <!-- webkit-test-runner [ enableBackForwardCache=true ] --> |
| <!DOCTYPE html> |
| <html> |
| <body> |
| <script src="resources/sw-test-pre.js"></script> |
| <script> |
| log("* Tests that a client is removed from the list of service worker clients while it is in the page cache"); |
| log(""); |
| |
| if (window.testRunner) |
| testRunner.setCanOpenWindows(); |
| |
| let topClientIdentifier = null; |
| let windowClientIdentifier = null; |
| |
| let tries = 0; |
| |
| function containsBothClients(clientIdentifiers) |
| { |
| return clientIdentifiers.includes(topClientIdentifier) && clientIdentifiers.includes(windowClientIdentifier); |
| } |
| |
| function containsOnlyTopClient(clientIdentifiers) |
| { |
| return clientIdentifiers.includes(topClientIdentifier) && !clientIdentifiers.includes(windowClientIdentifier); |
| } |
| |
| navigator.serviceWorker.addEventListener("message", function(event) { |
| if (step == "BothClientsInitiallyActive") { |
| if (!containsBothClients(event.data)) { |
| if (++tries > 20) { |
| log("FAIL: Wrong initial number of clients"); |
| finishSWTest(); |
| return; |
| } |
| worker.postMessage("getClientIds"); |
| return; |
| } |
| log("PASS: service worker has initially 2 clients"); |
| |
| otherWindow.addEventListener("pagehide", function(event) { |
| if (!event.persisted) { |
| log("FAIL: page failed to enter page cache"); |
| finishSWTest(); |
| return; |
| } |
| log("PASS: page is about to enter page cache"); |
| |
| setTimeout(function() { |
| step = "OnlyOneClientRemainsActive" |
| worker.postMessage("getClientIds"); |
| }, 0); |
| }); |
| |
| otherWindow.location.href = "about:blank"; |
| return; |
| } |
| |
| if (step == "OnlyOneClientRemainsActive") { |
| if (!containsOnlyTopClient(event.data)) { |
| log("FAIL: Wrong number of clients after one client entered page cache"); |
| finishSWTest(); |
| } |
| |
| log("PASS: service worker has only 1 client after 1 entered page cache"); |
| finishSWTest(); |
| } |
| }); |
| |
| navigator.serviceWorker.register("resources/getClientIds-worker.js", { }).then(function(registration) { |
| topClientIdentifier = internals.serviceWorkerClientIdentifier(document); |
| |
| worker = registration.installing; |
| otherWindow = open("other_resources/test.html"); |
| otherWindow.onload = function() { |
| windowClientIdentifier = internals.serviceWorkerClientIdentifier(otherWindow.document); |
| step = "BothClientsInitiallyActive" |
| worker.postMessage("getClientIds"); |
| }; |
| }); |
| </script> |
| </body> |
| </html> |