gecko/dom/devicestorage/test/test_fs_createFile.html

133 lines
3.3 KiB
HTML

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html> <!--
https://bugzilla.mozilla.org/show_bug.cgi?id=910412
-->
<head>
<title>Test createDirectory of the FileSystem API for device storage</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="devicestorage_common.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=910412">Mozilla Bug 910412</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
devicestorage_setup();
let gTestCount = 0;
let gFileReader = new FileReader();
let gRoot;
function str2array(str) {
let strlen = str.length;
let buf = new ArrayBuffer(strlen);
let bufView = new Uint8Array(buf);
for (let i=0; i < strlen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function array2str(data) {
return String.fromCharCode.apply(String, new Uint8Array(data));
}
let gTestCases = [
// Create with string data.
{
text: "My name is Yuan.",
get data() { return this.text; },
shouldPass: true,
mode: "replace"
},
// Create with array buffer data.
{
text: "I'm from Kunming.",
get data() { return str2array(this.text); },
shouldPass: true,
mode: "replace"
},
// Create with array buffer view data.
{
text: "Kunming is in Yunnan province of China.",
get data() { return Uint8Array(str2array(this.text)); },
shouldPass: true,
mode: "replace"
},
// Create with blob data.
{
text: "Kunming is in Yunnan province of China.",
get data() { return new Blob([this.text], {type: 'image/png'}); },
shouldPass: true,
mode: "replace"
},
// Don't overwrite existing file.
{
data: null,
shouldPass: false,
mode: "fail"
}
];
function next() {
if (gTestCount >= gTestCases.length) {
devicestorage_cleanup();
return;
}
let c = gTestCases[gTestCount++];
gRoot.createFile("text.png", {
data: c.data,
ifExists: c.mode
}).then(function(file) {
is(c.shouldPass, true, "[" + gTestCount + "] Success callback was called for createFile.");
if (!c.shouldPass) {
SimpleTest.executeSoon(next);
return;
}
// Check the file content.
gFileReader.readAsArrayBuffer(file);
gFileReader.onload = function(e) {
ab = e.target.result;
is(array2str(e.target.result), c.text, "[" + gTestCount + "] Wrong values.");
SimpleTest.executeSoon(next);
};
}, function(e) {
is(c.shouldPass, false, "[" + gTestCount + "] Error callback was called for createFile.");
SimpleTest.executeSoon(next);
});
}
ok(navigator.getDeviceStorage, "Should have getDeviceStorage.");
let storage = navigator.getDeviceStorage("pictures");
ok(storage, "Should have gotten a storage.");
// Get the root directory
storage.getRoot().then(function(dir) {
ok(dir, "Should have gotten the root directory.");
gRoot = dir;
next();
}, function(e) {
ok(false, e.name + " error should not arrive here!");
devicestorage_cleanup();
});
</script>
</pre>
</body>
</html>