summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webstorage/event_no_duplicates.html
blob: 8fbea2bef90b8b6a063d77b94987a98cb895f417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE HTML>
<html>
<title>WebStorage Test: StorageEvent - only if something changes</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
const iframe = document.createElement('iframe');

function tests(storageName) {
  test(t => assert_true(storageName in window), storageName + ' exists');

  const storage = window[storageName];
  const iframeStorage = iframe.contentWindow[storageName];

  add_completion_callback(() => {
    storage.clear();
  });

  promise_test(t => {
    const w = new EventWatcher(t, iframe.contentWindow, 'storage');

    // Random key to make sure we don't conflict with any cruft leftover from
    // earlier runs. Any synchronization would be really hard with localStorage
    // limited guarantees.
    const testKey = Math.random().toString(36).slice(2);

    storage.setItem(testKey, 'foo');
    storage.setItem(testKey, 'foo');
    storage.setItem(testKey, 'bar');
    return w.wait_for('storage')
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, testKey);
          assert_equals(e.newValue, 'foo');
          return w.wait_for('storage');
        })
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, testKey);
          assert_equals(e.oldValue, 'foo');
          assert_equals(e.newValue, 'bar');
        });
  }, 'Setting to same value does not trigger event for ' + storageName);

  promise_test(t => {
    const w = new EventWatcher(t, iframe.contentWindow, 'storage');

    // Random key to make sure we don't conflict with any cruft leftover from
    // earlier runs. Any synchronization would be really hard with localStorage
    // limited guarantees.
    const testKey1 = Math.random().toString(36).slice(2);
    const testKey2 = Math.random().toString(36).slice(2);

    storage.removeItem(testKey1);
    storage.setItem(testKey2, 'foo');
    return w.wait_for('storage')
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, testKey2);
          assert_equals(e.newValue, 'foo');
        });
  }, 'Deleting non-existent key does not trigger event for ' + storageName);


  promise_test(t => {
    const w = new EventWatcher(t, iframe.contentWindow, 'storage');

    // Random key to make sure we don't conflict with any cruft leftover from
    // earlier runs. Any synchronization would be really hard with localStorage
    // limited guarantees.
    const testKey = Math.random().toString(36).slice(2);

    storage.setItem(testKey, 'foo');
    storage.clear();
    storage.clear();
    storage.setItem(testKey, 'bar');
    return w.wait_for('storage')
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, testKey);
          assert_equals(e.newValue, 'foo');
          return w.wait_for('storage');
        })
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, null);
          assert_equals(e.oldValue, null);
          assert_equals(e.newValue, null);
          return w.wait_for('storage');
        })
      .then(e => {
          assert_equals(e.storageArea, iframeStorage);
          assert_equals(e.key, testKey);
          assert_equals(e.oldValue, null);
          assert_equals(e.newValue, 'bar');
        });
  }, 'Clearing empty storage does not trigger event for ' + storageName);

}

iframe.src = "resources/event_basic.html";
iframe.onload = () => {
  tests('sessionStorage');
  tests('localStorage');
};
document.body.appendChild(iframe);
</script>
</body>
</html>