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, substituteKeyValuePair) { | 7 function substitute(originalObject, substituteKeyValuePair) { |
8 for (var key in originalObject) { | 8 for (var key in originalObject) { |
9 if (originalObject.hasOwnProperty(key) && substituteKeyValuePair.hasOwnP
roperty(key)) { | 9 if (originalObject.hasOwnProperty(key) && substituteKeyValuePair.hasOwnP
roperty(key)) { |
10 originalObject[key] = substituteKeyValuePair[key]; | 10 originalObject[key] = substituteKeyValuePair[key]; |
11 } | 11 } |
12 } | 12 } |
13 } | 13 } |
14 | 14 |
15 function buildItem(optionalSubstituteKeyValuePair) { | 15 function buildItem(optionalSubstituteKeyValuePair) { |
16 var item = {'id': 'item_id', 'label': 'Item Description', 'amount': {'curren
cy': 'USD', 'value': '10.00'}}; | 16 var item = {'id': 'item_id', 'label': 'Item Description', 'amount': {'curren
cy': 'USD', 'value': '10.00'}}; |
17 | 17 |
18 if (optionalSubstituteKeyValuePair) { | 18 if (optionalSubstituteKeyValuePair) { |
19 substitute(item, optionalSubstituteKeyValuePair); | 19 substitute(item, optionalSubstituteKeyValuePair); |
20 substitute(item['amount'], optionalSubstituteKeyValuePair); | 20 substitute(item['amount'], optionalSubstituteKeyValuePair); |
21 } | 21 } |
22 | 22 |
23 return item; | 23 return item; |
24 } | 24 } |
25 | 25 |
26 function buildDetails(optionalDetailName, optionalSubstituteKeyValuePair) { | 26 function buildDetails(optionalDetailName, optionalSubstituteKeyValuePair) { |
27 var details = {}; | 27 var details = {}; |
28 var detailNames = ['displayItems', 'shippingOptions']; | 28 var detailNames = ['total', 'displayItems', 'shippingOptions']; |
29 | 29 |
30 assert_true(!optionalDetailName || detailNames.indexOf(optionalDetailName) >
= 0, 'Detail name "' + optionalDetailName + '" should be either "displayItems" o
r "shippingOptions".'); | 30 assert_true(!optionalDetailName || detailNames.indexOf(optionalDetailName) >
= 0, 'Detail name "' + optionalDetailName + '" should be either "total", "displa
yItems", or "shippingOptions".'); |
31 | 31 |
32 for (var i in detailNames) { | 32 for (var i in detailNames) { |
33 if (optionalDetailName == detailNames[i]) { | 33 if (optionalDetailName == detailNames[i]) { |
34 details[detailNames[i]] = [buildItem(optionalSubstituteKeyValuePair)
]; | 34 if (detailNames[i] == 'total') { |
| 35 details[detailNames[i]] = buildItem(optionalSubstituteKeyValuePa
ir); |
| 36 } else { |
| 37 details[detailNames[i]] = [buildItem(optionalSubstituteKeyValueP
air)]; |
| 38 } |
35 } else { | 39 } else { |
36 details[detailNames[i]] = [buildItem()]; | 40 if (detailNames[i] == 'total') { |
| 41 details[detailNames[i]] = buildItem(); |
| 42 } else { |
| 43 details[detailNames[i]] = [buildItem()]; |
| 44 } |
37 } | 45 } |
38 } | 46 } |
39 | 47 |
40 return details; | 48 return details; |
41 } | 49 } |
42 | 50 |
43 test(function() { | 51 test(function() { |
44 new PaymentRequest(['foo'], buildDetails(), {}, {}); | 52 new PaymentRequest(['foo'], buildDetails(), {}, {}); |
45 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.')
; | 53 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.')
; |
46 | 54 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 var request = new PaymentRequest(['foo'], buildDetails('shippingOptions', {'
id': 'standard'}), {'requestShipping': false}); | 107 var request = new PaymentRequest(['foo'], buildDetails('shippingOptions', {'
id': 'standard'}), {'requestShipping': false}); |
100 assert_equals(null, request.shippingOption); | 108 assert_equals(null, request.shippingOption); |
101 }, 'Shipping option identifier should be null if shipping is explicitly not requ
ested.'); | 109 }, 'Shipping option identifier should be null if shipping is explicitly not requ
ested.'); |
102 | 110 |
103 test(function() { | 111 test(function() { |
104 var request = new PaymentRequest(['foo'], buildDetails('shippingOptions', {'
id': 'standard'}), {'requestShipping': true}); | 112 var request = new PaymentRequest(['foo'], buildDetails('shippingOptions', {'
id': 'standard'}), {'requestShipping': true}); |
105 assert_equals('standard', request.shippingOption); | 113 assert_equals('standard', request.shippingOption); |
106 }, 'Shipping option identifier should default to the single provided option.'); | 114 }, 'Shipping option identifier should default to the single provided option.'); |
107 | 115 |
108 test(function() { | 116 test(function() { |
109 var request = new PaymentRequest(['foo'], {'displayItems': [buildItem()]}, {
'requestShipping': true}); | 117 var request = new PaymentRequest(['foo'], {'total': buildItem(), 'displayIte
ms': [buildItem()]}, {'requestShipping': true}); |
110 assert_equals(null, request.shippingOption); | 118 assert_equals(null, request.shippingOption); |
111 }, 'Shipping option identifier should be null when no shipping options are provi
ded.'); | 119 }, 'Shipping option identifier should be null when no shipping options are provi
ded.'); |
112 | 120 |
113 test(function() { | 121 test(function() { |
114 var request = new PaymentRequest(['foo'], {'displayItems': [buildItem()], 's
hippingOptions': [buildItem({'id': 'standard'}), buildItem({'id': 'express'})]},
{'requestShipping': true}); | 122 var request = new PaymentRequest(['foo'], {'total': buildItem(), 'displayIte
ms': [buildItem()], 'shippingOptions': [buildItem({'id': 'standard'}), buildItem
({'id': 'express'})]}, {'requestShipping': true}); |
115 assert_equals(null, request.shippingOption); | 123 assert_equals(null, request.shippingOption); |
116 }, 'Shipping option identifier should be null at first when multiple shipping op
tions are provided.'); | 124 }, 'Shipping option identifier should be null at first when multiple shipping op
tions are provided.'); |
117 | 125 |
| 126 test(function() { |
| 127 new PaymentRequest(['foo'], {'total': buildItem(), 'displayItems': undefined
}); |
| 128 }, 'Undefined display items should not throw.'); |
| 129 |
| 130 test(function() { |
| 131 new PaymentRequest(['foo'], {'total': buildItem(), 'displayItems': []}); |
| 132 }, 'Empty display items should not throw.'); |
| 133 |
| 134 test(function() { |
| 135 new PaymentRequest(['foo'], buildDetails('total', {'value': '0'})); |
| 136 }, 'Non-negative total value should not throw.'); |
| 137 |
| 138 test(function() { |
| 139 assert_throws(new TypeError(), function() { |
| 140 new PaymentRequest(['foo'], buildDetails('total', {'value': '-0.01'})); |
| 141 }); |
| 142 }, 'Negative total value should throw a TypeError.'); |
| 143 |
| 144 test(function() { |
| 145 new PaymentRequest(['foo'], buildDetails('displayItems', {'value': '-0.01'})
); |
| 146 }, 'Negative line item value should not throw.'); |
| 147 |
| 148 test(function() { |
| 149 assert_throws(new TypeError(), function() { |
| 150 new PaymentRequest(['foo'], {'displayItems': [buildItem()]}); |
| 151 }); |
| 152 }, 'Absence of total should throw TypeError.'); |
118 | 153 |
119 generate_tests(assert_throws, [ | 154 generate_tests(assert_throws, [ |
120 ['PaymentRequest constructor should throw for incorrect parameter types.', n
ull, function() { | 155 ['PaymentRequest constructor should throw for incorrect parameter types.', n
ull, function() { |
121 new PaymentRequest('', '', '', '') | 156 new PaymentRequest('', '', '', '') |
122 }], | 157 }], |
123 ['PaymentRequest constructor should throw for undefined required parameters.
', null, function() { | 158 ['PaymentRequest constructor should throw for undefined required parameters.
', null, function() { |
124 new PaymentRequest(undefined, undefined) | 159 new PaymentRequest(undefined, undefined) |
125 }], | 160 }], |
126 ['PaymentRequest constructor should throw for null required parameter.', nul
l, function() { | 161 ['PaymentRequest constructor should throw for null required parameter.', nul
l, function() { |
127 new PaymentRequest(null, null) | 162 new PaymentRequest(null, null) |
128 }], | 163 }], |
129 ['Empty list of supported payment method identifiers should throw TypeError.
', new TypeError(), function() { | 164 ['Empty list of supported payment method identifiers should throw TypeError.
', new TypeError(), function() { |
130 new PaymentRequest([], buildDetails()) | 165 new PaymentRequest([], buildDetails()) |
131 }], | 166 }], |
132 ['Keys in payment method specific data object should match accepted method i
dentifiers.', null, function() { | 167 ['Keys in payment method specific data object should match accepted method i
dentifiers.', null, function() { |
133 new PaymentRequest(['foo'], buildDetails(), {}, {'bar': {'gateway': 'baz
'}}) | 168 new PaymentRequest(['foo'], buildDetails(), {}, {'bar': {'gateway': 'baz
'}}) |
134 }], | 169 }], |
135 ['Empty details should throw', null, function() { | 170 ['Empty details should throw', null, function() { |
136 new PaymentRequest(['foo'], {}) | 171 new PaymentRequest(['foo'], {}) |
137 }], | 172 }], |
138 ['Empty items should throw', null, function() { | 173 ['Null items should throw', new TypeError(), function() { |
139 new PaymentRequest(['foo'], {'displayItems': []}) | 174 new PaymentRequest(['foo'], {'total': buildItem(), 'displayItems': null}
); |
| 175 }], |
| 176 ['Null shipping options should throw', new TypeError(), function() { |
| 177 new PaymentRequest(['foo'], {'total': buildItem(), 'displayItems': [buil
dItem()], 'shippingOptions': null}); |
140 }], | 178 }], |
141 ['Aborting before showing should throw.', null, function() { | 179 ['Aborting before showing should throw.', null, function() { |
142 new PaymentRequest(['foo'], buildDetails()).abort() | 180 new PaymentRequest(['foo'], buildDetails()).abort() |
143 }], | 181 }], |
144 | 182 |
145 // Payment method specific data should be a JSON-serializable object. | 183 // Payment method specific data should be a JSON-serializable object. |
146 ['Array value for payment method specific data parameter should throw', null
, function() { | 184 ['Array value for payment method specific data parameter should throw', null
, function() { |
147 new PaymentRequest(['foo'], buildDetails(), {}, []) | 185 new PaymentRequest(['foo'], buildDetails(), {}, []) |
148 }], | 186 }], |
149 ['String value for payment method specific data parameter should throw', nul
l, function() { | 187 ['String value for payment method specific data parameter should throw', nul
l, function() { |
(...skipping 13 matching lines...) Expand all Loading... |
163 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': []}) | 201 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': []}) |
164 }], | 202 }], |
165 ['String value for one of the payment method specific data pieces should thr
ow', null, function() { | 203 ['String value for one of the payment method specific data pieces should thr
ow', null, function() { |
166 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 'bar'}) | 204 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 'bar'}) |
167 }], | 205 }], |
168 ['Numeric value for one of the payment method specific data pieces should th
row', null, function() { | 206 ['Numeric value for one of the payment method specific data pieces should th
row', null, function() { |
169 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 42}) | 207 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 42}) |
170 }], | 208 }], |
171 ]); | 209 ]); |
172 | 210 |
173 var detailNames = ['displayItems', 'shippingOptions']; | 211 var detailNames = ['total', 'displayItems', 'shippingOptions']; |
174 for (var i in detailNames) { | 212 for (var i in detailNames) { |
175 generate_tests(assert_throws, [ | 213 generate_tests(assert_throws, [ |
176 // Invalid currency code formats. | 214 // Invalid currency code formats. |
177 ['Invalid currency code US1 should throw', null, function() { | 215 ['Invalid currency code US1 should throw', null, function() { |
178 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currency'
: 'US1'})) | 216 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currency'
: 'US1'})) |
179 }], | 217 }], |
180 ['Invalid currency code US should throw', null, function() { | 218 ['Invalid currency code US should throw', null, function() { |
181 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currency'
: 'US'})) | 219 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currency'
: 'US'})) |
182 }], | 220 }], |
183 ['Invalid currency code USDO should throw', null, function() { | 221 ['Invalid currency code USDO should throw', null, function() { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 }], | 273 }], |
236 ['Null amount should throw', null, function() { | 274 ['Null amount should throw', null, function() { |
237 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': n
ull})) | 275 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': n
ull})) |
238 }], | 276 }], |
239 ['Undefined amount should throw', null, function() { | 277 ['Undefined amount should throw', null, function() { |
240 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': u
ndefined})) | 278 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': u
ndefined})) |
241 }], | 279 }], |
242 ]); | 280 ]); |
243 } | 281 } |
244 </script> | 282 </script> |
OLD | NEW |