diff --git a/README.md b/README.md index 7ae178e7..5a80e12e 100644 --- a/README.md +++ b/README.md @@ -1108,7 +1108,7 @@ Create a new project cordova platform add android cordova plugin add ../phonegap-nfc cordova plugin add ../phonegap-nfc/tests - cordova plugin add http://git-wip-us.apache.org/repos/asf/cordova-plugin-test-framework.git + cordova plugin add https://github.com/apache/cordova-plugin-test-framework.git Change the start page in `config.xml` diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 00000000..d2e51349 --- /dev/null +++ b/tests/package.json @@ -0,0 +1,14 @@ +{ + "name": "com.chariotsolutions.nfc.plugin.tests", + "version": "0.0.1-dev", + "description": "Tests for phonegap-nfc", + "cordova": { + "id": "phonegap-nfc-tests", + "platforms": [] + }, + "keywords": [ + "ecosystem:cordova" + ], + "author": "Don Coleman ", + "license": "MIT" +} diff --git a/tests/plugin.xml b/tests/plugin.xml index 2c6ac71c..0a75e6e8 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -1,7 +1,7 @@ PhoneGap NFC Plugin Tests Apache 2.0 diff --git a/tests/tests.js b/tests/tests.js index 09a8e35f..0328b530 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -9,6 +9,16 @@ exports.defineAutoTests = function () { expect(typeof nfc.addNdefListener).toBeDefined(); expect(typeof nfc.addNdefListener).toBe("function"); }); + + it("should contain a readerMode function", function () { + expect(typeof nfc.readerMode).toBeDefined(); + expect(typeof nfc.readerMode).toBe("function"); + }); + + it("should contain a transceive function", function () { + expect(typeof nfc.transceive).toBeDefined(); + expect(typeof nfc.transceive).toBe("function"); + }); }); describe('UTF-8 Encoding and Decoding', function() { @@ -65,8 +75,59 @@ exports.defineAutoTests = function () { expect(roundTrip).toEqual(url); }); + }); - + + describe('Translate ArrayBuffer and HEX Strings', function() { + it('should convert array buffer to hex string', function() { + var source = new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF]).buffer; + var hexString = util.arrayBufferToHexString(source); + expect(hexString).toEqual('000102fdfeff'); + }); + + it('should convert hex string to array buffer', function() { + + var hexString = '000102FDFEFF' + var buffer = util.hexStringToArrayBuffer(hexString); + expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF])); + + // leading 0 + var buffer = util.hexStringToArrayBuffer('0x68656c6c6f2c20776f726c64'); + expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64])); + + // space delimiter + var buffer = util.hexStringToArrayBuffer('68 65 6c 6c 6f 2c 20 77 6f 72 6c 64'); + expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64])); + + // : delimiter + var buffer = util.hexStringToArrayBuffer('68:65:6c:6c:6f:2c:20:77:6f:72:6c:64'); + expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64])); + + // - delimiter + var buffer = util.hexStringToArrayBuffer('68-65-6c-6c-6f-2c-20-77-6f-72-6c-64'); + expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64])); + + }); + }); + + describe('nfc.transceive', function() { + + it('should reject invalid input', function(done) { + var promise = nfc.transceive(42); + + promise.then( + function resolve() { + throw new Error('Promise should not be resolved'); + }, + function reject(reason) { + expect(reason).toBe("Expecting an ArrayBuffer or String"); + done(); + } + ) + }); + + }); + }; exports.defineManualTests = function (contentEl, createActionButton) { diff --git a/www/phonegap-nfc.js b/www/phonegap-nfc.js index 86d83ea3..d584f9b6 100644 --- a/www/phonegap-nfc.js +++ b/www/phonegap-nfc.js @@ -513,7 +513,7 @@ var nfc = { transceive: function(data) { return new Promise(function(resolve, reject) { - let buffer; + var buffer; if (typeof data === 'string') { buffer = util.hexStringToArrayBuffer(data); } else if (data instanceof ArrayBuffer) { @@ -703,9 +703,10 @@ var util = { function toHexString(byte) { return ('0' + (byte & 0xFF).toString(16)).slice(-2); } - const typedArray = new Uint8Array(buffer); + var typedArray = new Uint8Array(buffer); var array = Array.from(typedArray); // need to convert to [] so our map result is not typed - const parts = array.map(i => toHexString(i)); + var parts = array.map(function(i) { return toHexString(i) }); + return parts.join(''); }, @@ -729,16 +730,16 @@ var util = { } // check for some non-hex characters - const bad = hexString.match(/[G-Z\s]/i); + var bad = hexString.match(/[G-Z\s]/i); if (bad) { console.log('WARNING: found non-hex characters', bad); } // split the string into pairs of octets - const pairs = hexString.match(/[\dA-F]{2}/gi); + var pairs = hexString.match(/[\dA-F]{2}/gi); // convert the octets to integers - const ints = pairs.map(s => parseInt(s, 16)); + var ints = pairs.map(function(s) { return parseInt(s, 16) }); var array = new Uint8Array(ints); return array.buffer;