summaryrefslogtreecommitdiffstats
path: root/dom/settings/tests/test_settings_blobs.html
blob: 6d24111cf8cc4cec49de20b68065aa3cb10a384c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=821630
-->
<head>
  <title>Test for Bug 821630 Settings API</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>

<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=821630">Mozilla Bug 821630</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">

"use strict";

var url = SimpleTest.getTestFileURL("file_loadserver.js");
var script = SpecialPowers.loadChromeScript(url);

function onUnwantedSuccess() {
  ok(false, "onUnwantedSuccess: shouldn't get here");
}

function onFailure() {
  return function(s) {
    if (s) {
      ok(false, "in on Failure! - " + s);
    } else {
      ok(false, "in on Failure!");
    }
  }
}

let req;

let storedBlob = new Blob(['12345'], {"type": "text/plain"});

function checkBlob(blob) {
  try {
    let url = URL.createObjectURL(blob);
    ok(true, "Valid blob");
  } catch (e) {
    ok(false, "Valid blob");
  }
}

let steps = [
  function() {
    let lock = navigator.mozSettings.createLock();
    req = lock.clear();
    req.onsuccess = next;
    req.onerror = onFailure("Deleting database");
  },
  function() {
    function obs(e) {
      checkBlob(e.settingValue);
      navigator.mozSettings.removeObserver("test1", obs);
      next();
    }
    navigator.mozSettings.addObserver("test1", obs);
    next();
  },
  function() {
    // next is called by the observer above
    let req = navigator.mozSettings.createLock().set({"test1": storedBlob});
    req.onerror = onFailure("Saving blob");
  },
  function() {
    let req = navigator.mozSettings.createLock().get("test1");
    req.onsuccess = function(event) {
      checkBlob(event.target.result["test1"]);
      next();
    };
    req.onerror = onFailure("Getting blob");
  },
  function() {
    let req = navigator.mozSettings.createLock().set({"test2": [1, 2, storedBlob, 4]});
    req.onsuccess = next;
    req.onerror = onFailure("Saving array");
  },
  function() {
    let req = navigator.mozSettings.createLock().get("test2");
    req.onsuccess = function(event) {
      let val = event.target.result["test2"];
      ok(Array.isArray(val), "Result is an array");
      ok(val[0] == 1 && val[1] == 2 && val[3] == 4, "Primitives are preserved");
      checkBlob(val[2]);
      next();
    };
    req.onerror = onFailure("Getting array");
  },
  function() {
    let req = navigator.mozSettings.createLock().set({"test3": {foo: "bar", baz: {number: 1, arr: [storedBlob]}}});
    req.onsuccess = next();
    req.onerror = onFailure("Saving object");
  },
  function() {
    let req = navigator.mozSettings.createLock().get("test3");
    req.onsuccess = function(event) {
      let val = event.target.result["test3"];
      ok(typeof(val) == "object", "Result is an object");
      ok("foo" in val && typeof(val.foo) == "string", "String property preserved");
      ok("baz" in val && typeof(val.baz) == "object", "Object property preserved");
      let baz = val.baz;
      ok("number" in baz && baz.number == 1, "Primite inside object preserved");
      ok("arr" in baz && Array.isArray(baz.arr), "Array inside object is preserved");
      checkBlob(baz.arr[0]);
      next();
    };
    req.onerror = onFailure("Getting object");
  },
  function() {
    let req = navigator.mozSettings.createLock().clear();
    req.onsuccess = function() {
      next();
    };
    req.onerror = onFailure("Deleting database");
  },
  function () {
    ok(true, "all done!\n");
    SimpleTest.finish();
  }
];

function next() {
  try {
    let step = steps.shift();
    if (step) {
      step();
    }
  } catch(ex) {
    ok(false, "Caught exception", ex);
  }
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(next);
</script>
</pre>
</body>
</html>