1description("This is a test to make sure DOM Storage mutations fire StorageEvents that are caught by the event listener attached via setattribute.");
2
3function test(storageString, callback)
4{
5    window.completionCallback = callback;
6    window.storage = eval(storageString);
7    if (!storage) {
8        testFailed(storageString + " DOES NOT exist");
9        return;
10    }
11
12    debug("Testing " + storageString);
13
14    evalAndLog("storage.clear()");
15    shouldBe("storage.length", "0");
16
17    evalAndLog("iframe.onload = step1");
18    evalAndLog("iframe.src = 'resources/setattribute-event-handler.html'");
19}
20
21function step1()
22{
23    debug("Reset storage event list");
24    evalAndLog("storageEventList = new Array()");
25    evalAndLog("storage.setItem('FOO', 'BAR')");
26
27    runAfterStorageEvents(step2);
28}
29
30function step2()
31{
32    shouldBe("storageEventList.length", "1");
33    shouldBeEqualToString("storageEventList[0].key", "FOO");
34    shouldBeNull("storageEventList[0].oldValue");
35    shouldBeEqualToString("storageEventList[0].newValue", "BAR");
36    evalAndLog("storage.setItem('FU', 'BAR')");
37    evalAndLog("storage.setItem('a', '1')");
38    evalAndLog("storage.setItem('b', '2')");
39    evalAndLog("storage.setItem('b', '3')");
40
41    runAfterStorageEvents(step3);
42}
43
44function step3()
45{
46    shouldBe("storageEventList.length", "5");
47    shouldBeEqualToString("storageEventList[1].key", "FU");
48    shouldBeNull("storageEventList[1].oldValue");
49    shouldBeEqualToString("storageEventList[1].newValue", "BAR");
50    shouldBeEqualToString("storageEventList[2].key", "a");
51    shouldBeNull("storageEventList[2].oldValue");
52    shouldBeEqualToString("storageEventList[2].newValue", "1");
53    shouldBeEqualToString("storageEventList[3].key", "b");
54    shouldBeNull("storageEventList[3].oldValue");
55    shouldBeEqualToString("storageEventList[3].newValue", "2");
56    shouldBeEqualToString("storageEventList[4].key", "b");
57    shouldBeEqualToString("storageEventList[4].oldValue", "2");
58    shouldBeEqualToString("storageEventList[4].newValue", "3");
59    evalAndLog("storage.removeItem('FOO')");
60
61    runAfterStorageEvents(step4);
62}
63
64function step4()
65{
66    shouldBe("storageEventList.length", "6");
67    shouldBeEqualToString("storageEventList[5].key", "FOO");
68    shouldBeEqualToString("storageEventList[5].oldValue", "BAR");
69    shouldBeNull("storageEventList[5].newValue");
70    evalAndLog("storage.removeItem('FU')");
71
72    runAfterStorageEvents(step5);
73}
74
75function step5()
76{
77    shouldBe("storageEventList.length", "7");
78    shouldBeEqualToString("storageEventList[6].key", "FU");
79    shouldBeEqualToString("storageEventList[6].oldValue", "BAR");
80    shouldBeNull("storageEventList[6].newValue");
81    evalAndLog("storage.clear()");
82
83    runAfterStorageEvents(step6);
84}
85
86function step6()
87{
88    shouldBe("storageEventList.length", "8");
89    shouldBeNull("storageEventList[7].key");
90    shouldBeNull("storageEventList[7].oldValue");
91    shouldBeNull("storageEventList[7].newValue");
92
93    completionCallback();
94}
95
96testStorages(test);
97
98var successfullyParsed = true;
99