dom/datastore/tests/file_changes.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/datastore/tests/file_changes.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for DataStore - basic operation on a readonly db</title>
     1.9 +</head>
    1.10 +<body>
    1.11 +<p id="display"></p>
    1.12 +<div id="content" style="display: none">
    1.13 +
    1.14 +</div>
    1.15 +<pre id="test">
    1.16 +  <script type="application/javascript;version=1.7">
    1.17 +
    1.18 +  var gStore;
    1.19 +  var gChangeId = null;
    1.20 +  var gChangeOperation = null;
    1.21 +
    1.22 +  function is(a, b, msg) {
    1.23 +    alert((a === b ? 'OK' : 'KO') + ' ' + msg)
    1.24 +  }
    1.25 +
    1.26 +  function ok(a, msg) {
    1.27 +    alert((a ? 'OK' : 'KO')+ ' ' + msg)
    1.28 +  }
    1.29 +
    1.30 +  function cbError() {
    1.31 +    alert('KO error');
    1.32 +  }
    1.33 +
    1.34 +  function finish() {
    1.35 +    alert('DONE');
    1.36 +  }
    1.37 +
    1.38 +  function testGetDataStores() {
    1.39 +    navigator.getDataStores('foo').then(function(stores) {
    1.40 +      is(stores.length, 1, "getDataStores('foo') returns 1 element");
    1.41 +      is(stores[0].name, 'foo', 'The dataStore.name is foo');
    1.42 +      is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
    1.43 +
    1.44 +      gStore = stores[0];
    1.45 +      runTest();
    1.46 +    }, cbError);
    1.47 +  }
    1.48 +
    1.49 +  function testStoreAdd(value, expectedId) {
    1.50 +    gStore.add(value).then(function(id) {
    1.51 +      is(id, expectedId, "store.add() is called");
    1.52 +    }, cbError);
    1.53 +  }
    1.54 +
    1.55 +  function testStorePut(value, id) {
    1.56 +    gStore.put(value, id).then(function(retId) {
    1.57 +      is(id, retId, "store.put() is called with the right id");
    1.58 +    }, cbError);
    1.59 +  }
    1.60 +
    1.61 +  function testStoreRemove(id, expectedSuccess) {
    1.62 +    gStore.remove(id).then(function(success) {
    1.63 +      is(success, expectedSuccess, "store.remove() returns the right value");
    1.64 +    }, cbError);
    1.65 +  }
    1.66 +
    1.67 +  function testStoreClear() {
    1.68 +    gStore.clear().catch(cbError);
    1.69 +  }
    1.70 +
    1.71 +  function eventListener(evt) {
    1.72 +    ok(evt, "OnChangeListener is called with data");
    1.73 +    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");
    1.74 +    if (gChangeId) {
    1.75 +      is(evt.id, gChangeId, "OnChangeListener is called with the right ID: " + evt.id);
    1.76 +    }
    1.77 +    is(evt.operation, gChangeOperation, "OnChangeListener is called with the right operation:" + evt.operation + " " + gChangeOperation);
    1.78 +    runTest();
    1.79 +  }
    1.80 +
    1.81 +  var tests = [
    1.82 +    // Test for GetDataStore
    1.83 +    testGetDataStores,
    1.84 +
    1.85 +    // Add onchange = function
    1.86 +    function() {
    1.87 +      gStore.onchange = eventListener;
    1.88 +      is(gStore.onchange, eventListener, "onChange is set");
    1.89 +      runTest();
    1.90 +    },
    1.91 +
    1.92 +    // Add
    1.93 +    function() { gChangeId = 1; gChangeOperation = 'added';
    1.94 +                 testStoreAdd({ number: 42 }, 1); },
    1.95 +
    1.96 +    // Put
    1.97 +    function() { gChangeId = 1; gChangeOperation = 'updated';
    1.98 +                 testStorePut({ number: 43 }, 1); },
    1.99 +
   1.100 +    // Remove
   1.101 +    function() { gChangeId = 1; gChangeOperation = 'removed';
   1.102 +                 testStoreRemove(1, true); },
   1.103 +
   1.104 +    // Clear
   1.105 +    function() { gChangeId = 0; gChangeOperation = 'cleared';
   1.106 +                 testStoreClear(); },
   1.107 +
   1.108 +    // Remove onchange function and replace it with addEventListener
   1.109 +    function() {
   1.110 +      gStore.onchange = null;
   1.111 +      gStore.addEventListener('change', eventListener);
   1.112 +      runTest();
   1.113 +    },
   1.114 +
   1.115 +    // Add
   1.116 +    function() { gChangeId = 2; gChangeOperation = 'added';
   1.117 +                 testStoreAdd({ number: 42 }, 2); },
   1.118 +
   1.119 +    // Put
   1.120 +    function() { gChangeId = 2; gChangeOperation = 'updated';
   1.121 +                 testStorePut({ number: 43 }, 2); },
   1.122 +
   1.123 +    // Remove
   1.124 +    function() { gChangeId = 2; gChangeOperation = 'removed';
   1.125 +                 testStoreRemove(2, true); },
   1.126 +
   1.127 +    // Clear
   1.128 +    function() { gChangeId = 0; gChangeOperation = 'cleared';
   1.129 +                 testStoreClear(); },
   1.130 +
   1.131 +    // Remove event listener
   1.132 +    function() {
   1.133 +      gStore.removeEventListener('change', eventListener);
   1.134 +      runTest();
   1.135 +    },
   1.136 +  ];
   1.137 +
   1.138 +  function runTest() {
   1.139 +    if (!tests.length) {
   1.140 +      finish();
   1.141 +      return;
   1.142 +    }
   1.143 +
   1.144 +    var test = tests.shift();
   1.145 +    test();
   1.146 +  }
   1.147 +
   1.148 +  runTest();
   1.149 +  </script>
   1.150 +</pre>
   1.151 +</body>
   1.152 +</html>

mercurial