| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
| 3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="../resources/mojo-helpers.js"></script> | 4 <script src="../resources/mojo-helpers.js"></script> |
| 5 <script src="resources/nfc-helpers.js"></script> | 5 <script src="resources/nfc-helpers.js"></script> |
| 6 <script> | 6 <script> |
| 7 | 7 |
| 8 'use strict'; | 8 'use strict'; |
| 9 | 9 |
| 10 const invalid_messages = | 10 const invalid_type_messages = |
| 11 [ | 11 [ |
| 12 // Invalid NFCPushMessage type | 12 // Invalid NFCPushMessage type |
| 13 undefined, | 13 undefined, |
| 14 | 14 |
| 15 // NFCMessage.data: should have at least 1 valid record. | 15 // NFCMessage.data: should have at least 1 valid record. |
| 16 // https://w3c.github.io/web-nfc/#the-push-method - Step 8. | 16 // https://w3c.github.io/web-nfc/#the-push-method - Step 8. |
| 17 createMessage([{}]), | 17 createMessage([{}]), |
| 18 | 18 |
| 19 // https://w3c.github.io/web-nfc/#dfn-map-text-to-ndef | 19 // https://w3c.github.io/web-nfc/#dfn-map-text-to-ndef |
| 20 // NFCRecord must have data. | 20 // NFCRecord must have data. |
| 21 createMessage([createTextRecord()]), | 21 createMessage([createTextRecord()]), |
| 22 | 22 |
| 23 // NFCRecord.mediaType for 'text' record must be 'text/*'. | |
| 24 createMessage([createRecord('text', 'application/json', | |
| 25 test_number_data)]), | |
| 26 | |
| 27 // NFCRecord.data for 'text' record must be number or string. | 23 // NFCRecord.data for 'text' record must be number or string. |
| 28 createMessage([createTextRecord(test_buffer_data)]), | 24 createMessage([createTextRecord(test_buffer_data)]), |
| 29 createMessage([createTextRecord(test_json_data)]), | 25 createMessage([createTextRecord(test_json_data)]), |
| 30 | 26 |
| 31 // https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef | 27 // https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef |
| 32 // NFCRecord must have data. | 28 // NFCRecord must have data. |
| 33 createMessage([createJsonRecord()]), | 29 createMessage([createJsonRecord()]), |
| 34 | 30 |
| 35 // NFCRecord.data for 'json' record must be object. | 31 // NFCRecord.data for 'json' record must be object. |
| 36 createMessage([createJsonRecord(test_buffer_data)]), | 32 createMessage([createJsonRecord(test_buffer_data)]), |
| 37 createMessage([createJsonRecord(test_number_data)]), | 33 createMessage([createJsonRecord(test_number_data)]), |
| 38 createMessage([createJsonRecord(test_text_data)]), | 34 createMessage([createJsonRecord(test_text_data)]), |
| 39 | 35 |
| 40 // NFCRecord.mediaType for 'json' record must be 'application/json'. | |
| 41 createMessage([createRecord('json', 'image/png', test_json_data)]), | |
| 42 | |
| 43 // https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef | 36 // https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef |
| 44 // NFCRecord must have data. | 37 // NFCRecord must have data. |
| 45 createMessage([createUrlRecord()]), | 38 createMessage([createUrlRecord()]), |
| 46 | 39 |
| 47 // NFCRecord.data for 'url' record must be string. | 40 // NFCRecord.data for 'url' record must be string. |
| 48 createMessage([createUrlRecord(test_buffer_data)]), | 41 createMessage([createUrlRecord(test_buffer_data)]), |
| 49 createMessage([createUrlRecord(test_number_data)]), | 42 createMessage([createUrlRecord(test_number_data)]), |
| 50 createMessage([createUrlRecord(test_json_data)]), | 43 createMessage([createUrlRecord(test_json_data)]), |
| 51 | 44 |
| 52 // https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef | 45 // https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef |
| 53 // NFCRecord must have data. | 46 // NFCRecord must have data. |
| 54 createMessage([createOpaqueRecord()]), | 47 createMessage([createOpaqueRecord()]), |
| 55 // NFCRecord.data for 'opaque' record must be ArrayBuffer. | 48 // NFCRecord.data for 'opaque' record must be ArrayBuffer. |
| 56 createMessage([createOpaqueRecord(test_text_data)]), | 49 createMessage([createOpaqueRecord(test_text_data)]), |
| 57 createMessage([createOpaqueRecord(test_number_data)]), | 50 createMessage([createOpaqueRecord(test_number_data)]), |
| 58 createMessage([createOpaqueRecord(test_json_data)]) | 51 createMessage([createOpaqueRecord(test_json_data)]) |
| 59 ]; | 52 ]; |
| 60 | 53 |
| 54 const invalid_syntax_messages = |
| 55 [ |
| 56 // NFCRecord.mediaType for 'text' record must be 'text/*'. |
| 57 createMessage([createRecord('text', 'application/json', |
| 58 test_number_data)]), |
| 59 |
| 60 // Data for 'url' record, must be a valid URL. |
| 61 createMessage([createUrlRecord('Invalid URL:// Data')]), |
| 62 |
| 63 // NFCRecord.mediaType for 'json' record must be 'application/json' or |
| 64 // starts with 'application/' and ends with '+json'. |
| 65 createMessage([createRecord('json', 'image/png', test_json_data)]), |
| 66 createMessage([createRecord('json', 'application/x+y', test_json_data)]), |
| 67 createMessage([createRecord('json', 'custom/app+json', test_json_data)]), |
| 68 ]; |
| 69 |
| 61 nfc_test(nfc => { | 70 nfc_test(nfc => { |
| 62 let promises = []; | 71 let promises = []; |
| 63 invalid_messages.forEach(message => { | 72 invalid_type_messages.forEach(message => { |
| 64 promises.push( | 73 promises.push( |
| 65 assertRejectsWithError(navigator.nfc.push(message), 'TypeError')); | 74 assertRejectsWithError(navigator.nfc.push(message), 'TypeError')); |
| 66 }); | 75 }); |
| 67 return Promise.all(promises) | 76 return Promise.all(promises) |
| 68 }, 'Test that promise is rejected with TypeError if NFCMessage is invalid.'); | 77 }, 'Test that promise is rejected with TypeError if NFCMessage is invalid.'); |
| 69 | 78 |
| 70 nfc_test(nfc => { | 79 nfc_test(nfc => { |
| 80 let promises = []; |
| 81 invalid_syntax_messages.forEach(message => { |
| 82 promises.push( |
| 83 assertRejectsWithError(navigator.nfc.push(message), 'SyntaxError')); |
| 84 }); |
| 85 return Promise.all(promises) |
| 86 }, 'Test that promise is rejected with SyntaxError if NFCMessage contains' + |
| 87 ' invalid records.'); |
| 88 |
| 89 nfc_test(nfc => { |
| 71 nfc.mockNFC.setHWStatus(NFCHWStatus.DISABLED); | 90 nfc.mockNFC.setHWStatus(NFCHWStatus.DISABLED); |
| 72 return assertRejectsWithError(navigator.nfc.push(test_text_data), | 91 return assertRejectsWithError(navigator.nfc.push(test_text_data), |
| 73 'NotSupportedError'); | 92 'NotSupportedError'); |
| 74 }, 'nfc.push should fail when NFC HW is disabled.') | 93 }, 'nfc.push should fail when NFC HW is disabled.'); |
| 75 | 94 |
| 76 nfc_test(nfc => { | 95 nfc_test(nfc => { |
| 77 nfc.mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED); | 96 nfc.mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED); |
| 78 return assertRejectsWithError(navigator.nfc.push(test_text_data), | 97 return assertRejectsWithError(navigator.nfc.push(test_text_data), |
| 79 'NotSupportedError'); | 98 'NotSupportedError'); |
| 80 }, 'nfc.push should fail when NFC HW is not supported.') | 99 }, 'nfc.push should fail when NFC HW is not supported.'); |
| 81 | 100 |
| 82 nfc_test(nfc => { | 101 nfc_test(nfc => { |
| 83 return navigator.nfc.push(test_text_data, { timeout: 1 }); | 102 return navigator.nfc.push(test_text_data, { timeout: 1 }); |
| 84 }, 'nfc.push should succeed when NFC HW is enabled'); | 103 }, 'nfc.push should succeed when NFC HW is enabled'); |
| 85 | 104 |
| 86 nfc_test(nfc => { | 105 nfc_test(nfc => { |
| 87 return assertRejectsWithError( | 106 return assertRejectsWithError( |
| 88 navigator.nfc.push(test_text_data, { timeout: 'invalid' }), 'TypeError'); | 107 navigator.nfc.push(test_text_data, { timeout: 'invalid' }), 'TypeError'); |
| 89 }, 'nfc.push should fail when invalid timeout is provided'); | 108 }, 'nfc.push should fail when invalid timeout is provided'); |
| 90 | 109 |
| 91 nfc_test(nfc => { | 110 nfc_test(nfc => { |
| 92 return assertRejectsWithError( | 111 return assertRejectsWithError( |
| 93 navigator.nfc.push(test_text_data, { timeout: -1 }), 'TypeError'); | 112 navigator.nfc.push(test_text_data, { timeout: -1 }), 'TypeError'); |
| 94 }, 'nfc.push should fail when invalid negative timeout value is provided'); | 113 }, 'nfc.push should fail when invalid negative timeout value is provided'); |
| 95 | 114 |
| 96 nfc_test(nfc => { | 115 nfc_test(nfc => { |
| 97 nfc.mockNFC.setPendingPushCompleted(false); | 116 nfc.mockNFC.setPendingPushCompleted(false); |
| 98 return assertRejectsWithError( | 117 return assertRejectsWithError( |
| 99 navigator.nfc.push(test_text_data,{ timeout: 1 }), | 118 navigator.nfc.push(test_text_data,{ timeout: 1 }), |
| 100 'TimeoutError'); | 119 'TimeoutError'); |
| 101 }, 'nfc.push should fail with TimeoutError when push operation is not' + | 120 }, 'nfc.push should fail with TimeoutError when push operation is not' + |
| 102 ' completed before specified timeout value.') | 121 ' completed before specified timeout value.'); |
| 103 | 122 |
| 104 nfc_test(nfc => { | 123 nfc_test(nfc => { |
| 105 let message = createMessage([createTextRecord(test_text_data), | 124 let message = createMessage([createTextRecord(test_text_data), |
| 106 createJsonRecord(test_json_data), | 125 createJsonRecord(test_json_data), |
| 107 createOpaqueRecord(test_buffer_data), | 126 createOpaqueRecord(test_buffer_data), |
| 108 createTextRecord(test_number_data), | 127 createTextRecord(test_number_data), |
| 109 createUrlRecord(test_url_data)], | 128 createUrlRecord(test_url_data)], |
| 110 test_message_origin); | 129 test_message_origin); |
| 111 return navigator.nfc.push(message).then(() => { | 130 return navigator.nfc.push(message).then(() => { |
| 112 nfc.assertNFCMessagesEqual(message, nfc.mockNFC.pushedMessage()); | 131 nfc.assertNFCMessagesEqual(message, nfc.mockNFC.pushedMessage()); |
| 113 }); | 132 }); |
| 114 }, 'nfc.push NFCMessage containing text, json, opaque and url records with' | 133 }, 'nfc.push NFCMessage containing text, json, opaque and url records with' |
| 115 + ' default NFCPushOptions.'); | 134 + ' default NFCPushOptions.'); |
| 116 | 135 |
| 117 nfc_test(nfc => { | 136 nfc_test(nfc => { |
| 118 return navigator.nfc.push(test_text_data).then(() => { | 137 return navigator.nfc.push(test_text_data).then(() => { |
| 119 nfc.assertNFCMessagesEqual(test_text_data, nfc.mockNFC.pushedMessage()); | 138 nfc.assertNFCMessagesEqual(test_text_data, nfc.mockNFC.pushedMessage()); |
| 120 }); | 139 }); |
| 121 }, 'nfc.push String with default NFCPushOptions.'); | 140 }, 'nfc.push String with default NFCPushOptions.'); |
| 122 | 141 |
| 123 nfc_test(nfc => { | 142 nfc_test(nfc => { |
| 124 return navigator.nfc.push(test_buffer_data).then(() => { | 143 return navigator.nfc.push(test_buffer_data).then(() => { |
| 125 nfc.assertNFCMessagesEqual(test_buffer_data, nfc.mockNFC.pushedMessage()); | 144 nfc.assertNFCMessagesEqual(test_buffer_data, nfc.mockNFC.pushedMessage()); |
| 126 }); | 145 }); |
| 127 }, 'nfc.push ArrayBuffer with default NFCPushOptions.'); | 146 }, 'nfc.push ArrayBuffer with default NFCPushOptions.'); |
| 128 | 147 |
| 129 nfc_test(nfc => { | 148 nfc_test(nfc => { |
| 149 return navigator.nfc.push(createMessage([createRecord('empty')])); |
| 150 }, 'nfc.push with "empty" record should succeed.'); |
| 151 |
| 152 nfc_test(nfc => { |
| 130 return navigator.nfc.push(test_text_data).then(() => { | 153 return navigator.nfc.push(test_text_data).then(() => { |
| 131 nfc.assertNFCPushOptionsEqual(createNFCPushOptions('any', Infinity, true), | 154 nfc.assertNFCPushOptionsEqual(createNFCPushOptions('any', Infinity, true), |
| 132 nfc.mockNFC.pushOptions()); | 155 nfc.mockNFC.pushOptions()); |
| 133 }); | 156 }); |
| 134 }, 'Check that default NFCPushOptions values are correctly set.'); | 157 }, 'Check that default NFCPushOptions values are correctly set.'); |
| 135 | 158 |
| 136 nfc_test(nfc => { | 159 nfc_test(nfc => { |
| 137 let nfcPushOptions = createNFCPushOptions('tag', 1, false); | 160 let nfcPushOptions = createNFCPushOptions('tag', 1, false); |
| 138 return navigator.nfc.push(test_text_data, nfcPushOptions).then(() => { | 161 return navigator.nfc.push(test_text_data, nfcPushOptions).then(() => { |
| 139 nfc.assertNFCPushOptionsEqual(nfcPushOptions, nfc.mockNFC.pushOptions()); | 162 nfc.assertNFCPushOptionsEqual(nfcPushOptions, nfc.mockNFC.pushOptions()); |
| 140 }); | 163 }); |
| 141 }, 'Check that provided NFCPushOptions values are correctly converted.'); | 164 }, 'Check that provided NFCPushOptions values are correctly converted.'); |
| 142 | 165 |
| 143 nfc_test(nfc => { | 166 nfc_test(nfc => { |
| 144 return navigator.nfc.push(test_text_data) | 167 return navigator.nfc.push(test_text_data) |
| 145 .then(() => { return navigator.nfc.cancelPush(); }); | 168 .then(() => { return navigator.nfc.cancelPush(); }); |
| 146 }, 'nfc.cancelPush should succeed if there is pending push operation.'); | 169 }, 'nfc.cancelPush should succeed if there is pending push operation.'); |
| 147 | 170 |
| 148 nfc_test(nfc => { | 171 nfc_test(nfc => { |
| 149 nfc.mockNFC.setPendingPushCompleted(false); | 172 nfc.mockNFC.setPendingPushCompleted(false); |
| 150 let promise = navigator.nfc.push(test_text_data, { timeout: 100 }); | 173 let promise = navigator.nfc.push(test_text_data, { timeout: 100 }); |
| 151 navigator.nfc.cancelPush(); | 174 navigator.nfc.cancelPush(); |
| 152 return assertRejectsWithError(promise, 'AbortError'); | 175 return assertRejectsWithError(promise, 'AbortError'); |
| 153 }, 'nfc.cancelPush should reject pending promise with AbortError.') | 176 }, 'nfc.cancelPush should reject pending promise with AbortError.'); |
| 154 | 177 |
| 155 nfc_test(nfc => { | 178 nfc_test(nfc => { |
| 156 return assertRejectsWithError( | 179 return assertRejectsWithError( |
| 157 navigator.nfc.push(new ArrayBuffer(32 * 1024 + 1)), | 180 navigator.nfc.push(new ArrayBuffer(32 * 1024 + 1)), |
| 158 'NotSupportedError'); | 181 'NotSupportedError'); |
| 159 }, 'Reject promise with NotSupportedError if NFC message size exceeds 32KB.'); | 182 }, 'Reject promise with NotSupportedError if NFC message size exceeds 32KB.'); |
| 160 | 183 |
| 184 nfc_test(nfc => { |
| 185 let message = createMessage([createTextRecord(test_text_data)]); |
| 186 message.url = '%00/invalid/ path'; |
| 187 return assertRejectsWithError( |
| 188 navigator.nfc.push(message), |
| 189 'SyntaxError'); |
| 190 }, 'Reject promise with SyntaxError if WebNFC Id cannot be created from' + |
| 191 ' provided URL.'); |
| 192 |
| 193 nfc_test(nfc => { |
| 194 let message = createMessage([createRecord('json','application/json', |
| 195 { get x(){ return this; } })]); |
| 196 return assertRejectsWithError( |
| 197 navigator.nfc.push(message), |
| 198 'SyntaxError'); |
| 199 }, 'Reject promise with SyntaxError if "json" record cannot be serialized.'); |
| 200 |
| 161 </script> | 201 </script> |
| OLD | NEW |