Chromium Code Reviews| 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 test(function() { | 7 function substiute(originalObject, substiuteKeyValuePair) { |
|
rwlbuis
2016/03/29 17:44:22
Typo: substitute substiute with substitute :)
please use gerrit instead
2016/03/29 22:15:44
Done.
| |
| 8 new PaymentRequest([], {}, {}, {}); | 8 for (var key in originalObject) { |
| 9 if (originalObject.hasOwnProperty(key) && substiuteKeyValuePair.hasOwnPr operty(key)) { | |
| 10 originalObject[key] = substiuteKeyValuePair[key]; | |
| 11 } | |
| 12 } | |
| 13 } | |
| 14 | |
| 15 function buildItem(optionalSubstituteKeyValuePair) { | |
| 16 var item = {'id': 'item_id', 'label': 'Item Description', 'amount': {'curren cyCode': 'USD', 'value': '10.00'}}; | |
| 17 | |
| 18 if (optionalSubstituteKeyValuePair) { | |
| 19 substiute(item, optionalSubstituteKeyValuePair); | |
| 20 substiute(item['amount'], optionalSubstituteKeyValuePair); | |
| 21 } | |
| 22 | |
| 23 return item; | |
| 24 } | |
| 25 | |
| 26 function buildDetails(optionalDetailName, optionalSubstituteKeyValuePair) { | |
| 27 var details = {}; | |
| 28 var detailNames = ['items', 'shippingOptions']; | |
| 29 | |
| 30 for (var i in detailNames) { | |
| 31 if (optionalDetailName == detailNames[i]) { | |
| 32 details[detailNames[i]] = [buildItem(optionalSubstituteKeyValuePair) ]; | |
| 33 } else { | |
| 34 details[detailNames[i]] = [buildItem()]; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 return details; | |
| 39 } | |
| 40 | |
| 41 test(function() { | |
| 42 new PaymentRequest(['foo'], buildDetails(), {}, {}); | |
| 9 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.') ; | 43 }, 'Creating a PaymentRequest with empty parameters should not throw or crash.') ; |
| 10 | 44 |
| 11 test(function() { | 45 test(function() { |
| 12 new PaymentRequest([], {}, {}, {}, ''); | 46 new PaymentRequest(['foo'], buildDetails(), {}, {}, ''); |
| 13 }, 'Creating a PaymentRequest with extra parameters should not throw or crash.') ; | 47 }, 'Creating a PaymentRequest with extra parameters should not throw or crash.') ; |
| 14 | 48 |
| 15 test(function() { | 49 test(function() { |
| 16 new PaymentRequest([], {}); | 50 new PaymentRequest(['foo'], buildDetails()); |
| 17 }, 'Creating a PaymentRequest with omitted optional parameters should not throw or crash.'); | 51 }, 'Creating a PaymentRequest with omitted optional parameters should not throw or crash.'); |
| 18 | 52 |
| 19 test(function() { | 53 test(function() { |
| 20 new PaymentRequest([], {}, undefined, undefined); | 54 new PaymentRequest(['foo'], buildDetails(), undefined, undefined); |
| 21 }, 'Creating a PaymentRequest with undefined optional parameters should not thro w or crash.'); | 55 }, 'Creating a PaymentRequest with undefined optional parameters should not thro w or crash.'); |
| 22 | 56 |
| 23 test(function() { | 57 test(function() { |
| 24 new PaymentRequest([], {}, null, null); | 58 new PaymentRequest(['foo'], buildDetails(), null, null); |
| 25 }, 'Creating a PaymentRequest with null optional parameters should not throw or crash.'); | 59 }, 'Creating a PaymentRequest with null optional parameters should not throw or crash.'); |
| 26 | 60 |
| 61 test(function() { | |
| 62 var request = new PaymentRequest(['foo'], buildDetails()); | |
| 63 assert_readonly(request, 'shippingAddress', 'PaymentRequest should have a re adonly shippingAddress property.'); | |
| 64 assert_readonly(request, 'shippingOption', 'PaymentRequest should have a rea donly shippingOption property.'); | |
| 65 }, 'PaymentRequest should have readonly shippingAddress and shippingOption prope rties.'); | |
| 66 | |
| 67 test(function() { | |
| 68 var request = new PaymentRequest(['foo'], buildDetails()); | |
| 69 assert_not_equals(request.onshippingaddresschange, undefined, 'PaymentReques t should have onShippingAddressChange event.'); | |
| 70 assert_not_equals(request.onshippingoptionchange, undefined, 'PaymentRequest should have onShippingOptionChange event.'); | |
| 71 }, 'PaymentRequest should have onShippingAddressChange and onShippingOptionChang e events.'); | |
| 72 | |
| 73 test(function() { | |
| 74 var request = new PaymentRequest(['foo'], buildDetails()); | |
| 75 assert_not_equals(request.abort, undefined, 'PaymentRequest should have abor t() method.'); | |
| 76 assert_not_equals(request.show, undefined, 'PaymentRequest should have show( ) method.'); | |
| 77 }, 'PaymentRequest should have methods abort() and show().'); | |
| 78 | |
| 79 test(function() { | |
| 80 var request = new PaymentRequest(['foo'], buildDetails()); | |
| 81 request.show(); | |
| 82 request.abort(); | |
| 83 }, 'PaymentRequest.abort() and PaymentRequest.show() should take no parameters.' ); | |
| 84 | |
| 85 test(function() { | |
| 86 var request = new PaymentRequest(['foo'], buildDetails(), {'requestShipping' : true}, {'foo': {'gateway': 'bar'}}); | |
| 87 request.show(); | |
| 88 request.abort(); | |
| 89 }, 'Valid data causes no errors.'); | |
| 90 | |
| 91 test(function() { | |
| 92 var request = new PaymentRequest(['foo'], buildDetails('shippingOptions', {' id': 'standard'})); | |
| 93 assert_equals('standard', request.shippingOption); | |
| 94 }, 'Shipping option identifier should default to the single provided option.'); | |
| 95 | |
| 96 test(function() { | |
| 97 var request = new PaymentRequest(['foo'], {"items": [buildItem()]}); | |
| 98 assert_equals(null, request.shippingOption); | |
| 99 }, 'Shipping option identifier should be null when no shipping options are provi ded.'); | |
| 100 | |
| 101 test(function() { | |
| 102 var request = new PaymentRequest(['foo'], {'items': [buildItem()], 'shipping Options': [buildItem({'id': 'standard'}), buildItem({'id': 'express'})]}); | |
| 103 assert_equals(null, request.shippingOption); | |
| 104 }, 'Shipping option identifier should be null at first when multiple shipping op tions are provided.'); | |
| 105 | |
| 27 generate_tests(assert_throws, [ | 106 generate_tests(assert_throws, [ |
| 28 ['PaymentRequest constructor should throw for incorrect parameter types.', n ull, function() { | 107 ['PaymentRequest constructor should throw for incorrect parameter types.', n ull, function() { |
| 29 new PaymentRequest('', '', '', '') | 108 new PaymentRequest('', '', '', '') |
| 30 }], | 109 }], |
| 31 ['PaymentRequest constructor should throw for undefined required parameters. ', null, function() { | 110 ['PaymentRequest constructor should throw for undefined required parameters. ', null, function() { |
| 32 new PaymentRequest(undefined, undefined) | 111 new PaymentRequest(undefined, undefined) |
| 33 }], | 112 }], |
| 34 ['PaymentRequest constructor should throw for null required parameter.', nul l, function() { | 113 ['PaymentRequest constructor should throw for null required parameter.', nul l, function() { |
| 35 new PaymentRequest(null, null) | 114 new PaymentRequest(null, null) |
| 36 }] | 115 }], |
| 116 ['Empty list of supported payment method identifiers should throw.', null, f unction() { | |
| 117 new PaymentRequest([], buildDetails()) | |
| 118 }], | |
| 119 ['Keys in payment method specific data object should match accepted method i dentifiers.', null, function() { | |
| 120 new PaymentRequest(['foo'], buildDetails(), {}, {'bar': {'gateway': 'baz '}}) | |
| 121 }], | |
| 122 ['Empty details should throw', null, function() { | |
| 123 new PaymentRequest(['foo'], {}) | |
| 124 }], | |
| 125 ['Empty items should throw', null, function() { | |
| 126 new PaymentRequest(['foo'], {'items': []}) | |
| 127 }], | |
| 128 ['Aborting before showing should throw.', null, function() { | |
| 129 new PaymentRequest(['foo'], buildDetails()).abort() | |
| 130 }], | |
| 131 | |
| 132 // Payment method specific data should be a JSON-serializable object. | |
| 133 ['Array value for payment method specific data parameter should throw', null , function() { | |
| 134 new PaymentRequest(['foo'], buildDetails(), {}, []) | |
| 135 }], | |
| 136 ['String value for payment method specific data parameter should throw', nul l, function() { | |
| 137 new PaymentRequest(['foo'], buildDetails(), {}, 'foo') | |
| 138 }], | |
| 139 ['Numeric value for payment method specific data parameter should throw', nu ll, function() { | |
| 140 new PaymentRequest(['foo'], buildDetails(), {}, 42) | |
| 141 }], | |
| 142 | |
| 143 // Values in payment method specific data object should be JSON-serializable objects. | |
| 144 ['Array value for one of the payment method specific data pieces should thro w', null, function() { | |
| 145 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': []}) | |
| 146 }], | |
| 147 ['String value for one of the payment method specific data pieces should thr ow', null, function() { | |
| 148 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 'bar'}) | |
| 149 }], | |
| 150 ['Numeric value for one of the payment method specific data pieces should th row', null, function() { | |
| 151 new PaymentRequest(['foo'], buildDetails(), {}, {'foo': 42}) | |
| 152 }], | |
| 37 ]); | 153 ]); |
| 38 | 154 |
| 39 test(function() { | 155 var detailNames = ['items', 'shippingOptions']; |
| 40 var request = new PaymentRequest([], {}); | 156 for (var i in detailNames) { |
| 41 assert_readonly(request, "shippingAddress", 'PaymentRequest should have a re adonly shippingAddress property.'); | 157 generate_tests(assert_throws, [ |
| 42 assert_readonly(request, "shippingOption", 'PaymentRequest should have a rea donly shippingOption property.'); | 158 // Invalid currency code formats. |
| 43 }, 'PaymentRequest should have readonly shippingAddress and shippingOption prope rties.'); | 159 ['Invalid currency code US1 should throw', null, function() { |
| 44 | 160 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': 'US1'})) |
| 45 test(function() { | 161 }], |
| 46 var request = new PaymentRequest([], {}); | 162 ['Invalid currency code US should throw', null, function() { |
| 47 assert_not_equals(request.onshippingaddresschange, undefined, 'PaymentReques t should have onShippingAddressChange event.'); | 163 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': 'US'})) |
| 48 assert_not_equals(request.onshippingoptionchange, undefined, 'PaymentRequest should have onShippingOptionChange event.'); | 164 }], |
| 49 }, 'PaymentRequest should have onShippingAddressChange and onShippingOptionChang e events.'); | 165 ['Invalid currency code USDO should throw', null, function() { |
| 50 | 166 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': 'USDO'})) |
| 51 | 167 }], |
| 52 test(function() { | 168 ['Invalid currency code usd should throw', null, function() { |
| 53 var request = new PaymentRequest([], {}); | 169 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': 'usd'})) |
| 54 assert_not_equals(request.abort, undefined, 'PaymentRequest should have abor t() method.'); | 170 }], |
| 55 assert_not_equals(request.show, undefined, 'PaymentRequest should have show( ) method.'); | 171 ['Empty currency code should throw', null, function() { |
| 56 }, 'PaymentRequest should have methods abort() and show().'); | 172 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': ''})) |
| 57 | 173 }], |
| 58 test(function() { | 174 ['Null currency code should throw', null, function() { |
| 59 var request = new PaymentRequest([], {}); | 175 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': null})) |
| 60 request.abort(); | 176 }], |
| 61 request.show(); | 177 ['Undefined currency code should throw', null, function() { |
| 62 }, 'PaymentRequest.abort() and PaymentRequest.show() should take no parameters.' ); | 178 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'currencyC ode': undefined})) |
| 179 }], | |
| 180 | |
| 181 // Invalid amount formats. | |
| 182 ['Invalid amount "-" should throw', null, function() { | |
| 183 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' -'})) | |
| 184 }], | |
| 185 ['Invalid amount "notdigits" should throw', null, function() { | |
| 186 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' notdigits'})) | |
| 187 }], | |
| 188 ['Invalid amount "ALSONOTDIGITS" should throw', null, function() { | |
| 189 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' ALSONOTDIGITS'})) | |
| 190 }], | |
| 191 ['Invalid amount "10." should throw', null, function() { | |
| 192 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' 10.'})) | |
| 193 }], | |
| 194 ['Invalid amount ".99" should throw', null, function() { | |
| 195 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' .99'})) | |
| 196 }], | |
| 197 ['Invalid amount "-10." should throw', null, function() { | |
| 198 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' -10.'})) | |
| 199 }], | |
| 200 ['Invalid amount "-.99" should throw', null, function() { | |
| 201 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' -.99'})) | |
| 202 }], | |
| 203 ['Invalid amount "10-" should throw', null, function() { | |
| 204 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' 10-'})) | |
| 205 }], | |
| 206 ['Invalid amount "1-0" should throw', null, function() { | |
| 207 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' 1-0'})) | |
| 208 }], | |
| 209 ['Invalid amount "1.0.0" should throw', null, function() { | |
| 210 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' 1.0.0'})) | |
| 211 }], | |
| 212 ['Invalid amount "1/3" should throw', null, function() { | |
| 213 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' 1/3'})) | |
| 214 }], | |
| 215 ['Empty amount should throw', null, function() { | |
| 216 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': ' '})) | |
| 217 }], | |
| 218 ['Null amount should throw', null, function() { | |
| 219 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': n ull})) | |
| 220 }], | |
| 221 ['Undefined amount should throw', null, function() { | |
| 222 new PaymentRequest(['foo'], buildDetails(detailNames[i], {'value': u ndefined})) | |
| 223 }], | |
| 224 ]); | |
| 225 } | |
| 63 </script> | 226 </script> |
| OLD | NEW |