michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 10000; michael@0: michael@0: const CB_MESSAGE_SIZE_ETWS = 56; michael@0: michael@0: const CB_GSM_GEOGRAPHICAL_SCOPE_NAMES = [ michael@0: "cell-immediate", michael@0: "plmn", michael@0: "location-area", michael@0: "cell" michael@0: ]; michael@0: michael@0: const CB_ETWS_WARNING_TYPE_NAMES = [ michael@0: "earthquake", michael@0: "tsunami", michael@0: "earthquake-tsunami", michael@0: "test", michael@0: "other" michael@0: ]; michael@0: michael@0: SpecialPowers.addPermission("cellbroadcast", true, document); michael@0: SpecialPowers.addPermission("mobileconnection", true, document); michael@0: michael@0: let cbs = window.navigator.mozCellBroadcast; michael@0: ok(cbs instanceof window.MozCellBroadcast, michael@0: "mozCellBroadcast is instanceof " + cbs.constructor); michael@0: michael@0: let pendingEmulatorCmdCount = 0; michael@0: function sendCellBroadcastMessage(pdu, callback) { michael@0: pendingEmulatorCmdCount++; michael@0: michael@0: let cmd = "cbs pdu " + pdu; michael@0: runEmulatorCmd(cmd, function(result) { michael@0: pendingEmulatorCmdCount--; michael@0: michael@0: is(result[0], "OK", "Emulator response"); michael@0: michael@0: if (callback) { michael@0: window.setTimeout(callback, 0); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: function buildHexStr(n, numSemiOctets) { michael@0: let str = n.toString(16); michael@0: ok(str.length <= numSemiOctets); michael@0: while (str.length < numSemiOctets) { michael@0: str = "0" + str; michael@0: } michael@0: return str; michael@0: } michael@0: michael@0: function seq(end, begin) { michael@0: let result = []; michael@0: for (let i = begin || 0; i < end; i++) { michael@0: result.push(i); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: function repeat(func, array, oncomplete) { michael@0: (function do_call(index) { michael@0: let next = index < (array.length - 1) ? do_call.bind(null, index + 1) : oncomplete; michael@0: func.apply(null, [array[index], next]); michael@0: })(0); michael@0: } michael@0: michael@0: function doTestHelper(pdu, nextTest, checkFunc) { michael@0: cbs.addEventListener("received", function onreceived(event) { michael@0: cbs.removeEventListener("received", onreceived); michael@0: michael@0: checkFunc(event.message); michael@0: michael@0: window.setTimeout(nextTest, 0); michael@0: }); michael@0: michael@0: if (Array.isArray(pdu)) { michael@0: repeat(sendCellBroadcastMessage, pdu); michael@0: } else { michael@0: sendCellBroadcastMessage(pdu); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Tests receiving Cell Broadcast messages, event instance type, all attributes michael@0: * of CellBroadcastMessage exist. michael@0: */ michael@0: function testEtwsMessageAttributes() { michael@0: log("Test ETWS Primary Notification message attributes"); michael@0: michael@0: cbs.addEventListener("received", function onreceived(event) { michael@0: cbs.removeEventListener("received", onreceived); michael@0: michael@0: // Bug 838542: following check throws an exception and fails this case. michael@0: // ok(event instanceof MozCellBroadcastEvent, michael@0: // "event is instanceof " + event.constructor) michael@0: ok(event, "event is valid"); michael@0: michael@0: let message = event.message; michael@0: ok(message, "event.message is valid"); michael@0: michael@0: // Attributes other than `language` and `body` should always be assigned. michael@0: ok(message.gsmGeographicalScope != null, "message.gsmGeographicalScope"); michael@0: ok(message.messageCode != null, "message.messageCode"); michael@0: ok(message.messageId != null, "message.messageId"); michael@0: ok('language' in message, "message.language"); michael@0: ok(message.language == null, "message.language"); michael@0: ok('body' in message, "message.body"); michael@0: ok(message.body == null, "message.body"); michael@0: is(message.messageClass, "normal", "message.messageClass"); michael@0: ok(message.timestamp != null, "message.timestamp"); michael@0: ok(message.etws != null, "message.etws"); michael@0: ok(message.etws.warningType != null, "message.etws.warningType"); michael@0: ok(message.etws.emergencyUserAlert != null, michael@0: "message.etws.emergencyUserAlert"); michael@0: ok(message.etws.popup != null, "message.etws.popup"); michael@0: ok(message.cdmaServiceCategory != null, "message.cdmaServiceCategory"); michael@0: michael@0: window.setTimeout(testReceiving_ETWS_GeographicalScope, 0); michael@0: }); michael@0: michael@0: // Here we use a simple ETWS message for test. michael@0: let pdu = buildHexStr(0, CB_MESSAGE_SIZE_ETWS * 2); // 6 octets michael@0: sendCellBroadcastMessage(pdu); michael@0: } michael@0: michael@0: function testReceiving_ETWS_GeographicalScope() { michael@0: log("Test receiving ETWS Primary Notification - Geographical Scope"); michael@0: michael@0: function do_test(gs, nextTest) { michael@0: // Here we use a simple ETWS message for test. michael@0: let pdu = buildHexStr(((gs & 0x03) << 14), 4) michael@0: + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 2) * 2); michael@0: michael@0: doTestHelper(pdu, nextTest, function(message) { michael@0: is(message.gsmGeographicalScope, CB_GSM_GEOGRAPHICAL_SCOPE_NAMES[gs], michael@0: "message.gsmGeographicalScope"); michael@0: }); michael@0: } michael@0: michael@0: repeat(do_test, seq(CB_GSM_GEOGRAPHICAL_SCOPE_NAMES.length), michael@0: testReceiving_ETWS_MessageCode); michael@0: } michael@0: michael@0: function testReceiving_ETWS_MessageCode() { michael@0: log("Test receiving ETWS Primary Notification - Message Code"); michael@0: michael@0: // Message Code has 10 bits, and is ORed into a 16 bits 'serial' number. Here michael@0: // we test every single bit to verify the operation doesn't go wrong. michael@0: let messageCodesToTest = [ michael@0: 0x000, 0x001, 0x002, 0x004, 0x008, 0x010, 0x020, 0x040, michael@0: 0x080, 0x100, 0x200, 0x251 michael@0: ]; michael@0: michael@0: function do_test(messageCode, nextTest) { michael@0: let pdu = buildHexStr(((messageCode & 0x3FF) << 4), 4) michael@0: + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 2) * 2); michael@0: michael@0: doTestHelper(pdu, nextTest, function(message) { michael@0: is(message.messageCode, messageCode, "message.messageCode"); michael@0: }); michael@0: } michael@0: michael@0: repeat(do_test, messageCodesToTest, testReceiving_ETWS_MessageId); michael@0: } michael@0: michael@0: function testReceiving_ETWS_MessageId() { michael@0: log("Test receiving ETWS Primary Notification - Message Identifier"); michael@0: michael@0: // Message Identifier has 16 bits, but no bitwise operation is needed. michael@0: // Test some selected values only. michael@0: let messageIdsToTest = [ michael@0: 0x0000, 0x0001, 0x0010, 0x0100, 0x1000, 0x1111, 0x8888, 0x8811 michael@0: ]; michael@0: michael@0: function do_test(messageId, nextTest) { michael@0: let pdu = buildHexStr(0, 4) michael@0: + buildHexStr((messageId & 0xFFFF), 4) michael@0: + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 4) * 2); michael@0: michael@0: doTestHelper(pdu, nextTest, function(message) { michael@0: is(message.messageId, messageId, "message.messageId"); michael@0: }); michael@0: } michael@0: michael@0: repeat(do_test, messageIdsToTest, testReceiving_ETWS_Timestamp); michael@0: } michael@0: michael@0: function testReceiving_ETWS_Timestamp() { michael@0: log("Test receiving ETWS Primary Notification - Timestamp"); michael@0: michael@0: // Here we use a simple ETWS message for test. michael@0: let pdu = buildHexStr(0, 12); // 6 octets michael@0: doTestHelper(pdu, testReceiving_ETWS_WarningType, function(message) { michael@0: // Cell Broadcast messages do not contain a timestamp field (however, ETWS michael@0: // does). We only check the timestamp doesn't go too far (60 seconds) here. michael@0: let msMessage = message.timestamp.getTime(); michael@0: let msNow = Date.now(); michael@0: ok(Math.abs(msMessage - msNow) < (1000 * 60), "message.timestamp"); michael@0: }); michael@0: } michael@0: michael@0: function testReceiving_ETWS_WarningType() { michael@0: log("Test receiving ETWS Primary Notification - Warning Type"); michael@0: michael@0: // Warning Type has 7 bits, and is ORed into a 16 bits 'WarningType' field. michael@0: // Here we test every single bit to verify the operation doesn't go wrong. michael@0: let warningTypesToTest = [ michael@0: 0x00, 0x01, 0x02, 0x03, 0x04, 0x08, 0x10, 0x20, 0x40, 0x41 michael@0: ]; michael@0: michael@0: function do_test(warningType, nextTest) { michael@0: let pdu = buildHexStr(0, 8) michael@0: + buildHexStr(((warningType & 0x7F) << 9), 4) michael@0: + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 6) * 2); michael@0: michael@0: doTestHelper(pdu, nextTest, function(message) { michael@0: ok(message.etws, "message.etws"); michael@0: is(message.etws.warningType, CB_ETWS_WARNING_TYPE_NAMES[warningType], michael@0: "message.etws.warningType"); michael@0: }); michael@0: } michael@0: michael@0: repeat(do_test, warningTypesToTest, testReceiving_ETWS_EmergencyUserAlert); michael@0: } michael@0: michael@0: function doTestEmergencyUserAlert_or_Popup(name, mask, nextTest) { michael@0: let pdu = buildHexStr(0, 8) michael@0: + buildHexStr(mask, 4) michael@0: + buildHexStr(0, (CB_MESSAGE_SIZE_ETWS - 6) * 2); michael@0: doTestHelper(pdu, nextTest, function(message) { michael@0: ok(message.etws != null, "message.etws"); michael@0: is(message.etws[name], mask != 0, "message.etws." + name); michael@0: }); michael@0: } michael@0: michael@0: function testReceiving_ETWS_EmergencyUserAlert() { michael@0: log("Test receiving ETWS Primary Notification - Emergency User Alert"); michael@0: michael@0: repeat(doTestEmergencyUserAlert_or_Popup.bind(null, "emergencyUserAlert"), michael@0: [0x100, 0x000], testReceiving_ETWS_Popup); michael@0: } michael@0: michael@0: function testReceiving_ETWS_Popup() { michael@0: log("Test receiving ETWS Primary Notification - Popup"); michael@0: michael@0: repeat(doTestEmergencyUserAlert_or_Popup.bind(null, "popup"), michael@0: [0x80, 0x000], cleanUp); michael@0: } michael@0: michael@0: function cleanUp() { michael@0: if (pendingEmulatorCmdCount > 0) { michael@0: window.setTimeout(cleanUp, 100); michael@0: return; michael@0: } michael@0: michael@0: SpecialPowers.removePermission("mobileconnection", document); michael@0: SpecialPowers.removePermission("cellbroadcast", true, document); michael@0: michael@0: finish(); michael@0: } michael@0: michael@0: waitFor(testEtwsMessageAttributes, function() { michael@0: return navigator.mozMobileConnections[0].voice.connected; michael@0: }); michael@0: