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

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: Rebase 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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 { 355 {
326 ScriptState::Scope scope(getScriptState()); 356 ScriptState::Scope scope(getScriptState());
327 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 357 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
328 EXPECT_FALSE(getExceptionState().hadException()); 358 EXPECT_FALSE(getExceptionState().hadException());
329 request->show(getScriptState()); 359 request->show(getScriptState());
330 static_cast<ContextLifecycleObserver*>(request)->contextDestroyed(); 360 static_cast<ContextLifecycleObserver*>(request)->contextDestroyed();
331 361
332 ThreadHeap::collectAllGarbage(); 362 ThreadHeap::collectAllGarbage();
333 } 363 }
334 364
365 TEST_F(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure)
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
371 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
372
373 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), " oops"));
374 }
375
376 TEST_F(PaymentRequestTest, RejectCompletePromiseOnUpdateDetailsFailure)
377 {
378 ScriptState::Scope scope(getScriptState());
379 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
380 EXPECT_FALSE(getExceptionState().hadException());
381 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
382 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
383
384 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
385
386 request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), " oops"));
387 }
388
389 TEST_F(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved)
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 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
395 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
396
397 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "foo"));
398 }
399
400 TEST_F(PaymentRequestTest, RejectShowPromiseOnNonPaymentDetailsUpdate)
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(), "NotPaym entDetails"));
409 }
410
411 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidPaymentDetailsUpdate)
412 {
413 ScriptState::Scope scope(getScriptState());
414 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
415 EXPECT_FALSE(getExceptionState().hadException());
416
417 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
418
419 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), "{}", getExceptionState())));
420 EXPECT_FALSE(getExceptionState().hadException());
421 }
422
423 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithoutShipp ingOptions)
424 {
425 ScriptState::Scope scope(getScriptState());
426 PaymentDetails details;
427 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
428 PaymentOptions options;
429 options.setRequestShipping(true);
430 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
431 EXPECT_FALSE(getExceptionState().hadException());
432 EXPECT_TRUE(request->shippingOption().isNull());
433 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
434 String detailWithShippingOptions = "{\"items\": [{\"id\": \"total\", \"label \": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
435 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\ "}}]}";
436 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detailWithShippingOptions, getExceptionState())));
437 EXPECT_FALSE(getExceptionState().hadException());
438 EXPECT_EQ("standardShippingOption", request->shippingOption());
439 String detailWithoutShippingOptions = "{\"items\": [{\"id\": \"total\", \"la bel\": \"Total\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}] }";
440
441 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detailWithoutShippingOptions, getExceptionState())));
442
443 EXPECT_FALSE(getExceptionState().hadException());
444 EXPECT_TRUE(request->shippingOption().isNull());
445 }
446
447 TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithMultiple ShippingOptions)
448 {
449 ScriptState::Scope scope(getScriptState());
450 PaymentOptions options;
451 options.setRequestShipping(true);
452 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState());
453 EXPECT_FALSE(getExceptionState().hadException());
454 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
455 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
456 "\"shippingOptions\": [{\"id\": \"slow\", \"label\": \"Slow\", \"amount\ ": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}},"
457 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"50.00\"}}]}";
458
459 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState())));
460 EXPECT_FALSE(getExceptionState().hadException());
461
462 EXPECT_TRUE(request->shippingOption().isNull());
463 }
464
465 TEST_F(PaymentRequestTest, UseTheSingleShippingOptionFromPaymentDetailsUpdate)
466 {
467 ScriptState::Scope scope(getScriptState());
468 PaymentOptions options;
469 options.setRequestShipping(true);
470 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState());
471 EXPECT_FALSE(getExceptionState().hadException());
472 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
473 String detail = "{\"items\": [{\"id\": \"total\", \"label\": \"Total\", \"am ount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\"}}],"
474 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currencyCode\": \"USD\", \"value\": \"5.00\ "}}]}";
475
476 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState())));
477 EXPECT_FALSE(getExceptionState().hadException());
478
479 EXPECT_EQ("standardShippingOption", request->shippingOption());
480 }
481
335 } // namespace 482 } // namespace
336 } // namespace blink 483 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698