blob: ffd2ead1a20b469885d73a8d5c30cbfbc383b4ab [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.expectEqual(event.data.key, "foo", "Should add key 'foo'.");
InspectorTest.expectEqual(event.data.value, "value1", "Should have value 'value1'.");
return;
}
if (count === 2) {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemAdded");
InspectorTest.expectEqual(event.data.key, "x", "Should add key 'x'.");
InspectorTest.expectEqual(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.expectEqual(event.data.key, "foo", "Should remove key 'x'.");
});
storageObject.awaitEvent(WI.DOMStorageObject.Event.ItemUpdated)
.then((event) => {
InspectorTest.pass("WI.DOMStorageObject.Event.ItemUpdated");
InspectorTest.expectEqual(event.data.key, "foo", "Should update key 'x'.");
InspectorTest.expectEqual(event.data.oldValue, "value1", "Should have oldValue 'value1'.");
InspectorTest.expectEqual(event.data.newValue, "value2", "Should have newValue '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.domStorageManager.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.domStorageManager.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>