dom/network/tests/test_networkstats_alarms.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <meta charset="utf-8">
     5   <title>Test for NetworkStats alarms</title>
     6   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     7   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     8 </head>
     9 <body>
    10 <p id="display"></p>
    11 <div id="content">
    12 </div>
    13 <pre id="test">
    14 <script type="application/javascript">
    16 function test() {
    17   ok(true, "Checking if no alarms are set.");
    19   req = navigator.mozNetworkStats.getAllAlarms();
    21   req.onsuccess = function () {
    22     ok(true, "Succeeded to get alarms.");
    23     ok(Array.isArray(req.result) && req.result.length == 0,
    24        "There are no alarms set.");
    25     next();
    26   };
    28   req.onerror = function () {
    29     ok(false, "getAllAlarms() shouldn't fail!");
    30   }
    31 }
    33 var req;
    34 var index = -1;
    36 var wifi = {'type': 0, 'id': '0'};
    37 var mobile = {'type': 1, 'id': '1'};
    39 var steps = [
    40   function () {
    41     ok(true, "Calling getAllAlarms() with invalid network.");
    43     req = navigator.mozNetworkStats.getAllAlarms(mobile);
    45     req.onsuccess = function () {
    46       ok(false, "getAllAlarms() shouldn't succeed!");
    47     };
    49     req.onerror = function () {
    50       ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
    51       next();
    52     }
    53   },
    54   function () {
    55     ok(true, "Calling addAlarm() with invalid network or parameters.");
    57     try {
    58       navigator.mozNetworkStats.addAlarm();
    59     } catch(ex) {
    60       ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    61          "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
    62     }
    64     try {
    65       navigator.mozNetworkStats.addAlarm(100000);
    66     } catch(ex) {
    67       ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    68          "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
    69     }
    71     try {
    72       navigator.mozNetworkStats.addAlarm(wifi);
    73     } catch(ex) {
    74       ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    75          "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
    76     }
    78     req = navigator.mozNetworkStats.addAlarm(mobile, -100000);
    80     req.onsuccess = function () {
    81       ok(false, "addAlarm() shouldn't succeed with negative threshold.");
    82     };
    84     req.onerror = function () {
    85       ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
    86       next();
    87     }
    88   },
    89   function () {
    90     ok(true, "Calling addAlarm()");
    92     req = navigator.mozNetworkStats.addAlarm(wifi, 1000000);
    94     req.onsuccess = function () {
    95       ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
    96       next();
    97     };
    98     req.onerror = function () {
    99       ok(false, "addAlarm() shouldn't fail.");
   100     };
   101   },
   102   function () {
   103     ok(true, "Calling getAllAlarms()");
   105     req = navigator.mozNetworkStats.getAllAlarms(wifi);
   107     req.onsuccess = function () {
   108       ok(req.result.length == 1, "Only one alarm");
   109       ok(req.result[0].alarmId == 1, "Get correct alarmId");
   110       next();
   111     };
   113     req.onerror = function () {
   114       ok(false, "getAllAlarms() shouldn't fail.");
   115     }
   116   },
   117   function () {
   118     ok(true, "Calling removeAlarms() to remove alarms.");
   120     req = navigator.mozNetworkStats.removeAlarms();
   122     req.onsuccess = function () {
   123       ok(req.result, "Succeeded to remove alarms.");
   124       next();
   125     };
   127     req.onerror = function () {
   128       ok(false, "removeAlarms() shouldn't fail.");
   129     }
   130   },
   131   function () {
   132     ok(true, "Checking if all alarms are removed.");
   134     req = navigator.mozNetworkStats.getAllAlarms();
   136     req.onsuccess = function () {
   137       ok(Array.isArray(req.result) && req.result.length == 0,
   138          "Succeeded to remove all alarms.");
   139       next();
   140     };
   142     req.onerror = function () {
   143       ok(false, "getAllAlarms() shouldn't fail.");
   144     }
   145   },
   146   function () {
   147     ok(true, "all done!\n");
   148     SpecialPowers.removePermission("networkstats-manage", document);
   149     SimpleTest.finish();
   150     return;
   151   }
   152 ];
   154 function next() {
   155   index += 1;
   156   if (index >= steps.length) {
   157     ok(false, "Shouldn't get here!");
   158     return;
   159   }
   160   try {
   161     steps[index]();
   162   } catch(ex) {
   163     ok(false, "Caught exception", ex);
   164   }
   165 }
   167 SimpleTest.waitForExplicitFinish();
   169 SpecialPowers.addPermission("networkstats-manage", true, document);
   170 SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test);
   172 </script>
   173 </pre>
   174 </body>
   175 </html>

mercurial