community-cordova-plugin-nfc 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/tests.js ADDED
@@ -0,0 +1,135 @@
1
+ exports.defineAutoTests = function () {
2
+
3
+ describe('NFC object', function () {
4
+ it("nfc should exist", function () {
5
+ expect(nfc).toBeDefined();
6
+ });
7
+
8
+ it("should contain a addNdefListener function", function () {
9
+ expect(typeof nfc.addNdefListener).toBeDefined();
10
+ expect(typeof nfc.addNdefListener).toBe("function");
11
+ });
12
+
13
+ it("should contain a readerMode function", function () {
14
+ expect(typeof nfc.readerMode).toBeDefined();
15
+ expect(typeof nfc.readerMode).toBe("function");
16
+ });
17
+
18
+ it("should contain a transceive function", function () {
19
+ expect(typeof nfc.transceive).toBeDefined();
20
+ expect(typeof nfc.transceive).toBe("function");
21
+ });
22
+ });
23
+
24
+ describe('UTF-8 Encoding and Decoding', function() {
25
+ // https://github.com/don/ndef-js/blob/master/test/util.js
26
+
27
+ it('should encode UTF-8', function() {
28
+
29
+ var bytes=[ 0x54, 0x65, 0x73, 0x74, 0x73, 0xd7, 0x90, 0xc2, 0xa2];
30
+
31
+ var encoded = util.stringToBytes("Testsא¢");
32
+ expect(encoded).toEqual(bytes);
33
+
34
+ });
35
+
36
+ it('should decode UTF-8', function() {
37
+
38
+ var bytes=[ 0x54, 0x65, 0x73, 0x74, 0x73, 0xd7, 0x90, 0xc2, 0xa2];
39
+
40
+ var decoded = util.bytesToString(bytes);
41
+ expect(decoded).toEqual("Testsא¢");
42
+
43
+ });
44
+
45
+ it('should encode and decode Russian', function() {
46
+
47
+ // http://www.columbia.edu/~kermit/utf8.html
48
+ var russian = "На берегу пустынных волн";
49
+ var russianBytes = [ 0xD0, 0x9D, 0xD0, 0xB0, 0x20, 0xD0, 0xB1, 0xD0, 0xB5, 0xD1, 0x80, 0xD0, 0xB5,
50
+ 0xD0, 0xB3, 0xD1, 0x83, 0x20, 0xD0, 0xBF, 0xD1, 0x83, 0xD1, 0x81, 0xD1, 0x82, 0xD1, 0x8B, 0xD0,
51
+ 0xBD, 0xD0, 0xBD, 0xD1, 0x8B, 0xD1, 0x85, 0x20, 0xD0, 0xB2, 0xD0, 0xBE, 0xD0, 0xBB, 0xD0, 0xBD ];
52
+
53
+ var encoded = util.stringToBytes(russian);
54
+ expect(encoded).toEqual(russianBytes);
55
+
56
+
57
+ var decoded = util.bytesToString(russianBytes);
58
+ expect(decoded).toEqual(russian);
59
+
60
+ });
61
+
62
+ it('should round trip encode and decode UTF-8', function() {
63
+
64
+ // http://www.columbia.edu/~kermit/utf8.html
65
+ var chinese = "我能吞下玻璃而不伤身体。";
66
+ var roundTrip = util.bytesToString(util.stringToBytes(chinese));
67
+ expect(roundTrip).toEqual(chinese);
68
+
69
+ var korean = "나는 유리를 먹을 수 있어요. 그래도 아프지 않아요";
70
+ roundTrip = util.bytesToString(util.stringToBytes(korean));
71
+ expect(roundTrip).toEqual(korean);
72
+
73
+ var url = "http://example.com/with-utf8-✓";
74
+ roundTrip = util.bytesToString(util.stringToBytes(url));
75
+ expect(roundTrip).toEqual(url);
76
+
77
+ });
78
+
79
+ });
80
+
81
+ describe('Translate ArrayBuffer and HEX Strings', function() {
82
+ it('should convert array buffer to hex string', function() {
83
+ var source = new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF]).buffer;
84
+ var hexString = util.arrayBufferToHexString(source);
85
+ expect(hexString).toEqual('000102fdfeff');
86
+ });
87
+
88
+ it('should convert hex string to array buffer', function() {
89
+
90
+ var hexString = '000102FDFEFF'
91
+ var buffer = util.hexStringToArrayBuffer(hexString);
92
+ expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF]));
93
+
94
+ // leading 0
95
+ var buffer = util.hexStringToArrayBuffer('0x68656c6c6f2c20776f726c64');
96
+ expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));
97
+
98
+ // space delimiter
99
+ var buffer = util.hexStringToArrayBuffer('68 65 6c 6c 6f 2c 20 77 6f 72 6c 64');
100
+ expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));
101
+
102
+ // : delimiter
103
+ var buffer = util.hexStringToArrayBuffer('68:65:6c:6c:6f:2c:20:77:6f:72:6c:64');
104
+ expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));
105
+
106
+ // - delimiter
107
+ var buffer = util.hexStringToArrayBuffer('68-65-6c-6c-6f-2c-20-77-6f-72-6c-64');
108
+ expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));
109
+
110
+ });
111
+ });
112
+
113
+ describe('nfc.transceive', function() {
114
+
115
+ it('should reject invalid input', function(done) {
116
+ var promise = nfc.transceive(42);
117
+
118
+ promise.then(
119
+ function resolve() {
120
+ throw new Error('Promise should not be resolved');
121
+ },
122
+ function reject(reason) {
123
+ expect(reason).toBe("Expecting an ArrayBuffer or String");
124
+ done();
125
+ }
126
+ )
127
+ });
128
+
129
+ });
130
+
131
+ };
132
+
133
+ exports.defineManualTests = function (contentEl, createActionButton) {
134
+ // TODO
135
+ };
@@ -0,0 +1,76 @@
1
+ /*jslint browser: true, unused: vars, quotmark: double */
2
+ /*global cordova, nfc, ndef, blackberry */
3
+
4
+ // blackberry requires the com.blackberry.invoke plugin installed
5
+
6
+ // you need to edit config.xml for your app and add an invoke-target
7
+ // <rim:invoke-target id="com.chariotsolutions.nfc.demo.reader.target">
8
+ // <type>APPLICATION</type>
9
+ // <filter>
10
+ // <action>bb.action.OPEN</action>
11
+ // <mime-type>application/vnd.rim.nfc.ndef</mime-type>
12
+ // <property value="ndef://0,ndef://1,ndef://2,ndef://3,ndef://4" var="uris" />
13
+ // </filter>
14
+ // </rim:invoke-target>
15
+
16
+ // clobber existing share function
17
+ nfc.share = function(ndefMessage, success, failure) {
18
+ "use strict";
19
+ var byteArray = ndef.encodeMessage(ndefMessage),
20
+ dataString = "",
21
+ data,
22
+ query;
23
+
24
+ for (var i=0; i< byteArray.length; ++i) {
25
+ dataString += String.fromCharCode(byteArray[i]);
26
+ }
27
+ data = btoa(dataString);
28
+
29
+ query = {
30
+ "action": "bb.action.SHARE",
31
+ "type": "application/vnd.rim.nfc.ndef",
32
+ "target": "sys.NFCViewer",
33
+ "data": data
34
+ };
35
+
36
+ // previously we used the invoke plugin, but now it encodes improperly
37
+ // blackberry.invoke.invoke(query, success, failure);
38
+
39
+ // call native invoke directly
40
+ cordova.exec(success, failure, "com.blackberry.invoke", "invoke", {request: query});
41
+ };
42
+
43
+ // clobber existing unshare function
44
+ nfc.unshare = function(success, failure) {
45
+ "use strict";
46
+ blackberry.invoke.closeChildCard();
47
+ if (success) { // no idea if it worked. assume success.
48
+ success();
49
+ }
50
+ };
51
+
52
+ // clobber existing showSettings function
53
+ nfc.showSettings = function(success, failure) {
54
+ "use strict";
55
+ blackberry.invoke.invoke({ uri: "settings://nfc" }, success, failure);
56
+ }
57
+
58
+ // takes an ndefMessage from the success callback and fires a javascript event
59
+ var proxy = function(ndefMessageAsString) {
60
+ "use strict";
61
+ var ndefMessage = JSON.parse(ndefMessageAsString);
62
+ cordova.fireDocumentEvent("ndef", {
63
+ type: "ndef",
64
+ tag: {
65
+ ndefMessage: ndefMessage
66
+ }
67
+ });
68
+ };
69
+
70
+ // clobber existing addNdefListener function
71
+ nfc.addNdefListener = function (callback, success, failure) {
72
+ "use strict";
73
+ document.addEventListener("ndef", callback, false);
74
+ cordova.exec(proxy, failure, "phonegap-nfc", "registerNdef", []);
75
+ success(); // assume success
76
+ };