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