dom/datastore/tests/file_sync.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

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <meta charset="utf-8">
     5   <title>Test for DataStore - sync</title>
     6 </head>
     7 <body>
     8 <div id="container"></div>
     9   <script type="application/javascript;version=1.7">
    11   var gStore;
    12   var gRevisions = [];
    13   var gCursor;
    14   var gExpectedEvents = true;
    16   function is(a, b, msg) {
    17     alert((a === b ? 'OK' : 'KO') + ' ' + msg)
    18   }
    20   function ok(a, msg) {
    21     alert((a ? 'OK' : 'KO')+ ' ' + msg)
    22   }
    24   function cbError() {
    25     alert('KO error');
    26   }
    28   function finish() {
    29     alert('DONE');
    30   }
    32   function testGetDataStores() {
    33     navigator.getDataStores('foo').then(function(stores) {
    34       is(stores.length, 1, "getDataStores('foo') returns 1 element");
    36       gStore = stores[0];
    37       gRevisions.push(gStore.revisionId);
    39       gStore.onchange = function(aEvent) {
    40         ok(gExpectedEvents, "Events received!");
    41         runTest();
    42       }
    44       runTest();
    45     }, cbError);
    46   }
    48   function testBasicInterface() {
    49     var cursor = gStore.sync();
    50     ok(cursor, "Cursor is created");
    51     is(cursor.store, gStore, "Cursor.store is the store");
    53     ok("next" in cursor, "Cursor.next exists");
    54     ok("close" in cursor, "Cursor.close exists");
    56     cursor.close();
    58     runTest();
    59   }
    61   function testCursor(cursor, steps) {
    62     if (!steps.length) {
    63       runTest();
    64       return;
    65     }
    67     var step = steps.shift();
    68     cursor.next().then(function(data) {
    69       ok(!!data, "Cursor.next returns data");
    70       is(data.operation, step.operation, "Waiting for operation: '" + step.operation + "' received '" + data.operation + "'");
    73       switch (data.operation) {
    74         case 'done':
    75           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");
    76           is (data.revisionId, gRevisions[gRevisions.length-1], "Last revision matches");
    77           break;
    79         case 'add':
    80         case 'update':
    81           if ('id' in step) {
    82             is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
    83           }
    85           if ('data' in step) {
    86             is(data.data, step.data, "next() add: data matches: " + data.data + " " + step.data);
    87           }
    89           break;
    91         case 'remove':
    92           if ('id' in step) {
    93             is(data.id, step.id, "next() add: id matches: " + data.id + " " + step.id);
    94           }
    96           break;
    97       }
    99       testCursor(cursor, steps);
   100     });
   101   }
   103   var tests = [
   104     // Test for GetDataStore
   105     testGetDataStores,
   107     // interface test
   108     testBasicInterface,
   110     // empty DataStore
   111     function() {
   112       var cursor = gStore.sync();
   113       var steps = [ { operation: 'clear' },
   114                     { operation: 'done' },
   115                     { operation: 'done' }];
   116       testCursor(cursor, steps);
   117     },
   119     function() {
   120       gExpectedEvents = false;
   121       var cursor = gStore.sync('wrong revision ID');
   122       var steps = [ { operation: 'clear' },
   123                     { operation: 'done' },
   124                     { operation: 'done' }];
   125       testCursor(cursor, steps);
   126     },
   128     function() {
   129       var cursor = gStore.sync(gRevisions[0]);
   130       var steps = [ { operation: 'done' },
   131                     { operation: 'done' }];
   132       testCursor(cursor, steps);
   133     },
   135     // Test add from scratch
   136     function() {
   137       gExpectedEvents = true;
   139       gStore.add(1).then(function(id) {
   140         gRevisions.push(gStore.revisionId);
   141         ok(true, "Item: " + id + " added");
   142       });
   143     },
   145     function() {
   146       gStore.add(2,"foobar").then(function(id) {
   147         gRevisions.push(gStore.revisionId);
   148         ok(true, "Item: " + id + " added");
   149       });
   150     },
   152     function() {
   153       gStore.add(3,3).then(function(id) {
   154         gRevisions.push(gStore.revisionId);
   155         ok(true, "Item: " + id + " added");
   156       });
   157     },
   159     function() {
   160       gExpectedEvents = false;
   161       var cursor = gStore.sync();
   162       var steps = [ { operation: 'clear', },
   163                     { operation: 'add', id: 1, data: 1 },
   164                     { operation: 'add', id: 3, data: 3 },
   165                     { operation: 'add', id: 'foobar', data: 2 },
   166                     { operation: 'done' }];
   167       testCursor(cursor, steps);
   168     },
   170     function() {
   171       var cursor = gStore.sync('wrong revision ID');
   172       var steps = [ { operation: 'clear', },
   173                     { operation: 'add', id: 1, data: 1 },
   174                     { operation: 'add', id: 3, data: 3 },
   175                     { operation: 'add', id: 'foobar', data: 2 },
   176                     { operation: 'done' }];
   177       testCursor(cursor, steps);
   178     },
   180     function() {
   181       var cursor = gStore.sync(gRevisions[0]);
   182       var steps = [ { operation: 'add', id: 1, data: 1 },
   183                     { operation: 'add', id: 'foobar', data: 2 },
   184                     { operation: 'add', id: 3, data: 3 },
   185                     { operation: 'done' }];
   186       testCursor(cursor, steps);
   187     },
   189     function() {
   190       var cursor = gStore.sync(gRevisions[1]);
   191       var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   192                     { operation: 'add', id: 3, data: 3 },
   193                     { operation: 'done' }];
   194       testCursor(cursor, steps);
   195     },
   197     function() {
   198       var cursor = gStore.sync(gRevisions[2]);
   199       var steps = [ { operation: 'add', id: 3, data: 3 },
   200                     { operation: 'done' }];
   201       testCursor(cursor, steps);
   202     },
   204     function() {
   205       var cursor = gStore.sync(gRevisions[3]);
   206       var steps = [ { operation: 'done' }];
   207       testCursor(cursor, steps);
   208     },
   210     // Test after an update
   211     function() {
   212       gExpectedEvents = true;
   213       gStore.put(123, 1).then(function() {
   214         gRevisions.push(gStore.revisionId);
   215       });
   216     },
   218     function() {
   219       gExpectedEvents = false;
   220       var cursor = gStore.sync();
   221       var steps = [ { operation: 'clear', },
   222                     { operation: 'add', id: 1, data: 123 },
   223                     { operation: 'add', id: 3, data: 3 },
   224                     { operation: 'add', id: 'foobar', data: 2 },
   225                     { operation: 'done' }];
   226       testCursor(cursor, steps);
   227     },
   229     function() {
   230       var cursor = gStore.sync('wrong revision ID');
   231       var steps = [ { operation: 'clear', },
   232                     { operation: 'add', id: 1, data: 123 },
   233                     { operation: 'add', id: 3, data: 3 },
   234                     { operation: 'add', id: 'foobar', data: 2 },
   235                     { operation: 'done' }];
   236       testCursor(cursor, steps);
   237     },
   239     function() {
   240       var cursor = gStore.sync(gRevisions[0]);
   241       var steps = [ { operation: 'add', id: 1, data: 123 },
   242                     { operation: 'add', id: 'foobar', data: 2 },
   243                     { operation: 'add', id: 3, data: 3 },
   244                     { operation: 'done' }];
   245       testCursor(cursor, steps);
   246     },
   248     function() {
   249       var cursor = gStore.sync(gRevisions[1]);
   250       var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   251                     { operation: 'add', id: 3, data: 3 },
   252                     { operation: 'update', id: 1, data: 123 },
   253                     { operation: 'done' }];
   254       testCursor(cursor, steps);
   255     },
   257     function() {
   258       var cursor = gStore.sync(gRevisions[2]);
   259       var steps = [ { operation: 'add', id: 3, data: 3 },
   260                     { operation: 'update', id: 1, data: 123 },
   261                     { operation: 'done' }];
   262       testCursor(cursor, steps);
   263     },
   265     function() {
   266       var cursor = gStore.sync(gRevisions[3]);
   267       var steps = [ { operation: 'update', id: 1, data: 123 },
   268                     { operation: 'done' }];
   269       testCursor(cursor, steps);
   270     },
   272     function() {
   273       var cursor = gStore.sync(gRevisions[4]);
   274       var steps = [ { operation: 'done' }];
   275       testCursor(cursor, steps);
   276     },
   278     // Test after a remove
   279     function() {
   280       gExpectedEvents = true;
   281       gStore.remove(3).then(function() {
   282         gRevisions.push(gStore.revisionId);
   283       });
   284     },
   286     function() {
   287       gExpectedEvents = false;
   288       var cursor = gStore.sync();
   289       var steps = [ { operation: 'clear', },
   290                     { operation: 'add', id: 1, data: 123 },
   291                     { operation: 'add', id: 'foobar', data: 2 },
   292                     { operation: 'done' }];
   293       testCursor(cursor, steps);
   294     },
   296     function() {
   297       var cursor = gStore.sync('wrong revision ID');
   298       var steps = [ { operation: 'clear', },
   299                     { operation: 'add', id: 1, data: 123 },
   300                     { operation: 'add', id: 'foobar', data: 2 },
   301                     { operation: 'done' }];
   302       testCursor(cursor, steps);
   303     },
   305     function() {
   306       var cursor = gStore.sync(gRevisions[0]);
   307       var steps = [ { operation: 'add', id: 1, data: 123 },
   308                     { operation: 'add', id: 'foobar', data: 2 },
   309                     { operation: 'done' }];
   310       testCursor(cursor, steps);
   311     },
   313     function() {
   314       var cursor = gStore.sync(gRevisions[1]);
   315       var steps = [ { operation: 'add', id: 'foobar', data: 2 },
   316                     { operation: 'update', id: 1, data: 123 },
   317                     { operation: 'done' }];
   318       testCursor(cursor, steps);
   319     },
   321     function() {
   322       var cursor = gStore.sync(gRevisions[2]);
   323       var steps = [ { operation: 'update', id: 1, data: 123 },
   324                     { operation: 'done' }];
   325       testCursor(cursor, steps);
   326     },
   328     function() {
   329       var cursor = gStore.sync(gRevisions[3]);
   330       var steps = [ { operation: 'update', id: 1, data: 123 },
   331                     { operation: 'remove', id: 3 },
   332                     { operation: 'done' }];
   333       testCursor(cursor, steps);
   334     },
   336     function() {
   337       var cursor = gStore.sync(gRevisions[4]);
   338       var steps = [ { operation: 'remove', id: 3 },
   339                     { operation: 'done' }];
   340       testCursor(cursor, steps);
   341     },
   343     function() {
   344       var cursor = gStore.sync(gRevisions[5]);
   345       var steps = [ { operation: 'done' }];
   346       testCursor(cursor, steps);
   347     },
   349     // New events when the cursor is active
   350     function() {
   351       gCursor = gStore.sync();
   352       var steps = [ { operation: 'clear', },
   353                     { operation: 'add', id: 1, data: 123 },
   354                     { operation: 'add', id: 'foobar', data: 2 } ];
   355       testCursor(gCursor, steps);
   356     },
   358     function() {
   359       gStore.add(42, 2).then(function(id) {
   360         ok(true, "Item: " + id + " added");
   361         gRevisions.push(gStore.revisionId);
   362         runTest();
   363       });
   364     },
   366     function() {
   367       var steps = [ { operation: 'clear', },
   368                     { operation: 'add', id: 1, data: 123 },
   369                     { operation: 'add', id: 2, data: 42 },
   370 		    { operation: 'add', id: 'foobar', data: 2 } ]
   371       testCursor(gCursor, steps);
   372     },
   374     function() {
   375       gStore.put(43, 2).then(function(id) {
   376         gRevisions.push(gStore.revisionId);
   377         runTest();
   378       });
   379     },
   381     function() {
   382       var steps = [ { operation: 'clear', },
   383                     { operation: 'add', id: 1, data: 123 },
   384                     { operation: 'add', id: 2, data: 43 },
   385 		    { operation: 'add', id: 'foobar', data: 2 } ]
   386       testCursor(gCursor, steps);
   387     },
   389     function() {
   390       gStore.remove(2).then(function(id) {
   391         gRevisions.push(gStore.revisionId);
   392         runTest();
   393       });
   394     },
   396     function() {
   397       var steps = [ { operation: 'clear', },
   398                     { operation: 'add', id: 1, data: 123 },
   399 		    { operation: 'add', id: 'foobar', data: 2 } ]
   400       testCursor(gCursor, steps);
   401     },
   403     function() {
   404       gStore.add(42).then(function(id) {
   405         ok(true, "Item: " + id + " added");
   406         gRevisions.push(gStore.revisionId);
   407         runTest();
   408       });
   409     },
   411     function() {
   412       var steps = [ { operation: 'clear', },
   413                     { operation: 'add', id: 1, data: 123 },
   414                     { operation: 'add', id: 4, data: 42 },
   415 		    { operation: 'add', id: 'foobar', data: 2 } ]
   416       testCursor(gCursor, steps);
   417     },
   419     function() {
   420       gStore.clear().then(function() {
   421         gRevisions.push(gStore.revisionId);
   422         runTest();
   423       });
   424     },
   426     function() {
   427       var steps = [ { operation: 'clear' } ];
   428       testCursor(gCursor, steps);
   429     },
   431     function() {
   432       gStore.add(42).then(function(id) {
   433         ok(true, "Item: " + id + " added");
   434         gRevisions.push(gStore.revisionId);
   435         runTest();
   436       });
   437     },
   439     function() {
   440       var steps = [ { operation: 'clear', },
   441                     { operation: 'add', id: 5, data: 42 } ];
   442       testCursor(gCursor, steps);
   443     },
   445     function() {
   446       gStore.clear().then(function() {
   447         gRevisions.push(gStore.revisionId);
   448         runTest();
   449       });
   450     },
   452     function() {
   453       gStore.add(42).then(function(id) {
   454         ok(true, "Item: " + id + " added");
   455         gRevisions.push(gStore.revisionId);
   456         runTest();
   457       });
   458     },
   460     function() {
   461       var steps = [ { operation: 'clear' },
   462                     { operation: 'add', id: 6, data: 42 },
   463                     { operation: 'done'} ];
   464       testCursor(gCursor, steps);
   465     },
   467     function() {
   468       gExpectedEvents = true;
   469       gStore.add(42).then(function(id) {
   470       });
   471     }
   472   ];
   474   function runTest() {
   475     if (!tests.length) {
   476       finish();
   477       return;
   478     }
   480     var test = tests.shift();
   481     test();
   482   }
   484   runTest();
   485   </script>
   486 </body>
   487 </html>

mercurial