-
Notifications
You must be signed in to change notification settings - Fork 54
/
nfc_help.js
81 lines (71 loc) · 2.24 KB
/
nfc_help.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const test_text_data = "Test text data.";
const test_text_byte_array = new TextEncoder('utf-8').encode(test_text_data);
const test_number_data = 42;
const test_json_data = {level: 1, score: 100, label: 'Game'};
const test_url_data = "https://w3c.github.io/web-nfc";
const test_buffer_data = new ArrayBuffer(test_text_byte_array.length);
function noop() {};
function createMessage(records) {
if (records !== undefined) {
let message = {};
message.records = records;
return message;
}
}
function createRecord(recordType, mediaType, data) {
let record = {};
if (recordType !== undefined) {
record.recordType = recordType;
}
if (mediaType !== undefined) {
record.mediaType = mediaType;
}
if (data !== undefined) {
record.data = data;
}
return record;
}
function createTextRecord(text) {
return createRecord('text', 'text/plain', text);
}
function createJsonRecord(json) {
return createRecord('json', 'application/json', json);
}
function createOpaqueRecord(buffer) {
return createRecord('opaque', 'application/octet-stream', buffer);
}
function createUrlRecord(url) {
return createRecord('url', 'text/plain', url);
}
function assertWebNFCMessagesEqual(a, b) {
assert_equals(a.records.length, b.records.length);
for(let i in a.records) {
let recordA = a.records[i];
let recordB = b.records[i];
assert_equals(recordA.recordType, recordB.recordType);
assert_equals(recordA.mediaType, recordB.mediaType);
if (recordA.data instanceof ArrayBuffer) {
assert_array_equals(new Uint8Array(recordA.data),
new Uint8Array(recordB.data));
} else if (typeof recordA.data === 'object') {
assert_object_equals(recordA.data, recordB.data);
}
if (typeof recordA.data === 'number'
|| typeof recordA.data === 'string') {
assert_true(recordA.data == recordB.data);
}
}
}
function testNFCMessage(pushedMessage, watchOptions, desc) {
promise_test(t => {
return navigator.nfc.push(pushedMessage)
.then(() => {
return new Promise(resolve => {
navigator.nfc.watch((message) => resolve(message), watchOptions);
}).then((message) => {
assertWebNFCMessagesEqual(message, pushedMessage);
});
});
}, desc);
}