summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/test_sandbox.html
blob: 0a15a53287862a03db2227e145afc4dde60e2dee (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
<!doctype html>
<html>
<head>
  <title>indexedDB in JS Sandbox</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link>
</head>
<body>
<script type="application/javascript">

SimpleTest.waitForExplicitFinish();

// This runs inside a same-origin sandbox.
// The intent being to show that the data store is the same.
function storeValue() {
  function createDB_inner() {
    var op = indexedDB.open("db");
    op.onupgradeneeded = e => {
      var db = e.target.result;
      db.createObjectStore("store");
    };
    return new Promise(resolve => {
      op.onsuccess = e => resolve(e.target.result);
    });
  }

  function add(k, v) {
    return createDB_inner().then(db => {
      var tx = db.transaction("store", "readwrite");
      var store = tx.objectStore("store");
      var op = store.add(v, k);
      return new Promise((resolve, reject) => {
        op.onsuccess = e => resolve(e.target.result);
        op.onerror = _ => reject(op.error);
        tx.onabort = _ => reject(tx.error);
      });
    });
  }

  return add("x", [ 10, {} ])
    .then(_ => step_done(),
          _ => ok(false, "failed to store"));
}

function createDB_outer() {
  var op = indexedDB.open("db");
  op.onupgradeneeded = e => {
    ok(false, "upgrade should not be needed");
    var db = e.target.result;
    db.createObjectStore("store");
  };
  return new Promise(resolve => {
    op.onsuccess = e => resolve(e.target.result);
  });
}

function get(k) {
  return createDB_outer().then(db => {
      var tx = db.transaction("store", "readonly");
      var store = tx.objectStore("store");
      var op = store.get(k);
      return new Promise((resolve, reject) => {
        op.onsuccess = e => resolve(e.target.result);
        op.onerror = _ => reject(op.error);
        tx.onabort = _ => reject(tx.error);
      });
  });
}

function runInSandbox(sandbox, testFunc) {
  is(typeof testFunc, "function");
  var resolvePromise;
  // Step-done is defined in the sandbox and used in the add() function above.
  /* global step_done */
  var testPromise = new Promise(r => resolvePromise = r);
  SpecialPowers.Cu.exportFunction(_ => resolvePromise(), sandbox,
                                  { defineAs: "step_done" });
  SpecialPowers.Cu.evalInSandbox("(" + testFunc.toString() + ")()" +
                                 ".then(step_done);", sandbox);
  return testPromise;
}

// Use the window principal for the sandbox; location.origin is not sufficient.
var sb = new SpecialPowers.Cu.Sandbox(window,
                                      { wantGlobalProperties: ["indexedDB"] });

sb.ok = SpecialPowers.Cu.exportFunction(ok, sb);

Promise.resolve()
  .then(_ => runInSandbox(sb, storeValue))
  .then(_ => get("x"))
  .then(x => {
    ok(x, "a value should be present");
    is(x.length, 2);
    is(x[0], 10);
    is(typeof x[1], "object");
    is(Object.keys(x[1]).length, 0);
  })
  .then(_ => SimpleTest.finish());

</script>
</body>
</html>