OLD | NEW |
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 var test_text_data = 'Test text data.'; | 3 var test_text_data = 'Test text data.'; |
4 var test_text_byte_array = new TextEncoder('utf-8').encode(test_text_data); | 4 var test_text_byte_array = new TextEncoder('utf-8').encode(test_text_data); |
5 var test_number_data = 42; | 5 var test_number_data = 42; |
6 var test_json_data = {level: 1, score: 100, label: 'Game'}; | 6 var test_json_data = {level: 1, score: 100, label: 'Game'}; |
7 var test_url_data = 'https://w3c.github.io/web-nfc/'; | 7 var test_url_data = 'https://w3c.github.io/web-nfc/'; |
8 var test_message_origin = 'https://127.0.0.1:8443'; | 8 var test_message_origin = 'https://127.0.0.1:8443'; |
9 var test_buffer_data = new ArrayBuffer(test_text_byte_array.length); | 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
); | 10 var test_buffer_view = new Uint8Array(test_buffer_data).set(test_text_byte_array
); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return createRecord('opaque', 'application/octet-stream', buffer); | 63 return createRecord('opaque', 'application/octet-stream', buffer); |
64 } | 64 } |
65 | 65 |
66 function createUrlRecord(url) { | 66 function createUrlRecord(url) { |
67 return createRecord('url', 'text/plain', url); | 67 return createRecord('url', 'text/plain', url); |
68 } | 68 } |
69 | 69 |
70 function nfc_mocks(mojo) { | 70 function nfc_mocks(mojo) { |
71 return define('NFC mocks', [ | 71 return define('NFC mocks', [ |
72 'mojo/public/js/bindings', | 72 'mojo/public/js/bindings', |
73 'mojo/public/js/connection', | |
74 'device/nfc/nfc.mojom', | 73 'device/nfc/nfc.mojom', |
75 ], (bindings, connection, nfc) => { | 74 ], (bindings, nfc) => { |
76 | 75 |
77 function toMojoNFCRecordType(type) { | 76 function toMojoNFCRecordType(type) { |
78 switch (type) { | 77 switch (type) { |
79 case 'text': | 78 case 'text': |
80 return nfc.NFCRecordType.TEXT; | 79 return nfc.NFCRecordType.TEXT; |
81 case 'url': | 80 case 'url': |
82 return nfc.NFCRecordType.URL; | 81 return nfc.NFCRecordType.URL; |
83 case 'json': | 82 case 'json': |
84 return nfc.NFCRecordType.JSON; | 83 return nfc.NFCRecordType.JSON; |
85 case 'opaque': | 84 case 'opaque': |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 251 } |
253 } | 252 } |
254 | 253 |
255 function createNFCError(type) { | 254 function createNFCError(type) { |
256 return { error: type ? | 255 return { error: type ? |
257 new nfc.NFCError({ error_type: type }) : null }; | 256 new nfc.NFCError({ error_type: type }) : null }; |
258 } | 257 } |
259 | 258 |
260 class MockNFC { | 259 class MockNFC { |
261 constructor() { | 260 constructor() { |
| 261 this.bindingSet = new bindings.BindingSet(nfc.NFC); |
| 262 |
262 this.hw_status_ = NFCHWStatus.ENABLED; | 263 this.hw_status_ = NFCHWStatus.ENABLED; |
263 this.pushed_message_ = null; | 264 this.pushed_message_ = null; |
264 this.push_options_ = null; | 265 this.push_options_ = null; |
265 this.pending_promise_func_ = null; | 266 this.pending_promise_func_ = null; |
266 this.push_timeout_id_ = null; | 267 this.push_timeout_id_ = null; |
267 this.push_completed_ = true; | 268 this.push_completed_ = true; |
268 this.client_ = null; | 269 this.client_ = null; |
269 this.watch_id_ = 0; | 270 this.watch_id_ = 0; |
270 this.watchers_ = []; | 271 this.watchers_ = []; |
271 } | 272 } |
272 | 273 |
273 // NFC.stubClass delegate functions | 274 // NFC delegate functions |
274 push(message, options) { | 275 push(message, options) { |
275 let error = this.isReady(); | 276 let error = this.isReady(); |
276 if (error) | 277 if (error) |
277 return Promise.resolve(error); | 278 return Promise.resolve(error); |
278 | 279 |
279 this.pushed_message_ = message; | 280 this.pushed_message_ = message; |
280 this.push_options_ = options; | 281 this.push_options_ = options; |
281 | 282 |
282 return new Promise((resolve, reject) => { | 283 return new Promise((resolve, reject) => { |
283 this.pending_promise_func_ = resolve; | 284 this.pending_promise_func_ = resolve; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 332 |
332 cancelAllWatches() { | 333 cancelAllWatches() { |
333 if (this.watchers_.length === 0) { | 334 if (this.watchers_.length === 0) { |
334 return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND)); | 335 return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND)); |
335 } | 336 } |
336 | 337 |
337 this.watchers_.splice(0, this.watchers_.length); | 338 this.watchers_.splice(0, this.watchers_.length); |
338 return Promise.resolve(createNFCError(null)); | 339 return Promise.resolve(createNFCError(null)); |
339 } | 340 } |
340 | 341 |
341 | |
342 // Mock utility functions | |
343 bindToPipe(pipe) { | |
344 this.stub_ = connection.bindHandleToStub( | |
345 pipe, nfc.NFC); | |
346 bindings.StubBindings(this.stub_).delegate = this; | |
347 } | |
348 | |
349 isReady() { | 342 isReady() { |
350 if (this.hw_status_ === NFCHWStatus.DISABLED) | 343 if (this.hw_status_ === NFCHWStatus.DISABLED) |
351 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); | 344 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); |
352 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) | 345 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) |
353 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); | 346 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); |
354 return null; | 347 return null; |
355 } | 348 } |
356 | 349 |
357 setHWStatus(status) { | 350 setHWStatus(status) { |
358 this.hw_status_ = status; | 351 this.hw_status_ = status; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 assert_true(this.client_ !== null); | 394 assert_true(this.client_ !== null); |
402 if (this.watchers_.length > 0) { | 395 if (this.watchers_.length > 0) { |
403 this.client_.onWatch([id], toMojoNFCMessage(message)); | 396 this.client_.onWatch([id], toMojoNFCMessage(message)); |
404 } | 397 } |
405 } | 398 } |
406 } | 399 } |
407 | 400 |
408 let mockNFC = new MockNFC; | 401 let mockNFC = new MockNFC; |
409 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 402 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
410 nfc.NFC.name, | 403 nfc.NFC.name, |
411 pipe => { | 404 handle => { |
412 mockNFC.bindToPipe(pipe); | 405 mockNFC.bindingSet.addBinding(mockNFC, handle); |
413 }); | 406 }); |
414 | 407 |
415 return Promise.resolve({ | 408 return Promise.resolve({ |
416 mockNFC: mockNFC, | 409 mockNFC: mockNFC, |
417 assertNFCMessagesEqual: assertNFCMessagesEqual, | 410 assertNFCMessagesEqual: assertNFCMessagesEqual, |
418 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, | 411 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, |
419 assertWebNFCMessagesEqual: assertWebNFCMessagesEqual, | 412 assertWebNFCMessagesEqual: assertWebNFCMessagesEqual, |
420 assertNFCWatchOptionsEqual: assertNFCWatchOptionsEqual, | 413 assertNFCWatchOptionsEqual: assertNFCWatchOptionsEqual, |
421 }); | 414 }); |
422 }); | 415 }); |
423 } | 416 } |
424 | 417 |
425 function nfc_test(func, name, properties) { | 418 function nfc_test(func, name, properties) { |
426 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { | 419 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { |
427 let result = Promise.resolve(func(nfc)); | 420 let result = Promise.resolve(func(nfc)); |
428 let cleanUp = () => nfc.mockNFC.reset(); | 421 let cleanUp = () => nfc.mockNFC.reset(); |
429 result.then(cleanUp, cleanUp); | 422 result.then(cleanUp, cleanUp); |
430 return result; | 423 return result; |
431 }), name, properties); | 424 }), name, properties); |
432 } | 425 } |
OLD | NEW |