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

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: 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 22
21 namespace {
22
23 class PaymentRequestTest : public testing::Test { 23 class PaymentRequestTest : public testing::Test {
24 public: 24 public:
25 PaymentRequestTest() 25 PaymentRequestTest()
26 : m_page(DummyPageHolder::create()) 26 : m_page(DummyPageHolder::create())
27 { 27 {
28 setSecurityOrigin("https://www.example.com/"); 28 setSecurityOrigin("https://www.example.com/");
29 } 29 }
30 30
31 ~PaymentRequestTest() override {} 31 ~PaymentRequestTest() override {}
32 32
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), details, getExceptionState()); 95 PaymentRequest::create(getScriptState(), Vector<String>(1, "foo"), details, getExceptionState());
96 96
97 EXPECT_TRUE(getExceptionState().hadException()); 97 EXPECT_TRUE(getExceptionState().hadException());
98 EXPECT_EQ(V8TypeError, getExceptionState().code()); 98 EXPECT_EQ(V8TypeError, getExceptionState().code());
99 } 99 }
100 100
101 TEST_F(PaymentRequestTest, NullShippingOptionWhenNoOptionsAvailable) 101 TEST_F(PaymentRequestTest, NullShippingOptionWhenNoOptionsAvailable)
102 { 102 {
103 PaymentDetails details; 103 PaymentDetails details;
104 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 104 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
105 PaymentOptions options;
106 options.setRequestShipping(true);
105 107
106 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 108 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
107 109
108 EXPECT_TRUE(request->shippingOption().isNull()); 110 EXPECT_TRUE(request->shippingOption().isNull());
109 } 111 }
110 112
111 TEST_F(PaymentRequestTest, NullShippingOptionWhenMultipleOptionsAvailable) 113 TEST_F(PaymentRequestTest, NullShippingOptionWhenMultipleOptionsAvailable)
112 { 114 {
113 PaymentDetails details; 115 PaymentDetails details;
114 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 116 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
115 details.setShippingOptions(HeapVector<ShippingOption>(2, buildShippingOption ForTest())); 117 details.setShippingOptions(HeapVector<ShippingOption>(2, buildShippingOption ForTest()));
118 PaymentOptions options;
119 options.setRequestShipping(true);
116 120
117 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 121 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
118 122
119 EXPECT_TRUE(request->shippingOption().isNull()); 123 EXPECT_TRUE(request->shippingOption().isNull());
120 } 124 }
121 125
122 TEST_F(PaymentRequestTest, SelectSingleAvailableShippingOption) 126 TEST_F(PaymentRequestTest, SelectSingleAvailableShippingOptionWhenShippingReques ted)
123 { 127 {
124 PaymentDetails details; 128 PaymentDetails details;
125 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest())); 129 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
130 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest(PaymentTestDataId, PaymentTestOverwriteValue, "standard")));
131 PaymentOptions options;
132 options.setRequestShipping(true);
133
134 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
135
136 EXPECT_EQ("standard", request->shippingOption());
137 }
138
139 TEST_F(PaymentRequestTest, DontSelectSingleAvailableShippingOptionByDefault)
140 {
141 PaymentDetails details;
142 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
126 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest())); 143 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest()));
127 144
128 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState()); 145 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, getExceptionState());
129 146
130 EXPECT_EQ("standard", request->shippingOption()); 147 EXPECT_TRUE(request->shippingOption().isNull());
148 }
149
150 TEST_F(PaymentRequestTest, DontSelectSingleAvailableShippingOptionWhenShippingNo tRequested)
151 {
152 PaymentDetails details;
153 details.setItems(HeapVector<PaymentItem>(1, buildPaymentItemForTest()));
154 details.setShippingOptions(HeapVector<ShippingOption>(1, buildShippingOption ForTest()));
155 PaymentOptions options;
156 options.setRequestShipping(false);
157
158 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), details, options, getExceptionState());
159
160 EXPECT_TRUE(request->shippingOption().isNull());
131 } 161 }
132 162
133 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow) 163 TEST_F(PaymentRequestTest, AbortWithoutShowShouldThrow)
134 { 164 {
135 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState()); 165 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
136 EXPECT_FALSE(getExceptionState().hadException()); 166 EXPECT_FALSE(getExceptionState().hadException());
137 167
138 request->abort(getExceptionState()); 168 request->abort(getExceptionState());
139 EXPECT_TRUE(getExceptionState().hadException()); 169 EXPECT_TRUE(getExceptionState().hadException());
140 } 170 }
141 171
142 } // namespace 172 class MockFunction : public ScriptFunction {
Marijn Kruisselbrink 2016/05/02 18:17:41 Something as generically named as MockFunction sho
please use gerrit instead 2016/05/02 21:19:45 Done.
173 public:
174 static v8::Local<v8::Function> noExpectations(ScriptState* scriptState)
175 {
176 MockFunction* self = new MockFunction(scriptState);
177 return self->bindToV8Function();
178 }
179
180 static v8::Local<v8::Function> expectCall(ScriptState* scriptState)
181 {
182 MockFunction* self = new MockFunction(scriptState);
183 EXPECT_CALL(*self, call(testing::_));
184 return self->bindToV8Function();
185 }
186
187 static v8::Local<v8::Function> expectNoCall(ScriptState* scriptState)
188 {
189 MockFunction* self = new MockFunction(scriptState);
190 EXPECT_CALL(*self, call(testing::_)).Times(0);
191 return self->bindToV8Function();
192 }
193
194 private:
195 explicit MockFunction(ScriptState* scriptState)
196 : ScriptFunction(scriptState)
197 {
198 ON_CALL(*this, call(testing::_)).WillByDefault(testing::ReturnArg<0>());
199 }
200
201 MOCK_METHOD1(call, ScriptValue(ScriptValue));
202 };
203
204 TEST_F(PaymentRequestTest, CanAbortAfterShow)
205 {
206 ScriptState::Scope scope(getScriptState());
207 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
208 EXPECT_FALSE(getExceptionState().hadException());
209
210 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::noExpectations(getScriptState()));
211 request->abort(getExceptionState());
212
213 EXPECT_FALSE(getExceptionState().hadException());
214 }
215
216 TEST_F(PaymentRequestTest, RejectShowPromiseOnInvalidShippingAddress)
217 {
218 ScriptState::Scope scope(getScriptState());
219 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
220 EXPECT_FALSE(getExceptionState().hadException());
221
222 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
223
224 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(mojo m::blink::ShippingAddress::New());
Marijn Kruisselbrink 2016/05/02 18:17:41 C-style casts are against the style-guide. In this
please use gerrit instead 2016/05/02 21:19:45 Woops, spending too much time in Java :-) Done.
225 }
226
227 TEST_F(PaymentRequestTest, DontRejectShowPromiseForValidShippingAddress)
228 {
229 ScriptState::Scope scope(getScriptState());
230 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
231 EXPECT_FALSE(getExceptionState().hadException());
232 mojom::blink::ShippingAddressPtr shippingAddress = mojom::blink::ShippingAdd ress::New();
233 shippingAddress->region_code = "US";
234 shippingAddress->language_code = "en";
235 shippingAddress->script_code = "Latn";
236
237 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
238
239 ((mojom::blink::PaymentRequestClient*)request)->OnShippingAddressChange(std: :move(shippingAddress));
240 }
241
242 TEST_F(PaymentRequestTest, ResolveShowPromiseWithoutShippingAddressInResponse)
243 {
244 ScriptState::Scope scope(getScriptState());
245 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
246 EXPECT_FALSE(getExceptionState().hadException());
247
248 request->show(getScriptState()).then(MockFunction::expectCall(getScriptState ()), MockFunction::expectNoCall(getScriptState()));
249
250 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
251 }
252
253 TEST_F(PaymentRequestTest, OnShippingOptionChange)
254 {
255 ScriptState::Scope scope(getScriptState());
256 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
257 EXPECT_FALSE(getExceptionState().hadException());
258
259 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectNoCall(getScriptState()));
260
261 ((mojom::blink::PaymentRequestClient*)request)->OnShippingOptionChange("stan dardShipping");
262 }
263
264 TEST_F(PaymentRequestTest, CannotCallShowTwice)
265 {
266 ScriptState::Scope scope(getScriptState());
267 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
268 EXPECT_FALSE(getExceptionState().hadException());
269 request->show(getScriptState());
270
271 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
272 }
273
274 TEST_F(PaymentRequestTest, CannotCallCompleteTwice)
275 {
276 ScriptState::Scope scope(getScriptState());
277 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
278 EXPECT_FALSE(getExceptionState().hadException());
279 request->show(getScriptState());
280 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
281 request->complete(getScriptState(), false);
282
283 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
284 }
285
286 TEST_F(PaymentRequestTest, RejectShowPromiseOnError)
287 {
288 ScriptState::Scope scope(getScriptState());
289 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
290 EXPECT_FALSE(getExceptionState().hadException());
291
292 request->show(getScriptState()).then(MockFunction::expectNoCall(getScriptSta te()), MockFunction::expectCall(getScriptState()));
293
294 ((mojom::blink::PaymentRequestClient*)request)->OnError();
295 }
296
297 TEST_F(PaymentRequestTest, RejectCompletePromiseOnError)
298 {
299 ScriptState::Scope scope(getScriptState());
300 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
301 EXPECT_FALSE(getExceptionState().hadException());
302 request->show(getScriptState());
303 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
304
305 request->complete(getScriptState(), true).then(MockFunction::expectNoCall(ge tScriptState()), MockFunction::expectCall(getScriptState()));
306
307 ((mojom::blink::PaymentRequestClient*)request)->OnError();
308 }
309
310 TEST_F(PaymentRequestTest, ResolvePromiseOnComplete)
311 {
312 ScriptState::Scope scope(getScriptState());
313 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), getExceptionState());
314 EXPECT_FALSE(getExceptionState().hadException());
315 request->show(getScriptState());
316 ((mojom::blink::PaymentRequestClient*)request)->OnPaymentResponse(mojom::bli nk::PaymentResponse::New());
317
318 request->complete(getScriptState(), true).then(MockFunction::expectCall(getS criptState()), MockFunction::expectNoCall(getScriptState()));
319
320 ((mojom::blink::PaymentRequestClient*)request)->OnComplete();
321 }
322
143 } // namespace blink 323 } // 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