Chromium Code Reviews| 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: target, timeout: timeout, ignoreRead: 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 'device/nfc/nfc.mojom', | |
| 59 ], nfc => { | |
| 60 | |
| 61 function toMojoNFCRecordType(type) { | |
| 62 switch (type) { | |
| 63 case 'text': | |
| 64 return nfc.NFCRecordType.TEXT; | |
| 65 case 'url': | |
| 66 return nfc.NFCRecordType.URL; | |
| 67 case 'json': | |
| 68 return nfc.NFCRecordType.JSON; | |
| 69 case 'opaque': | |
| 70 return nfc.NFCRecordType.OPAQUE_RECORD; | |
| 71 } | |
| 72 | |
| 73 return nfc.NFCRecordType.EMPTY; | |
| 74 } | |
| 75 | |
| 76 function toMojoNFCPushTarget(target) { | |
| 77 switch (target) { | |
| 78 case 'any': | |
| 79 return nfc.NFCPushTarget.ANY; | |
| 80 case 'peer': | |
| 81 return nfc.NFCPushTarget.PEER; | |
| 82 case 'tag': | |
| 83 return nfc.NFCPushTarget.TAG; | |
| 84 } | |
| 85 | |
| 86 return nfc.NFCPushTarget.ANY; | |
| 87 } | |
| 88 | |
| 89 function toByteArray(data) { | |
| 90 // Convert JS objects to byte array | |
| 91 let byteArray; | |
| 92 let tmpData = data; | |
| 93 | |
| 94 if (tmpData instanceof ArrayBuffer) | |
| 95 byteArray = new Uint8Array(tmpData); | |
| 96 else if (typeof tmpData === 'object' || typeof tmpData === 'number') | |
| 97 tmpData = JSON.stringify(tmpData); | |
| 98 | |
| 99 if (typeof tmpData === 'string') | |
| 100 byteArray = new TextEncoder('utf-8').encode(tmpData); | |
| 101 | |
| 102 return byteArray; | |
| 103 } | |
| 104 | |
| 105 // Compares NFCMessage that was provided to the API | |
| 106 // (e.g. navigator.nfc.push), and NFCMessage that was received by the | |
| 107 // mock NFC service. | |
| 108 function assertNFCMessagesEqual(providedMessage, receivedMessage) { | |
| 109 // If simple data type is passed, e.g. String or ArrayBuffer, convert it | |
| 110 // to NFCMessage before comparing. | |
| 111 // https://w3c.github.io/web-nfc/#idl-def-nfcpushmessage | |
| 112 let provided = providedMessage; | |
| 113 if (providedMessage instanceof ArrayBuffer) | |
| 114 provided = createMessage([createOpaqueRecord(providedMessage)]); | |
| 115 else if (typeof providedMessage === 'string') | |
| 116 provided = createMessage([createTextRecord(providedMessage)]); | |
| 117 | |
| 118 assert_equals(provided.data.length, receivedMessage.data.length, | |
| 119 'NFCMessages must have same number of NFCRecords'); | |
| 120 | |
| 121 // Compare contents of each individual NFCRecord | |
| 122 for (let i = 0; i < provided.data.length; ++i) | |
| 123 compareNFCRecords(provided.data[i], receivedMessage.data[i]); | |
| 124 } | |
| 125 | |
| 126 // Compares NFCRecords that were provided / received by the mock service. | |
| 127 function compareNFCRecords(providedRecord, receivedRecord) { | |
| 128 assert_equals(toMojoNFCRecordType(providedRecord.recordType), | |
| 129 receivedRecord.recordType); | |
| 130 | |
| 131 // Compare media types without charset. | |
| 132 // Charset should be compared when watch method is implemented, in order | |
| 133 // to check that written and read strings are equal. | |
| 134 assert_equals(providedRecord.mediaType, | |
| 135 receivedRecord.mediaType.substring(0, providedRecord.mediaType.length) ); | |
| 136 | |
| 137 assert_false(toMojoNFCRecordType(providedRecord.recordType) == | |
| 138 nfc.NFCRecordType.EMPTY); | |
| 139 | |
| 140 assert_array_equals(toByteArray(providedRecord.data), | |
| 141 new Uint8Array(receivedRecord.data)); | |
| 142 } | |
| 143 | |
| 144 // Compares NFCPushOptions structures that were provided to API and | |
| 145 // received by the mock mojo service. | |
| 146 function assertNFCPushOptionsEqual(provided, received) { | |
| 147 if (provided.ignoreRead !== undefined) | |
| 148 assert_equals(provided.ignoreRead, !!+received.ignoreRead); | |
| 149 else | |
| 150 assert_equals(!!+received.ignoreRead, true); | |
| 151 | |
| 152 if (provided.timeout !== undefined) | |
| 153 assert_equals(provided.timeout, received.timeout); | |
| 154 else | |
| 155 assert_equals(received.timeout, Infinity); | |
| 156 | |
| 157 if (provided.target !== undefined) | |
| 158 assert_equals(toMojoNFCPushTarget(provided.target), received.target); | |
| 159 else | |
| 160 assert_equals(received.target, nfc.NFCPushTarget.ANY); | |
| 161 } | |
| 162 | |
| 163 function createNFCError(type) { | |
| 164 return { error: type ? | |
| 165 new nfc.NFCError({ error_type: type }) : null }; | |
| 166 } | |
| 167 | |
| 168 class MockNFC extends nfc.NFC.stubClass { | |
| 169 constructor() { | |
| 170 super(); | |
| 171 this.router_ = null; | |
| 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 assert_equals(this.router_, null); | |
| 215 this.router_ = new mojo.router.Router(pipe); | |
| 216 this.router_.setIncomingReceiver(this); | |
|
Reilly Grant (use Gerrit)
2016/05/20 18:31:21
Check usb-helpers.js for the new way to bind to a
shalamov
2016/05/24 16:09:07
Done.
| |
| 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 if (this.push_timeout_id_) | |
| 248 window.clearTimeout(this.push_timeout_id_); | |
| 249 } | |
| 250 | |
| 251 cancelPendingPushOperation() { | |
| 252 if (this.pending_promise_func_) | |
| 253 this.pending_promise_func_(createNFCError(nfc.NFCErrorType.OPERATION_C ANCELLED)); | |
| 254 this.pushed_message_ = null; | |
| 255 this.push_options_ = null; | |
| 256 this.pending_promise_func_ = null; | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 let mockNFC = new MockNFC; | |
| 261 mojo.frameServiceRegistry.addServiceOverrideForTesting( | |
| 262 nfc.NFC.name, | |
| 263 pipe => { | |
| 264 mockNFC.bindToPipe(pipe); | |
| 265 }); | |
| 266 | |
| 267 return Promise.resolve({ | |
| 268 mockNFC: mockNFC, | |
| 269 assertNFCMessagesEqual: assertNFCMessagesEqual, | |
| 270 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, | |
| 271 }); | |
| 272 }); | |
| 273 } | |
| 274 | |
| 275 function nfc_test(func, name, properties) { | |
| 276 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { | |
| 277 let result = Promise.resolve(func(nfc)); | |
| 278 let cleanUp = () => nfc.mockNFC.reset(); | |
| 279 result.then(cleanUp, cleanUp); | |
| 280 return result; | |
| 281 }), name, properties); | |
| 282 } | |
| OLD | NEW |