blob: 2f38c1469b0b1f96906419865f515c6480212c50 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function clearStorages() {
sessionStorage.clear();
localStorage.clear();
}
function triggerActions(storage) {
storage.setItem("foo", "value1");
storage.setItem("x", "xvalue");
storage.setItem("foo", "value2");
storage.removeItem("foo");
storage.clear();
}
function test()
{
function testStorageEvents(storageObject, callback) {
let count = 0;
storageObject.addEventListener(WI.DOMStorageObject.Event.ItemAdded, (event) => {
count++;
if (count === 1) {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemAdded");
InspectorTest.expectThat(event.data.key === "foo", "Should add key 'foo'.");
InspectorTest.expectThat(event.data.value === "value1", "Should have value 'value1'.");
return;
}
if (count === 2) {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemAdded");
InspectorTest.expectThat(event.data.key === "x", "Should add key 'x'.");
InspectorTest.expectThat(event.data.value === "xvalue", "Should have value 'xvalue'.");
return;
}
InspectorTest.fail("Unexpected WI.DOMStorageObject.Event.ItemAdded");
});
storageObject.awaitEvent(WI.DOMStorageObject.Event.ItemRemoved)
.then((event) => {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemRemoved");
InspectorTest.expectThat(event.data.key === "foo", "Should remove key 'x'.");
});
storageObject.awaitEvent(WI.DOMStorageObject.Event.ItemUpdated)
.then((event) => {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemUpdated");
InspectorTest.expectThat(event.data.key === "foo", "Should update key 'x'.");
InspectorTest.expectThat(event.data.oldValue === "value1", "Should have oldValue 'value1'.");
InspectorTest.expectThat(event.data.value === "value2", "Should have new value 'value2'.");
});
storageObject.awaitEvent(WI.DOMStorageObject.Event.ItemsCleared)
.then((event) => {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemsCleared");
storageObject.removeEventListener(WI.DOMStorageObject.Event.ItemAdded, null, null);
callback();
});
}
let suite = InspectorTest.createAsyncSuite("DOMStorage.Events");
suite.addTestCase({
name: "TestSessionStorage",
description: "Test backend generated DOMStorage added, removed, updated, and cleared events for sessionStorage.",
test(resolve, reject) {
InspectorTest.evaluateInPage("triggerActions(sessionStorage)");
let sessionStorage = WI.storageManager.domStorageObjects.find((x) => !x.isLocalStorage());
InspectorTest.expectThat(sessionStorage, "Should have a DOMStorageObject for sessionStorage.");
testStorageEvents(sessionStorage, resolve);
}
});
suite.addTestCase({
name: "TestLocalStorage",
description: "Test backend generated DOMStorage added, removed, updated, and cleared events for localStorage.",
test(resolve, reject) {
InspectorTest.evaluateInPage("triggerActions(localStorage)");
let localStorage = WI.storageManager.domStorageObjects.find((x) => x.isLocalStorage());
InspectorTest.expectThat(localStorage, "Should have a DOMStorageObject for localStorage.");
testStorageEvents(localStorage, resolve);
}
});
InspectorTest.evaluateInPage("clearStorages()", () => {
suite.runTestCasesAndFinish();
});
}
</script>
</head>
<body onload="runTest()">
<p>Test for the DOMStorage events.</p>
</body>
</html>