1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/cellbroadcast/tests/marionette/test_cellbroadcast_etws.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,268 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 10000; 1.8 + 1.9 +const CB_MESSAGE_SIZE_ETWS = 56; 1.10 + 1.11 +const CB_GSM_GEOGRAPHICAL_SCOPE_NAMES = [ 1.12 + "cell-immediate", 1.13 + "plmn", 1.14 + "location-area", 1.15 + "cell" 1.16 +]; 1.17 + 1.18 +const CB_ETWS_WARNING_TYPE_NAMES = [ 1.19 + "earthquake", 1.20 + "tsunami", 1.21 + "earthquake-tsunami", 1.22 + "test", 1.23 + "other" 1.24 +]; 1.25 + 1.26 +SpecialPowers.addPermission("cellbroadcast", true, document); 1.27 +SpecialPowers.addPermission("mobileconnection", true, document); 1.28 + 1.29 +let cbs = window.navigator.mozCellBroadcast; 1.30 +ok(cbs instanceof window.MozCellBroadcast, 1.31 + "mozCellBroadcast is instanceof " + cbs.constructor); 1.32 + 1.33 +let pendingEmulatorCmdCount = 0; 1.34 +function sendCellBroadcastMessage(pdu, callback) { 1.35 + pendingEmulatorCmdCount++; 1.36 + 1.37 + let cmd = "cbs pdu " + pdu; 1.38 + runEmulatorCmd(cmd, function(result) { 1.39 + pendingEmulatorCmdCount--; 1.40 + 1.41 + is(result[0], "OK", "Emulator response"); 1.42 + 1.43 + if (callback) { 1.44 + window.setTimeout(callback, 0); 1.45 + } 1.46 + }); 1.47 +} 1.48 + 1.49 +function buildHexStr(n, numSemiOctets) { 1.50 + let str = n.toString(16); 1.51 + ok(str.length <= numSemiOctets); 1.52 + while (str.length < numSemiOctets) { 1.53 + str = "0" + str; 1.54 + } 1.55 + return str; 1.56 +} 1.57 + 1.58 +function seq(end, begin) { 1.59 + let result = []; 1.60 + for (let i = begin || 0; i < end; i++) { 1.61 + result.push(i); 1.62 + } 1.63 + return result; 1.64 +} 1.65 + 1.66 +function repeat(func, array, oncomplete) { 1.67 + (function do_call(index) { 1.68 + let next = index < (array.length - 1) ? do_call.bind(null, index + 1) : oncomplete; 1.69 + func.apply(null, [array[index], next]); 1.70 + })(0); 1.71 +} 1.72 + 1.73 +function doTestHelper(pdu, nextTest, checkFunc) { 1.74 + cbs.addEventListener("received", function onreceived(event) { 1.75 + cbs.removeEventListener("received", onreceived); 1.76 + 1.77 + checkFunc(event.message); 1.78 + 1.79 + window.setTimeout(nextTest, 0); 1.80 + }); 1.81 + 1.82 + if (Array.isArray(pdu)) { 1.83 + repeat(sendCellBroadcastMessage, pdu); 1.84 + } else { 1.85 + sendCellBroadcastMessage(pdu); 1.86 + } 1.87 +} 1.88 + 1.89 +/** 1.90 + * Tests receiving Cell Broadcast messages, event instance type, all attributes 1.91 + * of CellBroadcastMessage exist. 1.92 + */ 1.93 +function testEtwsMessageAttributes() { 1.94 + log("Test ETWS Primary Notification message attributes"); 1.95 + 1.96 + cbs.addEventListener("received", function onreceived(event) { 1.97 + cbs.removeEventListener("received", onreceived); 1.98 + 1.99 + // Bug 838542: following check throws an exception and fails this case. 1.100 + // ok(event instanceof MozCellBroadcastEvent, 1.101 + // "event is instanceof " + event.constructor) 1.102 + ok(event, "event is valid"); 1.103 + 1.104 + let message = event.message; 1.105 + ok(message, "event.message is valid"); 1.106 + 1.107 + // Attributes other than `language` and `body` should always be assigned. 1.108 + ok(message.gsmGeographicalScope != null, "message.gsmGeographicalScope"); 1.109 + ok(message.messageCode != null, "message.messageCode"); 1.110 + ok(message.messageId != null, "message.messageId"); 1.111 + ok('language' in message, "message.language"); 1.112 + ok(message.language == null, "message.language"); 1.113 + ok('body' in message, "message.body"); 1.114 + ok(message.body == null, "message.body"); 1.115 + is(message.messageClass, "normal", "message.messageClass"); 1.116 + ok(message.timestamp != null, "message.timestamp"); 1.117 + ok(message.etws != null, "message.etws"); 1.118 + ok(message.etws.warningType != null, "message.etws.warningType"); 1.119 + ok(message.etws.emergencyUserAlert != null, 1.120 + "message.etws.emergencyUserAlert"); 1.121 + ok(message.etws.popup != null, "message.etws.popup"); 1.122 + ok(message.cdmaServiceCategory != null, "message.cdmaServiceCategory"); 1.123 + 1.124 + window.setTimeout(testReceiving_ETWS_GeographicalScope, 0); 1.125 + }); 1.126 + 1.127 + // Here we use a simple ETWS message for test. 1.128 + let pdu = buildHexStr(0, CB_MESSAGE_SIZE_ETWS * 2); // 6 octets 1.129 + sendCellBroadcastMessage(pdu); 1.130 +} 1.131 + 1.132 +function testReceiving_ETWS_GeographicalScope() { 1.133 + log("Test receiving ETWS Primary Notification - Geographical Scope"); 1.134 + 1.135 + function do_test(gs, nextTest) { 1.136 + // Here we use a simple ETWS message for test. 1.137 + let pdu = buildHexStr(((gs & 0x03) << 14), 4) 1.138 + + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 2) * 2); 1.139 + 1.140 + doTestHelper(pdu, nextTest, function(message) { 1.141 + is(message.gsmGeographicalScope, CB_GSM_GEOGRAPHICAL_SCOPE_NAMES[gs], 1.142 + "message.gsmGeographicalScope"); 1.143 + }); 1.144 + } 1.145 + 1.146 + repeat(do_test, seq(CB_GSM_GEOGRAPHICAL_SCOPE_NAMES.length), 1.147 + testReceiving_ETWS_MessageCode); 1.148 +} 1.149 + 1.150 +function testReceiving_ETWS_MessageCode() { 1.151 + log("Test receiving ETWS Primary Notification - Message Code"); 1.152 + 1.153 + // Message Code has 10 bits, and is ORed into a 16 bits 'serial' number. Here 1.154 + // we test every single bit to verify the operation doesn't go wrong. 1.155 + let messageCodesToTest = [ 1.156 + 0x000, 0x001, 0x002, 0x004, 0x008, 0x010, 0x020, 0x040, 1.157 + 0x080, 0x100, 0x200, 0x251 1.158 + ]; 1.159 + 1.160 + function do_test(messageCode, nextTest) { 1.161 + let pdu = buildHexStr(((messageCode & 0x3FF) << 4), 4) 1.162 + + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 2) * 2); 1.163 + 1.164 + doTestHelper(pdu, nextTest, function(message) { 1.165 + is(message.messageCode, messageCode, "message.messageCode"); 1.166 + }); 1.167 + } 1.168 + 1.169 + repeat(do_test, messageCodesToTest, testReceiving_ETWS_MessageId); 1.170 +} 1.171 + 1.172 +function testReceiving_ETWS_MessageId() { 1.173 + log("Test receiving ETWS Primary Notification - Message Identifier"); 1.174 + 1.175 + // Message Identifier has 16 bits, but no bitwise operation is needed. 1.176 + // Test some selected values only. 1.177 + let messageIdsToTest = [ 1.178 + 0x0000, 0x0001, 0x0010, 0x0100, 0x1000, 0x1111, 0x8888, 0x8811 1.179 + ]; 1.180 + 1.181 + function do_test(messageId, nextTest) { 1.182 + let pdu = buildHexStr(0, 4) 1.183 + + buildHexStr((messageId & 0xFFFF), 4) 1.184 + + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 4) * 2); 1.185 + 1.186 + doTestHelper(pdu, nextTest, function(message) { 1.187 + is(message.messageId, messageId, "message.messageId"); 1.188 + }); 1.189 + } 1.190 + 1.191 + repeat(do_test, messageIdsToTest, testReceiving_ETWS_Timestamp); 1.192 +} 1.193 + 1.194 +function testReceiving_ETWS_Timestamp() { 1.195 + log("Test receiving ETWS Primary Notification - Timestamp"); 1.196 + 1.197 + // Here we use a simple ETWS message for test. 1.198 + let pdu = buildHexStr(0, 12); // 6 octets 1.199 + doTestHelper(pdu, testReceiving_ETWS_WarningType, function(message) { 1.200 + // Cell Broadcast messages do not contain a timestamp field (however, ETWS 1.201 + // does). We only check the timestamp doesn't go too far (60 seconds) here. 1.202 + let msMessage = message.timestamp.getTime(); 1.203 + let msNow = Date.now(); 1.204 + ok(Math.abs(msMessage - msNow) < (1000 * 60), "message.timestamp"); 1.205 + }); 1.206 +} 1.207 + 1.208 +function testReceiving_ETWS_WarningType() { 1.209 + log("Test receiving ETWS Primary Notification - Warning Type"); 1.210 + 1.211 + // Warning Type has 7 bits, and is ORed into a 16 bits 'WarningType' field. 1.212 + // Here we test every single bit to verify the operation doesn't go wrong. 1.213 + let warningTypesToTest = [ 1.214 + 0x00, 0x01, 0x02, 0x03, 0x04, 0x08, 0x10, 0x20, 0x40, 0x41 1.215 + ]; 1.216 + 1.217 + function do_test(warningType, nextTest) { 1.218 + let pdu = buildHexStr(0, 8) 1.219 + + buildHexStr(((warningType & 0x7F) << 9), 4) 1.220 + + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 6) * 2); 1.221 + 1.222 + doTestHelper(pdu, nextTest, function(message) { 1.223 + ok(message.etws, "message.etws"); 1.224 + is(message.etws.warningType, CB_ETWS_WARNING_TYPE_NAMES[warningType], 1.225 + "message.etws.warningType"); 1.226 + }); 1.227 + } 1.228 + 1.229 + repeat(do_test, warningTypesToTest, testReceiving_ETWS_EmergencyUserAlert); 1.230 +} 1.231 + 1.232 +function doTestEmergencyUserAlert_or_Popup(name, mask, nextTest) { 1.233 + let pdu = buildHexStr(0, 8) 1.234 + + buildHexStr(mask, 4) 1.235 + + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 6) * 2); 1.236 + doTestHelper(pdu, nextTest, function(message) { 1.237 + ok(message.etws != null, "message.etws"); 1.238 + is(message.etws[name], mask != 0, "message.etws." + name); 1.239 + }); 1.240 +} 1.241 + 1.242 +function testReceiving_ETWS_EmergencyUserAlert() { 1.243 + log("Test receiving ETWS Primary Notification - Emergency User Alert"); 1.244 + 1.245 + repeat(doTestEmergencyUserAlert_or_Popup.bind(null, "emergencyUserAlert"), 1.246 + [0x100, 0x000], testReceiving_ETWS_Popup); 1.247 +} 1.248 + 1.249 +function testReceiving_ETWS_Popup() { 1.250 + log("Test receiving ETWS Primary Notification - Popup"); 1.251 + 1.252 + repeat(doTestEmergencyUserAlert_or_Popup.bind(null, "popup"), 1.253 + [0x80, 0x000], cleanUp); 1.254 +} 1.255 + 1.256 +function cleanUp() { 1.257 + if (pendingEmulatorCmdCount > 0) { 1.258 + window.setTimeout(cleanUp, 100); 1.259 + return; 1.260 + } 1.261 + 1.262 + SpecialPowers.removePermission("mobileconnection", document); 1.263 + SpecialPowers.removePermission("cellbroadcast", true, document); 1.264 + 1.265 + finish(); 1.266 +} 1.267 + 1.268 +waitFor(testEtwsMessageAttributes, function() { 1.269 + return navigator.mozMobileConnections[0].voice.connected; 1.270 +}); 1.271 +