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

Unified Diff: third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp

Issue 2048823004: PaymentRequest.abort() should return a promise. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove DummyPageHolder and TrackExceptionState for haraken@ comment. Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp
diff --git a/third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp b/third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp
index 671cb955d32933136eae3540fce19289909a15c3..1f6c433d8f99a50ea40e47d40ea546322719b324 100644
--- a/third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp
+++ b/third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp
@@ -4,47 +4,25 @@
#include "modules/payments/PaymentRequest.h"
-#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/JSONValuesForV8.h"
+#include "bindings/core/v8/ScriptFunction.h"
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/V8BindingForTesting.h"
#include "bindings/modules/v8/V8PaymentResponse.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
-#include "modules/payments/CurrencyAmount.h"
#include "modules/payments/PaymentAddress.h"
-#include "modules/payments/PaymentItem.h"
+#include "modules/payments/PaymentRequestTestBase.h"
+#include "modules/payments/PaymentResponse.h"
#include "modules/payments/PaymentTestHelper.h"
-#include "modules/payments/ShippingOption.h"
#include "platform/heap/HeapAllocator.h"
-#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
-#include "wtf/OwnPtr.h"
#include <utility>
namespace blink {
namespace {
-class PaymentRequestTest : public testing::Test {
-public:
- PaymentRequestTest()
- {
- setSecurityOrigin("https://www.example.com/");
- }
-
- ~PaymentRequestTest() override {}
-
- ScriptState* getScriptState() { return m_scope.getScriptState(); }
- ExceptionState& getExceptionState() { return m_scope.getExceptionState(); }
-
- void setSecurityOrigin(const String& securityOrigin)
- {
- m_scope.document().setSecurityOrigin(SecurityOrigin::create(KURL(KURL(), securityOrigin)));
- }
-
-private:
- V8TestingScope m_scope;
-};
+class PaymentRequestTest : public PaymentRequestTestBase {};
TEST_F(PaymentRequestTest, NoExceptionWithValidData)
{
@@ -191,90 +169,12 @@ TEST_F(PaymentRequestTest, SelectLastSelectedShippingOptionWhenShippingRequested
EXPECT_EQ("express", request->shippingOption());
}
-TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow)
-{
- PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
- EXPECT_FALSE(getExceptionState().hadException());
-
- request->abort(getExceptionState());
- EXPECT_TRUE(getExceptionState().hadException());
-}
-
-class MockFunction : public ScriptFunction {
-public:
- static v8::Local<v8::Function> noExpectations(ScriptState* scriptState)
- {
- MockFunction* self = new MockFunction(scriptState);
- return self->bindToV8Function();
- }
-
- static v8::Local<v8::Function> expectCall(ScriptState* scriptState)
- {
- MockFunction* self = new MockFunction(scriptState);
- EXPECT_CALL(*self, call(testing::_));
- return self->bindToV8Function();
- }
-
- static v8::Local<v8::Function> expectNoCall(ScriptState* scriptState)
- {
- MockFunction* self = new MockFunction(scriptState);
- EXPECT_CALL(*self, call(testing::_)).Times(0);
- return self->bindToV8Function();
- }
-
-private:
- explicit MockFunction(ScriptState* scriptState)
- : ScriptFunction(scriptState)
- {
- ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>());
- }
-
- MOCK_METHOD1(call, ScriptValue(ScriptValue));
-};
-
-class PaymentResponseFunction : public ScriptFunction {
-public:
- static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue* outValue)
- {
- PaymentResponseFunction* self = new PaymentResponseFunction(scriptState, outValue);
- return self->bindToV8Function();
- }
-
- ScriptValue call(ScriptValue value) override
- {
- DCHECK(!value.isEmpty());
- *m_value = value;
- return value;
- }
-
-private:
- PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outValue)
- : ScriptFunction(scriptState)
- , m_value(outValue)
- {
- DCHECK(m_value);
- }
-
- ScriptValue* m_value;
-};
-
-TEST_F(PaymentRequestTest, CanAbortAfterShow)
-{
- PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
- EXPECT_FALSE(getExceptionState().hadException());
-
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::noExpectations(getScriptState()));
- request->abort(getExceptionState());
-
- EXPECT_FALSE(getExceptionState().hadException());
-}
-
TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress)
{
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddressChange(mojom::blink::PaymentAddress::New());
}
@@ -287,7 +187,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingTrueAndEmptyShipp
EXPECT_FALSE(getExceptionState().hadException());
mojom::blink::PaymentResponsePtr response = buildPaymentResponseForTest();
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(std::move(response));
}
@@ -301,7 +201,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingTrueAndInvalidShi
mojom::blink::PaymentResponsePtr response = buildPaymentResponseForTest();
response->shipping_address = mojom::blink::PaymentAddress::New();
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(std::move(response));
}
@@ -312,16 +212,43 @@ TEST_F(PaymentRequestTest, RejectShowPromiseWithRequestShippingFalseAndShippingA
options.setRequestShipping(false);
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), options, getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- mojom::blink::PaymentAddressPtr shippingAddress = mojom::blink::PaymentAddress::New();
- shippingAddress->country = "US";
- shippingAddress->language_code = "en";
- shippingAddress->script_code = "Latn";
+ mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::New();
+ response->shipping_address = mojom::blink::PaymentAddress::New();
+ response->shipping_address->country = "US";
+ response->shipping_address->language_code = "en";
+ response->shipping_address->script_code = "Latn";
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
- static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddressChange(std::move(shippingAddress));
+ static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(std::move(response));
}
+class PaymentResponseFunction : public ScriptFunction {
+public:
+ static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue* outValue)
+ {
+ PaymentResponseFunction* self = new PaymentResponseFunction(scriptState, outValue);
+ return self->bindToV8Function();
+ }
+
+ ScriptValue call(ScriptValue value) override
+ {
+ DCHECK(!value.isEmpty());
+ *m_value = value;
+ return value;
+ }
+
+private:
+ PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outValue)
+ : ScriptFunction(scriptState)
+ , m_value(outValue)
+ {
+ DCHECK(m_value);
+ }
+
+ ScriptValue* m_value;
+};
+
TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingTrueAndValidShippingAddressInResponse)
{
PaymentOptions options;
@@ -335,10 +262,10 @@ TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingTrueAndValidShip
response->shipping_address->script_code = "Latn";
ScriptValue outValue;
- request->show(getScriptState()).then(PaymentResponseFunction::create(getScriptState(), &outValue), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(PaymentResponseFunction::create(getScriptState(), &outValue), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(std::move(response));
- v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate());
+ firePromiseCallbacks();
PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState()->isolate(), outValue.v8Value());
EXPECT_EQ("US", pr->shippingAddress()->country());
@@ -353,10 +280,10 @@ TEST_F(PaymentRequestTest, ResolveShowPromiseWithRequestShippingFalseAndEmptyShi
EXPECT_FALSE(getExceptionState().hadException());
ScriptValue outValue;
- request->show(getScriptState()).then(PaymentResponseFunction::create(getScriptState(), &outValue), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(PaymentResponseFunction::create(getScriptState(), &outValue), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
- v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate());
+ firePromiseCallbacks();
PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState()->isolate(), outValue.v8Value());
EXPECT_EQ(nullptr, pr->shippingAddress());
@@ -367,7 +294,7 @@ TEST_F(PaymentRequestTest, OnShippingOptionChange)
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingOptionChange("standardShipping");
}
@@ -378,7 +305,7 @@ TEST_F(PaymentRequestTest, CannotCallShowTwice)
EXPECT_FALSE(getExceptionState().hadException());
request->show(getScriptState());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
}
TEST_F(PaymentRequestTest, CannotCallCompleteTwice)
@@ -389,7 +316,7 @@ TEST_F(PaymentRequestTest, CannotCallCompleteTwice)
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
request->complete(getScriptState(), false);
- request->complete(getScriptState(), true).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->complete(getScriptState(), true).then(expectNoCall(), expectCall());
}
TEST_F(PaymentRequestTest, RejectShowPromiseOnError)
@@ -397,7 +324,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseOnError)
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError();
}
@@ -409,7 +336,7 @@ TEST_F(PaymentRequestTest, RejectCompletePromiseOnError)
request->show(getScriptState());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
- request->complete(getScriptState(), true).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->complete(getScriptState(), true).then(expectNoCall(), expectCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError();
}
@@ -421,7 +348,7 @@ TEST_F(PaymentRequestTest, ResolvePromiseOnComplete)
request->show(getScriptState());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
- request->complete(getScriptState(), true).then(MockFunction::expectCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->complete(getScriptState(), true).then(expectCall(), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnComplete();
}
@@ -431,7 +358,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseOnUpdateDetailsFailure)
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), "oops"));
}
@@ -440,10 +367,10 @@ TEST_F(PaymentRequestTest, RejectCompletePromiseOnUpdateDetailsFailure)
{
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectCall(), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
- request->complete(getScriptState(), true).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->complete(getScriptState(), true).then(expectNoCall(), expectCall());
request->onUpdatePaymentDetailsFailure(ScriptValue::from(getScriptState(), "oops"));
}
@@ -452,7 +379,7 @@ TEST_F(PaymentRequestTest, IgnoreUpdatePaymentDetailsAfterShowPromiseResolved)
{
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectCall(), expectNoCall());
static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse(buildPaymentResponseForTest());
request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "foo"));
@@ -463,7 +390,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseOnNonPaymentDetailsUpdate)
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), "NotPaymentDetails"));
}
@@ -473,7 +400,7 @@ TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidPaymentDetailsUpdate)
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectCall());
request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSONString(getScriptState(), "{}", getExceptionState())));
EXPECT_FALSE(getExceptionState().hadException());
@@ -488,7 +415,7 @@ TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithoutShipp
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), details, options, getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
EXPECT_TRUE(request->shippingOption().isNull());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectNoCall());
String detailWithShippingOptions = "{\"total\": {\"label\": \"Total\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}},"
"\"shippingOptions\": [{\"id\": \"standardShippingOption\", \"label\": \"Standard shipping\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}, \"selected\": true}]}";
request->onUpdatePaymentDetails(ScriptValue::from(getScriptState(), fromJSONString(getScriptState(), detailWithShippingOptions, getExceptionState())));
@@ -508,7 +435,7 @@ TEST_F(PaymentRequestTest, ClearShippingOptionOnPaymentDetailsUpdateWithMultiple
options.setRequestShipping(true);
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), options, getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectNoCall());
String detail = "{\"total\": {\"label\": \"Total\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}},"
"\"shippingOptions\": [{\"id\": \"slow\", \"label\": \"Slow\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}},"
"{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"USD\", \"value\": \"50.00\"}}]}";
@@ -525,7 +452,7 @@ TEST_F(PaymentRequestTest, UseTheSelectedShippingOptionFromPaymentDetailsUpdate)
options.setRequestShipping(true);
PaymentRequest* request = PaymentRequest::create(getScriptState(), buildPaymentMethodDataForTest(), buildPaymentDetailsForTest(), options, getExceptionState());
EXPECT_FALSE(getExceptionState().hadException());
- request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptState()), MockFunction::expectNoCall(getScriptState()));
+ request->show(getScriptState()).then(expectNoCall(), expectNoCall());
String detail = "{\"total\": {\"label\": \"Total\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}},"
"\"shippingOptions\": [{\"id\": \"slow\", \"label\": \"Slow\", \"amount\": {\"currency\": \"USD\", \"value\": \"5.00\"}},"
"{\"id\": \"fast\", \"label\": \"Fast\", \"amount\": {\"currency\": \"USD\", \"value\": \"50.00\"}, \"selected\": true}]}";

Powered by Google App Engine
This is Rietveld 408576698