mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-04 13:07:52 +00:00
Bug 1049351 - [NFC] fix testPeerShouldThrow. r=dimi
This commit is contained in:
parent
7f04f74978
commit
74cd064ae6
@ -356,7 +356,7 @@ XPCOMUtils.defineLazyGetter(this, "gMessageManager", function () {
|
||||
case "NFC:NotifyUserAcceptedP2P":
|
||||
// Notify the 'NFC_PEER_EVENT_READY' since user has acknowledged
|
||||
if (!this.isPeerReadyTarget(msg.json.appId)) {
|
||||
debug("Application ID : " + appId + " is not a registered PeerReadytarget");
|
||||
debug("Application ID : " + msg.json.appId + " is not a registered PeerReadytarget");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,6 @@
|
||||
|
||||
const Cu = SpecialPowers.Cu;
|
||||
|
||||
let pendingEmulatorCmdCount = 0;
|
||||
|
||||
let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
|
||||
let nfc = window.navigator.mozNfc;
|
||||
|
||||
@ -35,6 +33,7 @@ let emulator = (function() {
|
||||
|
||||
return {
|
||||
run: run,
|
||||
pendingCmdCount: pendingCmdCount,
|
||||
P2P_RE_INDEX_0 : 0,
|
||||
P2P_RE_INDEX_1 : 1,
|
||||
T1T_RE_INDEX : 2,
|
||||
@ -44,6 +43,39 @@ let emulator = (function() {
|
||||
};
|
||||
}());
|
||||
|
||||
let sysMsgHelper = (function() {
|
||||
function techDiscovered(msg) {
|
||||
log("system message nfc-manager-tech-discovered");
|
||||
let discovered = mDiscovered.shift();
|
||||
if (discovered) {
|
||||
discovered(msg);
|
||||
}
|
||||
}
|
||||
|
||||
function techLost(msg) {
|
||||
log("system message nfc-manager-tech-lost");
|
||||
let lost = mLost.shift();
|
||||
if (lost) {
|
||||
lost(msg);
|
||||
}
|
||||
}
|
||||
|
||||
let mDiscovered = [], mLost = [];
|
||||
window.navigator.mozSetMessageHandler("nfc-manager-tech-discovered",
|
||||
techDiscovered);
|
||||
window.navigator.mozSetMessageHandler("nfc-manager-tech-lost", techLost);
|
||||
|
||||
return {
|
||||
waitForTechDiscovered: function (discovered) {
|
||||
mDiscovered.push(discovered);
|
||||
},
|
||||
|
||||
waitForTechLost: function (lost) {
|
||||
mLost.push(lost);
|
||||
},
|
||||
};
|
||||
}());
|
||||
|
||||
let NCI = (function() {
|
||||
function activateRE(re) {
|
||||
let deferred = Promise.defer();
|
||||
@ -184,7 +216,7 @@ function cleanUp() {
|
||||
finish()
|
||||
},
|
||||
function() {
|
||||
return pendingEmulatorCmdCount === 0;
|
||||
return emulator.pendingCmdCount === 0;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -82,12 +82,22 @@ function testNfcConnectError() {
|
||||
*/
|
||||
function testNoErrorInTechMsg() {
|
||||
log('testNoErrorInTechMsg');
|
||||
|
||||
let techDiscoveredHandler = function(msg) {
|
||||
ok('Message handler for nfc-manager-tech-discovered');
|
||||
is(msg.type, 'techDiscovered');
|
||||
is(msg.errorMsg, undefined, 'Should not get error msg in tech discovered');
|
||||
|
||||
setAndFireTechLostHandler()
|
||||
.then(() => toggleNFC(false))
|
||||
.then(endTest)
|
||||
.catch(handleRejectedPromise);
|
||||
};
|
||||
|
||||
sysMsgHelper.waitForTechDiscovered(techDiscoveredHandler);
|
||||
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0))
|
||||
.then(setTechDiscoveredHandler)
|
||||
.then(setAndFireTechLostHandler)
|
||||
.then(() => toggleNFC(false))
|
||||
.then(endTest)
|
||||
.catch(handleRejectedPromise);
|
||||
}
|
||||
|
||||
@ -170,23 +180,6 @@ function connectToNFCTagExpectError(sessionToken, tech, errorMsg) {
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
function setTechDiscoveredHandler() {
|
||||
let deferred = Promise.defer();
|
||||
|
||||
let techDiscoveredHandler = function(msg) {
|
||||
ok('Message handler for nfc-manager-tech-discovered');
|
||||
is(msg.type, 'techDiscovered');
|
||||
is(msg.errorMsg, undefined, 'Should not get error msg in tech discovered');
|
||||
|
||||
window.navigator.mozSetMessageHandler('nfc-manager-tech-discovered', null);
|
||||
deferred.resolve();
|
||||
};
|
||||
|
||||
window.navigator.mozSetMessageHandler('nfc-manager-tech-discovered',
|
||||
techDiscoveredHandler);
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
function setAndFireTechLostHandler() {
|
||||
let deferred = Promise.defer();
|
||||
|
||||
@ -195,12 +188,10 @@ function setAndFireTechLostHandler() {
|
||||
is(msg.type, 'techLost');
|
||||
is(msg.errorMsg, undefined, 'Should not get error msg in tech lost');
|
||||
|
||||
window.navigator.mozSetMessageHandler('nfc-manager-tech-lost', null);
|
||||
deferred.resolve();
|
||||
};
|
||||
|
||||
window.navigator.mozSetMessageHandler('nfc-manager-tech-lost',
|
||||
techLostHandler);
|
||||
sysMsgHelper.waitForTechLost(techLostHandler);
|
||||
|
||||
// triggers tech-lost
|
||||
NCI.deactivate();
|
||||
|
@ -13,8 +13,7 @@ function handleTechnologyDiscoveredRE0(msg) {
|
||||
|
||||
function testActivateRE0() {
|
||||
log('Running \'testActivateRE0\'');
|
||||
window.navigator.mozSetMessageHandler(
|
||||
'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
@ -23,8 +22,7 @@ function testActivateRE0() {
|
||||
// DISCOVERY -> W4_ALL_DISCOVERIES -> W4_HOST_SELECT -> POLL_ACTIVE
|
||||
function testRfDiscover() {
|
||||
log('Running \'testRfDiscover\'');
|
||||
window.navigator.mozSetMessageHandler(
|
||||
'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.notifyDiscoverRE(emulator.P2P_RE_INDEX_0, NCI.MORE_NOTIFICATIONS))
|
||||
|
@ -11,31 +11,31 @@ let payload = "https://www.example.com";
|
||||
|
||||
let ndef = null;
|
||||
|
||||
function handleSnep(msg) {
|
||||
ok(msg.records != null, "msg.records should have values");
|
||||
isnot(msg.techList.indexOf("NDEF"), -1, "check for correct tech type");
|
||||
// validate received NDEF message against reference
|
||||
let ndef = [new MozNDEFRecord(tnf,
|
||||
new Uint8Array(NfcUtils.fromUTF8(type)),
|
||||
new Uint8Array(NfcUtils.fromUTF8(id)),
|
||||
new Uint8Array(NfcUtils.fromUTF8(payload)))];
|
||||
NDEF.compare(ndef, msg.records);
|
||||
toggleNFC(false).then(runNextTest);
|
||||
}
|
||||
|
||||
function handleTechnologyDiscoveredRE0(msg) {
|
||||
log("Received 'nfc-manager-tech-discovered'");
|
||||
is(msg.type, "techDiscovered", "check for correct message type");
|
||||
is(msg.techList[0], "P2P", "check for correct tech type");
|
||||
|
||||
if (msg.records) {
|
||||
isnot(msg.techList.indexOf("NDEF"), -1, "check for correct tech type");
|
||||
// validate received NDEF message against reference
|
||||
let ndef = [new MozNDEFRecord(tnf,
|
||||
new Uint8Array(NfcUtils.fromUTF8(type)),
|
||||
new Uint8Array(NfcUtils.fromUTF8(id)),
|
||||
new Uint8Array(NfcUtils.fromUTF8(payload)))];
|
||||
NDEF.compare(ndef, msg.records);
|
||||
toggleNFC(false).then(runNextTest);
|
||||
}
|
||||
sysMsgHelper.waitForTechDiscovered(handleSnep);
|
||||
SNEP.put(SNEP.SAP_NDEF, SNEP.SAP_NDEF, 0, tnf, btoa(type), btoa(id), btoa(payload));
|
||||
}
|
||||
|
||||
function testReceiveNDEF() {
|
||||
log("Running 'testReceiveNDEF'");
|
||||
window.navigator.mozSetMessageHandler(
|
||||
"nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0);
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0))
|
||||
.then(() => SNEP.put(SNEP.SAP_NDEF, SNEP.SAP_NDEF, 0, tnf, btoa(type),
|
||||
btoa(id), btoa(payload)));
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
let tests = [
|
||||
|
@ -21,10 +21,8 @@ function handleTechnologyDiscoveredRE0(msg) {
|
||||
|
||||
function testTechLost() {
|
||||
log('Running \'testTechLost\'');
|
||||
window.navigator.mozSetMessageHandler(
|
||||
'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0);
|
||||
window.navigator.mozSetMessageHandler(
|
||||
'nfc-manager-tech-lost', handleTechnologyLost);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
sysMsgHelper.waitForTechLost(handleTechnologyLost);
|
||||
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
@ -60,86 +60,110 @@ function handleTechnologyDiscoveredRE0ForP2PRegFailure(msg) {
|
||||
is(request.result, false, "check for P2P registration result");
|
||||
|
||||
nfc.onpeerready = null;
|
||||
toggleNFC(false).then(runNextTest);
|
||||
NCI.deactivate().then(() => toggleNFC(false)).then(runNextTest);
|
||||
}
|
||||
|
||||
request.onerror = function () {
|
||||
ok(false, "checkP2PRegistration failed.");
|
||||
|
||||
nfc.onpeerready = null;
|
||||
toggleNFC(false).then(runNextTest);
|
||||
NCI.deactivate().then(() => toggleNFC(false)).then(runNextTest);
|
||||
}
|
||||
}
|
||||
|
||||
function testPeerReady() {
|
||||
window.navigator.mozSetMessageHandler(
|
||||
"nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
function testCheckP2PRegFailure() {
|
||||
window.navigator.mozSetMessageHandler(
|
||||
"nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0ForP2PRegFailure);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0ForP2PRegFailure);
|
||||
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
function testPeerLostShouldNotBeCalled() {
|
||||
log("testPeerLostShouldNotBeCalled");
|
||||
nfc.onpeerlost = function () {
|
||||
ok(false, "onpeerlost shouldn't be called");
|
||||
};
|
||||
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0))
|
||||
.then(NCI.deactivate)
|
||||
.then(() => toggleNFC(false));
|
||||
let isDiscovered = false;
|
||||
sysMsgHelper.waitForTechDiscovered(function() {
|
||||
log("testPeerLostShouldNotBeCalled techDiscoverd");
|
||||
isDiscovered = true;
|
||||
NCI.deactivate();
|
||||
});
|
||||
|
||||
nfc.onpeerlost = null;
|
||||
runNextTest();
|
||||
sysMsgHelper.waitForTechLost(function() {
|
||||
log("testPeerLostShouldNotBeCalled techLost");
|
||||
// if isDiscovered is still false, means this techLost is fired from
|
||||
// previous test.
|
||||
ok(isDiscovered, "tech-discovered should be fired first");
|
||||
nfc.onpeerlost = null;
|
||||
toggleNFC(false).then(runNextTest);
|
||||
});
|
||||
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
function testPeerShouldThrow() {
|
||||
log("testPeerShouldThrow");
|
||||
let peer;
|
||||
let tnf = NDEF.TNF_WELL_KNOWN;
|
||||
let type = new Uint8Array(NfcUtils.fromUTF8("U"));
|
||||
let id = new Uint8Array(NfcUtils.fromUTF8(""));
|
||||
let payload = new Uint8Array(NfcUtils.fromUTF8(url));
|
||||
let payload = new Uint8Array(NfcUtils.fromUTF8("http://www.hi.com"));
|
||||
let ndef = [new MozNDEFRecord(tnf, type, id, payload)];
|
||||
|
||||
nfc.onpeerready = function (evt) {
|
||||
log("testPeerShouldThrow peerready");
|
||||
peer = nfc.getNFCPeer(evt.detail);
|
||||
NCI.deactivate();
|
||||
};
|
||||
|
||||
let request = nfc.checkP2PRegistration(MANIFEST_URL);
|
||||
request.onsuccess = function (evt) {
|
||||
is(request.result, true, "check for P2P registration result");
|
||||
nfc.notifyUserAcceptedP2P(MANIFEST_URL);
|
||||
}
|
||||
nfc.onpeerlost = function () {
|
||||
log("testPeerShouldThrow peerlost");
|
||||
try {
|
||||
peer.sendNDEF(ndef);
|
||||
ok(false, "sendNDEF should throw error");
|
||||
} catch (e) {
|
||||
ok(true, "Exception expected " + e);
|
||||
}
|
||||
|
||||
try {
|
||||
peer.sendFile(new Blob());
|
||||
ok(false, "sendfile should throw error");
|
||||
} catch (e) {
|
||||
ok(true, "Exception expected" + e);
|
||||
}
|
||||
|
||||
nfc.onpeerready = null;
|
||||
nfc.onpeerlost = null;
|
||||
toggleNFC(false).then(runNextTest);
|
||||
};
|
||||
|
||||
sysMsgHelper.waitForTechDiscovered(function() {
|
||||
log("testPeerShouldThrow techDiscovered");
|
||||
let request = nfc.checkP2PRegistration(MANIFEST_URL);
|
||||
request.onsuccess = function (evt) {
|
||||
nfc.notifyUserAcceptedP2P(MANIFEST_URL);
|
||||
}
|
||||
|
||||
request.onerror = function () {
|
||||
ok(false, "checkP2PRegistration failed.");
|
||||
toggleNFC(false).then(runNextTest);
|
||||
}
|
||||
});
|
||||
|
||||
toggleNFC(true)
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0))
|
||||
.then(NCI.deactivate);
|
||||
|
||||
try {
|
||||
peer.sendNDEF(ndef);
|
||||
ok(false, "sendNDEF should throw error");
|
||||
} catch (e) {
|
||||
ok(true, "Exception expected");
|
||||
}
|
||||
|
||||
try {
|
||||
peer.sendFile(new Blob());
|
||||
ok(false, "sendfile should throw error");
|
||||
} catch (e) {
|
||||
ok(true, "Exception expected");
|
||||
}
|
||||
|
||||
nfc.onpeerready = null;
|
||||
toggleNFC(false).then(runNextTest);
|
||||
.then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
function testPeerInvalidToken() {
|
||||
log("testPeerInvalidToken");
|
||||
let peer = nfc.getNFCPeer("fakeSessionToken");
|
||||
is(peer, null, "NFCPeer should be null on wrong session token");
|
||||
|
||||
@ -151,6 +175,7 @@ function testPeerInvalidToken() {
|
||||
* TODO: remove once Bug 963531 lands
|
||||
*/
|
||||
function testTagInvalidToken() {
|
||||
log("testTagInvalidToken");
|
||||
let tag = nfc.getNFCTag("fakeSessionToken");
|
||||
is(tag, null, "NFCTag should be null on wrong session token");
|
||||
|
||||
|
@ -42,8 +42,7 @@ function handleTechnologyDiscoveredRE0(msg) {
|
||||
|
||||
function testOnPeerReadyRE0() {
|
||||
log("Running \'testOnPeerReadyRE0\'");
|
||||
window.navigator.mozSetMessageHandler(
|
||||
"nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0);
|
||||
sysMsgHelper.waitForTechDiscovered(handleTechnologyDiscoveredRE0);
|
||||
toggleNFC(true).then(() => NCI.activateRE(emulator.P2P_RE_INDEX_0));
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ function testUrlTagDiscover(re) {
|
||||
let type = "U";
|
||||
let payload = url;
|
||||
|
||||
window.navigator.mozSetMessageHandler("nfc-manager-tech-discovered", function(msg) {
|
||||
sysMsgHelper.waitForTechDiscovered(function(msg) {
|
||||
log("Received \'nfc-manager-tech-ndiscovered\'");
|
||||
is(msg.type, "techDiscovered", "check for correct message type");
|
||||
let index = msg.techList.indexOf("NDEF");
|
||||
@ -38,7 +38,7 @@ function testUrlTagDiscover(re) {
|
||||
function testEmptyTagDiscover(re) {
|
||||
log("Running \'testEmptyTagDiscover\'");
|
||||
|
||||
window.navigator.mozSetMessageHandler("nfc-manager-tech-discovered", function(msg) {
|
||||
sysMsgHelper.waitForTechDiscovered(function(msg) {
|
||||
log("Received \'nfc-manager-tech-ndiscovered\'");
|
||||
is(msg.type, "techDiscovered", "check for correct message type");
|
||||
let index = msg.techList.indexOf("NDEF");
|
||||
|
Loading…
x
Reference in New Issue
Block a user