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

Side by Side Diff: third_party/WebKit/Source/modules/payments/PaymentRequestTest.cpp

Issue 1938853002: More thorough tests for PaymentRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn-output-dir
Patch Set: Address comments 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
« no previous file with comments | « third_party/WebKit/Source/modules/payments/PaymentRequest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ScriptState.h" 8 #include "bindings/core/v8/ScriptState.h"
9 #include "core/dom/ExceptionCode.h" 9 #include "core/dom/ExceptionCode.h"
10 #include "core/testing/DummyPageHolder.h" 10 #include "core/testing/DummyPageHolder.h"
11 #include "modules/payments/CurrencyAmount.h" 11 #include "modules/payments/CurrencyAmount.h"
12 #include "modules/payments/PaymentDetailsTestHelper.h" 12 #include "modules/payments/PaymentDetailsTestHelper.h"
13 #include "modules/payments/PaymentItem.h" 13 #include "modules/payments/PaymentItem.h"
14 #include "modules/payments/ShippingOption.h" 14 #include "modules/payments/ShippingOption.h"
15 #include "platform/heap/HeapAllocator.h" 15 #include "platform/heap/HeapAllocator.h"
16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 #include "wtf/OwnPtr.h" 18 #include "wtf/OwnPtr.h"
19 #include <utility>
18 20
19 namespace blink { 21 namespace blink {
20
21 namespace { 22 namespace {
22 23
23 class PaymentRequestTest : public testing::Test { 24 class PaymentRequestTest : public testing::Test {
24 public: 25 public:
25 PaymentRequestTest() 26 PaymentRequestTest()
26 : m_page(DummyPageHolder::create()) 27 : m_page(DummyPageHolder::create())
27 { 28 {
28 setSecurityOrigin("https://www.example.com/"); 29 setSecurityOrigin("https://www.example.com/");
29 } 30 }
30 31
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), details, getExceptionState()); 96 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), details, getExceptionState());
96 97
97 EXPECT_TRUE(getExceptionState().hadException()); 98 EXPECT_TRUE(getExceptionState().hadException());
98 EXPECT_EQ(V8TypeError, getExceptionState().code()); 99 EXPECT_EQ(V8TypeError, getExceptionState().code());
99 } 100 }
100 101
101 TEST_F(PaymentRequestTest, NullShippingOptionWhenNoOptionsAvailable) 102 TEST_F(PaymentRequestTest, NullShippingOptionWhenNoOptionsAvailable)
102 { 103 {
103 PaymentDetails details; 104 PaymentDetails details;
104 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 105 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
106 PaymentOptions options;
107 options.setRequestShipping(true);
105 108
106 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 109 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
107 110
108 EXPECT_TRUE(request->shippingOption().isNull()); 111 EXPECT_TRUE(request->shippingOption().isNull());
109 } 112 }
110 113
111 TEST_F(PaymentRequestTest, NullShippingOptionWhenMultipleOptionsAvailable) 114 TEST_F(PaymentRequestTest, NullShippingOptionWhenMultipleOptionsAvailable)
112 { 115 {
113 PaymentDetails details; 116 PaymentDetails details;
114 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 117 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
115 details.setShippingOptions(HeapVector<ShippingOption>(2, buildShippingOption ForTest())); 118 details.setShippingOptions(HeapVector<ShippingOption>(2, buildShippingOption ForTest()));
119 PaymentOptions options;
120 options.setRequestShipping(true);
116 121
117 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 122 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
118 123
119 EXPECT_TRUE(request->shippingOption().isNull()); 124 EXPECT_TRUE(request->shippingOption().isNull());
120 } 125 }
121 126
122 TEST_F(PaymentRequestTest, SelectSingleAvailableShippingOption) 127 TEST_F(PaymentRequestTest, SelectSingleAvailableShippingOptionWhenShippingReques ted)
123 { 128 {
124 PaymentDetails details; 129 PaymentDetails details;
125 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 130 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
131 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest(PaymentTestDataId, PaymentTestOverwriteValue, "standard")));
132 PaymentOptions options;
133 options.setRequestShipping(true);
134
135 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
136
137 EXPECT_EQ("standard", request->shippingOption());
138 }
139
140 TEST_F(PaymentRequestTest, DontSelectSingleAvailableShippingOptionByDefault)
141 {
142 PaymentDetails details;
143 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
126 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest())); 144 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest()));
127 145
128 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 146 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState());
129 147
130 EXPECT_EQ("standard", request->shippingOption()); 148 EXPECT_TRUE(request->shippingOption().isNull());
149 }
150
151 TEST_F(PaymentRequestTest, DontSelectSingleAvailableShippingOptionWhenShippingNo tRequested)
152 {
153 PaymentDetails details;
154 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
155 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest()));
156 PaymentOptions options;
157 options.setRequestShipping(false);
158
159 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
160
161 EXPECT_TRUE(request->shippingOption().isNull());
131 } 162 }
132 163
133 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow) 164 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow)
134 { 165 {
135 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 166 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
136 EXPECT_FALSE(getExceptionState().hadException()); 167 EXPECT_FALSE(getExceptionState().hadException());
137 168
138 request->abort(getExceptionState()); 169 request->abort(getExceptionState());
139 EXPECT_TRUE(getExceptionState().hadException()); 170 EXPECT_TRUE(getExceptionState().hadException());
140 } 171 }
141 172
173 class MockFunction : public ScriptFunction {
174 public:
175 static v8::Local<v8::Function> noExpectations(ScriptState* scriptState)
176 {
177 MockFunction* self = new MockFunction(scriptState);
178 return self->bindToV8Function();
179 }
180
181 static v8::Local<v8::Function> expectCall(ScriptState* scriptState)
182 {
183 MockFunction* self = new MockFunction(scriptState);
184 EXPECT_CALL(*self, call(testing::_));
185 return self->bindToV8Function();
186 }
187
188 static v8::Local<v8::Function> expectNoCall(ScriptState* scriptState)
189 {
190 MockFunction* self = new MockFunction(scriptState);
191 EXPECT_CALL(*self, call(testing::_)).Times(0);
192 return self->bindToV8Function();
193 }
194
195 private:
196 explicit MockFunction(ScriptState* scriptState)
197 : ScriptFunction(scriptState)
198 {
199 ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>());
200 }
201
202 MOCK_METHOD1(call, ScriptValue(ScriptValue));
203 };
204
205 TEST_F(PaymentRequestTest, CanAbortAfterShow)
206 {
207 ScriptState::Scope scope(getScriptState());
208 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
209 EXPECT_FALSE(getExceptionState().hadException());
210
211 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::noExpectations(getScriptState()));
212 request->abort(getExceptionState());
213
214 EXPECT_FALSE(getExceptionState().hadException());
215 }
216
217 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress)
218 {
219 ScriptState::Scope scope(getScriptState());
220 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
221 EXPECT_FALSE(getExceptionState().hadException());
222
223 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
224
225 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(mojom::blink::ShippingAddress::New());
226 }
227
228 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress)
229 {
230 ScriptState::Scope scope(getScriptState());
231 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
232 EXPECT_FALSE(getExceptionState().hadException());
233 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd ress::New();
234 shippingAddress->region_code = "US";
235 shippingAddress->language_code = "en";
236 shippingAddress->script_code = "Latn";
237
238 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
239
240 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingAddress Change(std::move(shippingAddress));
241 }
242
243 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse)
244 {
245 ScriptState::Scope scope(getScriptState());
246 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
247 EXPECT_FALSE(getExceptionState().hadException());
248
249 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
250
251 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
252 }
253
254 TEST_F(PaymentRequestTest, OnShippingOptionChange)
255 {
256 ScriptState::Scope scope(getScriptState());
257 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
258 EXPECT_FALSE(getExceptionState().hadException());
259
260 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
261
262 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnShippingOptionC hange("standardShipping");
263 }
264
265 TEST_F(PaymentRequestTest, CannotCallShowTwice)
266 {
267 ScriptState::Scope scope(getScriptState());
268 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
269 EXPECT_FALSE(getExceptionState().hadException());
270 request->show(getScriptState());
271
272 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
273 }
274
275 TEST_F(PaymentRequestTest, CannotCallCompleteTwice)
276 {
277 ScriptState::Scope scope(getScriptState());
278 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
279 EXPECT_FALSE(getExceptionState().hadException());
280 request->show(getScriptState());
281 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
282 request->complete(getScriptState(), false);
283
284 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
285 }
286
287 TEST_F(PaymentRequestTest, RejectShowPromiseOnError)
288 {
289 ScriptState::Scope scope(getScriptState());
290 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
291 EXPECT_FALSE(getExceptionState().hadException());
292
293 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
294
295 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError();
296 }
297
298 TEST_F(PaymentRequestTest, RejectCompletePromiseOnError)
299 {
300 ScriptState::Scope scope(getScriptState());
301 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
302 EXPECT_FALSE(getExceptionState().hadException());
303 request->show(getScriptState());
304 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
305
306 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
307
308 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnError();
309 }
310
311 TEST_F(PaymentRequestTest, ResolvePromiseOnComplete)
312 {
313 ScriptState::Scope scope(getScriptState());
314 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
315 EXPECT_FALSE(getExceptionState().hadException());
316 request->show(getScriptState());
317 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (mojom::blink::PaymentResponse::New());
318
319 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS criptState()), MockFunction::expectNoCall(getScriptState()));
320
321 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnComplete();
322 }
323
142 } // namespace 324 } // namespace
143 } // namespace blink 325 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/payments/PaymentRequest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698