summaryrefslogtreecommitdiffstats
path: root/dom/network/tests/unit_stats/test_networkstats_service.js
blob: 8c43a9b54bfdc24a21891aba4d4142e72f446c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;

const NETWORK_STATUS_READY   = 0;
const NETWORK_STATUS_STANDBY = 1;
const NETWORK_STATUS_AWAY    = 2;

const QUEUE_TYPE_UPDATE_STATS = 0;

var wifiId = '00';

function getNetworks(callback) {
  NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
    callback(aError, aResult);
  });
}

add_test(function test_clearDB() {
  getNetworks(function onGetNetworks(error, result) {
    do_check_eq(error, null);
    var networks = result;
    networks.forEach(function(network, index) {
      networks[index] = {network: network, networkId: NetworkStatsService.getNetworkId(network.id, network.type)};
    }, this);

    NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) {
      do_check_eq(error, null);
      run_next_test();
    });
  });
});

function getNetworkId(callback) {
  getNetworks(function onGetNetworks(error, result) {
    do_check_eq(error, null);
    var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type);
    callback(null, netId);
  });
}

add_test(function test_networkStatsAvailable_ok() {
  getNetworkId(function onGetId(error, result) {
    do_check_eq(error, null);
    var netId = result;
    NetworkStatsService.networkStatsAvailable(function (success, msg) {
      do_check_eq(success, true);
      run_next_test();
    }, netId, true, 1234, 4321, Date.now());
  });
});

add_test(function test_networkStatsAvailable_failure() {
  getNetworkId(function onGetId(error, result) {
    do_check_eq(error, null);
    var netId = result;
    NetworkStatsService.networkStatsAvailable(function (success, msg) {
      do_check_eq(success, false);
      run_next_test();
    }, netId, false, 1234, 4321, Date.now());
  });
});

add_test(function test_update_invalidNetwork() {
  NetworkStatsService.update(-1, function (success, msg) {
    do_check_eq(success, false);
    do_check_eq(msg, "Invalid network -1");
    run_next_test();
  });
});

add_test(function test_update() {
  getNetworkId(function onGetId(error, result) {
    do_check_eq(error, null);
    var netId = result;
    NetworkStatsService.update(netId, function (success, msg) {
      do_check_eq(success, true);
      run_next_test();
    });
  });
});

add_test(function test_updateQueueIndex() {
  NetworkStatsService.updateQueue = [{netId: 0, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
                                     {netId: 1, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
                                     {netId: 2, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
                                     {netId: 3, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
                                     {netId: 4, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}];
  var index = NetworkStatsService.updateQueueIndex(3);
  do_check_eq(index, 3);
  index = NetworkStatsService.updateQueueIndex(10);
  do_check_eq(index, -1);

  NetworkStatsService.updateQueue = [];
  run_next_test();
});

add_test(function test_updateAllStats() {
  NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY;
  NetworkStatsService.updateAllStats(function(success, msg) {
    do_check_eq(success, true);
    NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY;
    NetworkStatsService.updateAllStats(function(success, msg) {
      do_check_eq(success, true);
      NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_AWAY;
      NetworkStatsService.updateAllStats(function(success, msg) {
        do_check_eq(success, true);
        run_next_test();
      });
    });
  });
});

add_test(function test_updateStats_ok() {
  getNetworkId(function onGetId(error, result) {
    do_check_eq(error, null);
    var netId = result;
    NetworkStatsService.updateStats(netId, function(success, msg){
      do_check_eq(success, true);
      run_next_test();
    });
  });
});

add_test(function test_updateStats_failure() {
  NetworkStatsService.updateStats(-1, function(success, msg){
    do_check_eq(success, false);
    run_next_test();
  });
});

// Define Mockup function to simulate a request to netd
function MockNetdRequest(aCallback) {
  var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  var event = {
    notify: function (timer) {
      aCallback();
    }
  };

  timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT);
}

add_test(function test_queue() {

  // Overwrite update function of NetworkStatsService to avoid netd errors due to use
  // fake interfaces. First, original function is stored to restore it at the end of the
  // test.
  var updateFunctionBackup = NetworkStatsService.update;

  NetworkStatsService.update = function update(aNetId, aCallback) {
    MockNetdRequest(function () {
      if (aCallback) {
        aCallback(true, "ok");
      }
    });
  };

  // Fill networks with fake network interfaces to enable netd async requests.
  var network = {id: "1234", type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE};
  var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
  NetworkStatsService._networks[netId1] = { network: network,
                                            interfaceName: "net1" };

  network = {id: "5678", type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE};
  var netId2 = NetworkStatsService.getNetworkId(network.id, network.type);
  NetworkStatsService._networks[netId2] = { network: network,
                                            interfaceName: "net2" };

  NetworkStatsService.updateStats(netId1);
  NetworkStatsService.updateStats(netId2);
  do_check_eq(NetworkStatsService.updateQueue.length, 2);
  do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1);

  var i = 0;
  var updateCount = 0;
  var callback = function(success, msg) {
    i++;
    if (i >= updateCount) {
      NetworkStatsService.update = updateFunctionBackup;
      run_next_test();
    }
  };

  NetworkStatsService.updateStats(netId1, callback);
  updateCount++;
  NetworkStatsService.updateStats(netId2, callback);
  updateCount++;

  do_check_eq(NetworkStatsService.updateQueue.length, 2);
  do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2);
  do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null);
  do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null);
});

add_test(function test_getAlarmQuota() {
  let alarm = { networkId: wifiId, absoluteThreshold: 10000 };

  NetworkStatsService._getAlarmQuota(alarm, function onSet(error, quota){
    do_check_eq(error, null);
    do_check_neq(quota, undefined);
    do_check_eq(alarm.absoluteThreshold, alarm.relativeThreshold);
    run_next_test();
  });
});

var testPageURL = "http://test.com";
var testManifestURL = "http://test.com/manifest.webapp";

add_test(function test_setAlarm() {
  let alarm = { id: null,
                networkId: wifiId,
                threshold: 10000,
                absoluteThreshold: null,
                alarmStart: null,
                alarmEnd: null,
                data: null,
                pageURL: testPageURL,
                manifestURL: testManifestURL };

  NetworkStatsService._setAlarm(alarm, function onSet(error, result) {
    do_check_eq(result, 1);
    run_next_test();
  });
});

add_test(function test_setAlarm_invalid_threshold() {
  let alarm = { id: null,
                networkId: wifiId,
                threshold: -10000,
                absoluteThreshold: null,
                alarmStart: null,
                alarmEnd: null,
                data: null,
                pageURL: testPageURL,
                manifestURL: testManifestURL };

  NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY;

  NetworkStatsService._setAlarm(alarm, function onSet(error, result) {
    do_check_eq(error, "InvalidStateError");
    run_next_test();
  });
});

add_test(function test_fireAlarm() {
  // Add a fake alarm into database.
  let alarm = { id: null,
                networkId: wifiId,
                threshold: 10000,
                absoluteThreshold: null,
                alarmStart: null,
                alarmEnd: null,
                data: null,
                pageURL: testPageURL,
                manifestURL: testManifestURL };

  // Set wifi status to standby to avoid connecting to netd when adding an alarm.
  NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY;

  NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) {
    NetworkStatsService._db.getAlarms(Ci.nsINetworkInfo.NETWORK_TYPE_WIFI,
                                      testManifestURL, function onGet(error, result) {
      do_check_eq(error, null);
      do_check_eq(result.length, 1);

      // Result of getAlarms is based on expected child's data format, so
      // some changes are needed to be able to use it.
      result[0].networkId = wifiId;
      result[0].pageURL = testPageURL;
      result[0].manifestURL = testManifestURL;

      NetworkStatsService._fireAlarm(result[0], false);
      NetworkStatsService._db.getAlarms(Ci.nsINetworkInfo.NETWORK_TYPE_WIFI,
                                        testManifestURL, function onGet(error, result) {
        do_check_eq(error, undefined);
        do_check_eq(result.length, 0);
        run_next_test();
      });
    });
  });
});

function run_test() {
  do_get_profile();

  Cu.import("resource://gre/modules/NetworkStatsService.jsm");
  run_next_test();
}