dom/alarm/test/test_alarm_add_data.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 data Paramter for Alarm API</title>
michael@0 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 7 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 8 </head>
michael@0 9 <body>
michael@0 10 <p id="display"></p>
michael@0 11 <div id="content" style="display: none"></div>
michael@0 12 <pre id="test">
michael@0 13 <script type="application/javascript">
michael@0 14
michael@0 15 "use strict";
michael@0 16
michael@0 17 // Verify passing {} for the data paramter
michael@0 18 function testEmptyObject() {
michael@0 19 var tomorrow = new Date();
michael@0 20 tomorrow.setDate(tomorrow.getDate() + 1);
michael@0 21
michael@0 22 var data = {};
michael@0 23
michael@0 24 var domRequest;
michael@0 25 try {
michael@0 26 domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
michael@0 27 } catch (e) {
michael@0 28 ok(false,
michael@0 29 "Unexpected exception trying to add alarm for tomorrow for empty object test.");
michael@0 30
michael@0 31 return testEmptyList();
michael@0 32 }
michael@0 33 domRequest.onsuccess = function(e) {
michael@0 34 var alarmId = e.target.result;
michael@0 35
michael@0 36 // Confirm the alarm added has the data we requested
michael@0 37 var allReq;
michael@0 38 try {
michael@0 39 allReq = navigator.mozAlarms.getAll();
michael@0 40 } catch (e) {
michael@0 41 ok(false,
michael@0 42 "Unexpected exception trying to get all alarms for empty object test.");
michael@0 43
michael@0 44 return testEmptyList();
michael@0 45 }
michael@0 46 allReq.onsuccess = function(ev) {
michael@0 47 navigator.mozAlarms.remove(alarmId);
michael@0 48
michael@0 49 var found = false;
michael@0 50 ev.target.result.forEach(function(alarm, i, arr) {
michael@0 51 if (alarm.id == alarmId) {
michael@0 52 // Found the one we added
michael@0 53 ok(Object.keys(alarm.data).length === 0,
michael@0 54 "Empty object passed for data parameter for new alarm.");
michael@0 55
michael@0 56 found = true;
michael@0 57 }
michael@0 58 });
michael@0 59
michael@0 60 if (!found) {
michael@0 61 ok(false, "Couldn't find alarm that was added for empty object test.");
michael@0 62 }
michael@0 63
michael@0 64 testEmptyList();
michael@0 65 }
michael@0 66 allReq.onerror = function(e) {
michael@0 67 ok(false, "Unable to get all alarms for empty object test.");
michael@0 68
michael@0 69 testEmptyList();
michael@0 70 }
michael@0 71 };
michael@0 72 domRequest.onerror = function(e) {
michael@0 73 ok(false, "Unable to add alarm for tomorrow for empty object test.");
michael@0 74
michael@0 75 testEmptyList();
michael@0 76 };
michael@0 77
michael@0 78 }
michael@0 79
michael@0 80 // Verify passing [] for the data paramter
michael@0 81 function testEmptyList() {
michael@0 82 var tomorrow = new Date();
michael@0 83 tomorrow.setDate(tomorrow.getDate() + 1);
michael@0 84
michael@0 85 var data = [];
michael@0 86
michael@0 87 var domRequest;
michael@0 88 try {
michael@0 89 domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
michael@0 90 } catch (e) {
michael@0 91 ok(false,
michael@0 92 "Unexpected exception trying to add alarm for tomorrow for empty list test.");
michael@0 93
michael@0 94 return testNull();
michael@0 95 }
michael@0 96 domRequest.onsuccess = function(e) {
michael@0 97 var alarmId = e.target.result;
michael@0 98
michael@0 99 // Confirm the alarm added has the data we requested
michael@0 100 var allReq;
michael@0 101 try {
michael@0 102 allReq = navigator.mozAlarms.getAll();
michael@0 103 } catch (e) {
michael@0 104 ok(false,
michael@0 105 "Unexpected exception trying to get all alarms for empty list test.");
michael@0 106
michael@0 107 return testNull();
michael@0 108 }
michael@0 109 allReq.onsuccess = function(ev) {
michael@0 110 navigator.mozAlarms.remove(alarmId);
michael@0 111
michael@0 112 var found = false;
michael@0 113 ev.target.result.forEach(function(alarm, i, arr) {
michael@0 114 if (alarm.id == alarmId) {
michael@0 115 // Found the one we added
michael@0 116 ok(alarm.data.length === 0,
michael@0 117 "Empty list passed for data parameter for new alarm.");
michael@0 118
michael@0 119 found = true;
michael@0 120 }
michael@0 121 });
michael@0 122
michael@0 123 if (!found) {
michael@0 124 ok(false, "Couldn't find alarm that was added for empty list test.");
michael@0 125 }
michael@0 126
michael@0 127 testNull();
michael@0 128 }
michael@0 129 allReq.onerror = function(e) {
michael@0 130 ok(false, "Unable to get all alarms for empty list test.");
michael@0 131
michael@0 132 testNull();
michael@0 133 }
michael@0 134 };
michael@0 135 domRequest.onerror = function(e) {
michael@0 136 ok(false, "Unable to add alarm for tomorrow for empty list test.");
michael@0 137
michael@0 138 testNull();
michael@0 139 };
michael@0 140
michael@0 141 }
michael@0 142
michael@0 143 // Verify passing null for the data paramter
michael@0 144 function testNull() {
michael@0 145 var tomorrow = new Date();
michael@0 146 tomorrow.setDate(tomorrow.getDate() + 1);
michael@0 147
michael@0 148 var data = null;
michael@0 149
michael@0 150 var domRequest;
michael@0 151 try {
michael@0 152 domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", data);
michael@0 153 } catch (e) {
michael@0 154 ok(false,
michael@0 155 "Unexpected exception trying to add alarm for tomorrow for null test.");
michael@0 156 return SimpleTest.finish();
michael@0 157 }
michael@0 158 domRequest.onsuccess = function(e) {
michael@0 159 var alarmId = e.target.result;
michael@0 160
michael@0 161 // Confirm the alarm added has the data we requested
michael@0 162 var allReq;
michael@0 163 try {
michael@0 164 allReq = navigator.mozAlarms.getAll();
michael@0 165 } catch (e) {
michael@0 166 ok(false,
michael@0 167 "Unexpected exception trying to get all alarms for null test.");
michael@0 168 return SimpleTest.finish();
michael@0 169 }
michael@0 170 allReq.onsuccess = function(ev) {
michael@0 171 navigator.mozAlarms.remove(alarmId);
michael@0 172
michael@0 173 var found = false;
michael@0 174 ev.target.result.forEach(function(alarm, i, arr) {
michael@0 175 if (alarm.id == alarmId) {
michael@0 176 // Found the one we added
michael@0 177 ok(alarm.data === null,
michael@0 178 "Null passed for data parameter for new alarm.");
michael@0 179
michael@0 180 found = true;
michael@0 181 }
michael@0 182 });
michael@0 183
michael@0 184 if (!found) {
michael@0 185 ok(false, "Couldn't find alarm that was added for null test.");
michael@0 186 }
michael@0 187 SimpleTest.finish();
michael@0 188 }
michael@0 189 allReq.onerror = function(e) {
michael@0 190 ok(false, "Unable to get all alarms for null test.");
michael@0 191 SimpleTest.finish();
michael@0 192 }
michael@0 193 };
michael@0 194 domRequest.onerror = function(e) {
michael@0 195 ok(false, "Unable to add alarm for tomorrow for null test.");
michael@0 196 SimpleTest.finish();
michael@0 197 };
michael@0 198
michael@0 199 }
michael@0 200
michael@0 201 function startTests() {
michael@0 202
michael@0 203 SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() {
michael@0 204
michael@0 205 // Currently applicable only on FxOS
michael@0 206 if (navigator.userAgent.indexOf("Mobile") != -1 &&
michael@0 207 navigator.appVersion.indexOf("Android") == -1) {
michael@0 208
michael@0 209 testEmptyObject();
michael@0 210
michael@0 211 } else {
michael@0 212 ok(true, "mozAlarms on Firefox OS only.");
michael@0 213 SimpleTest.finish();
michael@0 214 }
michael@0 215
michael@0 216 });
michael@0 217
michael@0 218 }
michael@0 219
michael@0 220 SimpleTest.expectAssertions(0, 9);
michael@0 221 SimpleTest.waitForExplicitFinish();
michael@0 222 if (SpecialPowers.hasPermission("alarms", document)) {
michael@0 223 startTests();
michael@0 224 } else {
michael@0 225 // Add the permission and reload the page so it propogates
michael@0 226 SpecialPowers.addPermission("alarms", true, document);
michael@0 227 window.location.reload();
michael@0 228 }
michael@0 229
michael@0 230 </script>
michael@0 231 </pre>
michael@0 232 </body>
michael@0 233 </html>

mercurial