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', |
73 'device/nfc/nfc.mojom', | 74 'device/nfc/nfc.mojom', |
74 ], (bindings, nfc) => { | 75 ], (bindings, connection, nfc) => { |
75 | 76 |
76 function toMojoNFCRecordType(type) { | 77 function toMojoNFCRecordType(type) { |
77 switch (type) { | 78 switch (type) { |
78 case 'text': | 79 case 'text': |
79 return nfc.NFCRecordType.TEXT; | 80 return nfc.NFCRecordType.TEXT; |
80 case 'url': | 81 case 'url': |
81 return nfc.NFCRecordType.URL; | 82 return nfc.NFCRecordType.URL; |
82 case 'json': | 83 case 'json': |
83 return nfc.NFCRecordType.JSON; | 84 return nfc.NFCRecordType.JSON; |
84 case 'opaque': | 85 case 'opaque': |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 } | 252 } |
252 } | 253 } |
253 | 254 |
254 function createNFCError(type) { | 255 function createNFCError(type) { |
255 return { error: type ? | 256 return { error: type ? |
256 new nfc.NFCError({ error_type: type }) : null }; | 257 new nfc.NFCError({ error_type: type }) : null }; |
257 } | 258 } |
258 | 259 |
259 class MockNFC { | 260 class MockNFC { |
260 constructor() { | 261 constructor() { |
261 this.bindingSet = new bindings.BindingSet(nfc.NFC); | |
262 | |
263 this.hw_status_ = NFCHWStatus.ENABLED; | 262 this.hw_status_ = NFCHWStatus.ENABLED; |
264 this.pushed_message_ = null; | 263 this.pushed_message_ = null; |
265 this.push_options_ = null; | 264 this.push_options_ = null; |
266 this.pending_promise_func_ = null; | 265 this.pending_promise_func_ = null; |
267 this.push_timeout_id_ = null; | 266 this.push_timeout_id_ = null; |
268 this.push_completed_ = true; | 267 this.push_completed_ = true; |
269 this.client_ = null; | 268 this.client_ = null; |
270 this.watch_id_ = 0; | 269 this.watch_id_ = 0; |
271 this.watchers_ = []; | 270 this.watchers_ = []; |
272 } | 271 } |
273 | 272 |
274 // NFC delegate functions | 273 // NFC.stubClass delegate functions |
275 push(message, options) { | 274 push(message, options) { |
276 let error = this.isReady(); | 275 let error = this.isReady(); |
277 if (error) | 276 if (error) |
278 return Promise.resolve(error); | 277 return Promise.resolve(error); |
279 | 278 |
280 this.pushed_message_ = message; | 279 this.pushed_message_ = message; |
281 this.push_options_ = options; | 280 this.push_options_ = options; |
282 | 281 |
283 return new Promise((resolve, reject) => { | 282 return new Promise((resolve, reject) => { |
284 this.pending_promise_func_ = resolve; | 283 this.pending_promise_func_ = resolve; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 331 |
333 cancelAllWatches() { | 332 cancelAllWatches() { |
334 if (this.watchers_.length === 0) { | 333 if (this.watchers_.length === 0) { |
335 return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND)); | 334 return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND)); |
336 } | 335 } |
337 | 336 |
338 this.watchers_.splice(0, this.watchers_.length); | 337 this.watchers_.splice(0, this.watchers_.length); |
339 return Promise.resolve(createNFCError(null)); | 338 return Promise.resolve(createNFCError(null)); |
340 } | 339 } |
341 | 340 |
| 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 |
342 isReady() { | 349 isReady() { |
343 if (this.hw_status_ === NFCHWStatus.DISABLED) | 350 if (this.hw_status_ === NFCHWStatus.DISABLED) |
344 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); | 351 return createNFCError(nfc.NFCErrorType.DEVICE_DISABLED); |
345 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) | 352 if (this.hw_status_ === NFCHWStatus.NOT_SUPPORTED) |
346 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); | 353 return createNFCError(nfc.NFCErrorType.NOT_SUPPORTED); |
347 return null; | 354 return null; |
348 } | 355 } |
349 | 356 |
350 setHWStatus(status) { | 357 setHWStatus(status) { |
351 this.hw_status_ = status; | 358 this.hw_status_ = status; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 assert_true(this.client_ !== null); | 401 assert_true(this.client_ !== null); |
395 if (this.watchers_.length > 0) { | 402 if (this.watchers_.length > 0) { |
396 this.client_.onWatch([id], toMojoNFCMessage(message)); | 403 this.client_.onWatch([id], toMojoNFCMessage(message)); |
397 } | 404 } |
398 } | 405 } |
399 } | 406 } |
400 | 407 |
401 let mockNFC = new MockNFC; | 408 let mockNFC = new MockNFC; |
402 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 409 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
403 nfc.NFC.name, | 410 nfc.NFC.name, |
404 handle => { | 411 pipe => { |
405 mockNFC.bindingSet.addBinding(mockNFC, handle); | 412 mockNFC.bindToPipe(pipe); |
406 }); | 413 }); |
407 | 414 |
408 return Promise.resolve({ | 415 return Promise.resolve({ |
409 mockNFC: mockNFC, | 416 mockNFC: mockNFC, |
410 assertNFCMessagesEqual: assertNFCMessagesEqual, | 417 assertNFCMessagesEqual: assertNFCMessagesEqual, |
411 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, | 418 assertNFCPushOptionsEqual: assertNFCPushOptionsEqual, |
412 assertWebNFCMessagesEqual: assertWebNFCMessagesEqual, | 419 assertWebNFCMessagesEqual: assertWebNFCMessagesEqual, |
413 assertNFCWatchOptionsEqual: assertNFCWatchOptionsEqual, | 420 assertNFCWatchOptionsEqual: assertNFCWatchOptionsEqual, |
414 }); | 421 }); |
415 }); | 422 }); |
416 } | 423 } |
417 | 424 |
418 function nfc_test(func, name, properties) { | 425 function nfc_test(func, name, properties) { |
419 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { | 426 mojo_test(mojo => nfc_mocks(mojo).then(nfc => { |
420 let result = Promise.resolve(func(nfc)); | 427 let result = Promise.resolve(func(nfc)); |
421 let cleanUp = () => nfc.mockNFC.reset(); | 428 let cleanUp = () => nfc.mockNFC.reset(); |
422 result.then(cleanUp, cleanUp); | 429 result.then(cleanUp, cleanUp); |
423 return result; | 430 return result; |
424 }), name, properties); | 431 }), name, properties); |
425 } | 432 } |
OLD | NEW |