dom/network/tests/test_networkstats_alarms.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/network/tests/test_networkstats_alarms.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for NetworkStats alarms</title>
     1.9 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.11 +</head>
    1.12 +<body>
    1.13 +<p id="display"></p>
    1.14 +<div id="content">
    1.15 +</div>
    1.16 +<pre id="test">
    1.17 +<script type="application/javascript">
    1.18 +
    1.19 +function test() {
    1.20 +  ok(true, "Checking if no alarms are set.");
    1.21 +
    1.22 +  req = navigator.mozNetworkStats.getAllAlarms();
    1.23 +
    1.24 +  req.onsuccess = function () {
    1.25 +    ok(true, "Succeeded to get alarms.");
    1.26 +    ok(Array.isArray(req.result) && req.result.length == 0,
    1.27 +       "There are no alarms set.");
    1.28 +    next();
    1.29 +  };
    1.30 +
    1.31 +  req.onerror = function () {
    1.32 +    ok(false, "getAllAlarms() shouldn't fail!");
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +var req;
    1.37 +var index = -1;
    1.38 +
    1.39 +var wifi = {'type': 0, 'id': '0'};
    1.40 +var mobile = {'type': 1, 'id': '1'};
    1.41 +
    1.42 +var steps = [
    1.43 +  function () {
    1.44 +    ok(true, "Calling getAllAlarms() with invalid network.");
    1.45 +
    1.46 +    req = navigator.mozNetworkStats.getAllAlarms(mobile);
    1.47 +
    1.48 +    req.onsuccess = function () {
    1.49 +      ok(false, "getAllAlarms() shouldn't succeed!");
    1.50 +    };
    1.51 +
    1.52 +    req.onerror = function () {
    1.53 +      ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
    1.54 +      next();
    1.55 +    }
    1.56 +  },
    1.57 +  function () {
    1.58 +    ok(true, "Calling addAlarm() with invalid network or parameters.");
    1.59 +
    1.60 +    try {
    1.61 +      navigator.mozNetworkStats.addAlarm();
    1.62 +    } catch(ex) {
    1.63 +      ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    1.64 +         "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
    1.65 +    }
    1.66 +
    1.67 +    try {
    1.68 +      navigator.mozNetworkStats.addAlarm(100000);
    1.69 +    } catch(ex) {
    1.70 +      ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    1.71 +         "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
    1.72 +    }
    1.73 +
    1.74 +    try {
    1.75 +      navigator.mozNetworkStats.addAlarm(wifi);
    1.76 +    } catch(ex) {
    1.77 +      ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
    1.78 +         "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
    1.79 +    }
    1.80 +
    1.81 +    req = navigator.mozNetworkStats.addAlarm(mobile, -100000);
    1.82 +
    1.83 +    req.onsuccess = function () {
    1.84 +      ok(false, "addAlarm() shouldn't succeed with negative threshold.");
    1.85 +    };
    1.86 +
    1.87 +    req.onerror = function () {
    1.88 +      ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
    1.89 +      next();
    1.90 +    }
    1.91 +  },
    1.92 +  function () {
    1.93 +    ok(true, "Calling addAlarm()");
    1.94 +
    1.95 +    req = navigator.mozNetworkStats.addAlarm(wifi, 1000000);
    1.96 +
    1.97 +    req.onsuccess = function () {
    1.98 +      ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
    1.99 +      next();
   1.100 +    };
   1.101 +    req.onerror = function () {
   1.102 +      ok(false, "addAlarm() shouldn't fail.");
   1.103 +    };
   1.104 +  },
   1.105 +  function () {
   1.106 +    ok(true, "Calling getAllAlarms()");
   1.107 +
   1.108 +    req = navigator.mozNetworkStats.getAllAlarms(wifi);
   1.109 +
   1.110 +    req.onsuccess = function () {
   1.111 +      ok(req.result.length == 1, "Only one alarm");
   1.112 +      ok(req.result[0].alarmId == 1, "Get correct alarmId");
   1.113 +      next();
   1.114 +    };
   1.115 +
   1.116 +    req.onerror = function () {
   1.117 +      ok(false, "getAllAlarms() shouldn't fail.");
   1.118 +    }
   1.119 +  },
   1.120 +  function () {
   1.121 +    ok(true, "Calling removeAlarms() to remove alarms.");
   1.122 +
   1.123 +    req = navigator.mozNetworkStats.removeAlarms();
   1.124 +
   1.125 +    req.onsuccess = function () {
   1.126 +      ok(req.result, "Succeeded to remove alarms.");
   1.127 +      next();
   1.128 +    };
   1.129 +
   1.130 +    req.onerror = function () {
   1.131 +      ok(false, "removeAlarms() shouldn't fail.");
   1.132 +    }
   1.133 +  },
   1.134 +  function () {
   1.135 +    ok(true, "Checking if all alarms are removed.");
   1.136 +
   1.137 +    req = navigator.mozNetworkStats.getAllAlarms();
   1.138 +
   1.139 +    req.onsuccess = function () {
   1.140 +      ok(Array.isArray(req.result) && req.result.length == 0,
   1.141 +         "Succeeded to remove all alarms.");
   1.142 +      next();
   1.143 +    };
   1.144 +
   1.145 +    req.onerror = function () {
   1.146 +      ok(false, "getAllAlarms() shouldn't fail.");
   1.147 +    }
   1.148 +  },
   1.149 +  function () {
   1.150 +    ok(true, "all done!\n");
   1.151 +    SpecialPowers.removePermission("networkstats-manage", document);
   1.152 +    SimpleTest.finish();
   1.153 +    return;
   1.154 +  }
   1.155 +];
   1.156 +
   1.157 +function next() {
   1.158 +  index += 1;
   1.159 +  if (index >= steps.length) {
   1.160 +    ok(false, "Shouldn't get here!");
   1.161 +    return;
   1.162 +  }
   1.163 +  try {
   1.164 +    steps[index]();
   1.165 +  } catch(ex) {
   1.166 +    ok(false, "Caught exception", ex);
   1.167 +  }
   1.168 +}
   1.169 +
   1.170 +SimpleTest.waitForExplicitFinish();
   1.171 +
   1.172 +SpecialPowers.addPermission("networkstats-manage", true, document);
   1.173 +SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test);
   1.174 +
   1.175 +</script>
   1.176 +</pre>
   1.177 +</body>
   1.178 +</html>

mercurial