dom/network/tests/unit_stats/test_networkstats_db.js

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 /* Any: copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
     6 Cu.import("resource://gre/modules/NetworkStatsDB.jsm");
     8 const netStatsDb = new NetworkStatsDB();
    10 function clearStore(store, callback) {
    11   netStatsDb.dbNewTxn(store, "readwrite", function(aTxn, aStore) {
    12     aStore.openCursor().onsuccess = function (event) {
    13       let cursor = event.target.result;
    14       if (cursor){
    15         cursor.delete();
    16         cursor.continue();
    17       }
    18     };
    19   }, callback);
    20 }
    22 function getNetworkId(aIccId, aNetworkType) {
    23   return aIccId + '' + aNetworkType;
    24 }
    26 add_test(function prepareDatabase() {
    27   // Clear whole database to avoid starting tests with unknown state
    28   // due to the previous tests.
    29   clearStore('net_stats_store', function() {
    30     clearStore('net_alarm', function() {
    31       run_next_test();
    32     });
    33   });
    34 });
    36 function filterTimestamp(date) {
    37   var sampleRate = netStatsDb.sampleRate;
    38   var offset = date.getTimezoneOffset() * 60 * 1000;
    39   return Math.floor((date.getTime() - offset) / sampleRate) * sampleRate;
    40 }
    42 function getNetworks() {
    43   return [{ id: '0', type: Ci.nsIDOMMozNetworkStatsManager.WIFI },
    44           { id: '1234', type: Ci.nsIDOMMozNetworkStatsManager.MOBILE }];
    45 }
    47 function compareNetworks(networkA, networkB) {
    48   return (networkA[0] == networkB[0] && networkA[1] == networkB[1]);
    49 }
    51 add_test(function test_sampleRate() {
    52   var sampleRate = netStatsDb.sampleRate;
    53   do_check_true(sampleRate > 0);
    54   netStatsDb.sampleRate = 0;
    55   sampleRate = netStatsDb.sampleRate;
    56   do_check_true(sampleRate > 0);
    58   run_next_test();
    59 });
    61 add_test(function test_maxStorageSamples() {
    62   var maxStorageSamples = netStatsDb.maxStorageSamples;
    63   do_check_true(maxStorageSamples > 0);
    64   netStatsDb.maxStorageSamples = 0;
    65   maxStorageSamples = netStatsDb.maxStorageSamples;
    66   do_check_true(maxStorageSamples > 0);
    68   run_next_test();
    69 });
    71 add_test(function test_fillResultSamples_emptyData() {
    72   var samples = 3;
    73   var data = [];
    74   var start = filterTimestamp(new Date());
    75   var sampleRate = netStatsDb.sampleRate;
    76   var end = start + (sampleRate * samples);
    77   netStatsDb.fillResultSamples(start, end, data);
    78   do_check_eq(data.length, samples + 1);
    80   var aux = start;
    81   var success = true;
    82   for (var i = 0; i <= samples; i++) {
    83     if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) {
    84       success = false;
    85       break;
    86     }
    87     aux += sampleRate;
    88   }
    89   do_check_true(success);
    91   run_next_test();
    92 });
    94 add_test(function test_fillResultSamples_noEmptyData() {
    95   var samples = 3;
    96   var sampleRate = netStatsDb.sampleRate;
    97   var start = filterTimestamp(new Date());
    98   var end = start + (sampleRate * samples);
    99   var data = [{date: new Date(start + sampleRate),
   100                rxBytes: 0,
   101                txBytes: 0}];
   102   netStatsDb.fillResultSamples(start, end, data);
   103   do_check_eq(data.length, samples + 1);
   105   var aux = start;
   106   var success = true;
   107   for (var i = 0; i <= samples; i++) {
   108     if (i == 1) {
   109       if (data[i].date.getTime() != aux || data[i].rxBytes != 0 || data[i].txBytes != 0) {
   110         success = false;
   111         break;
   112       }
   113     } else {
   114       if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) {
   115         success = false;
   116         break;
   117       }
   118     }
   119     aux += sampleRate;
   120   }
   121   do_check_true(success);
   123   run_next_test();
   124 });
   126 add_test(function test_clear() {
   127   var networks = getNetworks();
   128   networks.forEach(function(network, index) {
   129     networks[index] = {network: network, networkId: getNetworkId(network.id, network.type)};
   130   }, this);
   132   netStatsDb.clearStats(networks, function (error, result) {
   133     do_check_eq(error, null);
   134     run_next_test();
   135   });
   136 });
   138 add_test(function test_clear_interface() {
   139   var networks = getNetworks();
   140   networks.forEach(function(network, index) {
   141     networks[index] = {network: network, networkId: getNetworkId(network.id, network.type)};
   142   }, this);
   144   netStatsDb.clearInterfaceStats(networks[0], function (error, result) {
   145     do_check_eq(error, null);
   146     run_next_test();
   147   });
   148 });
   150 add_test(function test_internalSaveStats_singleSample() {
   151   var networks = getNetworks();
   153   var stats = { appId:         0,
   154                 serviceType:   "",
   155                 network:       [networks[0].id, networks[0].type],
   156                 timestamp:     Date.now(),
   157                 rxBytes:       0,
   158                 txBytes:       0,
   159                 rxSystemBytes: 1234,
   160                 txSystemBytes: 1234,
   161                 rxTotalBytes:  1234,
   162                 txTotalBytes:  1234 };
   164   netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   165     netStatsDb._saveStats(txn, store, stats);
   166   }, function(error, result) {
   167     do_check_eq(error, null);
   169     netStatsDb.logAllRecords(function(error, result) {
   170       do_check_eq(error, null);
   171       do_check_eq(result.length, 1);
   172       do_check_eq(result[0].appId, stats.appId);
   173       do_check_eq(result[0].serviceType, stats.serviceType);
   174       do_check_true(compareNetworks(result[0].network, stats.network));
   175       do_check_eq(result[0].timestamp, stats.timestamp);
   176       do_check_eq(result[0].rxBytes, stats.rxBytes);
   177       do_check_eq(result[0].txBytes, stats.txBytes);
   178       do_check_eq(result[0].rxSystemBytes, stats.rxSystemBytes);
   179       do_check_eq(result[0].txSystemBytes, stats.txSystemBytes);
   180       do_check_eq(result[0].rxTotalBytes, stats.rxTotalBytes);
   181       do_check_eq(result[0].txTotalBytes, stats.txTotalBytes);
   182       run_next_test();
   183     });
   184   });
   185 });
   187 add_test(function test_internalSaveStats_arraySamples() {
   188   clearStore('net_stats_store', function() {
   189     var networks = getNetworks();
   190     var network = [networks[0].id, networks[0].type];
   192     var samples = 2;
   193     var stats = [];
   194     for (var i = 0; i < samples; i++) {
   195       stats.push({ appId:         0,
   196                    serviceType:   "",
   197                    network:       network,
   198                    timestamp:     Date.now() + (10 * i),
   199                    rxBytes:       0,
   200                    txBytes:       0,
   201                    rxSystemBytes: 1234,
   202                    txSystemBytes: 1234,
   203                    rxTotalBytes:  1234,
   204                    txTotalBytes:  1234 });
   205     }
   207     netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   208       netStatsDb._saveStats(txn, store, stats);
   209     }, function(error, result) {
   210       do_check_eq(error, null);
   212       netStatsDb.logAllRecords(function(error, result) {
   213         do_check_eq(error, null);
   215         do_check_eq(result.length, samples);
   216         var success = true;
   217         for (var i = 0; i < samples; i++) {
   218           if (result[i].appId != stats[i].appId ||
   219               result[i].serviceType != stats[i].serviceType ||
   220               !compareNetworks(result[i].network, stats[i].network) ||
   221               result[i].timestamp != stats[i].timestamp ||
   222               result[i].rxBytes != stats[i].rxBytes ||
   223               result[i].txBytes != stats[i].txBytes ||
   224               result[i].rxSystemBytes != stats[i].rxSystemBytes ||
   225               result[i].txSystemBytes != stats[i].txSystemBytes ||
   226               result[i].rxTotalBytes != stats[i].rxTotalBytes ||
   227               result[i].txTotalBytes != stats[i].txTotalBytes) {
   228             success = false;
   229             break;
   230           }
   231         }
   232         do_check_true(success);
   233         run_next_test();
   234       });
   235     });
   236   });
   237 });
   239 add_test(function test_internalRemoveOldStats() {
   240   clearStore('net_stats_store', function() {
   241     var networks = getNetworks();
   242     var network = [networks[0].id, networks[0].type];
   243     var samples = 10;
   244     var stats = [];
   245     for (var i = 0; i < samples - 1; i++) {
   246       stats.push({ appId:               0, serviceType: "",
   247                    network:       network, timestamp:     Date.now() + (10 * i),
   248                    rxBytes:             0, txBytes:       0,
   249                    rxSystemBytes:    1234, txSystemBytes: 1234,
   250                    rxTotalBytes:     1234, txTotalBytes:  1234 });
   251     }
   253     stats.push({ appId:               0, serviceType: "",
   254                  network:       network, timestamp:     Date.now() + (10 * samples),
   255                  rxBytes:             0, txBytes:       0,
   256                  rxSystemBytes:    1234, txSystemBytes: 1234,
   257                  rxTotalBytes:     1234, txTotalBytes:  1234 });
   259     netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   260       netStatsDb._saveStats(txn, store, stats);
   261       var date = stats[stats.length - 1].timestamp
   262                  + (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1;
   263       netStatsDb._removeOldStats(txn, store, 0, "", network, date);
   264     }, function(error, result) {
   265       do_check_eq(error, null);
   267       netStatsDb.logAllRecords(function(error, result) {
   268         do_check_eq(error, null);
   269         do_check_eq(result.length, 1);
   271         run_next_test();
   272       });
   273     });
   274   });
   275 });
   277 function processSamplesDiff(networks, lastStat, newStat, callback) {
   278   clearStore('net_stats_store', function() {
   279     netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   280       netStatsDb._saveStats(txn, store, lastStat);
   281     }, function(error, result) {
   282       netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   283         let request = store.index("network").openCursor(newStat.network, "prev");
   284         request.onsuccess = function onsuccess(event) {
   285           let cursor = event.target.result;
   286           do_check_neq(cursor, null);
   287           netStatsDb._processSamplesDiff(txn, store, cursor, newStat, true);
   288         };
   289       }, function(error, result) {
   290         do_check_eq(error, null);
   291         netStatsDb.logAllRecords(function(error, result) {
   292           do_check_eq(error, null);
   293           callback(result);
   294         });
   295       });
   296     });
   297   });
   298 }
   300 add_test(function test_processSamplesDiffSameSample() {
   301   var networks = getNetworks();
   302   var network = [networks[0].id, networks[0].type];
   304   var sampleRate = netStatsDb.sampleRate;
   305   var date = filterTimestamp(new Date());
   307   var lastStat = { appId:               0, serviceType:   "",
   308                    network:       network, timestamp:     date,
   309                    rxBytes:             0, txBytes:       0,
   310                    rxSystemBytes:    1234, txSystemBytes: 1234,
   311                    rxTotalBytes:     2234, txTotalBytes:  2234 };
   313   var newStat = { appId:               0, serviceType:   "",
   314                   network:       network, timestamp:     date,
   315                   rxBytes:             0, txBytes:       0,
   316                   rxSystemBytes:    2234, txSystemBytes: 2234,
   317                   rxTotalBytes:     2234, txTotalBytes:  2234 };
   319   processSamplesDiff(networks, lastStat, newStat, function(result) {
   320     do_check_eq(result.length, 1);
   321     do_check_eq(result[0].appId, newStat.appId);
   322     do_check_eq(result[0].serviceType, newStat.serviceType);
   323     do_check_true(compareNetworks(result[0].network, newStat.network));
   324     do_check_eq(result[0].timestamp, newStat.timestamp);
   325     do_check_eq(result[0].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
   326     do_check_eq(result[0].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes);
   327     do_check_eq(result[0].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
   328     do_check_eq(result[0].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes);
   329     do_check_eq(result[0].rxSystemBytes, newStat.rxSystemBytes);
   330     do_check_eq(result[0].txSystemBytes, newStat.txSystemBytes);
   331     run_next_test();
   332   });
   333 });
   335 add_test(function test_processSamplesDiffNextSample() {
   336   var networks = getNetworks();
   337   var network = [networks[0].id, networks[0].type];
   339   var sampleRate = netStatsDb.sampleRate;
   340   var date = filterTimestamp(new Date());
   342   var lastStat = { appId:               0, serviceType:  "",
   343                    network:       network, timestamp:     date,
   344                    rxBytes:             0, txBytes:       0,
   345                    rxSystemBytes:    1234, txSystemBytes: 1234,
   346                    rxTotalBytes:     2234, txTotalBytes:  2234 };
   348   var newStat = { appId:               0, serviceType:  "",
   349                   network:       network, timestamp:     date + sampleRate,
   350                   rxBytes:             0, txBytes:       0,
   351                   rxSystemBytes:    1734, txSystemBytes: 1734,
   352                   rxTotalBytes:        0, txTotalBytes:  0 };
   354   processSamplesDiff(networks, lastStat, newStat, function(result) {
   355     do_check_eq(result.length, 2);
   356     do_check_eq(result[1].appId, newStat.appId);
   357     do_check_eq(result[1].serviceType, newStat.serviceType);
   358     do_check_true(compareNetworks(result[1].network, newStat.network));
   359     do_check_eq(result[1].timestamp, newStat.timestamp);
   360     do_check_eq(result[1].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
   361     do_check_eq(result[1].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes);
   362     do_check_eq(result[1].rxSystemBytes, newStat.rxSystemBytes);
   363     do_check_eq(result[1].txSystemBytes, newStat.txSystemBytes);
   364     do_check_eq(result[1].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
   365     do_check_eq(result[1].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes);
   366     run_next_test();
   367   });
   368 });
   370 add_test(function test_processSamplesDiffSamplesLost() {
   371   var networks = getNetworks();
   372   var network = [networks[0].id, networks[0].type];
   373   var samples = 5;
   374   var sampleRate = netStatsDb.sampleRate;
   375   var date = filterTimestamp(new Date());
   376   var lastStat = { appId:              0, serviceType:  "",
   377                    network:      network, timestamp:     date,
   378                    rxBytes:            0, txBytes:       0,
   379                    rxSystemBytes:   1234, txSystemBytes: 1234,
   380                    rxTotalBytes:    2234, txTotalBytes:  2234};
   382   var newStat = { appId:               0, serviceType:  "",
   383                   network:       network, timestamp:     date + (sampleRate * samples),
   384                   rxBytes:             0, txBytes:       0,
   385                   rxSystemBytes:    2234, txSystemBytes: 2234,
   386                   rxTotalBytes:        0, txTotalBytes:  0 };
   388   processSamplesDiff(networks, lastStat, newStat, function(result) {
   389     do_check_eq(result.length, samples + 1);
   390     do_check_eq(result[0].appId, newStat.appId);
   391     do_check_eq(result[0].serviceType, newStat.serviceType);
   392     do_check_true(compareNetworks(result[samples].network, newStat.network));
   393     do_check_eq(result[samples].timestamp, newStat.timestamp);
   394     do_check_eq(result[samples].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes);
   395     do_check_eq(result[samples].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes);
   396     do_check_eq(result[samples].rxSystemBytes, newStat.rxSystemBytes);
   397     do_check_eq(result[samples].txSystemBytes, newStat.txSystemBytes);
   398     do_check_eq(result[samples].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
   399     do_check_eq(result[samples].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes);
   400     run_next_test();
   401   });
   402 });
   404 add_test(function test_saveStats() {
   405   var networks = getNetworks();
   406   var network = [networks[0].id, networks[0].type];
   408   var stats = { appId:          0,
   409                 serviceType:    "",
   410                 networkId:      networks[0].id,
   411                 networkType:    networks[0].type,
   412                 date:           new Date(),
   413                 rxBytes:        2234,
   414                 txBytes:        2234,
   415                 isAccumulative: true };
   417   clearStore('net_stats_store', function() {
   418     netStatsDb.saveStats(stats, function(error, result) {
   419       do_check_eq(error, null);
   420       netStatsDb.logAllRecords(function(error, result) {
   421         do_check_eq(error, null);
   422         do_check_eq(result.length, 1);
   423         do_check_eq(result[0].appId, stats.appId);
   424         do_check_eq(result[0].serviceType, stats.serviceType);
   425         do_check_true(compareNetworks(result[0].network, network));
   426         let timestamp = filterTimestamp(stats.date);
   427         do_check_eq(result[0].timestamp, timestamp);
   428         do_check_eq(result[0].rxBytes, stats.rxBytes);
   429         do_check_eq(result[0].txBytes, stats.txBytes);
   430         do_check_eq(result[0].rxSystemBytes, stats.rxBytes);
   431         do_check_eq(result[0].txSystemBytes, stats.txBytes);
   432         do_check_eq(result[0].rxTotalBytes, stats.rxBytes);
   433         do_check_eq(result[0].txTotalBytes, stats.txBytes);
   434         run_next_test();
   435       });
   436     });
   437   });
   438 });
   440 add_test(function test_saveAppStats() {
   441   var networks = getNetworks();
   442   var network = [networks[0].id, networks[0].type];
   444   var stats = { appId:          1,
   445                 serviceType:    "",
   446                 networkId:      networks[0].id,
   447                 networkType:    networks[0].type,
   448                 date:           new Date(),
   449                 rxBytes:        2234,
   450                 txBytes:        2234,
   451                 isAccumulative: false };
   453   clearStore('net_stats_store', function() {
   454     netStatsDb.saveStats(stats, function(error, result) {
   455       do_check_eq(error, null);
   456       netStatsDb.logAllRecords(function(error, result) {
   457         do_check_eq(error, null);
   458         do_check_eq(result.length, 1);
   459         do_check_eq(result[0].appId, stats.appId);
   460         do_check_eq(result[0].serviceType, stats.serviceType);
   461         do_check_true(compareNetworks(result[0].network, network));
   462         let timestamp = filterTimestamp(stats.date);
   463         do_check_eq(result[0].timestamp, timestamp);
   464         do_check_eq(result[0].rxBytes, stats.rxBytes);
   465         do_check_eq(result[0].txBytes, stats.txBytes);
   466         do_check_eq(result[0].rxSystemBytes, 0);
   467         do_check_eq(result[0].txSystemBytes, 0);
   468         do_check_eq(result[0].rxTotalBytes, 0);
   469         do_check_eq(result[0].txTotalBytes, 0);
   470         run_next_test();
   471       });
   472     });
   473   });
   474 });
   476 add_test(function test_saveServiceStats() {
   477   var networks = getNetworks();
   478   var network = [networks[0].id, networks[0].type];
   480   var stats = { appId:          0,
   481                 serviceType:    "FakeType",
   482                 networkId:      networks[0].id,
   483                 networkType:    networks[0].type,
   484                 date:           new Date(),
   485                 rxBytes:        2234,
   486                 txBytes:        2234,
   487                 isAccumulative: false };
   489   clearStore('net_stats_store', function() {
   490     netStatsDb.saveStats(stats, function(error, result) {
   491       do_check_eq(error, null);
   492       netStatsDb.logAllRecords(function(error, result) {
   493         do_check_eq(error, null);
   494         do_check_eq(result.length, 1);
   495         do_check_eq(result[0].appId, stats.appId);
   496         do_check_eq(result[0].serviceType, stats.serviceType);
   497         do_check_true(compareNetworks(result[0].network, network));
   498         let timestamp = filterTimestamp(stats.date);
   499         do_check_eq(result[0].timestamp, timestamp);
   500         do_check_eq(result[0].rxBytes, stats.rxBytes);
   501         do_check_eq(result[0].txBytes, stats.txBytes);
   502         do_check_eq(result[0].rxSystemBytes, 0);
   503         do_check_eq(result[0].txSystemBytes, 0);
   504         do_check_eq(result[0].rxTotalBytes, 0);
   505         do_check_eq(result[0].txTotalBytes, 0);
   506         run_next_test();
   507       });
   508     });
   509   });
   510 });
   512 function prepareFind(stats, callback) {
   513   clearStore('net_stats_store', function() {
   514     netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
   515       netStatsDb._saveStats(txn, store, stats);
   516     }, function(error, result) {
   517         callback(error, result);
   518     });
   519   });
   520 }
   522 add_test(function test_find () {
   523   var networks = getNetworks();
   524   var networkWifi = [networks[0].id, networks[0].type];
   525   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   526   var appId = 0;
   527   var serviceType = "";
   529   var samples = 5;
   530   var sampleRate = netStatsDb.sampleRate;
   531   var start = Date.now();
   532   var saveDate = filterTimestamp(new Date());
   533   var end = new Date(start + (sampleRate * (samples - 1)));
   534   start = new Date(start - sampleRate);
   535   var stats = [];
   536   for (var i = 0; i < samples; i++) {
   537     stats.push({ appId:               appId, serviceType:   serviceType,
   538                  network:       networkWifi, timestamp:     saveDate + (sampleRate * i),
   539                  rxBytes:                 0, txBytes:       10,
   540                  rxSystemBytes:           0, txSystemBytes: 0,
   541                  rxTotalBytes:            0, txTotalBytes:  0 });
   544     stats.push({ appId:                 appId, serviceType:   serviceType,
   545                  network:       networkMobile, timestamp:     saveDate + (sampleRate * i),
   546                  rxBytes:                   0, txBytes:       10,
   547                  rxSystemBytes:             0, txSystemBytes: 0,
   548                  rxTotalBytes:              0, txTotalBytes:  0 });
   549   }
   551   prepareFind(stats, function(error, result) {
   552     do_check_eq(error, null);
   553     netStatsDb.find(function (error, result) {
   554       do_check_eq(error, null);
   555       do_check_eq(result.serviceType, serviceType);
   556       do_check_eq(result.network.id, networks[0].id);
   557       do_check_eq(result.network.type, networks[0].type);
   558       do_check_eq(result.start.getTime(), start.getTime());
   559       do_check_eq(result.end.getTime(), end.getTime());
   560       do_check_eq(result.data.length, samples + 1);
   561       do_check_eq(result.data[0].rxBytes, null);
   562       do_check_eq(result.data[1].rxBytes, 0);
   563       do_check_eq(result.data[samples].rxBytes, 0);
   564       run_next_test();
   565     }, appId, serviceType, networks[0], start, end);
   566   });
   567 });
   569 add_test(function test_findAppStats () {
   570   var networks = getNetworks();
   571   var networkWifi = [networks[0].id, networks[0].type];
   572   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   573   var appId = 1;
   574   var serviceType = "";
   576   var samples = 5;
   577   var sampleRate = netStatsDb.sampleRate;
   578   var start = Date.now();
   579   var saveDate = filterTimestamp(new Date());
   580   var end = new Date(start + (sampleRate * (samples - 1)));
   581   start = new Date(start - sampleRate);
   582   var stats = [];
   583   for (var i = 0; i < samples; i++) {
   584     stats.push({ appId:              appId, serviceType:  serviceType,
   585                  network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
   586                  rxBytes:                0, txBytes:      10,
   587                  rxTotalBytes:           0, txTotalBytes: 0 });
   589     stats.push({ appId:                appId, serviceType:  serviceType,
   590                  network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
   591                  rxBytes:                  0, txBytes:      10,
   592                  rxTotalBytes:             0, txTotalBytes: 0 });
   593   }
   595   prepareFind(stats, function(error, result) {
   596     do_check_eq(error, null);
   597     netStatsDb.find(function (error, result) {
   598       do_check_eq(error, null);
   599       do_check_eq(result.serviceType, serviceType);
   600       do_check_eq(result.network.id, networks[0].id);
   601       do_check_eq(result.network.type, networks[0].type);
   602       do_check_eq(result.start.getTime(), start.getTime());
   603       do_check_eq(result.end.getTime(), end.getTime());
   604       do_check_eq(result.data.length, samples + 1);
   605       do_check_eq(result.data[0].rxBytes, null);
   606       do_check_eq(result.data[1].rxBytes, 0);
   607       do_check_eq(result.data[samples].rxBytes, 0);
   608       run_next_test();
   609     }, appId, serviceType, networks[0], start, end);
   610   });
   611 });
   613 add_test(function test_findServiceStats () {
   614   var networks = getNetworks();
   615   var networkWifi = [networks[0].id, networks[0].type];
   616   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   617   var appId = 0;
   618   var serviceType = "FakeType";
   620   var samples = 5;
   621   var sampleRate = netStatsDb.sampleRate;
   622   var start = Date.now();
   623   var saveDate = filterTimestamp(new Date());
   624   var end = new Date(start + (sampleRate * (samples - 1)));
   625   start = new Date(start - sampleRate);
   626   var stats = [];
   627   for (var i = 0; i < samples; i++) {
   628     stats.push({ appId:              appId, serviceType:  serviceType,
   629                  network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
   630                  rxBytes:                0, txBytes:      10,
   631                  rxTotalBytes:           0, txTotalBytes: 0 });
   633     stats.push({ appId:                appId, serviceType:  serviceType,
   634                  network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
   635                  rxBytes:                  0, txBytes:      10,
   636                  rxTotalBytes:             0, txTotalBytes: 0 });
   637   }
   639   prepareFind(stats, function(error, result) {
   640     do_check_eq(error, null);
   641     netStatsDb.find(function (error, result) {
   642       do_check_eq(error, null);
   643       do_check_eq(result.serviceType, serviceType);
   644       do_check_eq(result.network.id, networks[0].id);
   645       do_check_eq(result.network.type, networks[0].type);
   646       do_check_eq(result.start.getTime(), start.getTime());
   647       do_check_eq(result.end.getTime(), end.getTime());
   648       do_check_eq(result.data.length, samples + 1);
   649       do_check_eq(result.data[0].rxBytes, null);
   650       do_check_eq(result.data[1].rxBytes, 0);
   651       do_check_eq(result.data[samples].rxBytes, 0);
   652       run_next_test();
   653     }, appId, serviceType, networks[0], start, end);
   654   });
   655 });
   657 add_test(function test_saveMultipleAppStats () {
   658   var networks = getNetworks();
   659   var networkWifi = networks[0];
   660   var networkMobile = networks[1]; // Fake mobile interface
   662   var saveDate = filterTimestamp(new Date());
   663   var cached = Object.create(null);
   664   var serviceType = "FakeType";
   665   var wifiNetId = networkWifi.id + '' + networkWifi.type;
   666   var mobileNetId = networkMobile.id + '' + networkMobile.type;
   668   cached[0 + '' + serviceType + wifiNetId] = {
   669     appId:                    0, date:           new Date(),
   670     networkId:   networkWifi.id, networkType:    networkWifi.type,
   671     rxBytes:                  0, txBytes:        10,
   672     serviceType:    serviceType, isAccumulative: false
   673   };
   675   cached[0 + '' + serviceType + mobileNetId] = {
   676     appId:                    0, date:           new Date(),
   677     networkId: networkMobile.id, networkType:    networkMobile.type,
   678     rxBytes:                  0, txBytes:        10,
   679     serviceType:    serviceType, isAccumulative: false
   680   };
   682   cached[1 + '' + wifiNetId] = {
   683     appId:                    1, date:           new Date(),
   684     networkId:   networkWifi.id, networkType:    networkWifi.type,
   685     rxBytes:                  0, txBytes:        10,
   686     serviceType:             "", isAccumulative: false
   687   };
   689   cached[1 + '' + mobileNetId] = {
   690     appId:                    1, date:           new Date(),
   691     networkId: networkMobile.id, networkType:    networkMobile.type,
   692     rxBytes:                  0, txBytes:        10,
   693     serviceType:             "", isAccumulative: false
   694   };
   696   cached[2 + '' + wifiNetId] = {
   697     appId:                    2, date:           new Date(),
   698     networkId:   networkWifi.id, networkType:    networkWifi.type,
   699     rxBytes:                  0, txBytes:        10,
   700     serviceType:             "", isAccumulative: false
   701   };
   703   cached[2 + '' + mobileNetId] = {
   704     appId:                    2, date:           new Date(),
   705     networkId: networkMobile.id, networkType:    networkMobile.type,
   706     rxBytes:                  0, txBytes:        10,
   707     serviceType:             "", isAccumulative: false
   708   };
   710   let keys = Object.keys(cached);
   711   let index = 0;
   713   networks.push(networkMobile);
   715   clearStore('net_stats_store', function() {
   716     netStatsDb.saveStats(cached[keys[index]],
   717       function callback(error, result) {
   718         do_check_eq(error, null);
   720         if (index == keys.length - 1) {
   721           netStatsDb.logAllRecords(function(error, result) {
   722             do_check_eq(error, null);
   723             do_check_eq(result.length, 6);
   724             do_check_eq(result[0].serviceType, serviceType);
   725             do_check_eq(result[3].appId, 1);
   726             do_check_true(compareNetworks(result[0].network, [networkWifi.id, networkWifi.type]));
   727             do_check_eq(result[0].rxBytes, 0);
   728             do_check_eq(result[0].txBytes, 10);
   729             run_next_test();
   730           });
   731          return;
   732         }
   734         index += 1;
   735         netStatsDb.saveStats(cached[keys[index]], callback);
   736     });
   737   });
   738 });
   740 var networkWifi = '00';
   741 var networkMobile = '11';
   743 var examplePageURL = "http://example.com/index.html";
   744 var exampleManifestURL = "http://example.com/manifest.webapp";
   746 var testPageURL = "http://test.com/index.html";
   747 var testManifestURL = "http://test.com/manifest.webapp";
   749 var alarms = [{ id:                null,
   750                 networkId:         networkWifi,
   751                 absoluteThreshold: 10000,
   752                 relativeThreshold: 10000,
   753                 data:              {foo: "something"},
   754                 pageURL:           examplePageURL,
   755                 manifestURL:       exampleManifestURL },
   756               { id:                null,
   757                 networkId:         networkWifi,
   758                 absoluteThreshold: 1000,
   759                 relativeThreshold: 1000,
   760                 data:              {foo: "else"},
   761                 pageURL:           examplePageURL,
   762                 manifestURL:       exampleManifestURL },
   763               { id:                null,
   764                 networkId:         networkMobile,
   765                 absoluteThreshold: 100,
   766                 relativeThreshold: 100,
   767                 data:              {foo: "to"},
   768                 pageURL:           examplePageURL,
   769                 manifestURL:       exampleManifestURL },
   770               { id:                null,
   771                 networkId:         networkMobile,
   772                 absoluteThreshold: 10,
   773                 relativeThreshold: 10,
   774                 data:              {foo: "test"},
   775                 pageURL:           testPageURL,
   776                 manifestURL:       testManifestURL }];
   778 var alarmsDbId = 1;
   780 add_test(function test_addAlarm() {
   781   // Add alarms[0] -> DB: [ alarms[0] (id: 1) ]
   782   // Check the insertion is OK.
   783   netStatsDb.addAlarm(alarms[0], function(error, result) {
   784     do_check_eq(error, null);
   785     alarmsDbId = result;
   786     netStatsDb.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, exampleManifestURL, function(error, result) {
   787       do_check_eq(error, null);
   788       do_check_eq(result.length, 1);
   789       do_check_eq(result[0].id, alarmsDbId);
   790       do_check_eq(result[0].networkId, alarms[0].networkId);
   791       do_check_eq(result[0].absoluteThreshold, alarms[0].absoluteThreshold);
   792       do_check_eq(result[0].relativeThreshold, alarms[0].relativeThreshold);
   793       do_check_eq(result[0].data.foo, alarms[0].data.foo);
   794       run_next_test();
   795     });
   796   });
   797 });
   799 add_test(function test_getFirstAlarm() {
   800   // Add alarms[1] -> DB: [ alarms[0] (id: 1), alarms[1] (id: 2) ]
   801   // Check first alarm is alarms[1] because threshold is lower.
   802   alarmsDbId += 1;
   803   netStatsDb.addAlarm(alarms[1], function (error, result) {
   804     do_check_eq(error, null);
   805     do_check_eq(result, alarmsDbId);
   806     netStatsDb.getFirstAlarm(networkWifi, function(error, result) {
   807       do_check_eq(error, null);
   808       do_check_eq(result.id, alarmsDbId);
   809       do_check_eq(result.networkId, alarms[1].networkId);
   810       do_check_eq(result.absoluteThreshold, alarms[1].absoluteThreshold);
   811       do_check_eq(result.relativeThreshold, alarms[1].relativeThreshold);
   812       do_check_eq(result.data.foo, alarms[1].data.foo);
   813       do_check_eq(result.pageURL, alarms[1].pageURL);
   814       do_check_eq(result.manifestURL, alarms[1].manifestURL);
   815       run_next_test();
   816     });
   817   });
   818 });
   820 add_test(function test_removeAlarm() {
   821   // Remove alarms[1] (id: 2) -> DB: [ alarms[0] (id: 1) ]
   822   // Check get first return alarms[0].
   823   netStatsDb.removeAlarm(alarmsDbId, alarms[0].manifestURL, function (error, result) {
   824     do_check_eq(error, null);
   825     netStatsDb.getFirstAlarm(networkWifi, function(error, result) {
   826       do_check_eq(error, null);
   827       do_check_eq(result.id, alarmsDbId - 1);
   828       do_check_eq(result.networkId, alarms[0].networkId);
   829       do_check_eq(result.absoluteThreshold, alarms[0].absoluteThreshold);
   830       do_check_eq(result.relativeThreshold, alarms[0].relativeThreshold);
   831       do_check_eq(result.data.foo, alarms[0].data.foo);
   832       do_check_eq(result.pageURL, alarms[0].pageURL);
   833       do_check_eq(result.manifestURL, alarms[0].manifestURL);
   834       run_next_test();
   835     });
   836   });
   837 });
   839 add_test(function test_removeAppAlarm() {
   840   // Remove alarms[0] (id: 1) -> DB: [ ]
   841   netStatsDb.removeAlarm(alarmsDbId - 1, alarms[0].manifestURL, function (error, result) {
   842     do_check_eq(error, null);
   843     netStatsDb.getAlarms(networkWifi, exampleManifestURL, function(error, result) {
   844       do_check_eq(error, null);
   845       do_check_eq(result.length, 0);
   846       run_next_test();
   847     });
   848   });
   849 });
   851 add_test(function test_getAlarms() {
   852   // Add all alarms -> DB: [ alarms[0] (id: 3),
   853   //                         alarms[1] (id: 4),
   854   //                         alarms[2] (id: 5),
   855   //                         alarms[3] (id: 6) ]
   856   // Check that getAlarms for wifi returns 2 alarms.
   857   // Check that getAlarms for all connections returns 3 alarms.
   859   var callback = function () {
   860     netStatsDb.getAlarms(networkWifi, exampleManifestURL, function(error, result) {
   861       do_check_eq(error, null);
   862       do_check_eq(result.length, 2);
   863       netStatsDb.getAlarms(null, exampleManifestURL, function(error, result) {
   864         do_check_eq(error, null);
   865         do_check_eq(result.length, 3);
   866         run_next_test();
   867       });
   868     });
   869   };
   871   var index = 0;
   873   var addFunction = function () {
   874     alarmsDbId += 1;
   875     netStatsDb.addAlarm(alarms[index], function (error, result) {
   876       do_check_eq(error, null);
   877       index += 1;
   878       do_check_eq(result, alarmsDbId);
   879       if (index >= alarms.length) {
   880         callback();
   881         return;
   882       }
   883       addFunction();
   884     });
   885   };
   887   addFunction();
   888 });
   890 add_test(function test_removeAppAllAlarms() {
   891   // Remove all alarms for exampleManifestURL -> DB: [ alarms[3] (id: 6) ]
   892   netStatsDb.removeAlarms(exampleManifestURL, function (error, result) {
   893     do_check_eq(error, null);
   894     netStatsDb.getAlarms(null, exampleManifestURL, function(error, result) {
   895       do_check_eq(error, null);
   896       do_check_eq(result.length, 0);
   897       netStatsDb.getAlarms(null, testManifestURL, function(error, result) {
   898         do_check_eq(error, null);
   899         do_check_eq(result.length, 1);
   900         run_next_test();
   901       });
   902     });
   903   });
   904 });
   906 add_test(function test_updateAlarm() {
   907   // Update alarms[3] (id: 6) -> DB: [ alarms[3]* (id: 6) ]
   909   var updatedAlarm = alarms[1];
   910   updatedAlarm.id = alarmsDbId;
   911   updatedAlarm.threshold = 10;
   913   netStatsDb.updateAlarm(updatedAlarm, function (error, result) {
   914     do_check_eq(error, null);
   915     netStatsDb.getFirstAlarm(networkWifi, function(error, result) {
   916       do_check_eq(error, null);
   917       do_check_eq(result.id, updatedAlarm.id);
   918       do_check_eq(result.networkId, updatedAlarm.networkId);
   919       do_check_eq(result.absoluteThreshold, updatedAlarm.absoluteThreshold);
   920       do_check_eq(result.relativeThreshold, updatedAlarm.relativeThreshold);
   921       do_check_eq(result.data.foo, updatedAlarm.data.foo);
   922       do_check_eq(result.pageURL, updatedAlarm.pageURL);
   923       do_check_eq(result.manifestURL, updatedAlarm.manifestURL);
   924       run_next_test();
   925     });
   926   });
   927 });
   929 function run_test() {
   930   do_get_profile();
   931   run_next_test();
   932 }

mercurial