dom/datastore/tests/file_changes.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <meta charset="utf-8">
michael@0 5 <title>Test for DataStore - basic operation on a readonly db</title>
michael@0 6 </head>
michael@0 7 <body>
michael@0 8 <p id="display"></p>
michael@0 9 <div id="content" style="display: none">
michael@0 10
michael@0 11 </div>
michael@0 12 <pre id="test">
michael@0 13 <script type="application/javascript;version=1.7">
michael@0 14
michael@0 15 var gStore;
michael@0 16 var gChangeId = null;
michael@0 17 var gChangeOperation = null;
michael@0 18
michael@0 19 function is(a, b, msg) {
michael@0 20 alert((a === b ? 'OK' : 'KO') + ' ' + msg)
michael@0 21 }
michael@0 22
michael@0 23 function ok(a, msg) {
michael@0 24 alert((a ? 'OK' : 'KO')+ ' ' + msg)
michael@0 25 }
michael@0 26
michael@0 27 function cbError() {
michael@0 28 alert('KO error');
michael@0 29 }
michael@0 30
michael@0 31 function finish() {
michael@0 32 alert('DONE');
michael@0 33 }
michael@0 34
michael@0 35 function testGetDataStores() {
michael@0 36 navigator.getDataStores('foo').then(function(stores) {
michael@0 37 is(stores.length, 1, "getDataStores('foo') returns 1 element");
michael@0 38 is(stores[0].name, 'foo', 'The dataStore.name is foo');
michael@0 39 is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
michael@0 40
michael@0 41 gStore = stores[0];
michael@0 42 runTest();
michael@0 43 }, cbError);
michael@0 44 }
michael@0 45
michael@0 46 function testStoreAdd(value, expectedId) {
michael@0 47 gStore.add(value).then(function(id) {
michael@0 48 is(id, expectedId, "store.add() is called");
michael@0 49 }, cbError);
michael@0 50 }
michael@0 51
michael@0 52 function testStorePut(value, id) {
michael@0 53 gStore.put(value, id).then(function(retId) {
michael@0 54 is(id, retId, "store.put() is called with the right id");
michael@0 55 }, cbError);
michael@0 56 }
michael@0 57
michael@0 58 function testStoreRemove(id, expectedSuccess) {
michael@0 59 gStore.remove(id).then(function(success) {
michael@0 60 is(success, expectedSuccess, "store.remove() returns the right value");
michael@0 61 }, cbError);
michael@0 62 }
michael@0 63
michael@0 64 function testStoreClear() {
michael@0 65 gStore.clear().catch(cbError);
michael@0 66 }
michael@0 67
michael@0 68 function eventListener(evt) {
michael@0 69 ok(evt, "OnChangeListener is called with data");
michael@0 70 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");
michael@0 71 if (gChangeId) {
michael@0 72 is(evt.id, gChangeId, "OnChangeListener is called with the right ID: " + evt.id);
michael@0 73 }
michael@0 74 is(evt.operation, gChangeOperation, "OnChangeListener is called with the right operation:" + evt.operation + " " + gChangeOperation);
michael@0 75 runTest();
michael@0 76 }
michael@0 77
michael@0 78 var tests = [
michael@0 79 // Test for GetDataStore
michael@0 80 testGetDataStores,
michael@0 81
michael@0 82 // Add onchange = function
michael@0 83 function() {
michael@0 84 gStore.onchange = eventListener;
michael@0 85 is(gStore.onchange, eventListener, "onChange is set");
michael@0 86 runTest();
michael@0 87 },
michael@0 88
michael@0 89 // Add
michael@0 90 function() { gChangeId = 1; gChangeOperation = 'added';
michael@0 91 testStoreAdd({ number: 42 }, 1); },
michael@0 92
michael@0 93 // Put
michael@0 94 function() { gChangeId = 1; gChangeOperation = 'updated';
michael@0 95 testStorePut({ number: 43 }, 1); },
michael@0 96
michael@0 97 // Remove
michael@0 98 function() { gChangeId = 1; gChangeOperation = 'removed';
michael@0 99 testStoreRemove(1, true); },
michael@0 100
michael@0 101 // Clear
michael@0 102 function() { gChangeId = 0; gChangeOperation = 'cleared';
michael@0 103 testStoreClear(); },
michael@0 104
michael@0 105 // Remove onchange function and replace it with addEventListener
michael@0 106 function() {
michael@0 107 gStore.onchange = null;
michael@0 108 gStore.addEventListener('change', eventListener);
michael@0 109 runTest();
michael@0 110 },
michael@0 111
michael@0 112 // Add
michael@0 113 function() { gChangeId = 2; gChangeOperation = 'added';
michael@0 114 testStoreAdd({ number: 42 }, 2); },
michael@0 115
michael@0 116 // Put
michael@0 117 function() { gChangeId = 2; gChangeOperation = 'updated';
michael@0 118 testStorePut({ number: 43 }, 2); },
michael@0 119
michael@0 120 // Remove
michael@0 121 function() { gChangeId = 2; gChangeOperation = 'removed';
michael@0 122 testStoreRemove(2, true); },
michael@0 123
michael@0 124 // Clear
michael@0 125 function() { gChangeId = 0; gChangeOperation = 'cleared';
michael@0 126 testStoreClear(); },
michael@0 127
michael@0 128 // Remove event listener
michael@0 129 function() {
michael@0 130 gStore.removeEventListener('change', eventListener);
michael@0 131 runTest();
michael@0 132 },
michael@0 133 ];
michael@0 134
michael@0 135 function runTest() {
michael@0 136 if (!tests.length) {
michael@0 137 finish();
michael@0 138 return;
michael@0 139 }
michael@0 140
michael@0 141 var test = tests.shift();
michael@0 142 test();
michael@0 143 }
michael@0 144
michael@0 145 runTest();
michael@0 146 </script>
michael@0 147 </pre>
michael@0 148 </body>
michael@0 149 </html>

mercurial