dom/datastore/tests/file_basic.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/datastore/tests/file_basic.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     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 +<div id="container"></div>
    1.12 +  <script type="application/javascript;version=1.7">
    1.13 +
    1.14 +  var gStore;
    1.15 +
    1.16 +  function is(a, b, msg) {
    1.17 +    alert((a === b ? 'OK' : 'KO') + ' ' + msg)
    1.18 +  }
    1.19 +
    1.20 +  function ok(a, msg) {
    1.21 +    alert((a ? 'OK' : 'KO')+ ' ' + msg)
    1.22 +  }
    1.23 +
    1.24 +  function cbError() {
    1.25 +    alert('KO error');
    1.26 +  }
    1.27 +
    1.28 +  function finish() {
    1.29 +    alert('DONE');
    1.30 +  }
    1.31 +
    1.32 +  function testGetDataStores() {
    1.33 +    navigator.getDataStores('foo').then(function(stores) {
    1.34 +      is(stores.length, 1, "getDataStores('foo') returns 1 element");
    1.35 +      is(stores[0].name, 'foo', 'The dataStore.name is foo');
    1.36 +      is(stores[0].readOnly, false, 'The dataStore foo is not in readonly');
    1.37 +
    1.38 +      var store = stores[0];
    1.39 +      ok("get" in store, "store.get exists");
    1.40 +      ok("put" in store, "store.put exists");
    1.41 +      ok("add" in store, "store.add exists");
    1.42 +      ok("remove" in store, "store.remove exists");
    1.43 +      ok("clear" in store, "store.clear exists");
    1.44 +      ok("revisionId" in store, "store.revisionId exists");
    1.45 +      ok("getLength" in store, "store.getLength exists");
    1.46 +      ok("sync" in store, "store.sync exists");
    1.47 +
    1.48 +      gStore = stores[0];
    1.49 +
    1.50 +      runTest();
    1.51 +    }, cbError);
    1.52 +  }
    1.53 +
    1.54 +  function testStoreGet(id, value) {
    1.55 +    gStore.get(id).then(function(what) {
    1.56 +      ok(true, "store.get() retrieves data");
    1.57 +      is(what, value, "store.get(" + id + ") returns " + value);
    1.58 +    }, function() {
    1.59 +      ok(false, "store.get(" + id + ") retrieves data");
    1.60 +    }).then(runTest, cbError);
    1.61 +  }
    1.62 +
    1.63 +  function testStoreAdd(value) {
    1.64 +    return gStore.add(value).then(function(what) {
    1.65 +      ok(true, "store.add() is called");
    1.66 +      ok(what > 0, "store.add() returns something");
    1.67 +      return what;
    1.68 +    }, cbError);
    1.69 +  }
    1.70 +
    1.71 +  function testStorePut(value, id) {
    1.72 +    return gStore.put(value, id).then(function() {
    1.73 +      ok(true, "store.put() is called");
    1.74 +    }, cbError);
    1.75 +  }
    1.76 +
    1.77 +  function testStoreGetLength(number) {
    1.78 +    return gStore.getLength().then(function(n) {
    1.79 +      is(number, n, "store.getLength() returns the right number");
    1.80 +    }, cbError);
    1.81 +  }
    1.82 +
    1.83 +  function testStoreRemove(id) {
    1.84 +    return gStore.remove(id).then(function() {
    1.85 +      ok(true, "store.remove() is called");
    1.86 +    }, cbError);
    1.87 +  }
    1.88 +
    1.89 +  function testStoreClear() {
    1.90 +    return gStore.clear().then(function() {
    1.91 +      ok(true, "store.clear() is called");
    1.92 +    }, cbError);
    1.93 +  }
    1.94 +
    1.95 +  var tests = [
    1.96 +    // Test for GetDataStore
    1.97 +    testGetDataStores,
    1.98 +
    1.99 +    // Unknown ID
   1.100 +    function() { testStoreGet(42, undefined); },
   1.101 +    function() { testStoreGet(42, undefined); }, // twice
   1.102 +
   1.103 +    // Add + Get - number
   1.104 +    function() { testStoreAdd(42).then(function(id) {
   1.105 +                   gId = id; runTest(); }, cbError); },
   1.106 +    function() { testStoreGet(gId, 42); },
   1.107 +
   1.108 +    // Add + Get - boolean
   1.109 +    function() { testStoreAdd(true).then(function(id) {
   1.110 +                   gId = id; runTest(); }, cbError); },
   1.111 +    function() { testStoreGet(gId, true); },
   1.112 +
   1.113 +    // Add + Get - string
   1.114 +    function() { testStoreAdd("hello world").then(function(id) {
   1.115 +                   gId = id; runTest(); }, cbError); },
   1.116 +    function() { testStoreGet(gId, "hello world"); },
   1.117 +
   1.118 +    // Put + Get - string
   1.119 +    function() { testStorePut("hello world 2", gId).then(function() {
   1.120 +                   runTest(); }, cbError); },
   1.121 +    function() { testStoreGet(gId, "hello world 2"); },
   1.122 +
   1.123 +    // getLength
   1.124 +    function() { testStoreGetLength(3).then(function() { runTest(); }, cbError); },
   1.125 +
   1.126 +    // Remove
   1.127 +    function() { testStoreRemove(gId).then(function(what) {
   1.128 +                   runTest(); }, cbError); },
   1.129 +    function() { testStoreGet(gId, undefined); },
   1.130 +
   1.131 +    // Remove - wrong ID
   1.132 +    function() { testStoreRemove(gId).then(function(what) {
   1.133 +                   runTest(); }, cbError); },
   1.134 +
   1.135 +    // Clear
   1.136 +    function() { testStoreClear().then(function(what) {
   1.137 +                   runTest(); }, cbError); },
   1.138 +  ];
   1.139 +
   1.140 +  function runTest() {
   1.141 +    if (!tests.length) {
   1.142 +      finish();
   1.143 +      return;
   1.144 +    }
   1.145 +
   1.146 +    var test = tests.shift();
   1.147 +    test();
   1.148 +  }
   1.149 +
   1.150 +  runTest();
   1.151 +  </script>
   1.152 +</body>
   1.153 +</html>

mercurial