gecko-dev/dom/settings/tests/test_settings_blobs.html

154 строки
4.0 KiB
HTML

<!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="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/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";
if (SpecialPowers.isMainProcess()) {
SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm");
}
SpecialPowers.addPermission("settings-read", true, document);
SpecialPowers.addPermission("settings-write", true, document);
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 mozSettings = window.navigator.mozSettings;
let req;
let storedBlob = new Blob([""], {"type": "text/xml"});
function checkBlob(blob) {
try {
let url = URL.createObjectURL(blob);
ok(true, "Valid blob");
} catch (e) {
ok(false, "Valid blob");
}
}
let steps = [
function() {
let lock = mozSettings.createLock();
req = lock.clear();
req.onsuccess = next;
req.onerror = onFailure("Deleting database");
},
function() {
function obs(e) {
checkBlob(e.settingValue);
mozSettings.removeObserver("test1", obs);
next();
}
mozSettings.addObserver("test1", obs);
next();
},
function() {
// next is called by the observer above
let req = mozSettings.createLock().set({"test1": storedBlob});
req.onerror = onFailure("Saving blob");
},
function() {
let req = mozSettings.createLock().get("test1");
req.onsuccess = function(event) {
checkBlob(event.target.result["test1"]);
next();
};
req.onerror = onFailure("Getting blob");
},
function() {
let req = mozSettings.createLock().set({"test2": [1, 2, storedBlob, 4]});
req.onsuccess = next;
req.onerror = onFailure("Saving array");
},
function() {
let req = 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 = mozSettings.createLock().set({"test3": {foo: "bar", baz: {number: 1, arr: [storedBlob]}}});
req.onsuccess = next();
req.onerror = onFailure("Saving object");
},
function() {
let req = 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 = 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>