| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/payments/PaymentRequest.h" | 5 #include "modules/payments/PaymentRequest.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/JSONValuesForV8.h" | 7 #include "bindings/core/v8/JSONValuesForV8.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" | 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "modules/payments/PaymentAddress.h" | |
| 12 #include "modules/payments/PaymentTestHelper.h" | 11 #include "modules/payments/PaymentTestHelper.h" |
| 13 #include "platform/heap/HeapAllocator.h" | 12 #include "platform/heap/HeapAllocator.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace blink { | 15 namespace blink { |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 TEST(PaymentRequestTest, SecureContextRequired) | 18 TEST(PaymentRequestTest, SecureContextRequired) |
| 20 { | 19 { |
| 21 V8TestingScope scope; | 20 V8TestingScope scope; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 V8TestingScope scope; | 215 V8TestingScope scope; |
| 217 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | 216 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); |
| 218 makePaymentRequestOriginSecure(scope.document()); | 217 makePaymentRequestOriginSecure(scope.document()); |
| 219 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | 218 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); |
| 220 EXPECT_FALSE(scope.getExceptionState().hadException()); | 219 EXPECT_FALSE(scope.getExceptionState().hadException()); |
| 221 request->show(scope.getScriptState()); | 220 request->show(scope.getScriptState()); |
| 222 | 221 |
| 223 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall()); | 222 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall()); |
| 224 } | 223 } |
| 225 | 224 |
| 226 TEST(PaymentRequestTest, CannotCallCompleteTwice) | |
| 227 { | |
| 228 V8TestingScope scope; | |
| 229 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | |
| 230 makePaymentRequestOriginSecure(scope.document()); | |
| 231 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | |
| 232 EXPECT_FALSE(scope.getExceptionState().hadException()); | |
| 233 request->show(scope.getScriptState()); | |
| 234 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | |
| 235 request->complete(scope.getScriptState(), Fail); | |
| 236 | |
| 237 request->complete(scope.getScriptState(), Success).then(funcs.expectNoCall()
, funcs.expectCall()); | |
| 238 } | |
| 239 | |
| 240 TEST(PaymentRequestTest, RejectShowPromiseOnErrorPaymentMethodNotSupported) | 225 TEST(PaymentRequestTest, RejectShowPromiseOnErrorPaymentMethodNotSupported) |
| 241 { | 226 { |
| 242 V8TestingScope scope; | 227 V8TestingScope scope; |
| 243 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | 228 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); |
| 244 makePaymentRequestOriginSecure(scope.document()); | 229 makePaymentRequestOriginSecure(scope.document()); |
| 245 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | 230 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); |
| 246 EXPECT_FALSE(scope.getExceptionState().hadException()); | 231 EXPECT_FALSE(scope.getExceptionState().hadException()); |
| 247 | 232 |
| 248 String errorMessage; | 233 String errorMessage; |
| 249 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); | 234 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 264 | 249 |
| 265 String errorMessage; | 250 String errorMessage; |
| 266 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); | 251 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); |
| 267 | 252 |
| 268 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError(mojom::bl
ink::PaymentErrorReason::USER_CANCEL); | 253 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError(mojom::bl
ink::PaymentErrorReason::USER_CANCEL); |
| 269 | 254 |
| 270 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); | 255 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); |
| 271 EXPECT_EQ("Request cancelled", errorMessage); | 256 EXPECT_EQ("Request cancelled", errorMessage); |
| 272 } | 257 } |
| 273 | 258 |
| 274 TEST(PaymentRequestTest, RejectCompletePromiseOnError) | |
| 275 { | |
| 276 V8TestingScope scope; | |
| 277 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | |
| 278 makePaymentRequestOriginSecure(scope.document()); | |
| 279 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | |
| 280 EXPECT_FALSE(scope.getExceptionState().hadException()); | |
| 281 request->show(scope.getScriptState()); | |
| 282 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | |
| 283 | |
| 284 String errorMessage; | |
| 285 request->complete(scope.getScriptState(), Success).then(funcs.expectNoCall()
, funcs.expectCall(&errorMessage)); | |
| 286 | |
| 287 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError(mojom::bl
ink::PaymentErrorReason::UNKNOWN); | |
| 288 | |
| 289 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); | |
| 290 EXPECT_EQ("UnknownError: Request failed", errorMessage); | |
| 291 } | |
| 292 | |
| 293 // If user cancels the transaction during processing, the complete() promise | |
| 294 // should be rejected. | |
| 295 TEST(PaymentRequestTest, RejectCompletePromiseAfterError) | |
| 296 { | |
| 297 V8TestingScope scope; | |
| 298 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | |
| 299 makePaymentRequestOriginSecure(scope.document()); | |
| 300 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | |
| 301 EXPECT_FALSE(scope.getExceptionState().hadException()); | |
| 302 request->show(scope.getScriptState()); | |
| 303 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | |
| 304 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError(mojom::bl
ink::PaymentErrorReason::USER_CANCEL); | |
| 305 | |
| 306 request->complete(scope.getScriptState(), Success).then(funcs.expectNoCall()
, funcs.expectCall()); | |
| 307 } | |
| 308 | |
| 309 TEST(PaymentRequestTest, ResolvePromiseOnComplete) | |
| 310 { | |
| 311 V8TestingScope scope; | |
| 312 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | |
| 313 makePaymentRequestOriginSecure(scope.document()); | |
| 314 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | |
| 315 EXPECT_FALSE(scope.getExceptionState().hadException()); | |
| 316 request->show(scope.getScriptState()); | |
| 317 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | |
| 318 | |
| 319 request->complete(scope.getScriptState(), Success).then(funcs.expectCall(),
funcs.expectNoCall()); | |
| 320 | |
| 321 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnComplete(); | |
| 322 } | |
| 323 | |
| 324 TEST(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure) | 259 TEST(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure) |
| 325 { | 260 { |
| 326 V8TestingScope scope; | 261 V8TestingScope scope; |
| 327 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | 262 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); |
| 328 makePaymentRequestOriginSecure(scope.document()); | 263 makePaymentRequestOriginSecure(scope.document()); |
| 329 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | 264 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); |
| 330 EXPECT_FALSE(scope.getExceptionState().hadException()); | 265 EXPECT_FALSE(scope.getExceptionState().hadException()); |
| 331 | 266 |
| 332 String errorMessage; | 267 String errorMessage; |
| 333 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); | 268 request->show(scope.getScriptState()).then(funcs.expectNoCall(), funcs.expec
tCall(&errorMessage)); |
| 334 | 269 |
| 335 request->onUpdatePaymentDetailsFailure("oops"); | 270 request->onUpdatePaymentDetailsFailure("oops"); |
| 336 | 271 |
| 337 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); | 272 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); |
| 338 EXPECT_EQ("AbortError: oops", errorMessage); | 273 EXPECT_EQ("AbortError: oops", errorMessage); |
| 339 } | 274 } |
| 340 | 275 |
| 341 TEST(PaymentRequestTest, RejectCompletePromiseOnUpdateDetailsFailure) | |
| 342 { | |
| 343 V8TestingScope scope; | |
| 344 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | |
| 345 makePaymentRequestOriginSecure(scope.document()); | |
| 346 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | |
| 347 EXPECT_FALSE(scope.getExceptionState().hadException()); | |
| 348 request->show(scope.getScriptState()).then(funcs.expectCall(), funcs.expectN
oCall()); | |
| 349 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | |
| 350 | |
| 351 String errorMessage; | |
| 352 request->complete(scope.getScriptState(), Success).then(funcs.expectNoCall()
, funcs.expectCall(&errorMessage)); | |
| 353 | |
| 354 request->onUpdatePaymentDetailsFailure("oops"); | |
| 355 | |
| 356 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); | |
| 357 EXPECT_EQ("AbortError: oops", errorMessage); | |
| 358 } | |
| 359 | |
| 360 TEST(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved) | 276 TEST(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved) |
| 361 { | 277 { |
| 362 V8TestingScope scope; | 278 V8TestingScope scope; |
| 363 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); | 279 PaymentRequestMockFunctionScope funcs(scope.getScriptState()); |
| 364 makePaymentRequestOriginSecure(scope.document()); | 280 makePaymentRequestOriginSecure(scope.document()); |
| 365 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); | 281 PaymentRequest* request = PaymentRequest::create(scope.getScriptState(), bui
ldPaymentMethodDataForTest(), buildPaymentDetailsForTest(), scope.getExceptionSt
ate()); |
| 366 EXPECT_FALSE(scope.getExceptionState().hadException()); | 282 EXPECT_FALSE(scope.getExceptionState().hadException()); |
| 367 request->show(scope.getScriptState()).then(funcs.expectCall(), funcs.expectN
oCall()); | 283 request->show(scope.getScriptState()).then(funcs.expectCall(), funcs.expectN
oCall()); |
| 368 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); | 284 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(buildPaymentResponseForTest()); |
| 369 | 285 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"US
D\", \"value\": \"50.00\"}, \"selected\": true}]}"; | 374 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"US
D\", \"value\": \"50.00\"}, \"selected\": true}]}"; |
| 459 | 375 |
| 460 request->onUpdatePaymentDetails(ScriptValue::from(scope.getScriptState(), fr
omJSONString(scope.getScriptState(), detail, scope.getExceptionState()))); | 376 request->onUpdatePaymentDetails(ScriptValue::from(scope.getScriptState(), fr
omJSONString(scope.getScriptState(), detail, scope.getExceptionState()))); |
| 461 EXPECT_FALSE(scope.getExceptionState().hadException()); | 377 EXPECT_FALSE(scope.getExceptionState().hadException()); |
| 462 | 378 |
| 463 EXPECT_EQ("fast", request->shippingOption()); | 379 EXPECT_EQ("fast", request->shippingOption()); |
| 464 } | 380 } |
| 465 | 381 |
| 466 } // namespace | 382 } // namespace |
| 467 } // namespace blink | 383 } // namespace blink |
| OLD | NEW |