summaryrefslogtreecommitdiffstats
path: root/devtools/client/storage/test/browser_storage_dynamic_updates_localStorage.js
blob: f220d16b0c28c30248025085fe87b8acdddd718d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Test dynamic updates in the storage inspector for localStorage.

add_task(async function () {
  const TEST_HOST = "https://test1.example.org";

  await openTabAndSetupStorage(MAIN_DOMAIN_SECURED + "storage-updates.html");

  gUI.tree.expandAll();

  ok(gUI.sidebar.hidden, "Sidebar is initially hidden");

  const expectedKeys = ["1", "2", "3", "4", "5", "null", "non-json-parsable"];

  // Test on string keys that JSON.parse can parse without throwing
  // (to verify the issue fixed by Bug 1578447 doesn't regress).
  await testRemoveAndChange("null", expectedKeys, TEST_HOST);
  await testRemoveAndChange("4", expectedKeys, TEST_HOST);
  // Test on a string that makes JSON.parse to throw.
  await testRemoveAndChange("non-json-parsable", expectedKeys, TEST_HOST);

  // Clearing items.
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.wrappedJSObject.clear();
  });

  await gUI.once("store-objects-cleared");

  await checkState([[["localStorage", TEST_HOST], []]]);
});

async function testRemoveAndChange(targetKey, expectedKeys, host) {
  await checkState([[["localStorage", host], expectedKeys]]);

  await removeLocalStorageItem(targetKey);
  await gUI.once("store-objects-edit");
  await checkState([
    [["localStorage", host], expectedKeys.filter(key => key !== targetKey)],
  ]);

  await setLocalStorageItem(targetKey, "again");
  await gUI.once("store-objects-edit");
  await checkState([[["localStorage", host], expectedKeys]]);

  // Updating a row set to the string "null"
  await setLocalStorageItem(targetKey, `key-${targetKey}-changed`);
  await gUI.once("store-objects-edit");
  checkCell(targetKey, "value", `key-${targetKey}-changed`);
}

async function setLocalStorageItem(key, value) {
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [[key, value]],
    ([innerKey, innerValue]) => {
      content.wrappedJSObject.localStorage.setItem(innerKey, innerValue);
    }
  );
}

async function removeLocalStorageItem(key) {
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [key], innerKey => {
    content.wrappedJSObject.localStorage.removeItem(innerKey);
  });
}