gecko/dom/datastore/tests/file_basic.html

195 lines
5.6 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for DataStore - basic operation on a readonly db</title>
</head>
<body>
<div id="container"></div>
<script type="application/javascript;version=1.7">
var gStore;
function is(a, b, msg) {
alert((a === b ? 'OK' : 'KO') + ' ' + msg)
}
function ok(a, msg) {
alert((a ? 'OK' : 'KO')+ ' ' + msg)
}
function cbError() {
alert('KO error');
}
function finish() {
alert('DONE');
}
function testGetDataStores() {
navigator.getDataStores('foo').then(function(stores) {
is(stores.length, 1, "getDataStores('foo') returns 1 element");
is(stores[0].name, 'foo', 'The dataStore.name is foo');
is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
var store = stores[0];
ok("get" in store, "store.get exists");
ok("update" in store, "store.update exists");
ok("add" in store, "store.add exists");
ok("remove" in store, "store.remove exists");
ok("clear" in store, "store.clear exists");
gStore = stores[0];
runTest();
}, cbError);
}
function testStoreErrorGet(id) {
gStore.get(id).then(function(what) {
ok(false, "store.get(" + id + ") retrieves data");
}, function(error) {
ok(true, "store.get() failed properly because the id is non-valid");
ok(error instanceof DOMError, "error is a DOMError");
is(error.name, "SyntaxError", "Error is a syntax error");
}).then(runTest, cbError);
}
function testStoreErrorUpdate(id) {
gStore.update(id, "foo").then(function(what) {
ok(false, "store.update(" + id + ") retrieves data");
}, function(error) {
ok(true, "store.update() failed properly because the id is non-valid");
ok(error instanceof DOMError, "error is a DOMError");
is(error.name, "SyntaxError", "Error is a syntax error");
}).then(runTest, cbError);
}
function testStoreErrorRemove(id) {
gStore.remove(id).then(function(what) {
ok(false, "store.remove(" + id + ") retrieves data");
}, function(error) {
ok(true, "store.remove() failed properly because the id is non-valid");
ok(error instanceof DOMError, "error is a DOMError");
is(error.name, "SyntaxError", "Error is a syntax error");
}).then(runTest, cbError);
}
function testStoreGet(id, value) {
gStore.get(id).then(function(what) {
ok(true, "store.get() retrieves data");
is(what, value, "store.get(" + id + ") returns " + value);
}, function() {
ok(false, "store.get(" + id + ") retrieves data");
}).then(runTest, cbError);
}
function testStoreAdd(value) {
return gStore.add(value).then(function(what) {
ok(true, "store.add() is called");
ok(what > 0, "store.add() returns something");
return what;
}, cbError);
}
function testStoreUpdate(id, value) {
return gStore.update(id, value).then(function() {
ok(true, "store.update() is called");
}, cbError);
}
function testStoreGetLength(number) {
return gStore.getLength().then(function(n) {
is(number, n, "store.getLength() returns the right number");
}, cbError);
}
function testStoreRemove(id) {
return gStore.remove(id).then(function() {
ok(true, "store.remove() is called");
}, cbError);
}
function testStoreClear() {
return gStore.clear().then(function() {
ok(true, "store.clear() is called");
}, cbError);
}
var tests = [
// Test for GetDataStore
testGetDataStores,
// Broken ID
function() { testStoreErrorGet('hello world'); },
function() { testStoreErrorGet(true); },
function() { testStoreErrorGet(null); },
// Unknown ID
function() { testStoreGet(42, undefined); },
function() { testStoreGet(42, undefined); }, // twice
// Add + Get - number
function() { testStoreAdd(42).then(function(id) {
gId = id; runTest(); }, cbError); },
function() { testStoreGet(gId, 42); },
function() { testStoreGet(gId + "", 42); },
// Add + Get - boolean
function() { testStoreAdd(true).then(function(id) {
gId = id; runTest(); }, cbError); },
function() { testStoreGet(gId, true); },
// Add + Get - string
function() { testStoreAdd("hello world").then(function(id) {
gId = id; runTest(); }, cbError); },
function() { testStoreGet(gId, "hello world"); },
// Broken update
function() { testStoreErrorUpdate('hello world'); },
function() { testStoreErrorUpdate(true); },
function() { testStoreErrorUpdate(null); },
// Update + Get - string
function() { testStoreUpdate(gId, "hello world 2").then(function() {
runTest(); }, cbError); },
function() { testStoreGet(gId, "hello world 2"); },
// getLength
function() { testStoreGetLength(3).then(function() { runTest(); }, cbError); },
// Broken remove
function() { testStoreErrorRemove('hello world'); },
function() { testStoreErrorRemove(true); },
function() { testStoreErrorRemove(null); },
// Remove
function() { testStoreRemove(gId).then(function(what) {
runTest(); }, cbError); },
function() { testStoreGet(gId).catch(function() {
runTest(); }); },
// Remove - wrong ID
function() { testStoreRemove(gId).then(function(what) {
runTest(); }, cbError); },
// Clear
function() { testStoreClear().then(function(what) {
runTest(); }, cbError); },
];
function runTest() {
if (!tests.length) {
finish();
return;
}
var test = tests.shift();
test();
}
runTest();
</script>
</body>
</html>