OLD | NEW |
(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 { |
| 171 constructor() { |
| 172 this.hw_status_ = NFCHWStatus.ENABLED; |
| 173 this.pushed_message_ = null; |
| 174 this.push_options_ = null; |
| 175 this.pending_promise_func_ = null; |
| 176 this.push_timeout_id_ = null; |
| 177 this.push_completed_ = true; |
| 178 } |
| 179 |
| 180 // NFC.stubClass delegate functions |
| 181 push(message, options) { |
| 182 let error = this.isReady(); |
| 183 if (error) |
| 184 return Promise.resolve(error); |
| 185 |
| 186 this.pushed_message_ = message; |
| 187 this.push_options_ = options; |
| 188 |
| 189 return new Promise((resolve, reject) => { |
| 190 this.pending_promise_func_ = resolve; |
| 191 if (options.timeout && options.timeout !== Infinity && |
| 192 !this.push_completed_) { |
| 193 this.push_timeout_id_ = |
| 194 window.setTimeout(() => { |
| 195 resolve(createNFCError(nfc.NFCErrorType.TIMER_EXPIRED)); |
| 196 }, options.timeout); |
| 197 } else { |
| 198 resolve(createNFCError(null)); |
| 199 } |
| 200 }); |
| 201 } |
| 202 |
| 203 cancelPush(target) { |
| 204 if (this.push_options_ && ((target === nfc.NFCPushTarget.ANY) || |
| 205 (this.push_options_.target === target))) { |
| 206 this.cancelPendingPushOperation(); |
| 207 } |
| 208 |
| 209 return Promise.resolve(createNFCError(null)); |
| 210 } |
| 211 |
| 212 // Mock utility functions |
| 213 bindToPipe(pipe) { |
| 214 this.stub_ = connection.bindHandleToStub( |
| 215 pipe, nfc.NFC); |
| 216 bindings.StubBindings(this.stub_).delegate = this; |
| 217 } |
| 218 |
| 219 isReady() { |
| 220 if (this.hw_status_ === NFCHWStatus.DISABLED) |
| 221 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); |
| 222 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) |
| 223 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); |
| 224 return null; |
| 225 } |
| 226 |
| 227 setHWStatus(status) { |
| 228 this.hw_status_ = status; |
| 229 } |
| 230 |
| 231 pushedMessage() { |
| 232 return this.pushed_message_; |
| 233 } |
| 234 |
| 235 pushOptions() { |
| 236 return this.push_options_; |
| 237 } |
| 238 |
| 239 setPendingPushCompleted(result) { |
| 240 this.push_completed_ = result; |
| 241 } |
| 242 |
| 243 reset() { |
| 244 this.hw_status_ = NFCHWStatus.ENABLED; |
| 245 this.push_completed_ = true; |
| 246 this.cancelPendingPushOperation(); |
| 247 } |
| 248 |
| 249 cancelPendingPushOperation() { |
| 250 if (this.push_timeout_id_) { |
| 251 window.clearTimeout(this.push_timeout_id_); |
| 252 } |
| 253 |
| 254 if (this.pending_promise_func_) { |
| 255 this.pending_promise_func_(createNFCError(nfc.NFCErrorType.OPERATION_C
ANCELLED)); |
| 256 } |
| 257 |
| 258 this.pushed_message_ = null; |
| 259 this.push_options_ = null; |
| 260 this.pending_promise_func_ = null; |
| 261 } |
| 262 } |
| 263 |
| 264 let mockNFC = new MockNFC; |
| 265 mojo.frameServiceRegistry.addServiceOverrideForTesting( |
| 266 nfc.NFC.name, |
| 267 pipe => { |
| 268 mockNFC.bindToPipe(pipe); |
| 269 }); |
| 270 |
| 271 return Promise.resolve({ |
| 272 mockNFC: mockNFC, |
| 273 assertNFCMessagesEqual: assertNFCMessagesEqual, |
| 274 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, |
| 275 }); |
| 276 }); |
| 277 } |
| 278 |
| 279 function nfc_test(func, name, properties) { |
| 280 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { |
| 281 let result = Promise.resolve(func(nfc)); |
| 282 let cleanUp = () => nfc.mockNFC.reset(); |
| 283 result.then(cleanUp, cleanUp); |
| 284 return result; |
| 285 }), name, properties); |
| 286 } |
OLD | NEW |