Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp

Issue 1931233002: Implement PaymentRequestUpdateEvent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@explicit-shipping
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 { 218 {
218 ScriptState::Scope scope(getScriptState()); 219 ScriptState::Scope scope(getScriptState());
219 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 220 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
220 EXPECT_FALSE(getExceptionState().hadException()); 221 EXPECT_FALSE(getExceptionState().hadException());
221 222
222 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); 223 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
223 224
224 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(mojo m::blink::ShippingAddress::New()); 225 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(mojo m::blink::ShippingAddress::New());
225 } 226 }
226 227
228 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddressInResponse)
229 {
230 ScriptState::Scope scope(getScriptState());
231 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
232 EXPECT_FALSE(getExceptionState().hadException());
233 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew();
234 response->shipping_address = mojom::blink::ShippingAddress::New();
235
236 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
237
238 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(std::move( response));
239 }
240
227 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress) 241 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress)
228 { 242 {
229 ScriptState::Scope scope(getScriptState()); 243 ScriptState::Scope scope(getScriptState());
230 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 244 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
231 EXPECT_FALSE(getExceptionState().hadException()); 245 EXPECT_FALSE(getExceptionState().hadException());
232 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd ress::New(); 246 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd ress::New();
233 shippingAddress->region_code = "US"; 247 shippingAddress->region_code = "US";
234 shippingAddress->language_code = "en"; 248 shippingAddress->language_code = "en";
235 shippingAddress->script_code = "Latn"; 249 shippingAddress->script_code = "Latn";
236 250
237 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState())); 251 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
238 252
239 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(std: :move(shippingAddress)); 253 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(std: :move(shippingAddress));
240 } 254 }
241 255
256 TEST_F(PaymentRequestTest, ResolveShowPromiseWithValidShippingAddressInResponse)
257 {
258 ScriptState::Scope scope(getScriptState());
259 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
260 EXPECT_FALSE(getExceptionState().hadException());
261 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew();
262 response->shipping_address = mojom::blink::ShippingAddress::New();
263 response->shipping_address->region_code = "US";
264 response->shipping_address->language_code = "en";
265 response->shipping_address->script_code = "Latn";
266
267 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
268
269 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(std::move( response));
270 }
271
242 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse) 272 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse)
243 { 273 {
244 ScriptState::Scope scope(getScriptState()); 274 ScriptState::Scope scope(getScriptState());
245 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 275 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
246 EXPECT_FALSE(getExceptionState().hadException()); 276 EXPECT_FALSE(getExceptionState().hadException());
247 277
248 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState())); 278 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
249 279
250 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New()); 280 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
251 } 281 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 343 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
314 EXPECT_FALSE(getExceptionState().hadException()); 344 EXPECT_FALSE(getExceptionState().hadException());
315 request->show(getScriptState()); 345 request->show(getScriptState());
316 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New()); 346 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
317 347
318 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS criptState()), MockFunction::expectNoCall(getScriptState())); 348 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS criptState()), MockFunction::expectNoCall(getScriptState()));
319 349
320 ((mojom::blink::PaymentRequestClient*)request)->OnComplete(); 350 ((mojom::blink::PaymentRequestClient*)request)->OnComplete();
321 } 351 }
322 352
353 TEST_F(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure)
354 {
355 ScriptState::Scope scope(getScriptState());
356 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
357 EXPECT_FALSE(getExceptionState().hadException());
358
359 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
360
361 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), " oops"));
362 }
363
364 TEST_F(PaymentRequestTest, RejectCompletePromiseOnUpdateDetailsFailure)
365 {
366 ScriptState::Scope scope(getScriptState());
367 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
368 EXPECT_FALSE(getExceptionState().hadException());
369 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
370 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
371
372 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
373
374 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), " oops"));
375 }
376
377 TEST_F(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved)
378 {
379 ScriptState::Scope scope(getScriptState());
380 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
381 EXPECT_FALSE(getExceptionState().hadException());
382 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
383 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
384
385 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "foo"));
386 }
387
388 TEST_F(PaymentRequestTest, RejectShowPromiseOnNonPaymentDetailsUpdate)
389 {
390 ScriptState::Scope scope(getScriptState());
391 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
392 EXPECT_FALSE(getExceptionState().hadException());
393
394 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
395
396 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "NotPaym entDetails"));
397 }
398
399 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidPaymentDetailsUpdate)
400 {
401 ScriptState::Scope scope(getScriptState());
402 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
403 EXPECT_FALSE(getExceptionState().hadException());
404
405 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
406
407 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), "{}", getExceptionState())));
408 EXPECT_FALSE(getExceptionState().hadException());
409 }
410
411 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithoutShipp ingOptions)
412 {
413 ScriptState::Scope scope(getScriptState());
414 PaymentDetails details;
415 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
416 PaymentOptions options;
417 options.setRequestShipping(true);
418 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
419 EXPECT_FALSE(getExceptionState().hadException());
420 EXPECT_TRUE(request->shippingOption().isNull());
421 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
422 String detailWithShippingOptions = "{\"items\": [{\"id\": \"total\", \"label \": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
423 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\ "}}]}";
424 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detailWithShippingOptions, getExceptionState())));
425 EXPECT_FALSE(getExceptionState().hadException());
426 EXPECT_EQ("standardShippingOption", request->shippingOption());
427 String detailWithoutShippingOptions = "{\"items\": [{\"id\": \"total\", \"la bel\": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}] }";
428
429 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detailWithoutShippingOptions, getExceptionState())));
430
431 EXPECT_FALSE(getExceptionState().hadException());
432 EXPECT_TRUE(request->shippingOption().isNull());
433 }
434
435 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithMultiple ShippingOptions)
436 {
437 ScriptState::Scope scope(getScriptState());
438 PaymentOptions options;
439 options.setRequestShipping(true);
440 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState());
441 EXPECT_FALSE(getExceptionState().hadException());
442 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
443 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
444 "\"shippingOptions\": [{\"id\": \"slow\", \"label\": \"Slow\", \"amount\ ": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}},"
445 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"50.00\"}}]}";
446
447 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState())));
448 EXPECT_FALSE(getExceptionState().hadException());
449
450 EXPECT_TRUE(request->shippingOption().isNull());
451 }
452
453 TEST_F(PaymentRequestTest, UseTheSingleShippingOptionFromPaymentDetailsUpdate)
454 {
455 ScriptState::Scope scope(getScriptState());
456 PaymentOptions options;
457 options.setRequestShipping(true);
458 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState());
459 EXPECT_FALSE(getExceptionState().hadException());
460 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
461 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
462 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\ "}}]}";
463
464 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState())));
465 EXPECT_FALSE(getExceptionState().hadException());
466
467 EXPECT_EQ("standardShippingOption", request->shippingOption());
468 }
469
323 } // namespace blink 470 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698