tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_async_storage.js (2657B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the basic functionality of async-storage.
      7 // Adapted from https://github.com/mozilla-b2g/gaia/blob/f09993563fb5fec4393eb71816ce76cb00463190/apps/sharedtest/test/unit/async_storage_test.js.
      8 
      9 const asyncStorage = require("resource://devtools/shared/async-storage.js");
     10 add_task(async function () {
     11  is(typeof asyncStorage.length, "function", "API exists.");
     12  is(typeof asyncStorage.key, "function", "API exists.");
     13  is(typeof asyncStorage.getItem, "function", "API exists.");
     14  is(typeof asyncStorage.setItem, "function", "API exists.");
     15  is(typeof asyncStorage.removeItem, "function", "API exists.");
     16  is(typeof asyncStorage.clear, "function", "API exists.");
     17 });
     18 
     19 add_task(async function () {
     20  await asyncStorage.setItem("foo", "bar");
     21  let value = await asyncStorage.getItem("foo");
     22  is(value, "bar", "value is correct");
     23  await asyncStorage.setItem("foo", "overwritten");
     24  value = await asyncStorage.getItem("foo");
     25  is(value, "overwritten", "value is correct");
     26  await asyncStorage.removeItem("foo");
     27  value = await asyncStorage.getItem("foo");
     28  is(value, null, "value is correct");
     29 });
     30 
     31 add_task(async function () {
     32  const object = {
     33    x: 1,
     34    y: "foo",
     35    z: true,
     36  };
     37 
     38  await asyncStorage.setItem("myobj", object);
     39  let value = await asyncStorage.getItem("myobj");
     40  is(object.x, value.x, "value is correct");
     41  is(object.y, value.y, "value is correct");
     42  is(object.z, value.z, "value is correct");
     43  await asyncStorage.removeItem("myobj");
     44  value = await asyncStorage.getItem("myobj");
     45  is(value, null, "value is correct");
     46 });
     47 
     48 add_task(async function () {
     49  await asyncStorage.clear();
     50  let len = await asyncStorage.length();
     51  is(len, 0, "length is correct");
     52  await asyncStorage.setItem("key1", "value1");
     53  len = await asyncStorage.length();
     54  is(len, 1, "length is correct");
     55  await asyncStorage.setItem("key2", "value2");
     56  len = await asyncStorage.length();
     57  is(len, 2, "length is correct");
     58  await asyncStorage.setItem("key3", "value3");
     59  len = await asyncStorage.length();
     60  is(len, 3, "length is correct");
     61 
     62  let key = await asyncStorage.key(0);
     63  is(key, "key1", "key is correct");
     64  key = await asyncStorage.key(1);
     65  is(key, "key2", "key is correct");
     66  key = await asyncStorage.key(2);
     67  is(key, "key3", "key is correct");
     68  key = await asyncStorage.key(3);
     69  is(key, null, "key is correct");
     70  await asyncStorage.clear();
     71  key = await asyncStorage.key(0);
     72  is(key, null, "key is correct");
     73 
     74  len = await asyncStorage.length();
     75  is(len, 0, "length is correct");
     76 });