Bug 986836 - Part 5: Expose MozNetworkInterface for testing. r=bzbarsky

This commit is contained in:
John Shih 2014-04-07 18:22:21 +08:00
parent 69d0547799
commit 240f374d41
3 changed files with 27 additions and 16 deletions

View File

@ -43,19 +43,24 @@ NetworkStatsData.prototype = {
};
// NetworkStatsInterface
const NETWORKSTATSINTERFACE_CONTRACTID = "@mozilla.org/networkstatsinterface;1";
const NETWORKSTATSINTERFACE_CID = Components.ID("{f540615b-d803-43ff-8200-2a9d145a5645}");
function NetworkStatsInterface(aNetwork) {
function NetworkStatsInterface() {
if (DEBUG) {
debug("NetworkStatsInterface Constructor");
}
this.type = aNetwork.type;
this.id = aNetwork.id;
}
NetworkStatsInterface.prototype = {
__init: function(aNetwork) {
this.type = aNetwork.type;
this.id = aNetwork.id;
},
classID : NETWORKSTATSINTERFACE_CID,
contractID: NETWORKSTATSINTERFACE_CONTRACTID,
QueryInterface : XPCOMUtils.generateQI([])
}
@ -68,8 +73,7 @@ function NetworkStats(aWindow, aStats) {
}
this.appManifestURL = aStats.appManifestURL || null;
this.serviceType = aStats.serviceType || null;
this.network = aWindow.MozNetworkStatsInterface._create(
aWindow, new NetworkStatsInterface(aStats.network));
this.network = new aWindow.MozNetworkStatsInterface(aStats.network);
this.start = aStats.start ? new aWindow.Date(aStats.start.getTime()) : null;
this.end = aStats.end ? new aWindow.Date(aStats.end.getTime()) : null;
@ -91,8 +95,7 @@ const NETWORKSTATSALARM_CID = Components.ID("{a93ea13e-409c-4189-9b1e-95fff220be
function NetworkStatsAlarm(aWindow, aAlarm) {
this.alarmId = aAlarm.id;
this.network = aWindow.MozNetworkStatsInterface._create(
aWindow, new NetworkStatsInterface(aAlarm.network));
this.network = new aWindow.MozNetworkStatsInterface(aAlarm.network);
this.threshold = aAlarm.threshold;
this.data = aAlarm.data;
}
@ -307,8 +310,7 @@ NetworkStatsManager.prototype = {
let networks = new this._window.Array();
for (let i = 0; i < msg.result.length; i++) {
let network = this._window.MozNetworkStatsInterface._create(
this._window, new NetworkStatsInterface(msg.result[i]));
let network = new this._window.MozNetworkStatsInterface(msg.result[i]);
networks.push(network);
}

View File

@ -40,7 +40,8 @@ var steps = [
function () {
ok(true, "Calling getAllAlarms() with invalid network.");
req = navigator.mozNetworkStats.getAllAlarms(mobile);
req = navigator.mozNetworkStats
.getAllAlarms(new window.MozNetworkStatsInterface(mobile));
req.onsuccess = function () {
ok(false, "getAllAlarms() shouldn't succeed!");
@ -69,13 +70,14 @@ var steps = [
}
try {
navigator.mozNetworkStats.addAlarm(wifi);
navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
}
req = navigator.mozNetworkStats.addAlarm(mobile, -100000);
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(mobile), -100000);
req.onsuccess = function () {
ok(false, "addAlarm() shouldn't succeed with negative threshold.");
@ -89,7 +91,8 @@ var steps = [
function () {
ok(true, "Calling addAlarm()");
req = navigator.mozNetworkStats.addAlarm(wifi, 1000000);
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(wifi), 1000000);
req.onsuccess = function () {
ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
@ -102,7 +105,8 @@ var steps = [
function () {
ok(true, "Calling getAllAlarms()");
req = navigator.mozNetworkStats.getAllAlarms(wifi);
req = navigator.mozNetworkStats
.getAllAlarms(new window.MozNetworkStatsInterface(wifi));
req.onsuccess = function () {
ok(req.result.length == 1, "Only one alarm");

View File

@ -2,11 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
dictionary NetworkInterface {
long type;
DOMString id;
};
/**
* Represents a data interface for which the manager is recording statistics.
*/
[JSImplementation="@mozilla.org/networkstatsinterface;1",
ChromeOnly,
[Constructor(optional NetworkInterface networkinterface),
JSImplementation="@mozilla.org/networkstatsinterface;1",
Pref="dom.mozNetworkStats.enabled",
Func="Navigator::HasNetworkStatsSupport"]
interface MozNetworkStatsInterface {