DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (777e60ca8853)

VCS Links

Line Code
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for NetworkStats alarms</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">

function test() {
  ok(true, "Checking if no alarms are set.");

  req = navigator.mozNetworkStats.getAllAlarms();

  req.onsuccess = function () {
    ok(true, "Succeeded to get alarms.");
    ok(Array.isArray(req.result) && req.result.length == 0,
       "There are no alarms set.");
    next();
  };

  req.onerror = function () {
    ok(false, "getAllAlarms() shouldn't fail!");
  }
}

var req;
var index = -1;

var wifi = {'type': 0, 'id': '0'};
var mobile = {'type': 1, 'id': '1'};

var steps = [
  function () {
    ok(true, "Calling getAllAlarms() with invalid network.");

    req = navigator.mozNetworkStats.getAllAlarms(mobile);

    req.onsuccess = function () {
      ok(false, "getAllAlarms() shouldn't succeed!");
    };

    req.onerror = function () {
      ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
      next();
    }
  },
  function () {
    ok(true, "Calling addAlarm() with invalid network or parameters.");

    try {
      navigator.mozNetworkStats.addAlarm();
    } catch(ex) {
      ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
         "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
    }

    try {
      navigator.mozNetworkStats.addAlarm(100000);
    } catch(ex) {
      ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
         "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
    }

    try {
      navigator.mozNetworkStats.addAlarm(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.onsuccess = function () {
      ok(false, "addAlarm() shouldn't succeed with negative threshold.");
    };

    req.onerror = function () {
      ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
      next();
    }
  },
  function () {
    ok(true, "Calling addAlarm()");

    req = navigator.mozNetworkStats.addAlarm(wifi, 1000000);

    req.onsuccess = function () {
      ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
      next();
    };
    req.onerror = function () {
      ok(false, "addAlarm() shouldn't fail.");
    };
  },
  function () {
    ok(true, "Calling getAllAlarms()");

    req = navigator.mozNetworkStats.getAllAlarms(wifi);

    req.onsuccess = function () {
      ok(req.result.length == 1, "Only one alarm");
      ok(req.result[0].alarmId == 1, "Get correct alarmId");
      next();
    };

    req.onerror = function () {
      ok(false, "getAllAlarms() shouldn't fail.");
    }
  },
  function () {
    ok(true, "Calling removeAlarms() to remove alarms.");

    req = navigator.mozNetworkStats.removeAlarms();

    req.onsuccess = function () {
      ok(req.result, "Succeeded to remove alarms.");
      next();
    };

    req.onerror = function () {
      ok(false, "removeAlarms() shouldn't fail.");
    }
  },
  function () {
    ok(true, "Checking if all alarms are removed.");

    req = navigator.mozNetworkStats.getAllAlarms();

    req.onsuccess = function () {
      ok(Array.isArray(req.result) && req.result.length == 0,
         "Succeeded to remove all alarms.");
      next();
    };

    req.onerror = function () {
      ok(false, "getAllAlarms() shouldn't fail.");
    }
  },
  function () {
    ok(true, "all done!\n");
    SpecialPowers.removePermission("networkstats-manage", document);
    SimpleTest.finish();
    return;
  }
];

function next() {
  index += 1;
  if (index >= steps.length) {
    ok(false, "Shouldn't get here!");
    return;
  }
  try {
    steps[index]();
  } catch(ex) {
    ok(false, "Caught exception", ex);
  }
}

SimpleTest.waitForExplicitFinish();

SpecialPowers.addPermission("networkstats-manage", true, document);
SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test);

</script>
</pre>
</body>
</html>