Chromium Code Reviews| 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/JSONValuesForV8.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/modules/v8/V8PaymentResponse.h" | |
| 10 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 11 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
| 12 #include "modules/payments/CurrencyAmount.h" | 13 #include "modules/payments/CurrencyAmount.h" |
| 14 #include "modules/payments/PaymentAddress.h" | |
| 13 #include "modules/payments/PaymentDetailsTestHelper.h" | 15 #include "modules/payments/PaymentDetailsTestHelper.h" |
| 14 #include "modules/payments/PaymentItem.h" | 16 #include "modules/payments/PaymentItem.h" |
| 15 #include "modules/payments/ShippingOption.h" | 17 #include "modules/payments/ShippingOption.h" |
| 16 #include "platform/heap/HeapAllocator.h" | 18 #include "platform/heap/HeapAllocator.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "wtf/OwnPtr.h" | 21 #include "wtf/OwnPtr.h" |
| 20 #include <utility> | 22 #include <utility> |
| 21 | 23 |
| 22 namespace blink { | 24 namespace blink { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 private: | 176 private: |
| 175 explicit MockFunction(ScriptState* scriptState) | 177 explicit MockFunction(ScriptState* scriptState) |
| 176 : ScriptFunction(scriptState) | 178 : ScriptFunction(scriptState) |
| 177 { | 179 { |
| 178 ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>()); | 180 ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>()); |
| 179 } | 181 } |
| 180 | 182 |
| 181 MOCK_METHOD1(call, ScriptValue(ScriptValue)); | 183 MOCK_METHOD1(call, ScriptValue(ScriptValue)); |
| 182 }; | 184 }; |
| 183 | 185 |
| 186 class PaymentResponseFunction : public ScriptFunction { | |
| 187 public: | |
| 188 static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue* outValue) | |
| 189 { | |
| 190 PaymentResponseFunction* self = new PaymentResponseFunction(scriptState, outValue); | |
| 191 return self->bindToV8Function(); | |
| 192 } | |
| 193 | |
| 194 ScriptValue call(ScriptValue value) override | |
| 195 { | |
| 196 DCHECK(!value.isEmpty()); | |
| 197 *m_value = value; | |
| 198 return value; | |
| 199 } | |
| 200 | |
| 201 private: | |
| 202 PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outValue) | |
| 203 : ScriptFunction(scriptState) | |
| 204 , m_value(outValue) | |
| 205 { | |
| 206 DCHECK(m_value); | |
| 207 } | |
| 208 | |
| 209 ScriptValue* m_value; | |
| 210 }; | |
| 211 | |
| 184 TEST_F(PaymentRequestTest, CanAbortAfterShow) | 212 TEST_F(PaymentRequestTest, CanAbortAfterShow) |
| 185 { | 213 { |
| 186 ScriptState::Scope scope(getScriptState()); | 214 ScriptState::Scope scope(getScriptState()); |
| 187 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 215 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 188 EXPECT_FALSE(getExceptionState().hadException()); | 216 EXPECT_FALSE(getExceptionState().hadException()); |
| 189 | 217 |
| 190 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::noExpectations(getScriptState())); | 218 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::noExpectations(getScriptState())); |
| 191 request->abort(getExceptionState()); | 219 request->abort(getExceptionState()); |
| 192 | 220 |
| 193 EXPECT_FALSE(getExceptionState().hadException()); | 221 EXPECT_FALSE(getExceptionState().hadException()); |
| 194 } | 222 } |
| 195 | 223 |
| 196 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress) | 224 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress) |
| 197 { | 225 { |
| 198 ScriptState::Scope scope(getScriptState()); | 226 ScriptState::Scope scope(getScriptState()); |
| 199 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 227 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 200 EXPECT_FALSE(getExceptionState().hadException()); | 228 EXPECT_FALSE(getExceptionState().hadException()); |
| 201 | 229 |
| 202 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); | 230 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); |
| 203 | 231 |
| 204 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(mojom::blink::PaymentAddress::New()); | 232 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(mojom::blink::PaymentAddress::New()); |
| 205 } | 233 } |
| 206 | 234 |
| 207 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddressInResponse) | 235 TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingTrueAndEmptyShipp ingAddressInResponse) |
|
zino
2016/06/06 10:02:13
option.requestShipping == true && shipping_address
| |
| 208 { | 236 { |
| 209 ScriptState::Scope scope(getScriptState()); | 237 ScriptState::Scope scope(getScriptState()); |
| 210 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 238 PaymentOptions options; |
| 239 options.setRequestShipping(true); | |
| 240 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 241 EXPECT_FALSE(getExceptionState().hadException()); | |
| 242 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); | |
| 243 | |
| 244 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); | |
| 245 | |
| 246 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); | |
| 247 } | |
| 248 | |
| 249 TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingTrueAndInvalidShi ppingAddressInResponse) | |
|
zino
2016/06/06 10:02:13
option.requestShipping == true && shipping_address
| |
| 250 { | |
| 251 ScriptState::Scope scope(getScriptState()); | |
| 252 PaymentOptions options; | |
| 253 options.setRequestShipping(true); | |
| 254 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 211 EXPECT_FALSE(getExceptionState().hadException()); | 255 EXPECT_FALSE(getExceptionState().hadException()); |
| 212 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); | 256 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); |
| 213 response->shipping_address = mojom::blink::PaymentAddress::New(); | 257 response->shipping_address = mojom::blink::PaymentAddress::New(); |
| 214 | 258 |
| 215 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); | 259 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); |
| 216 | 260 |
| 217 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); | 261 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); |
| 218 } | 262 } |
| 219 | 263 |
| 220 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress) | 264 TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingFalseAndShippingA ddressExistsInResponse) |
|
zino
2016/06/06 10:02:13
option.requestShipping == false && shipping_addres
| |
| 221 { | 265 { |
| 222 ScriptState::Scope scope(getScriptState()); | 266 ScriptState::Scope scope(getScriptState()); |
| 223 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 267 PaymentOptions options; |
| 268 options.setRequestShipping(false); | |
| 269 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 224 EXPECT_FALSE(getExceptionState().hadException()); | 270 EXPECT_FALSE(getExceptionState().hadException()); |
| 225 mojom::blink::PaymentAddressPtr shippingAddress = mojom::blink::PaymentAddre ss::New(); | 271 mojom::blink::PaymentAddressPtr shippingAddress = mojom::blink::PaymentAddre ss::New(); |
| 226 shippingAddress->region_code = "US"; | 272 shippingAddress->region_code = "US"; |
| 227 shippingAddress->language_code = "en"; | 273 shippingAddress->language_code = "en"; |
| 228 shippingAddress->script_code = "Latn"; | 274 shippingAddress->script_code = "Latn"; |
| 229 | 275 |
| 230 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState())); | 276 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState())); |
| 231 | 277 |
| 232 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(std::move(shippingAddress)); | 278 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(std::move(shippingAddress)); |
| 233 } | 279 } |
| 234 | 280 |
| 235 TEST_F(PaymentRequestTest, ResolveShowPromiseWithValidShippingAddressInResponse) | 281 TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingTrueAndValidShip pingAddressInResponse) |
|
zino
2016/06/06 10:02:13
option.requestShipping == true && shipping_address
| |
| 236 { | 282 { |
| 237 ScriptState::Scope scope(getScriptState()); | 283 ScriptState::Scope scope(getScriptState()); |
| 238 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 284 PaymentOptions options; |
| 285 options.setRequestShipping(true); | |
| 286 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 239 EXPECT_FALSE(getExceptionState().hadException()); | 287 EXPECT_FALSE(getExceptionState().hadException()); |
| 240 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); | 288 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); |
| 241 response->shipping_address = mojom::blink::PaymentAddress::New(); | 289 response->shipping_address = mojom::blink::PaymentAddress::New(); |
| 242 response->shipping_address->region_code = "US"; | 290 response->shipping_address->region_code = "US"; |
| 243 response->shipping_address->language_code = "en"; | 291 response->shipping_address->language_code = "en"; |
| 244 response->shipping_address->script_code = "Latn"; | 292 response->shipping_address->script_code = "Latn"; |
| 245 | 293 |
| 246 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState())); | 294 ScriptValue outValue; |
| 295 request->show(getScriptState()).then(PaymentResponseFunction::create(getScri ptState(), &outValue), MockFunction::expectNoCall(getScriptState())); | |
| 247 | 296 |
| 248 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); | 297 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); |
| 298 v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate()); | |
| 299 PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState( )->isolate(), outValue.v8Value()); | |
| 300 | |
| 301 EXPECT_EQ("US", pr->shippingAddress()->regionCode()); | |
| 302 EXPECT_EQ("en-Latn", pr->shippingAddress()->languageCode()); | |
| 249 } | 303 } |
| 250 | 304 |
| 251 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse) | 305 TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingFalseAndEmptyShi ppingAddressInResponse) |
|
zino
2016/06/06 10:02:14
option.requestShipping == false && shipping_addres
| |
| 252 { | 306 { |
| 253 ScriptState::Scope scope(getScriptState()); | 307 ScriptState::Scope scope(getScriptState()); |
| 254 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 308 PaymentOptions options; |
| 309 options.setRequestShipping(false); | |
| 310 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 255 EXPECT_FALSE(getExceptionState().hadException()); | 311 EXPECT_FALSE(getExceptionState().hadException()); |
| 256 | 312 |
| 257 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState())); | 313 ScriptValue outValue; |
| 314 request->show(getScriptState()).then(PaymentResponseFunction::create(getScri ptState(), &outValue), MockFunction::expectNoCall(getScriptState())); | |
| 258 | 315 |
| 259 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New()); | 316 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New()); |
| 317 v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate()); | |
| 318 PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState( )->isolate(), outValue.v8Value()); | |
| 319 | |
| 320 EXPECT_EQ(nullptr, pr->shippingAddress()); | |
| 260 } | 321 } |
| 261 | 322 |
| 262 TEST_F(PaymentRequestTest, OnShippingOptionChange) | 323 TEST_F(PaymentRequestTest, OnShippingOptionChange) |
| 263 { | 324 { |
| 264 ScriptState::Scope scope(getScriptState()); | 325 ScriptState::Scope scope(getScriptState()); |
| 265 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); | 326 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); |
| 266 EXPECT_FALSE(getExceptionState().hadException()); | 327 EXPECT_FALSE(getExceptionState().hadException()); |
| 267 | 328 |
| 268 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState())); | 329 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState())); |
| 269 | 330 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 441 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}}] }"; | 502 "\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \ "Standard shipping\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}}] }"; |
| 442 | 503 |
| 443 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState()))); | 504 request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSON String(getScriptState(), detail, getExceptionState()))); |
| 444 EXPECT_FALSE(getExceptionState().hadException()); | 505 EXPECT_FALSE(getExceptionState().hadException()); |
| 445 | 506 |
| 446 EXPECT_EQ("standardShippingOption", request->shippingOption()); | 507 EXPECT_EQ("standardShippingOption", request->shippingOption()); |
| 447 } | 508 } |
| 448 | 509 |
| 449 } // namespace | 510 } // namespace |
| 450 } // namespace blink | 511 } // namespace blink |
| OLD | NEW |