Bug 1043186 - Part 4: Add throw in MozNFCPeer. r=smaug, dimi

This commit is contained in:
Yoshi Huang 2014-07-29 11:36:58 +08:00
parent c9a2372387
commit be0bc676de
3 changed files with 58 additions and 1 deletions

View File

@ -98,6 +98,7 @@ function MozNFCPeer() {
MozNFCPeer.prototype = {
_nfcContentHelper: null,
_window: null,
_isLost: false,
initialize: function(aWindow, aSessionToken) {
this._window = aWindow;
@ -106,11 +107,19 @@ MozNFCPeer.prototype = {
// NFCPeer interface:
sendNDEF: function sendNDEF(records) {
if (this._isLost) {
throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
}
// Just forward sendNDEF to writeNDEF
return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
},
sendFile: function sendFile(blob) {
if (this._isLost) {
throw new this._window.DOMError("InvalidStateError", "NFCPeer object is invalid");
}
let data = {
"blob": blob
};
@ -119,6 +128,10 @@ MozNFCPeer.prototype = {
this.session);
},
invalidate: function invalidate() {
this._isLost = true;
},
classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"),
contractID: "@mozilla.org/nfc/NFCPeer;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
@ -277,6 +290,7 @@ mozNfc.prototype = {
}
if (this.nfcObject && (this.nfcObject.session == sessionToken)) {
this.nfcObject.invalidate();
this.nfcObject = null;
}

View File

@ -111,11 +111,52 @@ function testPeerLostShouldNotBeCalled() {
runNextTest();
}
function 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 ndef = [new MozNDEFRecord(tnf, type, id, payload)];
nfc.onpeerready = function (evt) {
peer = nfc.getNFCPeer(evt.detail);
};
let request = nfc.checkP2PRegistration(MANIFEST_URL);
request.onsuccess = function (evt) {
is(request.result, true, "check for P2P registration result");
nfc.notifyUserAcceptedP2P(MANIFEST_URL);
}
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);
}
let tests = [
testPeerReady,
testCheckP2PRegFailure,
testCheckNfcPeerObjForInvalidToken,
testPeerLostShouldNotBeCalled
testPeerLostShouldNotBeCalled,
testPeerShouldThrow
];
SpecialPowers.pushPermissions(

View File

@ -10,7 +10,9 @@
[JSImplementation="@mozilla.org/nfc/NFCPeer;1"]
interface MozNFCPeer {
[Throws]
DOMRequest sendNDEF(sequence<MozNDEFRecord> records);
[Throws]
DOMRequest sendFile(Blob blob);
};