Bug 1070944 - Part 4: Update and add test cases. r=albert
authorEthan Tseng <ettseng@mozilla.com>
Mon, 09 Feb 2015 10:30:57 +0800
changeset 228245 2ca4450d6a33c7247802f43a34432911b5169a2a
parent 228244 a67a9f37cb211c69426032b3032918943bc50836
child 228246 3d85bc28eaad1537b7bf12bf0a7593bf67c21100
push id13814
push usercbook@mozilla.com
push dateTue, 10 Feb 2015 11:49:28 +0000
treeherderb2g-inbound@2ca4450d6a33 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersalbert
bugs1070944
milestone38.0a1
Bug 1070944 - Part 4: Update and add test cases. r=albert
dom/network/tests/unit_stats/test_networkstats_db.js
dom/network/tests/unit_stats/test_networkstats_service_proxy.js
--- a/dom/network/tests/unit_stats/test_networkstats_db.js
+++ b/dom/network/tests/unit_stats/test_networkstats_db.js
@@ -1,15 +1,16 @@
 /* Any: copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/NetworkStatsDB.jsm");
 
+const STATS_STORE_NAME = "net_stats_store_v3";
 const netStatsDb = new NetworkStatsDB();
 
 function clearStore(store, callback) {
   netStatsDb.dbNewTxn(store, "readwrite", function(aTxn, aStore) {
     aStore.openCursor().onsuccess = function (event) {
       let cursor = event.target.result;
       if (cursor){
         cursor.delete();
@@ -21,17 +22,17 @@ function clearStore(store, callback) {
 
 function getNetworkId(aIccId, aNetworkType) {
   return aIccId + '' + aNetworkType;
 }
 
 add_test(function prepareDatabase() {
   // Clear whole database to avoid starting tests with unknown state
   // due to the previous tests.
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     clearStore('net_alarm', function() {
       run_next_test();
     });
   });
 });
 
 function filterTimestamp(date) {
   var sampleRate = netStatsDb.sampleRate;
@@ -146,81 +147,85 @@ add_test(function test_clear_interface()
     run_next_test();
   });
 });
 
 add_test(function test_internalSaveStats_singleSample() {
   var networks = getNetworks();
 
   var stats = { appId:         0,
+                isInBrowser:   0,
                 serviceType:   "",
                 network:       [networks[0].id, networks[0].type],
                 timestamp:     Date.now(),
                 rxBytes:       0,
                 txBytes:       0,
                 rxSystemBytes: 1234,
                 txSystemBytes: 1234,
                 rxTotalBytes:  1234,
                 txTotalBytes:  1234 };
 
-  netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+  netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
     netStatsDb._saveStats(txn, store, stats);
   }, function(error, result) {
     do_check_eq(error, null);
 
     netStatsDb.logAllRecords(function(error, result) {
       do_check_eq(error, null);
       do_check_eq(result.length, 1);
       do_check_eq(result[0].appId, stats.appId);
+      do_check_eq(result[0].isInBrowser, stats.isInBrowser);
       do_check_eq(result[0].serviceType, stats.serviceType);
       do_check_true(compareNetworks(result[0].network, stats.network));
       do_check_eq(result[0].timestamp, stats.timestamp);
       do_check_eq(result[0].rxBytes, stats.rxBytes);
       do_check_eq(result[0].txBytes, stats.txBytes);
       do_check_eq(result[0].rxSystemBytes, stats.rxSystemBytes);
       do_check_eq(result[0].txSystemBytes, stats.txSystemBytes);
       do_check_eq(result[0].rxTotalBytes, stats.rxTotalBytes);
       do_check_eq(result[0].txTotalBytes, stats.txTotalBytes);
       run_next_test();
     });
   });
 });
 
 add_test(function test_internalSaveStats_arraySamples() {
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     var networks = getNetworks();
     var network = [networks[0].id, networks[0].type];
 
     var samples = 2;
     var stats = [];
     for (var i = 0; i < samples; i++) {
       stats.push({ appId:         0,
+                   isInBrowser:   0,
                    serviceType:   "",
                    network:       network,
                    timestamp:     Date.now() + (10 * i),
                    rxBytes:       0,
                    txBytes:       0,
                    rxSystemBytes: 1234,
                    txSystemBytes: 1234,
                    rxTotalBytes:  1234,
                    txTotalBytes:  1234 });
     }
 
-    netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+    netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
     }, function(error, result) {
       do_check_eq(error, null);
 
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
 
         do_check_eq(result.length, samples);
         var success = true;
         for (var i = 0; i < samples; i++) {
           if (result[i].appId != stats[i].appId ||
+              result[i].isInBrowser != stats[i].isInBrowser ||
               result[i].serviceType != stats[i].serviceType ||
               !compareNetworks(result[i].network, stats[i].network) ||
               result[i].timestamp != stats[i].timestamp ||
               result[i].rxBytes != stats[i].rxBytes ||
               result[i].txBytes != stats[i].txBytes ||
               result[i].rxSystemBytes != stats[i].rxSystemBytes ||
               result[i].txSystemBytes != stats[i].txSystemBytes ||
               result[i].rxTotalBytes != stats[i].rxTotalBytes ||
@@ -232,59 +237,61 @@ add_test(function test_internalSaveStats
         do_check_true(success);
         run_next_test();
       });
     });
   });
 });
 
 add_test(function test_internalRemoveOldStats() {
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     var networks = getNetworks();
     var network = [networks[0].id, networks[0].type];
     var samples = 10;
     var stats = [];
     for (var i = 0; i < samples - 1; i++) {
-      stats.push({ appId:               0, serviceType: "",
+      stats.push({ appId:               0, isInBrowser:   0,
+                   serviceType:        "",
                    network:       network, timestamp:     Date.now() + (10 * i),
                    rxBytes:             0, txBytes:       0,
                    rxSystemBytes:    1234, txSystemBytes: 1234,
                    rxTotalBytes:     1234, txTotalBytes:  1234 });
     }
 
-    stats.push({ appId:               0, serviceType: "",
+    stats.push({ appId:               0, isInBrowser:   0,
+                 serviceType:        "",
                  network:       network, timestamp:     Date.now() + (10 * samples),
                  rxBytes:             0, txBytes:       0,
                  rxSystemBytes:    1234, txSystemBytes: 1234,
                  rxTotalBytes:     1234, txTotalBytes:  1234 });
 
-    netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+    netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
       var date = stats[stats.length - 1].timestamp
                  + (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1;
-      netStatsDb._removeOldStats(txn, store, 0, "", network, date);
+      netStatsDb._removeOldStats(txn, store, 0, 0, "", network, date);
     }, function(error, result) {
       do_check_eq(error, null);
 
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
 
         run_next_test();
       });
     });
   });
 });
 
 function processSamplesDiff(networks, lastStat, newStat, callback) {
-  clearStore('net_stats_store', function() {
-    netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+  clearStore(STATS_STORE_NAME, function() {
+    netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, lastStat);
     }, function(error, result) {
-      netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+      netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
         let request = store.index("network").openCursor(newStat.network, "prev");
         request.onsuccess = function onsuccess(event) {
           let cursor = event.target.result;
           do_check_neq(cursor, null);
           netStatsDb._processSamplesDiff(txn, store, cursor, newStat, true);
         };
       }, function(error, result) {
         do_check_eq(error, null);
@@ -299,31 +306,34 @@ function processSamplesDiff(networks, la
 
 add_test(function test_processSamplesDiffSameSample() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
 
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
 
-  var lastStat = { appId:               0, serviceType:   "",
+  var lastStat = { appId:               0, isInBrowser:   0,
+                   serviceType:        "",
                    network:       network, timestamp:     date,
                    rxBytes:             0, txBytes:       0,
                    rxSystemBytes:    1234, txSystemBytes: 1234,
                    rxTotalBytes:     2234, txTotalBytes:  2234 };
 
-  var newStat = { appId:               0, serviceType:   "",
+  var newStat = { appId:               0, isInBrowser:   0,
+                  serviceType:        "",
                   network:       network, timestamp:     date,
                   rxBytes:             0, txBytes:       0,
                   rxSystemBytes:    2234, txSystemBytes: 2234,
                   rxTotalBytes:     2234, txTotalBytes:  2234 };
 
   processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, 1);
     do_check_eq(result[0].appId, newStat.appId);
+    do_check_eq(result[0].isInBrowser, newStat.isInBrowser);
     do_check_eq(result[0].serviceType, newStat.serviceType);
     do_check_true(compareNetworks(result[0].network, newStat.network));
     do_check_eq(result[0].timestamp, newStat.timestamp);
     do_check_eq(result[0].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
     do_check_eq(result[0].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes);
     do_check_eq(result[0].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
     do_check_eq(result[0].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes);
     do_check_eq(result[0].rxSystemBytes, newStat.rxSystemBytes);
@@ -334,31 +344,34 @@ add_test(function test_processSamplesDif
 
 add_test(function test_processSamplesDiffNextSample() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
 
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
 
-  var lastStat = { appId:               0, serviceType:  "",
+  var lastStat = { appId:               0, isInBrowser:   0,
+                   serviceType:        "",
                    network:       network, timestamp:     date,
                    rxBytes:             0, txBytes:       0,
                    rxSystemBytes:    1234, txSystemBytes: 1234,
                    rxTotalBytes:     2234, txTotalBytes:  2234 };
 
-  var newStat = { appId:               0, serviceType:  "",
+  var newStat = { appId:               0, isInBrowser:   0,
+                  serviceType:        "",
                   network:       network, timestamp:     date + sampleRate,
                   rxBytes:             0, txBytes:       0,
                   rxSystemBytes:    1734, txSystemBytes: 1734,
                   rxTotalBytes:        0, txTotalBytes:  0 };
 
   processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, 2);
     do_check_eq(result[1].appId, newStat.appId);
+    do_check_eq(result[1].isInBrowser, newStat.isInBrowser);
     do_check_eq(result[1].serviceType, newStat.serviceType);
     do_check_true(compareNetworks(result[1].network, newStat.network));
     do_check_eq(result[1].timestamp, newStat.timestamp);
     do_check_eq(result[1].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
     do_check_eq(result[1].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes);
     do_check_eq(result[1].rxSystemBytes, newStat.rxSystemBytes);
     do_check_eq(result[1].txSystemBytes, newStat.txSystemBytes);
     do_check_eq(result[1].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
@@ -368,31 +381,34 @@ add_test(function test_processSamplesDif
 });
 
 add_test(function test_processSamplesDiffSamplesLost() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
-  var lastStat = { appId:              0, serviceType:  "",
+  var lastStat = { appId:              0, isInBrowser:   0,
+                   serviceType:       "",
                    network:      network, timestamp:     date,
                    rxBytes:            0, txBytes:       0,
                    rxSystemBytes:   1234, txSystemBytes: 1234,
                    rxTotalBytes:    2234, txTotalBytes:  2234};
 
-  var newStat = { appId:               0, serviceType:  "",
+  var newStat = { appId:               0, isInBrowser:   0,
+                  serviceType:        "",
                   network:       network, timestamp:     date + (sampleRate * samples),
                   rxBytes:             0, txBytes:       0,
                   rxSystemBytes:    2234, txSystemBytes: 2234,
                   rxTotalBytes:        0, txTotalBytes:  0 };
 
   processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, samples + 1);
     do_check_eq(result[0].appId, newStat.appId);
+    do_check_eq(result[0].isInBrowser, newStat.isInBrowser);
     do_check_eq(result[0].serviceType, newStat.serviceType);
     do_check_true(compareNetworks(result[samples].network, newStat.network));
     do_check_eq(result[samples].timestamp, newStat.timestamp);
     do_check_eq(result[samples].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes);
     do_check_eq(result[samples].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes);
     do_check_eq(result[samples].rxSystemBytes, newStat.rxSystemBytes);
     do_check_eq(result[samples].txSystemBytes, newStat.txSystemBytes);
     do_check_eq(result[samples].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes);
@@ -401,31 +417,33 @@ add_test(function test_processSamplesDif
   });
 });
 
 add_test(function test_saveStats() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
 
   var stats = { appId:          0,
+                isInBrowser:    false,
                 serviceType:    "",
                 networkId:      networks[0].id,
                 networkType:    networks[0].type,
                 date:           new Date(),
                 rxBytes:        2234,
                 txBytes:        2234,
                 isAccumulative: true };
 
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     netStatsDb.saveStats(stats, function(error, result) {
       do_check_eq(error, null);
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
         do_check_eq(result[0].appId, stats.appId);
+        do_check_eq(result[0].isInBrowser, stats.isInBrowser);
         do_check_eq(result[0].serviceType, stats.serviceType);
         do_check_true(compareNetworks(result[0].network, network));
         let timestamp = filterTimestamp(stats.date);
         do_check_eq(result[0].timestamp, timestamp);
         do_check_eq(result[0].rxBytes, stats.rxBytes);
         do_check_eq(result[0].txBytes, stats.txBytes);
         do_check_eq(result[0].rxSystemBytes, stats.rxBytes);
         do_check_eq(result[0].txSystemBytes, stats.txBytes);
@@ -437,31 +455,33 @@ add_test(function test_saveStats() {
   });
 });
 
 add_test(function test_saveAppStats() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
 
   var stats = { appId:          1,
+                isInBrowser:    false,
                 serviceType:    "",
                 networkId:      networks[0].id,
                 networkType:    networks[0].type,
                 date:           new Date(),
                 rxBytes:        2234,
                 txBytes:        2234,
                 isAccumulative: false };
 
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     netStatsDb.saveStats(stats, function(error, result) {
       do_check_eq(error, null);
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
         do_check_eq(result[0].appId, stats.appId);
+        do_check_eq(result[0].isInBrowser, 0);
         do_check_eq(result[0].serviceType, stats.serviceType);
         do_check_true(compareNetworks(result[0].network, network));
         let timestamp = filterTimestamp(stats.date);
         do_check_eq(result[0].timestamp, timestamp);
         do_check_eq(result[0].rxBytes, stats.rxBytes);
         do_check_eq(result[0].txBytes, stats.txBytes);
         do_check_eq(result[0].rxSystemBytes, 0);
         do_check_eq(result[0].txSystemBytes, 0);
@@ -473,31 +493,33 @@ add_test(function test_saveAppStats() {
   });
 });
 
 add_test(function test_saveServiceStats() {
   var networks = getNetworks();
   var network = [networks[0].id, networks[0].type];
 
   var stats = { appId:          0,
+                isInBrowser:    false,
                 serviceType:    "FakeType",
                 networkId:      networks[0].id,
                 networkType:    networks[0].type,
                 date:           new Date(),
                 rxBytes:        2234,
                 txBytes:        2234,
                 isAccumulative: false };
 
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     netStatsDb.saveStats(stats, function(error, result) {
       do_check_eq(error, null);
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
         do_check_eq(result[0].appId, stats.appId);
+        do_check_eq(result[0].isInBrowser, 0);
         do_check_eq(result[0].serviceType, stats.serviceType);
         do_check_true(compareNetworks(result[0].network, network));
         let timestamp = filterTimestamp(stats.date);
         do_check_eq(result[0].timestamp, timestamp);
         do_check_eq(result[0].rxBytes, stats.rxBytes);
         do_check_eq(result[0].txBytes, stats.txBytes);
         do_check_eq(result[0].rxSystemBytes, 0);
         do_check_eq(result[0].txSystemBytes, 0);
@@ -505,157 +527,169 @@ add_test(function test_saveServiceStats(
         do_check_eq(result[0].txTotalBytes, 0);
         run_next_test();
       });
     });
   });
 });
 
 function prepareFind(stats, callback) {
-  clearStore('net_stats_store', function() {
-    netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
+  clearStore(STATS_STORE_NAME, function() {
+    netStatsDb.dbNewTxn(STATS_STORE_NAME, "readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
     }, function(error, result) {
         callback(error, result);
     });
   });
 }
 
 add_test(function test_find () {
   var networks = getNetworks();
   var networkWifi = [networks[0].id, networks[0].type];
   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   var appId = 0;
+  var isInBrowser = 0;
   var serviceType = "";
 
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var start = Date.now();
   var saveDate = filterTimestamp(new Date());
   var end = new Date(start + (sampleRate * (samples - 1)));
   start = new Date(start - sampleRate);
   var stats = [];
   for (var i = 0; i < samples; i++) {
-    stats.push({ appId:               appId, serviceType:   serviceType,
+    stats.push({ appId:               appId, isInBrowser:   isInBrowser,
+                 serviceType:   serviceType,
                  network:       networkWifi, timestamp:     saveDate + (sampleRate * i),
                  rxBytes:                 0, txBytes:       10,
                  rxSystemBytes:           0, txSystemBytes: 0,
                  rxTotalBytes:            0, txTotalBytes:  0 });
 
 
-    stats.push({ appId:                 appId, serviceType:   serviceType,
+    stats.push({ appId:                 appId, isInBrowser:   isInBrowser,
+                 serviceType:     serviceType,
                  network:       networkMobile, timestamp:     saveDate + (sampleRate * i),
                  rxBytes:                   0, txBytes:       10,
                  rxSystemBytes:             0, txSystemBytes: 0,
                  rxTotalBytes:              0, txTotalBytes:  0 });
   }
 
   prepareFind(stats, function(error, result) {
     do_check_eq(error, null);
     netStatsDb.find(function (error, result) {
       do_check_eq(error, null);
+      do_check_eq(result.browsingTrafficOnly, false);
       do_check_eq(result.serviceType, serviceType);
       do_check_eq(result.network.id, networks[0].id);
       do_check_eq(result.network.type, networks[0].type);
       do_check_eq(result.start.getTime(), start.getTime());
       do_check_eq(result.end.getTime(), end.getTime());
       do_check_eq(result.data.length, samples + 1);
       do_check_eq(result.data[0].rxBytes, null);
       do_check_eq(result.data[1].rxBytes, 0);
       do_check_eq(result.data[samples].rxBytes, 0);
       run_next_test();
-    }, appId, serviceType, networks[0], start, end);
+    }, appId, false, serviceType, networks[0], start, end);
   });
 });
 
 add_test(function test_findAppStats () {
   var networks = getNetworks();
   var networkWifi = [networks[0].id, networks[0].type];
   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   var appId = 1;
+  var isInBrowser = 0;
   var serviceType = "";
 
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var start = Date.now();
   var saveDate = filterTimestamp(new Date());
   var end = new Date(start + (sampleRate * (samples - 1)));
   start = new Date(start - sampleRate);
   var stats = [];
   for (var i = 0; i < samples; i++) {
-    stats.push({ appId:              appId, serviceType:  serviceType,
+    stats.push({ appId:              appId, isInBrowser:  isInBrowser,
+                 serviceType:  serviceType,
                  network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
                  rxBytes:                0, txBytes:      10,
                  rxTotalBytes:           0, txTotalBytes: 0 });
 
-    stats.push({ appId:                appId, serviceType:  serviceType,
+    stats.push({ appId:                appId, isInBrowser:  isInBrowser,
+                 serviceType:    serviceType,
                  network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
                  rxBytes:                  0, txBytes:      10,
                  rxTotalBytes:             0, txTotalBytes: 0 });
   }
 
   prepareFind(stats, function(error, result) {
     do_check_eq(error, null);
     netStatsDb.find(function (error, result) {
       do_check_eq(error, null);
+      do_check_eq(result.browsingTrafficOnly, false);
       do_check_eq(result.serviceType, serviceType);
       do_check_eq(result.network.id, networks[0].id);
       do_check_eq(result.network.type, networks[0].type);
       do_check_eq(result.start.getTime(), start.getTime());
       do_check_eq(result.end.getTime(), end.getTime());
       do_check_eq(result.data.length, samples + 1);
       do_check_eq(result.data[0].rxBytes, null);
       do_check_eq(result.data[1].rxBytes, 0);
       do_check_eq(result.data[samples].rxBytes, 0);
       run_next_test();
-    }, appId, serviceType, networks[0], start, end);
+    }, appId, false, serviceType, networks[0], start, end);
   });
 });
 
 add_test(function test_findServiceStats () {
   var networks = getNetworks();
   var networkWifi = [networks[0].id, networks[0].type];
   var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
   var appId = 0;
+  var isInBrowser = 0;
   var serviceType = "FakeType";
 
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var start = Date.now();
   var saveDate = filterTimestamp(new Date());
   var end = new Date(start + (sampleRate * (samples - 1)));
   start = new Date(start - sampleRate);
   var stats = [];
   for (var i = 0; i < samples; i++) {
-    stats.push({ appId:              appId, serviceType:  serviceType,
+    stats.push({ appId:              appId, isInBrowser:  isInBrowser,
+                 serviceType:  serviceType,
                  network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
                  rxBytes:                0, txBytes:      10,
                  rxTotalBytes:           0, txTotalBytes: 0 });
 
-    stats.push({ appId:                appId, serviceType:  serviceType,
+    stats.push({ appId:                appId, isInBrowser:  isInBrowser,
+                 serviceType:    serviceType,
                  network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
                  rxBytes:                  0, txBytes:      10,
                  rxTotalBytes:             0, txTotalBytes: 0 });
   }
 
   prepareFind(stats, function(error, result) {
     do_check_eq(error, null);
     netStatsDb.find(function (error, result) {
       do_check_eq(error, null);
+      do_check_eq(result.browsingTrafficOnly, false);
       do_check_eq(result.serviceType, serviceType);
       do_check_eq(result.network.id, networks[0].id);
       do_check_eq(result.network.type, networks[0].type);
       do_check_eq(result.start.getTime(), start.getTime());
       do_check_eq(result.end.getTime(), end.getTime());
       do_check_eq(result.data.length, samples + 1);
       do_check_eq(result.data[0].rxBytes, null);
       do_check_eq(result.data[1].rxBytes, 0);
       do_check_eq(result.data[samples].rxBytes, 0);
       run_next_test();
-    }, appId, serviceType, networks[0], start, end);
+    }, appId, false, serviceType, networks[0], start, end);
   });
 });
 
 add_test(function test_saveMultipleAppStats () {
   var networks = getNetworks();
   var networkWifi = networks[0];
   var networkMobile = networks[1]; // Fake mobile interface
 
@@ -664,84 +698,203 @@ add_test(function test_saveMultipleAppSt
   var serviceType = "FakeType";
   var wifiNetId = networkWifi.id + '' + networkWifi.type;
   var mobileNetId = networkMobile.id + '' + networkMobile.type;
 
   cached[0 + '' + serviceType + wifiNetId] = {
     appId:                    0, date:           new Date(),
     networkId:   networkWifi.id, networkType:    networkWifi.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:    serviceType, isAccumulative: false
+    serviceType:    serviceType, isAccumulative: false,
+    isInBrowser:          false
   };
 
   cached[0 + '' + serviceType + mobileNetId] = {
     appId:                    0, date:           new Date(),
     networkId: networkMobile.id, networkType:    networkMobile.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:    serviceType, isAccumulative: false
+    serviceType:    serviceType, isAccumulative: false,
+    isInBrowser:          false
   };
 
   cached[1 + '' + wifiNetId] = {
     appId:                    1, date:           new Date(),
     networkId:   networkWifi.id, networkType:    networkWifi.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:             "", isAccumulative: false
+    serviceType:             "", isAccumulative: false,
+    isInBrowser:          false
   };
 
   cached[1 + '' + mobileNetId] = {
     appId:                    1, date:           new Date(),
     networkId: networkMobile.id, networkType:    networkMobile.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:             "", isAccumulative: false
+    serviceType:             "", isAccumulative: false,
+    isInBrowser:          false
   };
 
   cached[2 + '' + wifiNetId] = {
     appId:                    2, date:           new Date(),
     networkId:   networkWifi.id, networkType:    networkWifi.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:             "", isAccumulative: false
+    serviceType:             "", isAccumulative: false,
+    isInBrowser:          false
   };
 
   cached[2 + '' + mobileNetId] = {
     appId:                    2, date:           new Date(),
     networkId: networkMobile.id, networkType:    networkMobile.type,
     rxBytes:                  0, txBytes:        10,
-    serviceType:             "", isAccumulative: false
+    serviceType:             "", isAccumulative: false,
+    isInBrowser:          false
   };
 
   let keys = Object.keys(cached);
   let index = 0;
 
   networks.push(networkMobile);
 
-  clearStore('net_stats_store', function() {
+  clearStore(STATS_STORE_NAME, function() {
     netStatsDb.saveStats(cached[keys[index]],
       function callback(error, result) {
         do_check_eq(error, null);
 
         if (index == keys.length - 1) {
           netStatsDb.logAllRecords(function(error, result) {
             do_check_eq(error, null);
             do_check_eq(result.length, 6);
+            do_check_eq(result[0].isInBrowser, 0);
             do_check_eq(result[0].serviceType, serviceType);
             do_check_eq(result[3].appId, 1);
             do_check_true(compareNetworks(result[0].network, [networkWifi.id, networkWifi.type]));
             do_check_eq(result[0].rxBytes, 0);
             do_check_eq(result[0].txBytes, 10);
             run_next_test();
           });
          return;
         }
 
         index += 1;
         netStatsDb.saveStats(cached[keys[index]], callback);
     });
   });
 });
 
+// Test case for find samples with browsingTrafficOnly option.
+add_test(function test_findBrowsingTrafficStats() {
+  var networks = getNetworks();
+  var networkWifi = [networks[0].id, networks[0].type];
+  var networkMobile = [networks[1].id, networks[1].type];
+  var serviceType = "";
+  var samples = 5;
+  var sampleRate = netStatsDb.sampleRate;
+  var start = Date.now();
+  var end = new Date(start + (sampleRate * (samples - 1)));
+  var saveDate = filterTimestamp(new Date());
+  start = new Date(start - sampleRate);
+  var stats = [];
+
+  for (var i = 0; i < samples; i++) {
+    // System app.
+    stats.push({ appId: 1008, isInBrowser: 0,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 200, txBytes: 100});
+    // Browser of system app.
+    stats.push({ appId: 1008, isInBrowser: 1,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 1000, txBytes: 500});
+    // Another app.
+    stats.push({ appId: 1021, isInBrowser: 0,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 300, txBytes: 150});
+    // Browser of another app.
+    stats.push({ appId: 1021, isInBrowser: 1,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 600, txBytes: 300});
+  }
+
+  prepareFind(stats, function(error, result) {
+    do_check_eq(error, null);
+    netStatsDb.find(function(error, result) {
+      do_check_eq(error, null);
+      do_check_eq(result.browsingTrafficOnly, true);
+      do_check_eq(result.serviceType, serviceType);
+      do_check_eq(result.network.id, networks[1].id);
+      do_check_eq(result.network.type, networks[1].type);
+      do_check_eq(result.start.getTime(), start.getTime());
+      do_check_eq(result.end.getTime(), end.getTime());
+      do_check_eq(result.data.length, samples + 1);
+      do_check_eq(result.data[0].rxBytes, null);
+      do_check_eq(result.data[1].txBytes, 500);
+      do_check_eq(result.data[2].rxBytes, 1000);
+      run_next_test();
+    }, 1008, true, serviceType, networks[1], start, end);
+  });
+});
+
+// Test case for find samples with browsingTrafficOnly option.
+add_test(function test_findAppTrafficStats() {
+  var networks = getNetworks();
+  var networkWifi = [networks[0].id, networks[0].type];
+  var networkMobile = [networks[1].id, networks[1].type];
+  var serviceType = "";
+  var samples = 5;
+  var sampleRate = netStatsDb.sampleRate;
+  var start = Date.now();
+  var end = new Date(start + (sampleRate * (samples - 1)));
+  var saveDate = filterTimestamp(new Date());
+  start = new Date(start - sampleRate);
+  var stats = [];
+
+  for (var i = 0; i < samples; i++) {
+    // System app.
+    stats.push({ appId: 1008, isInBrowser: 0,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 200, txBytes: 100});
+    // Browser of system app.
+    stats.push({ appId: 1008, isInBrowser: 1,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 1000, txBytes: 500});
+    // Another app.
+    stats.push({ appId: 1021, isInBrowser: 0,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 300, txBytes: 150});
+    // Browser of another app.
+    stats.push({ appId: 1021, isInBrowser: 1,
+                 serviceType: serviceType, network: networkMobile,
+                 timestamp: saveDate + (sampleRate * i),
+                 rxBytes: 600, txBytes: 300});
+  }
+
+  prepareFind(stats, function(error, result) {
+    do_check_eq(error, null);
+    netStatsDb.find(function(error, result) {
+      do_check_eq(error, null);
+      do_check_eq(result.browsingTrafficOnly, false);
+      do_check_eq(result.serviceType, serviceType);
+      do_check_eq(result.network.id, networks[1].id);
+      do_check_eq(result.network.type, networks[1].type);
+      do_check_eq(result.start.getTime(), start.getTime());
+      do_check_eq(result.end.getTime(), end.getTime());
+      do_check_eq(result.data.length, samples + 1);
+      do_check_eq(result.data[0].rxBytes, null);
+      do_check_eq(result.data[1].txBytes, 600);
+      do_check_eq(result.data[2].rxBytes, 1200);
+      run_next_test();
+    }, 1008, false, serviceType, networks[1], start, end);
+  });
+});
+
 var networkWifi = '00';
 var networkMobile = '11';
 
 var examplePageURL = "http://example.com/index.html";
 var exampleManifestURL = "http://example.com/manifest.webapp";
 
 var testPageURL = "http://test.com/index.html";
 var testManifestURL = "http://test.com/manifest.webapp";
--- a/dom/network/tests/unit_stats/test_networkstats_service_proxy.js
+++ b/dom/network/tests/unit_stats/test_networkstats_service_proxy.js
@@ -43,26 +43,27 @@ add_test(function test_saveAppStats() {
   var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
   var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
 
   // Insert fake mobile network interface in NetworkStatsService
   var mobileNetId = NetworkStatsService.getNetworkId(mobile.id, mobile.type);
 
   do_check_eq(Object.keys(cachedStats).length, 0);
 
-  nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false,
+  nssProxy.saveAppStats(1, false, wifi, timestamp, 10, 20, false,
                         function (success, message) {
     do_check_eq(success, true);
-    nssProxy.saveAppStats(1, mobile, timestamp, 10, 20, false,
+    nssProxy.saveAppStats(1, false, mobile, timestamp, 10, 20, false,
                           function (success, message) {
-      var key1 = 1 + "" + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
-      var key2 = 1 + "" + mobileNetId + "";
+      var key1 = 1 + "" + false + "" + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
+      var key2 = 1 + "" + false + "" + mobileNetId + "";
 
       do_check_eq(Object.keys(cachedStats).length, 2);
       do_check_eq(cachedStats[key1].appId, 1);
+      do_check_eq(cachedStats[key1].isInBrowser, false);
       do_check_eq(cachedStats[key1].serviceType.length, 0);
       do_check_eq(cachedStats[key1].networkId, wifi.id);
       do_check_eq(cachedStats[key1].networkType, wifi.type);
       do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
                   Math.floor(timestamp / 1000));
       do_check_eq(cachedStats[key1].rxBytes, 10);
       do_check_eq(cachedStats[key1].txBytes, 20);
       do_check_eq(cachedStats[key2].appId, 1);
@@ -99,22 +100,23 @@ add_test(function test_saveServiceStats(
 
     var serviceType = 'FakeType';
     nssProxy.saveServiceStats(serviceType, wifi, timestamp, 10, 20, false,
                               function (success, message) {
       do_check_eq(success, true);
       nssProxy.saveServiceStats(serviceType, mobile, timestamp, 10, 20, false,
                                 function (success, message) {
         do_check_eq(success, true);
-        var key1 = 0 + "" + serviceType +
+        var key1 = 0 + "" + false + "" + serviceType +
                    NetworkStatsService.getNetworkId(wifi.id, wifi.type);
-        var key2 = 0 + "" + serviceType + mobileNetId + "";
+        var key2 = 0 + "" + false + "" + serviceType + mobileNetId + "";
 
         do_check_eq(Object.keys(cachedStats).length, 2);
         do_check_eq(cachedStats[key1].appId, 0);
+        do_check_eq(cachedStats[key1].isInBrowser, false);
         do_check_eq(cachedStats[key1].serviceType, serviceType);
         do_check_eq(cachedStats[key1].networkId, wifi.id);
         do_check_eq(cachedStats[key1].networkType, wifi.type);
         do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
                     Math.floor(timestamp / 1000));
         do_check_eq(cachedStats[key1].rxBytes, 10);
         do_check_eq(cachedStats[key1].txBytes, 20);
         do_check_eq(cachedStats[key2].appId, 0);
@@ -137,28 +139,29 @@ add_test(function test_saveStatsWithDiff
   var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
 
   var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
 
   NetworkStatsService.updateCachedStats(function (success, message) {
     do_check_eq(success, true);
 
     do_check_eq(Object.keys(NetworkStatsService.cachedStats).length, 0);
-    nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20, false,
+    nssProxy.saveAppStats(1, false, mobile, today.getTime(), 10, 20, false,
                           function (success, message) {
       do_check_eq(success, true);
-      nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, false,
+      nssProxy.saveAppStats(2, false, mobile, tomorrow.getTime(), 30, 40, false,
                             function (success, message) {
         do_check_eq(success, true);
 
         var cachedStats = NetworkStatsService.cachedStats;
-        var key = 2 + "" +
+        var key = 2 + "" + false + "" +
                   NetworkStatsService.getNetworkId(mobile.id, mobile.type);
         do_check_eq(Object.keys(cachedStats).length, 1);
         do_check_eq(cachedStats[key].appId, 2);
+        do_check_eq(cachedStats[key].isInBrowser, false);
         do_check_eq(cachedStats[key].networkId, mobile.id);
         do_check_eq(cachedStats[key].networkType, mobile.type);
         do_check_eq(new Date(cachedStats[key].date).getTime() / 1000,
                     Math.floor(tomorrow.getTime() / 1000));
         do_check_eq(cachedStats[key].rxBytes, 30);
         do_check_eq(cachedStats[key].txBytes, 40);
 
         run_next_test();
@@ -172,21 +175,21 @@ add_test(function test_saveStatsWithMaxC
   var maxtraffic = NetworkStatsService.maxCachedTraffic;
   var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
 
   NetworkStatsService.updateCachedStats(function (success, message) {
     do_check_eq(success, true);
 
     var cachedStats = NetworkStatsService.cachedStats;
     do_check_eq(Object.keys(cachedStats).length, 0);
-    nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false,
+    nssProxy.saveAppStats(1, false, wifi, timestamp, 10, 20, false,
                           function (success, message) {
       do_check_eq(success, true);
       do_check_eq(Object.keys(cachedStats).length, 1);
-      nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20, false,
+      nssProxy.saveAppStats(1, false, wifi, timestamp, maxtraffic, 20, false,
                             function (success, message) {
         do_check_eq(success, true);
         do_check_eq(Object.keys(cachedStats).length, 0);
 
         run_next_test();
       });
     });
   });
@@ -202,29 +205,29 @@ add_test(function test_saveAppStats() {
   var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
   var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
 
   // Insert fake mobile network interface in NetworkStatsService
   var mobileNetId = NetworkStatsService.getNetworkId(mobile.id, mobile.type);
 
   do_check_eq(Object.keys(cachedStats).length, 0);
 
-  nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false, { notify:
+  nssProxy.saveAppStats(1, false, wifi, timestamp, 10, 20, false, { notify:
                         function (success, message) {
     do_check_eq(success, true);
     var iterations = 10;
     var counter = 0;
     var callback = function (success, message) {
       if (counter == iterations - 1)
         run_next_test();
       counter++;
     };
 
     for (var i = 0; i < iterations; i++) {
-      nssProxy.saveAppStats(1, mobile, timestamp, 10, 20, false, callback);
+      nssProxy.saveAppStats(1, false, mobile, timestamp, 10, 20, false, callback);
     }
   }});
 });
 
 function run_test() {
   do_get_profile();
 
   Cu.import("resource://gre/modules/NetworkStatsService.jsm");