OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/payments/PaymentRequest.h" |
| 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/testing/DummyPageHolder.h" |
| 11 #include "modules/payments/CurrencyAmount.h" |
| 12 #include "modules/payments/PaymentDetailsTestHelper.h" |
| 13 #include "modules/payments/PaymentItem.h" |
| 14 #include "modules/payments/ShippingOption.h" |
| 15 #include "platform/heap/HeapAllocator.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "wtf/OwnPtr.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class PaymentRequestTest : public testing::Test { |
| 24 public: |
| 25 PaymentRequestTest() |
| 26 : m_page(DummyPageHolder::create()) |
| 27 { |
| 28 setSecurityOrigin("https://www.example.com/"); |
| 29 } |
| 30 |
| 31 ~PaymentRequestTest() override {} |
| 32 |
| 33 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } |
| 34 ExceptionState& getExceptionState() { return m_exceptionState; } |
| 35 |
| 36 void setSecurityOrigin(const String& securityOrigin) |
| 37 { |
| 38 m_page->document().setSecurityOrigin(SecurityOrigin::create(KURL(KURL(),
securityOrigin))); |
| 39 } |
| 40 |
| 41 private: |
| 42 OwnPtr<DummyPageHolder> m_page; |
| 43 TrackExceptionState m_exceptionState; |
| 44 }; |
| 45 |
| 46 TEST_F(PaymentRequestTest, NoExceptionWithValidData) |
| 47 { |
| 48 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), buildPaym
entDetailsForTest(), getExceptionState()); |
| 49 |
| 50 EXPECT_FALSE(getExceptionState().hadException()); |
| 51 } |
| 52 |
| 53 TEST_F(PaymentRequestTest, SecureContextRequired) |
| 54 { |
| 55 setSecurityOrigin("http://www.example.com"); |
| 56 |
| 57 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), buildPaym
entDetailsForTest(), getExceptionState()); |
| 58 |
| 59 EXPECT_TRUE(getExceptionState().hadException()); |
| 60 EXPECT_EQ(SecurityError, getExceptionState().code()); |
| 61 } |
| 62 |
| 63 TEST_F(PaymentRequestTest, SupportedMethodListRequired) |
| 64 { |
| 65 PaymentRequest::create(getScriptState(), Vector<String>(), buildPaymentDetai
lsForTest(), getExceptionState()); |
| 66 |
| 67 EXPECT_TRUE(getExceptionState().hadException()); |
| 68 EXPECT_EQ(V8TypeError, getExceptionState().code()); |
| 69 } |
| 70 |
| 71 TEST_F(PaymentRequestTest, ItemListRequired) |
| 72 { |
| 73 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), PaymentDe
tails(), getExceptionState()); |
| 74 |
| 75 EXPECT_TRUE(getExceptionState().hadException()); |
| 76 EXPECT_EQ(V8TypeError, getExceptionState().code()); |
| 77 } |
| 78 |
| 79 TEST_F(PaymentRequestTest, AtLeastOnePaymentDetailsItemRequired) |
| 80 { |
| 81 PaymentDetails emptyItems; |
| 82 emptyItems.setItems(HeapVector<PaymentItem>()); |
| 83 |
| 84 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), emptyItem
s, getExceptionState()); |
| 85 |
| 86 EXPECT_TRUE(getExceptionState().hadException()); |
| 87 EXPECT_EQ(V8TypeError, getExceptionState().code()); |
| 88 } |
| 89 |
| 90 TEST_F(PaymentRequestTest, NullShippingOptionWhenNoOptionsAvailable) |
| 91 { |
| 92 PaymentDetails details = buildPaymentDetailsForTest(); |
| 93 details.setShippingOptions(HeapVector<ShippingOption>()); |
| 94 |
| 95 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, getExceptionState()); |
| 96 |
| 97 EXPECT_TRUE(request->shippingOption().isNull()); |
| 98 } |
| 99 |
| 100 TEST_F(PaymentRequestTest, NullShippingOptionWhenMultipleOptionsAvailable) |
| 101 { |
| 102 PaymentDetails details = buildPaymentDetailsForTest(); |
| 103 EXPECT_LE(2U, details.shippingOptions().size()); |
| 104 |
| 105 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, getExceptionState()); |
| 106 |
| 107 EXPECT_TRUE(request->shippingOption().isNull()); |
| 108 } |
| 109 |
| 110 TEST_F(PaymentRequestTest, SelectSingleAvailableShippingOption) |
| 111 { |
| 112 CurrencyAmount amount; |
| 113 amount.setCurrencyCode("USD"); |
| 114 amount.setValue("10.00"); |
| 115 ShippingOption option; |
| 116 option.setAmount(amount); |
| 117 option.setId("standard"); |
| 118 option.setLabel("Standard shipping"); |
| 119 PaymentDetails details = buildPaymentDetailsForTest(); |
| 120 details.setShippingOptions(HeapVector<ShippingOption>(1, option)); |
| 121 |
| 122 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, getExceptionState()); |
| 123 |
| 124 EXPECT_EQ("standard", request->shippingOption()); |
| 125 } |
| 126 |
| 127 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow) |
| 128 { |
| 129 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 130 EXPECT_FALSE(getExceptionState().hadException()); |
| 131 |
| 132 request->abort(getExceptionState()); |
| 133 EXPECT_TRUE(getExceptionState().hadException()); |
| 134 } |
| 135 |
| 136 } // namespace |
| 137 } // namespace blink |
OLD | NEW |