Bug 1177688, part 4 - Add DeviceStorage tests for the new .getFilesAndDirectories() and .path API on Directory. r=baku

This commit is contained in:
Jonathan Watt 2015-06-23 00:31:36 +01:00
parent c8a606b5b5
commit abff17a4db
4 changed files with 149 additions and 50 deletions

View File

@ -77,3 +77,44 @@ function reportErrorAndQuit(e) {
ok(false, "handleError was called : " + e.target.error.name);
devicestorage_cleanup();
}
function createTestFiles(storage, paths) {
function createTestFile(path) {
return new Promise(function(resolve, reject) {
function addNamed() {
var req = storage.addNamed(createRandomBlob("image/png"), path);
req.onsuccess = function() {
ok(true, path + " was created.");
resolve();
};
req.onerror = function(e) {
ok(false, "Failed to create " + path + ': ' + e.target.error.name);
reject();
};
}
// Bug 980136. Check if the file exists before we create.
var req = storage.get(path);
req.onsuccess = function() {
ok(true, path + " exists. Do not need to create.");
resolve();
};
req.onerror = function(e) {
ok(true, path + " does not exists: " + e.target.error.name);
addNamed();
};
});
}
var arr = [];
paths.forEach(function(path) {
arr.push(createTestFile(path));
});
return Promise.all(arr);
}

View File

@ -31,6 +31,7 @@ skip-if = (toolkit != 'gonk')
[test_fs_basic.html]
[test_fs_createDirectory.html]
[test_fs_get.html]
[test_fs_getFilesAndDirectories.html]
[test_fs_remove.html]
[test_fs_createFile.html]
[test_fs_appendFile.html]

View File

@ -0,0 +1,97 @@
<!--
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=XXX
-->
<head>
<title>Test Directory#getFilesAndDirectories 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=XXX">Mozilla Bug XXX</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
devicestorage_setup();
SimpleTest.requestCompleteLog();
// The root directory object.
var gRoot = null;
function checkContents1(contents) {
var expected = {
"sub2": "/sub",
"sub3": "/sub",
"a.png": "/sub",
"b.png": "/sub",
};
is(contents.length, Object.keys(expected).length,
"The sub-directory should contain four children");
var sub2;
for (var child of contents) {
if (child.name in expected) {
ok(true, "Found '" + child.name + "' in /sub");
if (child.name == "sub2") {
sub2 = child;
}
} else {
ok(false, "Did not expect '" + child.name + "' in /sub");
}
delete expected[child.name];
}
// 'expected' should now be "empty"
for (var missing in Object.keys(expected)) {
ok(false, "Expected '" + missing.name + "' in /sub");
}
sub2.getFilesAndDirectories().then(checkContents2, handleError);
}
function checkContents2(contents) {
is(contents[0].name, "c.png", "'sub2' should contain 'c.png'");
devicestorage_cleanup();
}
function handleError(e) {
ok(false, "Should not arrive at handleError! Error: " + e.name);
devicestorage_cleanup();
}
var gStorage = navigator.getDeviceStorage("pictures");
ok(gStorage, "Should have gotten a storage.");
function runTests() {
gStorage.getRoot().then(function(rootDir) {
gRoot = rootDir;
return rootDir.get("sub");
}).then(function(subDir) {
return subDir.getFilesAndDirectories();
}).then(checkContents1).catch(handleError);
}
createTestFiles(gStorage, ["sub/a.png", "sub/b.png", "sub/sub2/c.png", "sub/sub3/d.png"]).then(function() {
runTests();
}, function() {
ok(false, "Failed to created test files.");
devicestorage_cleanup();
});
</script>
</pre>
</body>
</html>

View File

@ -88,55 +88,9 @@ let gTestCases = [
}
];
function createTestFiles(storage, callback) {
function createTestFile(path) {
return new Promise(function(resolve, reject) {
function addNamed() {
var req = storage.addNamed(createRandomBlob("image/png"), path);
req.onsuccess = function() {
ok(true, path + " was created.");
resolve();
};
req.onerror = function(e) {
ok(false, "Failed to create " + path + ': ' + e.target.error.name);
reject();
};
}
// Bug 980136. Check if the file exists before we create.
var req = storage.get(path);
req.onsuccess = function() {
ok(true, path + " exists. Do not need to create.");
resolve();
};
req.onerror = function(e) {
ok(true, path + " does not exists: " + e.target.error.name);
addNamed();
};
});
}
let arr = [];
["sub1/sub2/a.png", "sub/b.png"].forEach(function(path) {
arr.push(createTestFile(path));
});
Promise.all(arr).then(function() {
callback();
}, function() {
ok(false, "Failed to created test files.");
devicestorage_cleanup();
});
}
function runTest() {
function runNextTests() {
gTestCount = 0;
createTestFiles(gStorage, function() {
function runTests() {
function cbError(e) {
ok(false, "Should not arrive at cbError! Error: " + e.name);
devicestorage_cleanup();
@ -165,6 +119,12 @@ function runTest() {
devicestorage_cleanup();
});
}, cbError);
};
createTestFiles(gStorage, ["sub1/sub2/a.png", "sub/b.png"]).then(function() {
runTests();
}, function() {
ok(false, "Failed to created test files.");
devicestorage_cleanup();
});
}
@ -192,7 +152,7 @@ function testNextRemove() {
if (gRemoveDeep) {
// Test "remove" after "removeDeep".
gRemoveDeep = false;
runTest();
runNextTests();
return;
}
@ -206,7 +166,7 @@ ok(gStorage, "Should have gotten a storage.");
// Test "removeDeep" first.
gRemoveDeep = true;
runTest();
runNextTests();
</script>
</pre>