OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <script src="../resources/testharness.js"></script> | |
3 <script src="../resources/testharness-helpers.js"></script> | |
4 <script src="../resources/testharnessreport.js"></script> | |
5 <script src="../resources/mojo-helpers.js"></script> | |
6 <script src="resources/nfc-helpers.js"></script> | |
7 <script> | |
8 | |
9 'use strict'; | |
10 | |
11 var invalid_messages = | |
kenneth.christiansen
2016/04/14 14:29:27
Guess we could use const/let here
shalamov
2016/04/15 07:56:39
Done.
| |
12 [ | |
13 // NFCMessage.data: should have at least 1 valid record. | |
14 // https://w3c.github.io/web-nfc/#the-push-method - Step 8. | |
15 createMessage([{}]), | |
16 | |
17 // https://w3c.github.io/web-nfc/#dfn-map-text-to-ndef | |
18 // NFCRecord must have data. | |
19 createMessage([createTextRecord()]), | |
20 | |
21 // NFCRecord.mediaType for 'text' record must be text/* . | |
22 createMessage([createRecord('text', 'application/json', | |
23 test_number_data)]), | |
24 | |
25 // NFCRecord.data for 'text' record must be number or string. | |
26 createMessage([createTextRecord(test_buffer_data)]), | |
27 createMessage([createTextRecord(test_json_data)]), | |
28 | |
29 // https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef | |
30 // NFCRecord must have data. | |
31 createMessage([createJsonRecord()]), | |
32 | |
33 // NFCRecord.data for 'json' record must be object. | |
34 createMessage([createJsonRecord(test_buffer_data)]), | |
35 createMessage([createJsonRecord(test_number_data)]), | |
36 createMessage([createJsonRecord(test_text_data)]), | |
37 | |
38 // NFCRecord.mediaType for 'json' record must be 'application/json'. | |
39 createMessage([createRecord('json', 'image/png', test_json_data)]), | |
40 | |
41 // https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef | |
42 // NFCRecord must have data. | |
43 createMessage([createUrlRecord()]), | |
44 | |
45 // NFCRecord.data for 'url' record must be string. | |
46 createMessage([createUrlRecord(test_buffer_data)]), | |
47 createMessage([createUrlRecord(test_number_data)]), | |
48 createMessage([createUrlRecord(test_json_data)]), | |
49 | |
50 // https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef | |
51 // NFCRecord must have data. | |
52 createMessage([createOpaqueRecord()]), | |
53 // NFCRecord.data for 'opaque' record must be ArrayBuffer. | |
54 createMessage([createOpaqueRecord(test_text_data)]), | |
55 createMessage([createOpaqueRecord(test_number_data)]), | |
56 createMessage([createOpaqueRecord(test_json_data)]) | |
57 ]; | |
58 | |
59 promise_test(() => { | |
60 return assert_promise_rejects( | |
61 navigator.nfc.push(undefined), | |
62 'TypeMismatchError'); | |
63 }, 'Reject promise with TypeMismatchError if invalid type is provided.'); | |
64 | |
65 invalid_messages.forEach((message) => { | |
kenneth.christiansen
2016/04/14 14:29:27
no need to put () around message
shalamov
2016/04/15 07:56:39
Done.
| |
66 promise_test(() => { | |
67 return assert_promise_rejects( | |
68 navigator.nfc.push(message), | |
69 'SyntaxError'); | |
70 }, 'Reject promise with SyntaxError if NFCMessage is invalid.'); | |
71 }); | |
72 | |
73 nfc_test(nfc => { | |
74 nfc.mockNFC.setHWStatus(NFCHWStatus.DISABLED); | |
75 return assert_promise_rejects( | |
76 navigator.nfc.push(test_text_data), | |
77 'NotSupportedError'); | |
78 }, 'nfc.push should fail when NFC HW is disabled.') | |
79 | |
80 nfc_test(nfc => { | |
81 nfc.mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED); | |
82 return assert_promise_rejects( | |
83 navigator.nfc.push(test_text_data), | |
84 'NotSupportedError'); | |
85 }, 'nfc.push should fail when NFC HW is not supported.') | |
86 | |
87 nfc_test(nfc => { | |
88 return navigator.nfc.push(test_text_data, { timeout: 1 }); | |
89 }, 'nfc.push should succeed when NFC HW is enabled') | |
90 | |
91 nfc_test(nfc => { | |
92 nfc.mockNFC.setPendingPushCompleted(false); | |
93 return assert_promise_rejects( | |
94 navigator.nfc.push(test_text_data, { timeout: 1 }), | |
95 'TimeoutError'); | |
96 }, 'nfc.push should fail with TimeoutError when push operation' + | |
97 ' is not completed before specified timeout value.') | |
98 | |
99 nfc_test(nfc => { | |
100 let message = createMessage([createTextRecord(test_text_data), | |
101 createJsonRecord(test_json_data), | |
102 createOpaqueRecord(test_buffer_data), | |
103 createTextRecord(test_number_data), | |
104 createUrlRecord(test_url_data)], | |
105 test_message_origin); | |
106 return navigator.nfc.push(message).then(() => { | |
107 nfc.assertNFCMessagesEqual(message, nfc.mockNFC.pushedMessage()); | |
108 }); | |
109 }, 'nfc.push NFCMessage containing text, json, opaque and url records with defau lt ' | |
110 + 'NFCPushOptions.') | |
111 | |
112 nfc_test(nfc => { | |
113 return navigator.nfc.push(test_text_data).then(() => { | |
114 nfc.assertNFCMessagesEqual(test_text_data, nfc.mockNFC.pushedMessage()); | |
115 }); | |
116 }, 'nfc.push String with default NFCPushOptions.') | |
117 | |
118 nfc_test(nfc => { | |
119 return navigator.nfc.push(test_buffer_data).then(() => { | |
120 nfc.assertNFCMessagesEqual(test_buffer_data, nfc.mockNFC.pushedMessage()); | |
121 }); | |
122 }, 'nfc.push ArrayBuffer with default NFCPushOptions.') | |
123 | |
124 nfc_test(nfc => { | |
125 return navigator.nfc.push(test_text_data).then(() => { | |
126 nfc.assertNFCPushOptionsEqual(createNFCPushOptions('any', Infinity, true), n fc.mockNFC.pushOptions()); | |
127 }); | |
128 }, 'Check that default NFCPushOptions values are correctly set.') | |
129 | |
130 nfc_test(nfc => { | |
131 let nfcPushOptions = createNFCPushOptions('tag', 10, false); | |
132 return navigator.nfc.push(test_text_data, nfcPushOptions).then(() => { | |
133 nfc.assertNFCPushOptionsEqual(nfcPushOptions, nfc.mockNFC.pushOptions()); | |
134 }); | |
135 }, 'Check that provided NFCPushOptions values are correctly converted.') | |
136 | |
137 nfc_test(nfc => { | |
138 return navigator.nfc.push(test_buffer_data).then(() => { | |
139 nfc.assertNFCMessagesEqual(test_buffer_data, nfc.mockNFC.pushedMessage()); | |
140 }); | |
141 }, 'Push ArrayBuffer with default NFCPushOptions.') | |
142 | |
143 nfc_test(nfc => { | |
144 return navigator.nfc.push(test_text_data) | |
145 .then(() => { navigator.nfc.cancelPush }); | |
146 }, 'nfc.cancelPush should succeed if there is pending push operation.') | |
147 | |
148 nfc_test(nfc => { | |
149 nfc.mockNFC.setPendingPushCompleted(false); | |
150 let promise = navigator.nfc.push(test_text_data, { timeout: 10000 }); | |
151 navigator.nfc.cancelPush(); | |
152 return assert_promise_rejects( | |
153 promise, | |
154 'AbortError'); | |
155 }, 'nfc.cancelPush should reject pending promise with AbortError.') | |
156 | |
157 </script> | |
OLD | NEW |