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/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/JSONValuesForV8.h" |
8 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
9 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
10 #include "core/testing/DummyPageHolder.h" | 11 #include "core/testing/DummyPageHolder.h" |
11 #include "modules/payments/CurrencyAmount.h" | 12 #include "modules/payments/CurrencyAmount.h" |
12 #include "modules/payments/PaymentDetailsTestHelper.h" | 13 #include "modules/payments/PaymentDetailsTestHelper.h" |
13 #include "modules/payments/PaymentItem.h" | 14 #include "modules/payments/PaymentItem.h" |
14 #include "modules/payments/ShippingOption.h" | 15 #include "modules/payments/ShippingOption.h" |
15 #include "platform/heap/HeapAllocator.h" | 16 #include "platform/heap/HeapAllocator.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 { | 219 { |
219 ScriptState::Scope scope(getScriptState()); | 220 ScriptState::Scope scope(getScriptState()); |
220 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 221 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
221 EXPECT_FALSE(getExceptionState().hadException()); | 222 EXPECT_FALSE(getExceptionState().hadException()); |
222 | 223 |
223 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); | 224 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
224 | 225 |
225 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(mojom::blink::ShippingAddress::New()); | 226 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(mojom::blink::ShippingAddress::New()); |
226 } | 227 } |
227 | 228 |
| 229 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddressInResponse) |
| 230 { |
| 231 ScriptState::Scope scope(getScriptState()); |
| 232 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 233 EXPECT_FALSE(getExceptionState().hadException()); |
| 234 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N
ew(); |
| 235 response->shipping_address = mojom::blink::ShippingAddress::New(); |
| 236 |
| 237 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
| 238 |
| 239 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(std::move(response)); |
| 240 } |
| 241 |
228 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress) | 242 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress) |
229 { | 243 { |
230 ScriptState::Scope scope(getScriptState()); | 244 ScriptState::Scope scope(getScriptState()); |
231 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 245 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
232 EXPECT_FALSE(getExceptionState().hadException()); | 246 EXPECT_FALSE(getExceptionState().hadException()); |
233 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd
ress::New(); | 247 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd
ress::New(); |
234 shippingAddress->region_code = "US"; | 248 shippingAddress->region_code = "US"; |
235 shippingAddress->language_code = "en"; | 249 shippingAddress->language_code = "en"; |
236 shippingAddress->script_code = "Latn"; | 250 shippingAddress->script_code = "Latn"; |
237 | 251 |
238 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectNoCall(getScriptState())); | 252 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectNoCall(getScriptState())); |
239 | 253 |
240 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(std::move(shippingAddress)); | 254 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(std::move(shippingAddress)); |
241 } | 255 } |
242 | 256 |
| 257 TEST_F(PaymentRequestTest, ResolveShowPromiseWithValidShippingAddressInResponse) |
| 258 { |
| 259 ScriptState::Scope scope(getScriptState()); |
| 260 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 261 EXPECT_FALSE(getExceptionState().hadException()); |
| 262 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N
ew(); |
| 263 response->shipping_address = mojom::blink::ShippingAddress::New(); |
| 264 response->shipping_address->region_code = "US"; |
| 265 response->shipping_address->language_code = "en"; |
| 266 response->shipping_address->script_code = "Latn"; |
| 267 |
| 268 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState
()), MockFunction::expectNoCall(getScriptState())); |
| 269 |
| 270 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(std::move(response)); |
| 271 } |
| 272 |
243 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse) | 273 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse) |
244 { | 274 { |
245 ScriptState::Scope scope(getScriptState()); | 275 ScriptState::Scope scope(getScriptState()); |
246 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 276 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
247 EXPECT_FALSE(getExceptionState().hadException()); | 277 EXPECT_FALSE(getExceptionState().hadException()); |
248 | 278 |
249 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState
()), MockFunction::expectNoCall(getScriptState())); | 279 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState
()), MockFunction::expectNoCall(getScriptState())); |
250 | 280 |
251 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); | 281 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); |
252 } | 282 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 344 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
315 EXPECT_FALSE(getExceptionState().hadException()); | 345 EXPECT_FALSE(getExceptionState().hadException()); |
316 request->show(getScriptState()); | 346 request->show(getScriptState()); |
317 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); | 347 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); |
318 | 348 |
319 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS
criptState()), MockFunction::expectNoCall(getScriptState())); | 349 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS
criptState()), MockFunction::expectNoCall(getScriptState())); |
320 | 350 |
321 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnComplete(); | 351 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnComplete(); |
322 } | 352 } |
323 | 353 |
| 354 TEST_F(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure) |
| 355 { |
| 356 ScriptState::Scope scope(getScriptState()); |
| 357 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 358 EXPECT_FALSE(getExceptionState().hadException()); |
| 359 |
| 360 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
| 361 |
| 362 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), "
oops")); |
| 363 } |
| 364 |
| 365 TEST_F(PaymentRequestTest, RejectCompletePromiseOnUpdateDetailsFailure) |
| 366 { |
| 367 ScriptState::Scope scope(getScriptState()); |
| 368 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 369 EXPECT_FALSE(getExceptionState().hadException()); |
| 370 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState
()), MockFunction::expectNoCall(getScriptState())); |
| 371 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); |
| 372 |
| 373 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge
tScriptState()), MockFunction::expectCall(getScriptState())); |
| 374 |
| 375 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), "
oops")); |
| 376 } |
| 377 |
| 378 TEST_F(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved) |
| 379 { |
| 380 ScriptState::Scope scope(getScriptState()); |
| 381 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 382 EXPECT_FALSE(getExceptionState().hadException()); |
| 383 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState
()), MockFunction::expectNoCall(getScriptState())); |
| 384 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(mojom::blink::PaymentResponse::New()); |
| 385 |
| 386 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "foo")); |
| 387 } |
| 388 |
| 389 TEST_F(PaymentRequestTest, RejectShowPromiseOnNonPaymentDetailsUpdate) |
| 390 { |
| 391 ScriptState::Scope scope(getScriptState()); |
| 392 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 393 EXPECT_FALSE(getExceptionState().hadException()); |
| 394 |
| 395 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
| 396 |
| 397 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "NotPaym
entDetails")); |
| 398 } |
| 399 |
| 400 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidPaymentDetailsUpdate) |
| 401 { |
| 402 ScriptState::Scope scope(getScriptState()); |
| 403 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 404 EXPECT_FALSE(getExceptionState().hadException()); |
| 405 |
| 406 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
| 407 |
| 408 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), "{}", getExceptionState()))); |
| 409 EXPECT_FALSE(getExceptionState().hadException()); |
| 410 } |
| 411 |
| 412 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithoutShipp
ingOptions) |
| 413 { |
| 414 ScriptState::Scope scope(getScriptState()); |
| 415 PaymentDetails details; |
| 416 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); |
| 417 PaymentOptions options; |
| 418 options.setRequestShipping(true); |
| 419 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, options, getExceptionState()); |
| 420 EXPECT_FALSE(getExceptionState().hadException()); |
| 421 EXPECT_TRUE(request->shippingOption().isNull()); |
| 422 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectNoCall(getScriptState())); |
| 423 String detailWithShippingOptions = "{\"items\": [{\"id\": \"total\", \"label
\": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}]," |
| 424 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \
"Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\
"}}]}"; |
| 425 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detailWithShippingOptions, getExceptionState()))); |
| 426 EXPECT_FALSE(getExceptionState().hadException()); |
| 427 EXPECT_EQ("standardShippingOption", request->shippingOption()); |
| 428 String detailWithoutShippingOptions = "{\"items\": [{\"id\": \"total\", \"la
bel\": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}]
}"; |
| 429 |
| 430 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detailWithoutShippingOptions, getExceptionState()))); |
| 431 |
| 432 EXPECT_FALSE(getExceptionState().hadException()); |
| 433 EXPECT_TRUE(request->shippingOption().isNull()); |
| 434 } |
| 435 |
| 436 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithMultiple
ShippingOptions) |
| 437 { |
| 438 ScriptState::Scope scope(getScriptState()); |
| 439 PaymentOptions options; |
| 440 options.setRequestShipping(true); |
| 441 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); |
| 442 EXPECT_FALSE(getExceptionState().hadException()); |
| 443 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectNoCall(getScriptState())); |
| 444 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am
ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}]," |
| 445 "\"shippingOptions\": [{\"id\": \"slow\", \"label\": \"Slow\", \"amount\
": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}," |
| 446 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currencyCode\":
\"USD\", \"value\": \"50.00\"}}]}"; |
| 447 |
| 448 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detail, getExceptionState()))); |
| 449 EXPECT_FALSE(getExceptionState().hadException()); |
| 450 |
| 451 EXPECT_TRUE(request->shippingOption().isNull()); |
| 452 } |
| 453 |
| 454 TEST_F(PaymentRequestTest, UseTheSingleShippingOptionFromPaymentDetailsUpdate) |
| 455 { |
| 456 ScriptState::Scope scope(getScriptState()); |
| 457 PaymentOptions options; |
| 458 options.setRequestShipping(true); |
| 459 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); |
| 460 EXPECT_FALSE(getExceptionState().hadException()); |
| 461 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectNoCall(getScriptState())); |
| 462 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am
ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}]," |
| 463 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \
"Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\
"}}]}"; |
| 464 |
| 465 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detail, getExceptionState()))); |
| 466 EXPECT_FALSE(getExceptionState().hadException()); |
| 467 |
| 468 EXPECT_EQ("standardShippingOption", request->shippingOption()); |
| 469 } |
| 470 |
324 } // namespace | 471 } // namespace |
325 } // namespace blink | 472 } // namespace blink |
OLD | NEW |