зеркало из https://github.com/mozilla/gecko-dev.git
121 строка
3.1 KiB
HTML
121 строка
3.1 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Test for DataStore - notify updates with system messages</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;
|
|
var gIsSystemMessageFired = false;
|
|
|
|
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);
|
|
}
|
|
|
|
function eventListener(evt) {
|
|
ok(evt instanceof DataStoreChangeEvent, "DataStoreChangeEvent has been received");
|
|
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");
|
|
is(evt.id, gChangeId, "OnChangeListener is called with the right ID: " + evt.id);
|
|
is(evt.operation, gChangeOperation, "OnChangeListener is called with the right operation:" + evt.operation + " " + gChangeOperation);
|
|
runTest();
|
|
}
|
|
|
|
function onDatastoreUpdateFoo(message) {
|
|
gIsSystemMessageFired = true;
|
|
ok(true, "System message 'datastore-update-foo' has been received");
|
|
runTest();
|
|
}
|
|
|
|
var tests = [
|
|
// Test for GetDataStore.
|
|
testGetDataStores,
|
|
|
|
// Add onchange = function.
|
|
function() {
|
|
gStore.onchange = eventListener;
|
|
is(gStore.onchange, eventListener, "onChange is set");
|
|
runTest();
|
|
},
|
|
|
|
// Set system message handler.
|
|
function() {
|
|
navigator.mozSetMessageHandler('datastore-update-foo', onDatastoreUpdateFoo);
|
|
runTest();
|
|
},
|
|
|
|
// Add.
|
|
function() { gChangeId = 1; gChangeOperation = 'added';
|
|
testStoreAdd({ number: 42 }, 1); },
|
|
|
|
// Remove event listener.
|
|
function() {
|
|
gStore.removeEventListener('change', eventListener);
|
|
runTest();
|
|
},
|
|
|
|
// Ensure the system message has fired and no more pending ones.
|
|
function() {
|
|
// Periodically check whether the system message has fired.
|
|
var timer = setInterval(function() {
|
|
if (gIsSystemMessageFired) {
|
|
clearInterval(timer);
|
|
ok(true, "The system message has fired");
|
|
ok(!navigator.mozHasPendingMessage('datastore-update-foo'), "No more pending system message");
|
|
finish();
|
|
}
|
|
}, 1000);
|
|
}
|
|
];
|
|
|
|
function runTest() {
|
|
if (tests.length) {
|
|
var test = tests.shift();
|
|
test();
|
|
}
|
|
}
|
|
|
|
runTest();
|
|
</script>
|
|
</pre>
|
|
</body>
|
|
</html>
|