summaryrefslogtreecommitdiffstats
path: root/dom/localstorage/test/unit/test_orderingAfterRemoveAdd.js
blob: 88a2e45d2a13917012473d23d066635fe1069b6d (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

add_task(async function testSteps() {
  const url = "http://example.com";

  info("Setting pref");

  Services.prefs.setBoolPref("dom.storage.snapshot_reusing", false);

  const items = [
    { key: "key01", value: "value01" },
    { key: "key02", value: "value02" },
    { key: "key03", value: "value03" },
    { key: "key04", value: "value04" },
    { key: "key05", value: "value05" },
  ];

  info("Getting storage");

  let storage = getLocalStorage(getPrincipal(url));

  // 1st snapshot

  info("Adding data");

  for (let item of items) {
    storage.setItem(item.key, item.value);
  }

  info("Returning to event loop");

  await returnToEventLoop();

  // 2nd snapshot

  // Remove first two items, add some new items and add the two items back.

  storage.removeItem("key01");
  storage.removeItem("key02");

  storage.setItem("key06", "value06");
  storage.setItem("key07", "value07");
  storage.setItem("key08", "value08");

  storage.setItem("key01", "value01");
  storage.setItem("key02", "value02");

  info("Saving key order");

  let savedKeys = Object.keys(storage);

  info("Returning to event loop");

  await returnToEventLoop();

  // 3rd snapshot

  info("Verifying key order");

  let keys = Object.keys(storage);

  is(keys.length, savedKeys.length);

  for (let i = 0; i < keys.length; i++) {
    is(keys[i], savedKeys[i], "Correct key");
  }
});