Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(723)

Side by Side Diff: third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js

Issue 1708543002: [webnfc] Implement push() method in blink nfc module. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@onionsoup_webnfc
Patch Set: Rebased, fixes for Reilly's comments. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 'use strict';
2
3 var test_text_data = 'Test text data.';
4 var test_text_byte_array = new TextEncoder('utf-8').encode(test_text_data);
5 var test_number_data = 42;
6 var test_json_data = {level: 1, score: 100, label: 'Game'};
7 var test_url_data = 'https://w3c.github.io/web-nfc/';
8 var test_message_origin = 'https://127.0.0.1:8443';
9 var test_buffer_data = new ArrayBuffer(test_text_byte_array.length);
10 var test_buffer_view = new Uint8Array(test_buffer_data).set(test_text_byte_array );
11
12 var NFCHWStatus = {};
13 NFCHWStatus.ENABLED = 1;
14 NFCHWStatus.NOT_SUPPORTED = NFCHWStatus.ENABLED + 1;
15 NFCHWStatus.DISABLED = NFCHWStatus.NOT_SUPPORTED + 1;
16
17 function createMessage(records) {
18 if (records !== undefined) {
19 let message = {}
20 message.data = records;
21 return message;
22 }
23 }
24
25 function createRecord(recordType, mediaType, data) {
26 let record = {};
27 if (recordType !== undefined)
28 record.recordType = recordType;
29 if (mediaType !== undefined)
30 record.mediaType = mediaType;
31 if (data !== undefined)
32 record.data = data;
33 return record;
34 }
35
36 function createNFCPushOptions(target, timeout, ignoreRead) {
37 return { target, timeout, ignoreRead };
38 }
39
40 function createTextRecord(text) {
41 return createRecord('text', 'text/plain', text);
42 }
43
44 function createJsonRecord(json) {
45 return createRecord('json', 'application/json', json);
46 }
47
48 function createOpaqueRecord(buffer) {
49 return createRecord('opaque', 'application/octet-stream', buffer);
50 }
51
52 function createUrlRecord(url) {
53 return createRecord('url', 'text/plain', url);
54 }
55
56 function nfc_mocks(mojo) {
57 return define('NFC mocks', [
58 'mojo/public/js/bindings',
59 'mojo/public/js/connection',
60 'device/nfc/nfc.mojom',
61 ], (bindings, connection, nfc) => {
62
63 function toMojoNFCRecordType(type) {
64 switch (type) {
65 case 'text':
66 return nfc.NFCRecordType.TEXT;
67 case 'url':
68 return nfc.NFCRecordType.URL;
69 case 'json':
70 return nfc.NFCRecordType.JSON;
71 case 'opaque':
72 return nfc.NFCRecordType.OPAQUE_RECORD;
73 }
74
75 return nfc.NFCRecordType.EMPTY;
76 }
77
78 function toMojoNFCPushTarget(target) {
79 switch (target) {
80 case 'any':
81 return nfc.NFCPushTarget.ANY;
82 case 'peer':
83 return nfc.NFCPushTarget.PEER;
84 case 'tag':
85 return nfc.NFCPushTarget.TAG;
86 }
87
88 return nfc.NFCPushTarget.ANY;
89 }
90
91 function toByteArray(data) {
92 // Convert JS objects to byte array
93 let byteArray;
94 let tmpData = data;
95
96 if (tmpData instanceof ArrayBuffer)
97 byteArray = new Uint8Array(tmpData);
98 else if (typeof tmpData === 'object' || typeof tmpData === 'number')
99 tmpData = JSON.stringify(tmpData);
100
101 if (typeof tmpData === 'string')
102 byteArray = new TextEncoder('utf-8').encode(tmpData);
103
104 return byteArray;
105 }
106
107 // Compares NFCMessage that was provided to the API
108 // (e.g. navigator.nfc.push), and NFCMessage that was received by the
109 // mock NFC service.
110 function assertNFCMessagesEqual(providedMessage, receivedMessage) {
111 // If simple data type is passed, e.g. String or ArrayBuffer, convert it
112 // to NFCMessage before comparing.
113 // https://w3c.github.io/web-nfc/#idl-def-nfcpushmessage
114 let provided = providedMessage;
115 if (providedMessage instanceof ArrayBuffer)
116 provided = createMessage([createOpaqueRecord(providedMessage)]);
117 else if (typeof providedMessage === 'string')
118 provided = createMessage([createTextRecord(providedMessage)]);
119
120 assert_equals(provided.data.length, receivedMessage.data.length,
121 'NFCMessages must have same number of NFCRecords');
122
123 // Compare contents of each individual NFCRecord
124 for (let i = 0; i < provided.data.length; ++i)
125 compareNFCRecords(provided.data[i], receivedMessage.data[i]);
126 }
127
128 // Compares NFCRecords that were provided / received by the mock service.
129 function compareNFCRecords(providedRecord, receivedRecord) {
130 assert_equals(toMojoNFCRecordType(providedRecord.recordType),
131 receivedRecord.recordType);
132
133 // Compare media types without charset.
134 // Charset should be compared when watch method is implemented, in order
135 // to check that written and read strings are equal.
136 assert_equals(providedRecord.mediaType,
137 receivedRecord.mediaType.substring(0, providedRecord.mediaType.length) );
138
139 assert_false(toMojoNFCRecordType(providedRecord.recordType) ==
140 nfc.NFCRecordType.EMPTY);
141
142 assert_array_equals(toByteArray(providedRecord.data),
143 new Uint8Array(receivedRecord.data));
144 }
145
146 // Compares NFCPushOptions structures that were provided to API and
147 // received by the mock mojo service.
148 function assertNFCPushOptionsEqual(provided, received) {
149 if (provided.ignoreRead !== undefined)
150 assert_equals(provided.ignoreRead, !!+received.ignoreRead);
151 else
152 assert_equals(!!+received.ignoreRead, true);
153
154 if (provided.timeout !== undefined)
155 assert_equals(provided.timeout, received.timeout);
156 else
157 assert_equals(received.timeout, Infinity);
158
159 if (provided.target !== undefined)
160 assert_equals(toMojoNFCPushTarget(provided.target), received.target);
161 else
162 assert_equals(received.target, nfc.NFCPushTarget.ANY);
163 }
164
165 function createNFCError(type) {
166 return { error: type ?
167 new nfc.NFCError({ error_type: type }) : null };
168 }
169
170 class MockNFC extends nfc.NFC.stubClass {
Reilly Grant (use Gerrit) 2016/05/24 19:25:39 No need to extend nfc.NFC.stubClass.
171 constructor() {
172 super();
173 this.hw_status_ = NFCHWStatus.ENABLED;
174 this.pushed_message_ = null;
175 this.push_options_ = null;
176 this.pending_promise_func_ = null;
177 this.push_timeout_id_ = null;
178 this.push_completed_ = true;
179 }
180
181 // NFC.stubClass delegate functions
182 push(message, options) {
183 let error = this.isReady();
184 if (error)
185 return Promise.resolve(error);
186
187 this.pushed_message_ = message;
188 this.push_options_ = options;
189
190 return new Promise((resolve, reject) => {
191 this.pending_promise_func_ = resolve;
192 if (options.timeout && options.timeout !== Infinity
193 && !this.push_completed_) {
Reilly Grant (use Gerrit) 2016/05/24 19:25:39 nit: put && on the end of the previous line
194 this.push_timeout_id_ =
195 window.setTimeout(() => {
196 resolve(createNFCError(nfc.NFCErrorType.TIMER_EXPIRED));
197 }, options.timeout);
198 } else {
199 resolve(createNFCError(null));
200 }
201 });
202 }
203
204 cancelPush(target) {
205 if (this.push_options_ && ((target === nfc.NFCPushTarget.ANY)
206 || (this.push_options_.target === target))) {
Reilly Grant (use Gerrit) 2016/05/24 19:25:39 nit: put || on the end of the previous line
207 this.cancelPendingPushOperation();
208 }
209
210 return Promise.resolve(createNFCError(null));
211 }
212
213 // Mock utility functions
214 bindToPipe(pipe) {
215 this.stub_ = connection.bindHandleToStub(
216 pipe, nfc.NFC);
217 bindings.StubBindings(this.stub_).delegate = this;
218 }
219
220 isReady() {
221 if (this.hw_status_ === NFCHWStatus.DISABLED)
222 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED);
223 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED)
224 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED);
225 return null;
226 }
227
228 setHWStatus(status) {
229 this.hw_status_ = status;
230 }
231
232 pushedMessage() {
233 return this.pushed_message_;
234 }
235
236 pushOptions() {
237 return this.push_options_;
238 }
239
240 setPendingPushCompleted(result) {
241 this.push_completed_ = result;
242 }
243
244 reset() {
245 this.hw_status_ = NFCHWStatus.ENABLED;
246 this.push_completed_ = true;
247 this.cancelPendingPushOperation();
248 }
249
250 cancelPendingPushOperation() {
251 if (this.push_timeout_id_) {
252 window.clearTimeout(this.push_timeout_id_);
253 }
254
255 if (this.pending_promise_func_) {
256 this.pending_promise_func_(createNFCError(nfc.NFCErrorType.OPERATION_C ANCELLED));
257 }
258
259 this.pushed_message_ = null;
260 this.push_options_ = null;
261 this.pending_promise_func_ = null;
262 }
263 }
264
265 let mockNFC = new MockNFC;
266 mojo.frameServiceRegistry.addServiceOverrideForTesting(
267 nfc.NFC.name,
268 pipe => {
269 mockNFC.bindToPipe(pipe);
270 });
271
272 return Promise.resolve({
273 mockNFC: mockNFC,
274 assertNFCMessagesEqual: assertNFCMessagesEqual,
275 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual,
276 });
277 });
278 }
279
280 function nfc_test(func, name, properties) {
281 mojo_test(mojo => nfc_mocks(mojo).then(nfc => {
282 let result = Promise.resolve(func(nfc));
283 let cleanUp = () => nfc.mockNFC.reset();
284 result.then(cleanUp, cleanUp);
285 return result;
286 }), name, properties);
287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698