Bug 993836 - Testcase for reading NDEF data from type 2 tag. r=tzimmermann

This commit is contained in:
Dimi Lee 2014-05-27 14:53:45 +08:00
parent ab79df0314
commit 263ab9b736
3 changed files with 84 additions and 2 deletions

View File

@ -159,5 +159,15 @@ var NfcUtils = {
buf[i] = str.charCodeAt(i);
}
return buf;
},
toUTF8: function(array) {
if (!array) {
return null;
}
let str = "";
for (var i = 0; i < array.length; i++) {
str += String.fromCharCode(array[i]);
}
return str;
}
};

View File

@ -8,5 +8,4 @@ qemu=true
[test_nfc_manager_tech_discovered.js]
[test_nfc_peer.js]
[test_nfc_peer_sendndef.js]
[test_nfc_checkP2PRegistration.js]
[test_nfc_error_messages.js]
[test_nfc_tag.js]

View File

@ -0,0 +1,73 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 30000;
MARIONETTE_HEAD_JS = "head.js";
let url = "http://www.mozilla.org";
// TODO : Get this from emulator console command.
const T2T_RE_INDEX = 2;
function activateRE(re) {
let deferred = Promise.defer();
let cmd = "nfc nci rf_intf_activated_ntf " + re;
emulator.run(cmd, function(result) {
is(result.pop(), "OK", "check activation of RE" + re);
deferred.resolve();
});
return deferred.promise;
}
function setTagData(re, flag, tnf, type, payload) {
let deferred = Promise.defer();
let cmd = "nfc tag set " + re +
" [" + flag + "," + tnf + "," + type + "," + payload + ",]";
log("Executing \'" + cmd + "\'");
emulator.run(cmd, function(result) {
is(result.pop(), "OK", "set NDEF data of tag" + re);
deferred.resolve();
});
return deferred.promise;
}
function testUrlTagDiscover() {
log("Running \'testUrlTagDiscover\'");
// TODO : Make flag value readable.
let flag = 0xd0;
let tnf = NDEF.TNF_WELL_KNOWN;
let type = "U";
let payload = url;
window.navigator.mozSetMessageHandler("nfc-manager-tech-discovered", function(msg) {
log("Received \'nfc-manager-tech-ndiscovered\'");
is(msg.type, "techDiscovered", "check for correct message type");
let index = msg.techList.indexOf("NDEF");
isnot(index, -1, "check for \'NDEF\' in tech list");
let records = msg.records;
ok(records.length > 0);
is(tnf, records[0].tnf, "check for TNF field in NDEF");
is(type, NfcUtils.toUTF8(records[0].type), "check for type field in NDEF");
is(payload, NfcUtils.toUTF8(records[0].payload), "check for payload field in NDEF");
toggleNFC(false, runNextTest);
});
toggleNFC(true, function() {
setTagData(T2T_RE_INDEX, flag, tnf, btoa(type), btoa(payload))
.then(() => activateRE(T2T_RE_INDEX));
});
}
let tests = [
testUrlTagDiscover
];
SpecialPowers.pushPermissions(
[{'type': 'nfc-manager', 'allow': true, context: document}], runTests);