Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 'use strict'; | |
| 2 | |
| 3 var test_text_data = 'Test text data.'; | |
| 4 var test_number_data = 42; | |
| 5 var test_json_data = {level: 1, score: 100, label: 'Game'}; | |
| 6 var test_url_data = 'https://w3c.github.io/web-nfc/'; | |
| 7 var test_message_origin = 'https://127.0.0.1:8443'; | |
| 8 var test_buffer_data = new ArrayBuffer(test_text_data.length); | |
| 9 var test_buffer_view = new Uint8Array(test_buffer_data); | |
| 10 stringToArrayBuffer(test_text_data, test_buffer_view); | |
| 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 stringToArrayBuffer(string, buffer) { | |
| 18 for (let i = 0; i < string.length; i++) | |
| 19 buffer[i] = string.charCodeAt(i); | |
|
kenneth.christiansen
2016/04/14 14:29:27
can't you use TextEncoder instead?
new TextEncode
shalamov
2016/04/15 07:56:39
Done.
| |
| 20 } | |
| 21 | |
| 22 function createMessage(records) { | |
| 23 if (records !== undefined) { | |
| 24 let message = {} | |
| 25 message.data = records; | |
| 26 return message; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 function createRecord(recordType, mediaType, data) { | |
| 31 let record = {}; | |
| 32 if (recordType !== undefined) | |
| 33 record.recordType = recordType; | |
| 34 if (mediaType !== undefined) | |
| 35 record.mediaType = mediaType; | |
| 36 if (data !== undefined) | |
| 37 record.data = data; | |
| 38 return record; | |
| 39 } | |
| 40 | |
| 41 function createNFCPushOptions(target, timeout, ignoreRead) { | |
| 42 return { target: target, timeout: timeout, ignoreRead: ignoreRead }; | |
| 43 } | |
| 44 | |
| 45 function createTextRecord(text) { | |
| 46 return createRecord('text', 'text/plain', text); | |
| 47 } | |
| 48 | |
| 49 function createJsonRecord(json) { | |
| 50 return createRecord('json', 'application/json', json); | |
| 51 } | |
| 52 | |
| 53 function createOpaqueRecord(buffer) { | |
| 54 return createRecord('opaque', 'application/octet-stream', buffer); | |
| 55 } | |
| 56 | |
| 57 function createUrlRecord(url) { | |
| 58 return createRecord('url', 'text/plain', url); | |
| 59 } | |
| 60 | |
| 61 function nfc_mocks(mojo) { | |
| 62 return define('NFC mocks', [ | |
| 63 'device/nfc/nfc.mojom', | |
| 64 ], nfc => { | |
| 65 | |
| 66 function toMojoNFCRecordType(type) { | |
| 67 if (type === 'text') | |
|
kenneth.christiansen
2016/04/14 14:29:27
switch() on type
shalamov
2016/04/15 07:56:39
Done.
| |
| 68 return nfc.NFCRecordType.TEXT; | |
| 69 if (type === 'url') | |
| 70 return nfc.NFCRecordType.URL; | |
| 71 if (type === 'json') | |
| 72 return nfc.NFCRecordType.JSON; | |
| 73 if (type === 'opaque') | |
| 74 return nfc.NFCRecordType.OPAQUE_RECORD; | |
| 75 return nfc.NFCRecordType.EMPTY; | |
| 76 } | |
| 77 | |
| 78 function toMojoNFCPushTarget(target) { | |
| 79 if (target === 'any') | |
| 80 return nfc.NFCPushTarget.ANY; | |
| 81 if (target === 'peer') | |
| 82 return nfc.NFCPushTarget.PEER; | |
| 83 if (target === 'tag') | |
| 84 return nfc.NFCPushTarget.TAG; | |
| 85 return nfc.NFCPushTarget.ANY; | |
| 86 } | |
| 87 | |
| 88 function toByteArray(data) { | |
| 89 // Convert JS objects to byte array | |
| 90 let byteArray; | |
| 91 let tmpData = data; | |
| 92 | |
| 93 if (tmpData instanceof ArrayBuffer) | |
| 94 byteArray = new Uint8Array(tmpData); | |
| 95 else if (typeof tmpData === 'object' || typeof tmpData === 'number') | |
| 96 tmpData = JSON.stringify(tmpData); | |
| 97 | |
| 98 if (typeof tmpData === 'string') { | |
| 99 byteArray = new Uint8Array(tmpData.length); | |
|
kenneth.christiansen
2016/04/14 14:29:27
TextEncoder seems better here :)
shalamov
2016/04/15 07:56:39
Done.
| |
| 100 stringToArrayBuffer(tmpData, byteArray); | |
| 101 } | |
| 102 | |
| 103 return byteArray; | |
| 104 } | |
| 105 | |
| 106 // Compares NFCMessage that was provided to the API | |
| 107 // (e.g. navigator.nfc.push), and NFCMessage that was received by the | |
| 108 // mock NFC service. | |
| 109 function assertNFCMessagesEqual(providedMessage, receivedMessage) { | |
| 110 // If simple data type is passed, e.g. String or ArrayBuffer, convert it | |
| 111 // to NFCMessage before comparing. | |
| 112 // https://w3c.github.io/web-nfc/#idl-def-nfcpushmessage | |
| 113 let provided = providedMessage; | |
| 114 if (providedMessage instanceof ArrayBuffer) | |
| 115 provided = createMessage([createOpaqueRecord(providedMessage)]); | |
| 116 else if (typeof providedMessage === 'string') | |
| 117 provided = createMessage([createTextRecord(providedMessage)]); | |
| 118 | |
| 119 assert_equals(provided.data.length, receivedMessage.data.length, | |
| 120 'NFCMessages must have same number of NFCRecords'); | |
| 121 | |
| 122 // Compare contents of each individual NFCRecord | |
| 123 for (let i = 0; i < provided.data.length; ++i) | |
| 124 compareNFCRecords(provided.data[i], receivedMessage.data[i]); | |
| 125 } | |
| 126 | |
| 127 // Compares NFCRecords that were provided / received by the mock service. | |
| 128 function compareNFCRecords(providedRecord, receivedRecord) { | |
| 129 assert_equals(toMojoNFCRecordType(providedRecord.recordType), | |
| 130 receivedRecord.recordType); | |
| 131 assert_equals(providedRecord.mediaType, receivedRecord.mediaType); | |
| 132 | |
| 133 assert_false(toMojoNFCRecordType(providedRecord.recordType) == | |
| 134 nfc.NFCRecordType.EMPTY); | |
| 135 | |
| 136 assert_array_equals(toByteArray(providedRecord.data), | |
| 137 new Uint8Array(receivedRecord.data)); | |
| 138 } | |
| 139 | |
| 140 // Compares NFCPushOptions structures that were provided to API and | |
| 141 // received by the mock mojo service. | |
| 142 function assertNFCPushOptionsEqual(provided, received) { | |
| 143 if(provided.ignoreRead !== undefined) | |
| 144 assert_equals(provided.ignoreRead, !!+received.ignoreRead); | |
| 145 else | |
| 146 assert_equals(!!+received.ignoreRead, true); | |
| 147 | |
| 148 if(provided.timeout !== undefined) | |
| 149 assert_equals(provided.timeout, received.timeout); | |
| 150 else | |
| 151 assert_equals(received.timeout, Infinity); | |
| 152 | |
| 153 if(provided.target !== undefined) | |
| 154 assert_equals(toMojoNFCPushTarget(provided.target), received.target); | |
| 155 else | |
| 156 assert_equals(received.target, nfc.NFCPushTarget.ANY); | |
| 157 } | |
| 158 | |
| 159 function createNFCError(type) { | |
| 160 return { error: type ? | |
| 161 new nfc.NFCError({ error_type: type }) : null }; | |
| 162 } | |
| 163 | |
| 164 class MockNFC extends nfc.NFC.stubClass { | |
| 165 constructor() { | |
| 166 super(); | |
| 167 this.router_ = null; | |
| 168 this.hw_status_ = NFCHWStatus.ENABLED; | |
| 169 this.pushed_message_ = null; | |
| 170 this.push_options_ = null; | |
| 171 this.pending_promise_func_ = null; | |
| 172 this.push_timeout_id_ = null; | |
| 173 this.push_completed_ = true; | |
| 174 } | |
| 175 | |
| 176 // NFC.stubClass delegate functions | |
| 177 push(message, options) { | |
| 178 let error = this.isReady(); | |
| 179 if (error) | |
| 180 return Promise.resolve(error); | |
| 181 | |
| 182 this.pushed_message_ = message; | |
| 183 this.push_options_ = options; | |
| 184 | |
| 185 return new Promise((resolve, reject) => { | |
| 186 this.pending_promise_func_ = resolve; | |
| 187 if (options.timeout && options.timeout !== Infinity | |
| 188 && !this.push_completed_) { | |
| 189 this.push_timeout_id_ = | |
| 190 window.setTimeout(() => { | |
| 191 resolve(createNFCError(nfc.NFCErrorType.TIMER_EXPIRED)); | |
| 192 }, options.timeout); | |
| 193 } else { | |
| 194 resolve(createNFCError(null)); | |
| 195 } | |
| 196 }); | |
| 197 } | |
| 198 | |
| 199 cancelPush(target) { | |
| 200 if (this.push_options_ && ((target === nfc.NFCPushTarget.ANY) | |
| 201 || (this.push_options_.target === target))) { | |
| 202 this.cancelPendingPushOperation(); | |
| 203 } | |
| 204 | |
| 205 return Promise.resolve(createNFCError(null)); | |
| 206 } | |
| 207 | |
| 208 // Mock utility functions | |
| 209 bindToPipe(pipe) { | |
| 210 assert_equals(this.router_, null); | |
| 211 this.router_ = new mojo.router.Router(pipe); | |
| 212 this.router_.setIncomingReceiver(this); | |
| 213 } | |
| 214 | |
| 215 isReady() { | |
| 216 if (this.hw_status_ === NFCHWStatus.DISABLED) | |
| 217 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); | |
| 218 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) | |
| 219 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); | |
| 220 return null; | |
| 221 } | |
| 222 | |
| 223 setHWStatus(status) { | |
| 224 this.hw_status_ = status; | |
| 225 } | |
| 226 | |
| 227 pushedMessage() { | |
| 228 return this.pushed_message_; | |
| 229 } | |
| 230 | |
| 231 pushOptions() { | |
| 232 return this.push_options_; | |
| 233 } | |
| 234 | |
| 235 setPendingPushCompleted(result) { | |
| 236 this.push_completed_ = result; | |
| 237 } | |
| 238 | |
| 239 reset() { | |
| 240 this.hw_status_ = NFCHWStatus.ENABLED; | |
| 241 this.push_completed_ = true; | |
| 242 this.cancelPendingPushOperation(); | |
| 243 if (this.push_timeout_id_) | |
| 244 window.clearTimeout(this.push_timeout_id_); | |
| 245 } | |
| 246 | |
| 247 cancelPendingPushOperation() { | |
| 248 if (this.pending_promise_func_) | |
| 249 this.pending_promise_func_(createNFCError(nfc.NFCErrorType.OPERATION_C ANCELLED)); | |
| 250 this.pushed_message_ = null; | |
| 251 this.push_options_ = null; | |
| 252 this.pending_promise_func_ = null; | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 let mockNFC = new MockNFC; | |
| 257 mojo.frameServiceRegistry.addServiceOverrideForTesting( | |
| 258 nfc.NFC.name, | |
| 259 pipe => { | |
| 260 mockNFC.bindToPipe(pipe); | |
| 261 }); | |
| 262 | |
| 263 return Promise.resolve({ | |
| 264 mockNFC: mockNFC, | |
| 265 assertNFCMessagesEqual: assertNFCMessagesEqual, | |
| 266 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, | |
| 267 }); | |
| 268 }); | |
| 269 } | |
| 270 | |
| 271 function nfc_test(func, name, properties) { | |
| 272 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { | |
| 273 let result = Promise.resolve(func(nfc)); | |
| 274 let cleanUp = () => nfc.mockNFC.reset(); | |
| 275 result.then(cleanUp, cleanUp); | |
| 276 return result; | |
| 277 }), name, properties); | |
| 278 } | |
| OLD | NEW |