|
| 1 | +<!doctype html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>Storage event recipients are selected when storage changes</title> |
| 4 | +<link rel="help" href="https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast"> |
| 5 | +<script src="/resources/testharness.js"></script> |
| 6 | +<script src="/resources/testharnessreport.js"></script> |
| 7 | +<script src="/common/utils.js"></script> |
| 8 | +<script src="/html/browsers/browsing-the-web/resources/helpers.js"></script> |
| 9 | +<body> |
| 10 | +<script> |
| 11 | +"use strict"; |
| 12 | +/* global addIframe, token */ |
| 13 | + |
| 14 | +for (const storageType of ["localStorage", "sessionStorage"]) { |
| 15 | + for (const operation of ["setItem", "removeItem", "clear"]) { |
| 16 | + promise_test(async t => { |
| 17 | + const existing = await addIframe(); |
| 18 | + t.add_cleanup(() => existing.remove()); |
| 19 | + const storage = window[storageType]; |
| 20 | + const key = token(); |
| 21 | + t.add_cleanup(() => storage.removeItem(key)); |
| 22 | + const watcher = new EventWatcher(t, existing.contentWindow, "storage"); |
| 23 | + if (operation !== "setItem") { |
| 24 | + const initialized = watcher.wait_for("storage"); |
| 25 | + storage.setItem(key, "initial"); |
| 26 | + await initialized; |
| 27 | + } |
| 28 | + const delivered = watcher.wait_for("storage"); |
| 29 | + if (operation === "setItem") { |
| 30 | + storage.setItem(key, "value"); |
| 31 | + } else if (operation === "removeItem") { |
| 32 | + storage.removeItem(key); |
| 33 | + } else { |
| 34 | + storage.clear(); |
| 35 | + } |
| 36 | + const late = document.createElement("iframe"); |
| 37 | + t.add_cleanup(() => late.remove()); |
| 38 | + document.body.append(late); |
| 39 | + const events = []; |
| 40 | + const checkpointKey = token(); |
| 41 | + t.add_cleanup(() => storage.removeItem(checkpointKey)); |
| 42 | + const checkpoint = new Promise(resolve => { |
| 43 | + late.contentWindow.onstorage = event => { |
| 44 | + events.push(event.key); |
| 45 | + if (event.key === checkpointKey) { |
| 46 | + resolve(); |
| 47 | + } |
| 48 | + }; |
| 49 | + }); |
| 50 | + const event = await delivered; |
| 51 | + watcher.stop_watching(); |
| 52 | + assert_equals(event.key, operation === "clear" ? null : key); |
| 53 | + // Deliver a subsequent storage event to the late document before checking that it missed the earlier one. |
| 54 | + storage.setItem(checkpointKey, "checkpoint"); |
| 55 | + await checkpoint; |
| 56 | + assert_array_equals(events, [checkpointKey], "A document created after the mutation is not a recipient"); |
| 57 | + }, `${storageType}.${operation} does not send events to documents created after the mutation`); |
| 58 | + } |
| 59 | + |
| 60 | + promise_test(async t => { |
| 61 | + const source = await addIframe(); |
| 62 | + t.add_cleanup(() => source.remove()); |
| 63 | + const key = token(); |
| 64 | + t.add_cleanup(() => window[storageType].removeItem(key)); |
| 65 | + const delivered = new EventWatcher(t, window, "storage").wait_for("storage"); |
| 66 | + source.contentWindow[storageType].setItem(key, "value"); |
| 67 | + source.remove(); |
| 68 | + assert_equals((await delivered).key, key); |
| 69 | + }, `${storageType} events survive destruction of the source document`); |
| 70 | + |
| 71 | + promise_test(async t => { |
| 72 | + const recipient = await addIframe(); |
| 73 | + t.add_cleanup(() => recipient.remove()); |
| 74 | + const surviving = await addIframe(); |
| 75 | + t.add_cleanup(() => surviving.remove()); |
| 76 | + const child = recipient.contentWindow; |
| 77 | + const key = token(); |
| 78 | + t.add_cleanup(() => window[storageType].removeItem(key)); |
| 79 | + const delivered = new EventWatcher(t, surviving.contentWindow, "storage").wait_for("storage"); |
| 80 | + window[storageType].setItem(key, "value"); |
| 81 | + recipient.remove(); |
| 82 | + // Install the listener after removal so clearing listeners during destruction cannot make the test pass. |
| 83 | + const events = []; |
| 84 | + child.onstorage = event => events.push(event.key); |
| 85 | + assert_equals((await delivered).key, key); |
| 86 | + assert_array_equals(events, []); |
| 87 | + }, `${storageType} events are discarded when the recipient document is destroyed`); |
| 88 | +} |
| 89 | +</script> |
0 commit comments