2013-10-02 21:27:15 +04:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Test for DataStore - basic operation on a readonly db</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script type="application/javascript;version=1.7">
|
|
|
|
|
|
|
|
var gStore;
|
|
|
|
var gChangeId = null;
|
|
|
|
var gChangeOperation = null;
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
gStore = stores[0];
|
|
|
|
runTest();
|
|
|
|
}, cbError);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testStoreAdd(value, expectedId) {
|
|
|
|
gStore.add(value).then(function(id) {
|
|
|
|
is(id, expectedId, "store.add() is called");
|
|
|
|
}, cbError);
|
|
|
|
}
|
|
|
|
|
2013-11-09 03:10:29 +04:00
|
|
|
function testStorePut(value, id) {
|
|
|
|
gStore.put(value, id).then(function(retId) {
|
|
|
|
is(id, retId, "store.put() is called with the right id");
|
2013-10-02 21:27:15 +04:00
|
|
|
}, cbError);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testStoreRemove(id, expectedSuccess) {
|
|
|
|
gStore.remove(id).then(function(success) {
|
|
|
|
is(success, expectedSuccess, "store.remove() returns the right value");
|
|
|
|
}, cbError);
|
|
|
|
}
|
|
|
|
|
2014-01-04 01:06:19 +04:00
|
|
|
function testStoreClear() {
|
|
|
|
gStore.clear().catch(cbError);
|
|
|
|
}
|
|
|
|
|
2013-10-02 21:27:15 +04:00
|
|
|
function eventListener(evt) {
|
2014-04-28 13:54:38 +04:00
|
|
|
ok(evt instanceof DataStoreChangeEvent, "DataStoreChangeEvent has been received");
|
2013-10-02 21:27:15 +04:00
|
|
|
ok(evt, "OnChangeListener is called with data");
|
|
|
|
is(/[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}/.test(evt.revisionId), true, "event.revisionId returns something");
|
2014-04-28 13:54:38 +04:00
|
|
|
is(evt.id, gChangeId, "OnChangeListener is called with the right ID: " + evt.id);
|
2013-10-02 21:27:15 +04:00
|
|
|
is(evt.operation, gChangeOperation, "OnChangeListener is called with the right operation:" + evt.operation + " " + gChangeOperation);
|
2013-10-02 21:27:15 +04:00
|
|
|
runTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = [
|
|
|
|
// Test for GetDataStore
|
|
|
|
testGetDataStores,
|
|
|
|
|
|
|
|
// Add onchange = function
|
|
|
|
function() {
|
|
|
|
gStore.onchange = eventListener;
|
2013-10-02 21:27:15 +04:00
|
|
|
is(gStore.onchange, eventListener, "onChange is set");
|
2013-10-02 21:27:15 +04:00
|
|
|
runTest();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Add
|
|
|
|
function() { gChangeId = 1; gChangeOperation = 'added';
|
|
|
|
testStoreAdd({ number: 42 }, 1); },
|
|
|
|
|
2013-11-09 03:10:29 +04:00
|
|
|
// Put
|
2013-10-02 21:27:15 +04:00
|
|
|
function() { gChangeId = 1; gChangeOperation = 'updated';
|
2013-11-09 03:10:29 +04:00
|
|
|
testStorePut({ number: 43 }, 1); },
|
2013-10-02 21:27:15 +04:00
|
|
|
|
|
|
|
// Remove
|
|
|
|
function() { gChangeId = 1; gChangeOperation = 'removed';
|
|
|
|
testStoreRemove(1, true); },
|
|
|
|
|
2014-01-04 01:06:19 +04:00
|
|
|
// Clear
|
2014-04-28 13:54:38 +04:00
|
|
|
function() { gChangeId = null; gChangeOperation = 'cleared';
|
2014-01-04 01:06:19 +04:00
|
|
|
testStoreClear(); },
|
|
|
|
|
2013-10-02 21:27:15 +04:00
|
|
|
// Remove onchange function and replace it with addEventListener
|
|
|
|
function() {
|
|
|
|
gStore.onchange = null;
|
|
|
|
gStore.addEventListener('change', eventListener);
|
|
|
|
runTest();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Add
|
|
|
|
function() { gChangeId = 2; gChangeOperation = 'added';
|
|
|
|
testStoreAdd({ number: 42 }, 2); },
|
|
|
|
|
2013-11-09 03:10:29 +04:00
|
|
|
// Put
|
2013-10-02 21:27:15 +04:00
|
|
|
function() { gChangeId = 2; gChangeOperation = 'updated';
|
2013-11-09 03:10:29 +04:00
|
|
|
testStorePut({ number: 43 }, 2); },
|
2013-10-02 21:27:15 +04:00
|
|
|
|
|
|
|
// Remove
|
|
|
|
function() { gChangeId = 2; gChangeOperation = 'removed';
|
|
|
|
testStoreRemove(2, true); },
|
|
|
|
|
2014-01-04 01:06:19 +04:00
|
|
|
// Clear
|
2014-04-28 13:54:38 +04:00
|
|
|
function() { gChangeId = null; gChangeOperation = 'cleared';
|
2014-01-04 01:06:19 +04:00
|
|
|
testStoreClear(); },
|
|
|
|
|
2013-10-02 21:27:15 +04:00
|
|
|
// Remove event listener
|
|
|
|
function() {
|
|
|
|
gStore.removeEventListener('change', eventListener);
|
|
|
|
runTest();
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
function runTest() {
|
|
|
|
if (!tests.length) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var test = tests.shift();
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
|
|
|
runTest();
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|