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" | |
8 #include "bindings/core/v8/JSONValuesForV8.h" | 7 #include "bindings/core/v8/JSONValuesForV8.h" |
| 8 #include "bindings/core/v8/ScriptFunction.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "bindings/modules/v8/V8PaymentResponse.h" | 10 #include "bindings/modules/v8/V8PaymentResponse.h" |
11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
12 #include "core/testing/DummyPageHolder.h" | 12 #include "modules/payments/MockFunction.h" |
13 #include "modules/payments/CurrencyAmount.h" | |
14 #include "modules/payments/PaymentAddress.h" | 13 #include "modules/payments/PaymentAddress.h" |
15 #include "modules/payments/PaymentDetailsTestHelper.h" | 14 #include "modules/payments/PaymentDetailsTestHelper.h" |
16 #include "modules/payments/PaymentItem.h" | 15 #include "modules/payments/PaymentRequestTestBase.h" |
17 #include "modules/payments/ShippingOption.h" | |
18 #include "platform/heap/HeapAllocator.h" | 16 #include "platform/heap/HeapAllocator.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "wtf/OwnPtr.h" | |
22 #include <utility> | 18 #include <utility> |
23 | 19 |
24 namespace blink { | 20 namespace blink { |
25 namespace { | 21 namespace { |
26 | 22 |
27 class PaymentRequestTest : public testing::Test { | 23 class PaymentRequestTest : public PaymentRequestTestBase { |
28 public: | |
29 PaymentRequestTest() | |
30 : m_page(DummyPageHolder::create()) | |
31 { | |
32 setSecurityOrigin("https://www.example.com/"); | |
33 } | |
34 | |
35 ~PaymentRequestTest() override {} | |
36 | |
37 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } | |
38 ExceptionState& getExceptionState() { return m_exceptionState; } | |
39 | |
40 void setSecurityOrigin(const String& securityOrigin) | |
41 { | |
42 m_page->document().setSecurityOrigin(SecurityOrigin::create(KURL(KURL(),
securityOrigin))); | |
43 } | |
44 | |
45 private: | |
46 OwnPtr<DummyPageHolder> m_page; | |
47 TrackExceptionState m_exceptionState; | |
48 }; | 24 }; |
49 | 25 |
50 TEST_F(PaymentRequestTest, NoExceptionWithValidData) | 26 TEST_F(PaymentRequestTest, NoExceptionWithValidData) |
51 { | 27 { |
52 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), buildPaym
entDetailsForTest(), getExceptionState()); | 28 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), buildPaym
entDetailsForTest(), getExceptionState()); |
53 | 29 |
54 EXPECT_FALSE(getExceptionState().hadException()); | 30 EXPECT_FALSE(getExceptionState().hadException()); |
55 } | 31 } |
56 | 32 |
57 TEST_F(PaymentRequestTest, SecureContextRequired) | 33 TEST_F(PaymentRequestTest, SecureContextRequired) |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 shippingOptions[1].setSelected(true); | 161 shippingOptions[1].setSelected(true); |
186 details.setShippingOptions(shippingOptions); | 162 details.setShippingOptions(shippingOptions); |
187 PaymentOptions options; | 163 PaymentOptions options; |
188 options.setRequestShipping(true); | 164 options.setRequestShipping(true); |
189 | 165 |
190 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, options, getExceptionState()); | 166 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), details, options, getExceptionState()); |
191 | 167 |
192 EXPECT_EQ("express", request->shippingOption()); | 168 EXPECT_EQ("express", request->shippingOption()); |
193 } | 169 } |
194 | 170 |
195 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow) | |
196 { | |
197 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | |
198 EXPECT_FALSE(getExceptionState().hadException()); | |
199 | |
200 request->abort(getExceptionState()); | |
201 EXPECT_TRUE(getExceptionState().hadException()); | |
202 } | |
203 | |
204 class MockFunction : public ScriptFunction { | |
205 public: | |
206 static v8::Local<v8::Function> noExpectations(ScriptState* scriptState) | |
207 { | |
208 MockFunction* self = new MockFunction(scriptState); | |
209 return self->bindToV8Function(); | |
210 } | |
211 | |
212 static v8::Local<v8::Function> expectCall(ScriptState* scriptState) | |
213 { | |
214 MockFunction* self = new MockFunction(scriptState); | |
215 EXPECT_CALL(*self, call(testing::_)); | |
216 return self->bindToV8Function(); | |
217 } | |
218 | |
219 static v8::Local<v8::Function> expectNoCall(ScriptState* scriptState) | |
220 { | |
221 MockFunction* self = new MockFunction(scriptState); | |
222 EXPECT_CALL(*self, call(testing::_)).Times(0); | |
223 return self->bindToV8Function(); | |
224 } | |
225 | |
226 private: | |
227 explicit MockFunction(ScriptState* scriptState) | |
228 : ScriptFunction(scriptState) | |
229 { | |
230 ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>()); | |
231 } | |
232 | |
233 MOCK_METHOD1(call, ScriptValue(ScriptValue)); | |
234 }; | |
235 | |
236 class PaymentResponseFunction : public ScriptFunction { | 171 class PaymentResponseFunction : public ScriptFunction { |
237 public: | 172 public: |
238 static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue*
outValue) | 173 static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue*
outValue) |
239 { | 174 { |
240 PaymentResponseFunction* self = new PaymentResponseFunction(scriptState,
outValue); | 175 PaymentResponseFunction* self = new PaymentResponseFunction(scriptState,
outValue); |
241 return self->bindToV8Function(); | 176 return self->bindToV8Function(); |
242 } | 177 } |
243 | 178 |
244 ScriptValue call(ScriptValue value) override | 179 ScriptValue call(ScriptValue value) override |
245 { | 180 { |
246 DCHECK(!value.isEmpty()); | 181 DCHECK(!value.isEmpty()); |
247 *m_value = value; | 182 *m_value = value; |
248 return value; | 183 return value; |
249 } | 184 } |
250 | 185 |
251 private: | 186 private: |
252 PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outValue) | 187 PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outValue) |
253 : ScriptFunction(scriptState) | 188 : ScriptFunction(scriptState) |
254 , m_value(outValue) | 189 , m_value(outValue) |
255 { | 190 { |
256 DCHECK(m_value); | 191 DCHECK(m_value); |
257 } | 192 } |
258 | 193 |
259 ScriptValue* m_value; | 194 ScriptValue* m_value; |
260 }; | 195 }; |
261 | 196 |
262 TEST_F(PaymentRequestTest, CanAbortAfterShow) | |
263 { | |
264 ScriptState::Scope scope(getScriptState()); | |
265 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | |
266 EXPECT_FALSE(getExceptionState().hadException()); | |
267 | |
268 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::noExpectations(getScriptState())); | |
269 request->abort(getExceptionState()); | |
270 | |
271 EXPECT_FALSE(getExceptionState().hadException()); | |
272 } | |
273 | |
274 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress) | 197 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress) |
275 { | 198 { |
276 ScriptState::Scope scope(getScriptState()); | 199 ScriptState::Scope scope(getScriptState()); |
277 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 200 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
278 EXPECT_FALSE(getExceptionState().hadException()); | 201 EXPECT_FALSE(getExceptionState().hadException()); |
279 | 202 |
280 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); | 203 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
281 | 204 |
282 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(mojom::blink::PaymentAddress::New()); | 205 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(mojom::blink::PaymentAddress::New()); |
283 } | 206 } |
(...skipping 27 matching lines...) Expand all Loading... |
311 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(std::move(response)); | 234 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(std::move(response)); |
312 } | 235 } |
313 | 236 |
314 TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingFalseAndShippingA
ddressExistsInResponse) | 237 TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingFalseAndShippingA
ddressExistsInResponse) |
315 { | 238 { |
316 ScriptState::Scope scope(getScriptState()); | 239 ScriptState::Scope scope(getScriptState()); |
317 PaymentOptions options; | 240 PaymentOptions options; |
318 options.setRequestShipping(false); | 241 options.setRequestShipping(false); |
319 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | 242 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); |
320 EXPECT_FALSE(getExceptionState().hadException()); | 243 EXPECT_FALSE(getExceptionState().hadException()); |
321 mojom::blink::PaymentAddressPtr shippingAddress = mojom::blink::PaymentAddre
ss::New(); | 244 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N
ew(); |
322 shippingAddress->country = "US"; | 245 response->shipping_address = mojom::blink::PaymentAddress::New(); |
323 shippingAddress->language_code = "en"; | 246 response->shipping_address->country = "US"; |
324 shippingAddress->script_code = "Latn"; | 247 response->shipping_address->language_code = "en"; |
| 248 response->shipping_address->script_code = "Latn"; |
325 | 249 |
326 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); | 250 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta
te()), MockFunction::expectCall(getScriptState())); |
327 | 251 |
328 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress
Change(std::move(shippingAddress)); | 252 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse
(std::move(response)); |
329 } | 253 } |
330 | 254 |
331 TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingTrueAndValidShip
pingAddressInResponse) | 255 TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingTrueAndValidShip
pingAddressInResponse) |
332 { | 256 { |
333 ScriptState::Scope scope(getScriptState()); | 257 ScriptState::Scope scope(getScriptState()); |
334 PaymentOptions options; | 258 PaymentOptions options; |
335 options.setRequestShipping(true); | 259 options.setRequestShipping(true); |
336 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | 260 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St
ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); |
337 EXPECT_FALSE(getExceptionState().hadException()); | 261 EXPECT_FALSE(getExceptionState().hadException()); |
338 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N
ew(); | 262 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N
ew(); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"US
D\", \"value\": \"50.00\"}, \"selected\": true}]}"; | 477 "{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"US
D\", \"value\": \"50.00\"}, \"selected\": true}]}"; |
554 | 478 |
555 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detail, getExceptionState()))); | 479 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON
String(getScriptState(), detail, getExceptionState()))); |
556 EXPECT_FALSE(getExceptionState().hadException()); | 480 EXPECT_FALSE(getExceptionState().hadException()); |
557 | 481 |
558 EXPECT_EQ("fast", request->shippingOption()); | 482 EXPECT_EQ("fast", request->shippingOption()); |
559 } | 483 } |
560 | 484 |
561 } // namespace | 485 } // namespace |
562 } // namespace blink | 486 } // namespace blink |
OLD | NEW |