OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <meta charset="utf-8"> | 2 <meta charset="utf-8"> |
3 <title>Tests for PaymentRequest interface</title> | 3 <title>Tests for PaymentRequest interface</title> |
4 <script src="../resources/testharness.js"></script> | 4 <script src="../resources/testharness.js"></script> |
5 <script src="../resources/testharnessreport.js"></script> | 5 <script src="../resources/testharnessreport.js"></script> |
6 <script> | 6 <script> |
7 function substitute(originalObject, substituteKeyValuePairs) { | 7 function substitute(originalObject, substituteKeyValuePairs) { |
8 for (var key in originalObject) { | 8 for (var key in originalObject) { |
9 if (originalObject.hasOwnProperty(key) && substituteKeyValuePairs.hasOwn
Property(key)) { | 9 if (originalObject.hasOwnProperty(key) && substituteKeyValuePairs.hasOwn
Property(key)) { |
10 originalObject[key] = substituteKeyValuePairs[key]; | 10 originalObject[key] = substituteKeyValuePairs[key]; |
(...skipping 17 matching lines...) Expand all Loading... |
28 assert_true(item.hasOwnProperty(key) || item['amount'].hasOwnPropert
y(key), 'Unrecognized substitution key "' + key + '"'); | 28 assert_true(item.hasOwnProperty(key) || item['amount'].hasOwnPropert
y(key), 'Unrecognized substitution key "' + key + '"'); |
29 } | 29 } |
30 | 30 |
31 substitute(item, optionalSubstituteKeyValuePairs); | 31 substitute(item, optionalSubstituteKeyValuePairs); |
32 substitute(item['amount'], optionalSubstituteKeyValuePairs); | 32 substitute(item['amount'], optionalSubstituteKeyValuePairs); |
33 } | 33 } |
34 | 34 |
35 return item; | 35 return item; |
36 } | 36 } |
37 | 37 |
| 38 function setValue(obj, key, val) { |
| 39 keys = key.split(/\./); |
| 40 key = keys.pop(); |
| 41 keys.forEach((k) => { obj = obj[k]; }); |
| 42 assert_true(obj != undefined); |
| 43 obj[key] = val; |
| 44 } |
| 45 |
38 function buildDetails(optionalDetailName, optionalSubstituteKeyValuePairs) { | 46 function buildDetails(optionalDetailName, optionalSubstituteKeyValuePairs) { |
39 var details = {}; | 47 var details = { |
40 var detailNames = ['total', 'displayItems', 'shippingOptions']; | 48 'total': buildItem(), |
| 49 'displayItems': [buildItem()], |
| 50 'shippingOptions': [buildItem()], |
| 51 'modifiers': [{ |
| 52 'supportedMethods': ['foo'], |
| 53 'total': buildItem(), |
| 54 'additionalDisplayItems': [buildItem()] |
| 55 }] |
| 56 }; |
41 | 57 |
42 assert_true(!optionalDetailName || detailNames.indexOf(optionalDetailName) >
= 0, 'Detail name "' + optionalDetailName + '" should be either "total", "displa
yItems", or "shippingOptions".'); | 58 if (optionalDetailName) |
43 | 59 setValue(details, optionalDetailName, buildItem(optionalSubstituteKeyVal
uePairs)); |
44 for (var i in detailNames) { | |
45 if (optionalDetailName == detailNames[i]) { | |
46 if (detailNames[i] == 'total') { | |
47 details[detailNames[i]] = buildItem(optionalSubstituteKeyValuePa
irs); | |
48 } else { | |
49 details[detailNames[i]] = [buildItem(optionalSubstituteKeyValueP
airs)]; | |
50 } | |
51 } else { | |
52 if (detailNames[i] == 'total') { | |
53 details[detailNames[i]] = buildItem(); | |
54 } else { | |
55 details[detailNames[i]] = [buildItem()]; | |
56 } | |
57 } | |
58 } | |
59 | 60 |
60 return details; | 61 return details; |
61 } | 62 } |
62 | 63 |
63 test(function() { | 64 test(function() { |
64 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(), {}); | 65 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(), {}); |
65 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.')
; | 66 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.')
; |
66 | 67 |
67 test(function() { | 68 test(function() { |
68 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(), {}, ''); | 69 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(), {}, ''); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 request.abort(); | 105 request.abort(); |
105 }, 'PaymentRequest.abort() and PaymentRequest.show() should take no parameters.'
); | 106 }, 'PaymentRequest.abort() and PaymentRequest.show() should take no parameters.'
); |
106 | 107 |
107 test(function() { | 108 test(function() { |
108 var request = new PaymentRequest([{'supportedMethods': ['foo'], 'data': {'fo
o': {'gateway': 'bar'}}}], buildDetails(), {'requestShipping': true}); | 109 var request = new PaymentRequest([{'supportedMethods': ['foo'], 'data': {'fo
o': {'gateway': 'bar'}}}], buildDetails(), {'requestShipping': true}); |
109 request.show(); | 110 request.show(); |
110 request.abort(); | 111 request.abort(); |
111 }, 'Valid data causes no errors.'); | 112 }, 'Valid data causes no errors.'); |
112 | 113 |
113 test(function() { | 114 test(function() { |
114 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions', {'id': 'standard'})); | 115 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions.0', {'id': 'standard'})); |
115 assert_equals(null, request.shippingOption); | 116 assert_equals(null, request.shippingOption); |
116 }, 'Shipping option identifier should be null if shipping request is omitted.'); | 117 }, 'Shipping option identifier should be null if shipping request is omitted.'); |
117 | 118 |
118 test(function() { | 119 test(function() { |
119 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions', {'id': 'standard'}), {'requestShipping': false}); | 120 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions.0', {'id': 'standard'}), {'requestShipping': false}); |
120 assert_equals(null, request.shippingOption); | 121 assert_equals(null, request.shippingOption); |
121 }, 'Shipping option identifier should be null if shipping is explicitly not requ
ested.'); | 122 }, 'Shipping option identifier should be null if shipping is explicitly not requ
ested.'); |
122 | 123 |
123 test(function() { | 124 test(function() { |
124 var request = new PaymentRequest([{'supportedMethods': ['foo']}], {'total':
buildItem(), 'displayItems': [buildItem()]}, {'requestShipping': true}); | 125 var request = new PaymentRequest([{'supportedMethods': ['foo']}], {'total':
buildItem(), 'displayItems': [buildItem()]}, {'requestShipping': true}); |
125 assert_equals(null, request.shippingOption); | 126 assert_equals(null, request.shippingOption); |
126 }, 'Shipping option identifier should be null if no shipping options are provide
d.'); | 127 }, 'Shipping option identifier should be null if no shipping options are provide
d.'); |
127 | 128 |
128 test(function() { | 129 test(function() { |
129 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions', {'selected': false}), {'requestShipping': true}); | 130 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions.0', {'selected': false}), {'requestShipping': true}); |
130 assert_equals(null, request.shippingOption); | 131 assert_equals(null, request.shippingOption); |
131 }, 'Shipping option identifier should be null if the single provided option is n
ot selected.'); | 132 }, 'Shipping option identifier should be null if the single provided option is n
ot selected.'); |
132 | 133 |
133 test(function() { | 134 test(function() { |
134 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions', {'id': 'standard', 'selected': true}), {'requestShipping':
true}); | 135 var request = new PaymentRequest([{'supportedMethods': ['foo']}], buildDetai
ls('shippingOptions.0', {'id': 'standard', 'selected': true}), {'requestShipping
': true}); |
135 assert_equals('standard', request.shippingOption); | 136 assert_equals('standard', request.shippingOption); |
136 }, 'Shipping option identifier should default to the single provided option if i
t is selected.'); | 137 }, 'Shipping option identifier should default to the single provided option if i
t is selected.'); |
137 | 138 |
138 test(function() { | 139 test(function() { |
139 var shippingOptions = [buildItem({'id': 'standard'}), buildItem({'id': 'expr
ess'})]; | 140 var shippingOptions = [buildItem({'id': 'standard'}), buildItem({'id': 'expr
ess'})]; |
140 var request = new PaymentRequest([{'supportedMethods': ['foo']}], {'total':
buildItem(), 'displayItems': [buildItem()], 'shippingOptions': shippingOptions},
{'requestShipping': true}); | 141 var request = new PaymentRequest([{'supportedMethods': ['foo']}], {'total':
buildItem(), 'displayItems': [buildItem()], 'shippingOptions': shippingOptions},
{'requestShipping': true}); |
141 assert_equals(null, request.shippingOption); | 142 assert_equals(null, request.shippingOption); |
142 }, 'Shipping option identifier should be null if multiple unselected shipping op
tions are provided.'); | 143 }, 'Shipping option identifier should be null if multiple unselected shipping op
tions are provided.'); |
143 | 144 |
144 test(function() { | 145 test(function() { |
(...skipping 20 matching lines...) Expand all Loading... |
165 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('total', {'
value': '0'})); | 166 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('total', {'
value': '0'})); |
166 }, 'Non-negative total value should not throw.'); | 167 }, 'Non-negative total value should not throw.'); |
167 | 168 |
168 test(function() { | 169 test(function() { |
169 assert_throws(new TypeError(), function() { | 170 assert_throws(new TypeError(), function() { |
170 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('total'
, {'value': '-0.01'})); | 171 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('total'
, {'value': '-0.01'})); |
171 }); | 172 }); |
172 }, 'Negative total value should throw a TypeError.'); | 173 }, 'Negative total value should throw a TypeError.'); |
173 | 174 |
174 test(function() { | 175 test(function() { |
175 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('displayIte
ms', {'value': '-0.01'})); | 176 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails('displayIte
ms.0', {'value': '-0.01'})); |
176 }, 'Negative line item value should not throw.'); | 177 }, 'Negative line item value should not throw.'); |
177 | 178 |
178 test(function() { | 179 test(function() { |
179 assert_throws(new TypeError(), function() { | 180 assert_throws(new TypeError(), function() { |
180 new PaymentRequest([{'supportedMethods': ['foo']}], {'displayItems': [bu
ildItem()]}); | 181 new PaymentRequest([{'supportedMethods': ['foo']}], {'displayItems': [bu
ildItem()]}); |
181 }); | 182 }); |
182 }, 'Absence of total should throw TypeError.'); | 183 }, 'Absence of total should throw TypeError.'); |
183 | 184 |
| 185 test(function() { |
| 186 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(), '
modifiers': undefined}); |
| 187 }, 'Undefined modifiers should not throw.'); |
| 188 |
| 189 test(function() { |
| 190 assert_throws(new TypeError(), function() { |
| 191 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': []}); |
| 192 }); |
| 193 }, 'Empty modifiers should throw TypeError.'); |
| 194 |
| 195 test(function() { |
| 196 assert_throws(new TypeError(), function() { |
| 197 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': [{'total': buildItem()}]}); |
| 198 }); |
| 199 }, 'Absence of supportedMethods in modifiers should throw TypeError.'); |
| 200 |
| 201 test(function() { |
| 202 assert_throws(new TypeError(), function() { |
| 203 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': [{'supportedMethods': []}]}); |
| 204 }); |
| 205 }, 'Empty supportedMethods in modifiers should throw TypeError.'); |
| 206 |
| 207 test(function() { |
| 208 assert_throws(new TypeError(), function() { |
| 209 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': [{'supportedMethods': undefined}]}); |
| 210 }); |
| 211 }, 'Undefined supportedMethods in modifiers should throw TypeError.'); |
| 212 |
| 213 test(function() { |
| 214 assert_throws(new TypeError(), function() { |
| 215 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': [{'supportedMethods': null}]}); |
| 216 }); |
| 217 }, 'Null supportedMethods in modifiers should throw TypeError.'); |
| 218 |
| 219 test(function() { |
| 220 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(), '
modifiers': [{'supportedMethods': ['foo'], 'total': buildItem({'value': '0.0'})}
]}); |
| 221 }, 'Non-negative total value in PaymentDetailsModifier should not throw.'); |
| 222 |
| 223 test(function() { |
| 224 assert_throws(new TypeError(), function() { |
| 225 new PaymentRequest([{'supportedMethods': ['foo']}], {'total': buildItem(
), 'modifiers': [{'supportedMethods': ['foo'], 'total': buildItem({'value': '-0.
01'})}]}); |
| 226 }); |
| 227 }, 'Negative total value in PaymentDetailsModifier should throw a TypeError.'); |
| 228 |
184 promise_test(function(t) { | 229 promise_test(function(t) { |
185 return promise_rejects(t, null, new PaymentRequest([{'supportedMethods': ['f
oo']}], buildDetails()).abort()); | 230 return promise_rejects(t, null, new PaymentRequest([{'supportedMethods': ['f
oo']}], buildDetails()).abort()); |
186 }, 'abort() without show() should reject with error'); | 231 }, 'abort() without show() should reject with error'); |
187 | 232 |
188 generate_tests(assert_throws, [ | 233 generate_tests(assert_throws, [ |
189 ['PaymentRequest constructor should throw for incorrect parameter types.', n
ull, function() { | 234 ['PaymentRequest constructor should throw for incorrect parameter types.', n
ull, function() { |
190 new PaymentRequest('', '', '') | 235 new PaymentRequest('', '', '') |
191 }], | 236 }], |
192 ['PaymentRequest constructor should throw for undefined required parameters.
', null, function() { | 237 ['PaymentRequest constructor should throw for undefined required parameters.
', null, function() { |
193 new PaymentRequest(undefined, undefined) | 238 new PaymentRequest(undefined, undefined) |
(...skipping 30 matching lines...) Expand all Loading... |
224 new PaymentRequest([{'supportedMethods': ['foo'], 'data': infiniteData}]
, buildDetails()) | 269 new PaymentRequest([{'supportedMethods': ['foo'], 'data': infiniteData}]
, buildDetails()) |
225 }], | 270 }], |
226 ['Null for payment method specific data parameter should throw', null, funct
ion() { | 271 ['Null for payment method specific data parameter should throw', null, funct
ion() { |
227 new PaymentRequest([{'supportedMethods': ['foo'], 'data': null}], buildD
etails()) | 272 new PaymentRequest([{'supportedMethods': ['foo'], 'data': null}], buildD
etails()) |
228 }], | 273 }], |
229 ['Empty string for payment method specific data parameter should throw', nul
l, function() { | 274 ['Empty string for payment method specific data parameter should throw', nul
l, function() { |
230 new PaymentRequest([{'supportedMethods': ['foo'], 'data': ''}], buildDet
ails()) | 275 new PaymentRequest([{'supportedMethods': ['foo'], 'data': ''}], buildDet
ails()) |
231 }] | 276 }] |
232 ]); | 277 ]); |
233 | 278 |
234 var detailNames = ['total', 'displayItems', 'shippingOptions']; | 279 var detailNames = ['total', 'displayItems.0', 'shippingOptions.0', 'modifiers.0.
total', 'modifiers.0.additionalDisplayItems.0']; |
235 for (var i in detailNames) { | 280 for (var i in detailNames) { |
236 generate_tests(assert_throws, [ | 281 generate_tests(assert_throws, [ |
237 // Invalid currency code formats. | 282 // Invalid currency code formats. |
238 ['Invalid currency code US1 should throw', null, function() { | 283 ['Invalid currency code US1 should throw', null, function() { |
239 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'currency': 'US1'})) | 284 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'currency': 'US1'})) |
240 }], | 285 }], |
241 ['Invalid currency code US should throw', null, function() { | 286 ['Invalid currency code US should throw', null, function() { |
242 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'currency': 'US'})) | 287 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'currency': 'US'})) |
243 }], | 288 }], |
244 ['Invalid currency code USDO should throw', null, function() { | 289 ['Invalid currency code USDO should throw', null, function() { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 }], | 341 }], |
297 ['Null amount should throw', null, function() { | 342 ['Null amount should throw', null, function() { |
298 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'value': null})) | 343 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'value': null})) |
299 }], | 344 }], |
300 ['Undefined amount should throw', null, function() { | 345 ['Undefined amount should throw', null, function() { |
301 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'value': undefined})) | 346 new PaymentRequest([{'supportedMethods': ['foo']}], buildDetails(det
ailNames[i], {'value': undefined})) |
302 }], | 347 }], |
303 ]); | 348 ]); |
304 } | 349 } |
305 </script> | 350 </script> |
OLD | NEW |