dom/datastore/tests/file_sync.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/datastore/tests/file_sync.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,487 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for DataStore - sync</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 +  var gRevisions = [];
    1.16 +  var gCursor;
    1.17 +  var gExpectedEvents = true;
    1.18 +
    1.19 +  function is(a, b, msg) {
    1.20 +    alert((a === b ? 'OK' : 'KO') + ' ' + msg)
    1.21 +  }
    1.22 +
    1.23 +  function ok(a, msg) {
    1.24 +    alert((a ? 'OK' : 'KO')+ ' ' + msg)
    1.25 +  }
    1.26 +
    1.27 +  function cbError() {
    1.28 +    alert('KO error');
    1.29 +  }
    1.30 +
    1.31 +  function finish() {
    1.32 +    alert('DONE');
    1.33 +  }
    1.34 +
    1.35 +  function testGetDataStores() {
    1.36 +    navigator.getDataStores('foo').then(function(stores) {
    1.37 +      is(stores.length, 1, "getDataStores('foo') returns 1 element");
    1.38 +
    1.39 +      gStore = stores[0];
    1.40 +      gRevisions.push(gStore.revisionId);
    1.41 +
    1.42 +      gStore.onchange = function(aEvent) {
    1.43 +        ok(gExpectedEvents, "Events received!");
    1.44 +        runTest();
    1.45 +      }
    1.46 +
    1.47 +      runTest();
    1.48 +    }, cbError);
    1.49 +  }
    1.50 +
    1.51 +  function testBasicInterface() {
    1.52 +    var cursor = gStore.sync();
    1.53 +    ok(cursor, "Cursor is created");
    1.54 +    is(cursor.store, gStore, "Cursor.store is the store");
    1.55 +
    1.56 +    ok("next" in cursor, "Cursor.next exists");
    1.57 +    ok("close" in cursor, "Cursor.close exists");
    1.58 +
    1.59 +    cursor.close();
    1.60 +
    1.61 +    runTest();
    1.62 +  }
    1.63 +
    1.64 +  function testCursor(cursor, steps) {
    1.65 +    if (!steps.length) {
    1.66 +      runTest();
    1.67 +      return;
    1.68 +    }
    1.69 +
    1.70 +    var step = steps.shift();
    1.71 +    cursor.next().then(function(data) {
    1.72 +      ok(!!data, "Cursor.next returns data");
    1.73 +      is(data.operation, step.operation, "Waiting for operation: '" + step.operation + "' received '" + data.operation + "'");
    1.74 +
    1.75 +
    1.76 +      switch (data.operation) {
    1.77 +        case 'done':
    1.78 +          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(data.revisionId), true, "done has a valid revisionId");
    1.79 +          is (data.revisionId, gRevisions[gRevisions.length-1], "Last revision matches");
    1.80 +          break;
    1.81 +
    1.82 +        case 'add':
    1.83 +        case 'update':
    1.84 +          if ('id' in step) {
    1.85 +            is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
    1.86 +          }
    1.87 +
    1.88 +          if ('data' in step) {
    1.89 +            is(data.data, step.data, "next() add: data matches: " + data.data + " " + step.data);
    1.90 +          }
    1.91 +
    1.92 +          break;
    1.93 +
    1.94 +        case 'remove':
    1.95 +          if ('id' in step) {
    1.96 +            is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
    1.97 +          }
    1.98 +
    1.99 +          break;
   1.100 +      }
   1.101 +
   1.102 +      testCursor(cursor, steps);
   1.103 +    });
   1.104 +  }
   1.105 +
   1.106 +  var tests = [
   1.107 +    // Test for GetDataStore
   1.108 +    testGetDataStores,
   1.109 +
   1.110 +    // interface test
   1.111 +    testBasicInterface,
   1.112 +
   1.113 +    // empty DataStore
   1.114 +    function() {
   1.115 +      var cursor = gStore.sync();
   1.116 +      var steps = [ { operation: 'clear' },
   1.117 +                    { operation: 'done' },
   1.118 +                    { operation: 'done' }];
   1.119 +      testCursor(cursor, steps);
   1.120 +    },
   1.121 +
   1.122 +    function() {
   1.123 +      gExpectedEvents = false;
   1.124 +      var cursor = gStore.sync('wrong revision ID');
   1.125 +      var steps = [ { operation: 'clear' },
   1.126 +                    { operation: 'done' },
   1.127 +                    { operation: 'done' }];
   1.128 +      testCursor(cursor, steps);
   1.129 +    },
   1.130 +
   1.131 +    function() {
   1.132 +      var cursor = gStore.sync(gRevisions[0]);
   1.133 +      var steps = [ { operation: 'done' },
   1.134 +                    { operation: 'done' }];
   1.135 +      testCursor(cursor, steps);
   1.136 +    },
   1.137 +
   1.138 +    // Test add from scratch
   1.139 +    function() {
   1.140 +      gExpectedEvents = true;
   1.141 +
   1.142 +      gStore.add(1).then(function(id) {
   1.143 +        gRevisions.push(gStore.revisionId);
   1.144 +        ok(true, "Item: " + id + " added");
   1.145 +      });
   1.146 +    },
   1.147 +
   1.148 +    function() {
   1.149 +      gStore.add(2,"foobar").then(function(id) {
   1.150 +        gRevisions.push(gStore.revisionId);
   1.151 +        ok(true, "Item: " + id + " added");
   1.152 +      });
   1.153 +    },
   1.154 +
   1.155 +    function() {
   1.156 +      gStore.add(3,3).then(function(id) {
   1.157 +        gRevisions.push(gStore.revisionId);
   1.158 +        ok(true, "Item: " + id + " added");
   1.159 +      });
   1.160 +    },
   1.161 +
   1.162 +    function() {
   1.163 +      gExpectedEvents = false;
   1.164 +      var cursor = gStore.sync();
   1.165 +      var steps = [ { operation: 'clear', },
   1.166 +                    { operation: 'add', id: 1, data: 1 },
   1.167 +                    { operation: 'add', id: 3, data: 3 },
   1.168 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.169 +                    { operation: 'done' }];
   1.170 +      testCursor(cursor, steps);
   1.171 +    },
   1.172 +
   1.173 +    function() {
   1.174 +      var cursor = gStore.sync('wrong revision ID');
   1.175 +      var steps = [ { operation: 'clear', },
   1.176 +                    { operation: 'add', id: 1, data: 1 },
   1.177 +                    { operation: 'add', id: 3, data: 3 },
   1.178 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.179 +                    { operation: 'done' }];
   1.180 +      testCursor(cursor, steps);
   1.181 +    },
   1.182 +
   1.183 +    function() {
   1.184 +      var cursor = gStore.sync(gRevisions[0]);
   1.185 +      var steps = [ { operation: 'add', id: 1, data: 1 },
   1.186 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.187 +                    { operation: 'add', id: 3, data: 3 },
   1.188 +                    { operation: 'done' }];
   1.189 +      testCursor(cursor, steps);
   1.190 +    },
   1.191 +
   1.192 +    function() {
   1.193 +      var cursor = gStore.sync(gRevisions[1]);
   1.194 +      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   1.195 +                    { operation: 'add', id: 3, data: 3 },
   1.196 +                    { operation: 'done' }];
   1.197 +      testCursor(cursor, steps);
   1.198 +    },
   1.199 +
   1.200 +    function() {
   1.201 +      var cursor = gStore.sync(gRevisions[2]);
   1.202 +      var steps = [ { operation: 'add', id: 3, data: 3 },
   1.203 +                    { operation: 'done' }];
   1.204 +      testCursor(cursor, steps);
   1.205 +    },
   1.206 +
   1.207 +    function() {
   1.208 +      var cursor = gStore.sync(gRevisions[3]);
   1.209 +      var steps = [ { operation: 'done' }];
   1.210 +      testCursor(cursor, steps);
   1.211 +    },
   1.212 +
   1.213 +    // Test after an update
   1.214 +    function() {
   1.215 +      gExpectedEvents = true;
   1.216 +      gStore.put(123, 1).then(function() {
   1.217 +        gRevisions.push(gStore.revisionId);
   1.218 +      });
   1.219 +    },
   1.220 +
   1.221 +    function() {
   1.222 +      gExpectedEvents = false;
   1.223 +      var cursor = gStore.sync();
   1.224 +      var steps = [ { operation: 'clear', },
   1.225 +                    { operation: 'add', id: 1, data: 123 },
   1.226 +                    { operation: 'add', id: 3, data: 3 },
   1.227 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.228 +                    { operation: 'done' }];
   1.229 +      testCursor(cursor, steps);
   1.230 +    },
   1.231 +
   1.232 +    function() {
   1.233 +      var cursor = gStore.sync('wrong revision ID');
   1.234 +      var steps = [ { operation: 'clear', },
   1.235 +                    { operation: 'add', id: 1, data: 123 },
   1.236 +                    { operation: 'add', id: 3, data: 3 },
   1.237 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.238 +                    { operation: 'done' }];
   1.239 +      testCursor(cursor, steps);
   1.240 +    },
   1.241 +
   1.242 +    function() {
   1.243 +      var cursor = gStore.sync(gRevisions[0]);
   1.244 +      var steps = [ { operation: 'add', id: 1, data: 123 },
   1.245 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.246 +                    { operation: 'add', id: 3, data: 3 },
   1.247 +                    { operation: 'done' }];
   1.248 +      testCursor(cursor, steps);
   1.249 +    },
   1.250 +
   1.251 +    function() {
   1.252 +      var cursor = gStore.sync(gRevisions[1]);
   1.253 +      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   1.254 +                    { operation: 'add', id: 3, data: 3 },
   1.255 +                    { operation: 'update', id: 1, data: 123 },
   1.256 +                    { operation: 'done' }];
   1.257 +      testCursor(cursor, steps);
   1.258 +    },
   1.259 +
   1.260 +    function() {
   1.261 +      var cursor = gStore.sync(gRevisions[2]);
   1.262 +      var steps = [ { operation: 'add', id: 3, data: 3 },
   1.263 +                    { operation: 'update', id: 1, data: 123 },
   1.264 +                    { operation: 'done' }];
   1.265 +      testCursor(cursor, steps);
   1.266 +    },
   1.267 +
   1.268 +    function() {
   1.269 +      var cursor = gStore.sync(gRevisions[3]);
   1.270 +      var steps = [ { operation: 'update', id: 1, data: 123 },
   1.271 +                    { operation: 'done' }];
   1.272 +      testCursor(cursor, steps);
   1.273 +    },
   1.274 +
   1.275 +    function() {
   1.276 +      var cursor = gStore.sync(gRevisions[4]);
   1.277 +      var steps = [ { operation: 'done' }];
   1.278 +      testCursor(cursor, steps);
   1.279 +    },
   1.280 +
   1.281 +    // Test after a remove
   1.282 +    function() {
   1.283 +      gExpectedEvents = true;
   1.284 +      gStore.remove(3).then(function() {
   1.285 +        gRevisions.push(gStore.revisionId);
   1.286 +      });
   1.287 +    },
   1.288 +
   1.289 +    function() {
   1.290 +      gExpectedEvents = false;
   1.291 +      var cursor = gStore.sync();
   1.292 +      var steps = [ { operation: 'clear', },
   1.293 +                    { operation: 'add', id: 1, data: 123 },
   1.294 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.295 +                    { operation: 'done' }];
   1.296 +      testCursor(cursor, steps);
   1.297 +    },
   1.298 +
   1.299 +    function() {
   1.300 +      var cursor = gStore.sync('wrong revision ID');
   1.301 +      var steps = [ { operation: 'clear', },
   1.302 +                    { operation: 'add', id: 1, data: 123 },
   1.303 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.304 +                    { operation: 'done' }];
   1.305 +      testCursor(cursor, steps);
   1.306 +    },
   1.307 +
   1.308 +    function() {
   1.309 +      var cursor = gStore.sync(gRevisions[0]);
   1.310 +      var steps = [ { operation: 'add', id: 1, data: 123 },
   1.311 +                    { operation: 'add', id: 'foobar', data: 2 },
   1.312 +                    { operation: 'done' }];
   1.313 +      testCursor(cursor, steps);
   1.314 +    },
   1.315 +
   1.316 +    function() {
   1.317 +      var cursor = gStore.sync(gRevisions[1]);
   1.318 +      var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   1.319 +                    { operation: 'update', id: 1, data: 123 },
   1.320 +                    { operation: 'done' }];
   1.321 +      testCursor(cursor, steps);
   1.322 +    },
   1.323 +
   1.324 +    function() {
   1.325 +      var cursor = gStore.sync(gRevisions[2]);
   1.326 +      var steps = [ { operation: 'update', id: 1, data: 123 },
   1.327 +                    { operation: 'done' }];
   1.328 +      testCursor(cursor, steps);
   1.329 +    },
   1.330 +
   1.331 +    function() {
   1.332 +      var cursor = gStore.sync(gRevisions[3]);
   1.333 +      var steps = [ { operation: 'update', id: 1, data: 123 },
   1.334 +                    { operation: 'remove', id: 3 },
   1.335 +                    { operation: 'done' }];
   1.336 +      testCursor(cursor, steps);
   1.337 +    },
   1.338 +
   1.339 +    function() {
   1.340 +      var cursor = gStore.sync(gRevisions[4]);
   1.341 +      var steps = [ { operation: 'remove', id: 3 },
   1.342 +                    { operation: 'done' }];
   1.343 +      testCursor(cursor, steps);
   1.344 +    },
   1.345 +
   1.346 +    function() {
   1.347 +      var cursor = gStore.sync(gRevisions[5]);
   1.348 +      var steps = [ { operation: 'done' }];
   1.349 +      testCursor(cursor, steps);
   1.350 +    },
   1.351 +
   1.352 +    // New events when the cursor is active
   1.353 +    function() {
   1.354 +      gCursor = gStore.sync();
   1.355 +      var steps = [ { operation: 'clear', },
   1.356 +                    { operation: 'add', id: 1, data: 123 },
   1.357 +                    { operation: 'add', id: 'foobar', data: 2 } ];
   1.358 +      testCursor(gCursor, steps);
   1.359 +    },
   1.360 +
   1.361 +    function() {
   1.362 +      gStore.add(42, 2).then(function(id) {
   1.363 +        ok(true, "Item: " + id + " added");
   1.364 +        gRevisions.push(gStore.revisionId);
   1.365 +        runTest();
   1.366 +      });
   1.367 +    },
   1.368 +
   1.369 +    function() {
   1.370 +      var steps = [ { operation: 'clear', },
   1.371 +                    { operation: 'add', id: 1, data: 123 },
   1.372 +                    { operation: 'add', id: 2, data: 42 },
   1.373 +		    { operation: 'add', id: 'foobar', data: 2 } ]
   1.374 +      testCursor(gCursor, steps);
   1.375 +    },
   1.376 +
   1.377 +    function() {
   1.378 +      gStore.put(43, 2).then(function(id) {
   1.379 +        gRevisions.push(gStore.revisionId);
   1.380 +        runTest();
   1.381 +      });
   1.382 +    },
   1.383 +
   1.384 +    function() {
   1.385 +      var steps = [ { operation: 'clear', },
   1.386 +                    { operation: 'add', id: 1, data: 123 },
   1.387 +                    { operation: 'add', id: 2, data: 43 },
   1.388 +		    { operation: 'add', id: 'foobar', data: 2 } ]
   1.389 +      testCursor(gCursor, steps);
   1.390 +    },
   1.391 +
   1.392 +    function() {
   1.393 +      gStore.remove(2).then(function(id) {
   1.394 +        gRevisions.push(gStore.revisionId);
   1.395 +        runTest();
   1.396 +      });
   1.397 +    },
   1.398 +
   1.399 +    function() {
   1.400 +      var steps = [ { operation: 'clear', },
   1.401 +                    { operation: 'add', id: 1, data: 123 },
   1.402 +		    { operation: 'add', id: 'foobar', data: 2 } ]
   1.403 +      testCursor(gCursor, steps);
   1.404 +    },
   1.405 +
   1.406 +    function() {
   1.407 +      gStore.add(42).then(function(id) {
   1.408 +        ok(true, "Item: " + id + " added");
   1.409 +        gRevisions.push(gStore.revisionId);
   1.410 +        runTest();
   1.411 +      });
   1.412 +    },
   1.413 +
   1.414 +    function() {
   1.415 +      var steps = [ { operation: 'clear', },
   1.416 +                    { operation: 'add', id: 1, data: 123 },
   1.417 +                    { operation: 'add', id: 4, data: 42 },
   1.418 +		    { operation: 'add', id: 'foobar', data: 2 } ]
   1.419 +      testCursor(gCursor, steps);
   1.420 +    },
   1.421 +
   1.422 +    function() {
   1.423 +      gStore.clear().then(function() {
   1.424 +        gRevisions.push(gStore.revisionId);
   1.425 +        runTest();
   1.426 +      });
   1.427 +    },
   1.428 +
   1.429 +    function() {
   1.430 +      var steps = [ { operation: 'clear' } ];
   1.431 +      testCursor(gCursor, steps);
   1.432 +    },
   1.433 +
   1.434 +    function() {
   1.435 +      gStore.add(42).then(function(id) {
   1.436 +        ok(true, "Item: " + id + " added");
   1.437 +        gRevisions.push(gStore.revisionId);
   1.438 +        runTest();
   1.439 +      });
   1.440 +    },
   1.441 +
   1.442 +    function() {
   1.443 +      var steps = [ { operation: 'clear', },
   1.444 +                    { operation: 'add', id: 5, data: 42 } ];
   1.445 +      testCursor(gCursor, steps);
   1.446 +    },
   1.447 +
   1.448 +    function() {
   1.449 +      gStore.clear().then(function() {
   1.450 +        gRevisions.push(gStore.revisionId);
   1.451 +        runTest();
   1.452 +      });
   1.453 +    },
   1.454 +
   1.455 +    function() {
   1.456 +      gStore.add(42).then(function(id) {
   1.457 +        ok(true, "Item: " + id + " added");
   1.458 +        gRevisions.push(gStore.revisionId);
   1.459 +        runTest();
   1.460 +      });
   1.461 +    },
   1.462 +
   1.463 +    function() {
   1.464 +      var steps = [ { operation: 'clear' },
   1.465 +                    { operation: 'add', id: 6, data: 42 },
   1.466 +                    { operation: 'done'} ];
   1.467 +      testCursor(gCursor, steps);
   1.468 +    },
   1.469 +
   1.470 +    function() {
   1.471 +      gExpectedEvents = true;
   1.472 +      gStore.add(42).then(function(id) {
   1.473 +      });
   1.474 +    }
   1.475 +  ];
   1.476 +
   1.477 +  function runTest() {
   1.478 +    if (!tests.length) {
   1.479 +      finish();
   1.480 +      return;
   1.481 +    }
   1.482 +
   1.483 +    var test = tests.shift();
   1.484 +    test();
   1.485 +  }
   1.486 +
   1.487 +  runTest();
   1.488 +  </script>
   1.489 +</body>
   1.490 +</html>

mercurial