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 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
6 | 6 |
| 7 import android.os.Handler; |
7 import android.view.View; | 8 import android.view.View; |
8 import android.view.ViewGroup; | 9 import android.view.ViewGroup; |
9 import android.widget.CheckBox; | 10 import android.widget.CheckBox; |
10 import android.widget.EditText; | 11 import android.widget.EditText; |
11 import android.widget.Spinner; | 12 import android.widget.Spinner; |
12 import android.widget.TextView; | 13 import android.widget.TextView; |
13 | 14 |
14 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
15 import org.chromium.base.test.util.UrlUtils; | 16 import org.chromium.base.test.util.UrlUtils; |
16 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
17 import org.chromium.chrome.browser.ChromeActivity; | 18 import org.chromium.chrome.browser.ChromeActivity; |
18 import org.chromium.chrome.browser.autofill.CardUnmaskPrompt; | 19 import org.chromium.chrome.browser.autofill.CardUnmaskPrompt; |
19 import org.chromium.chrome.browser.autofill.CardUnmaskPrompt.CardUnmaskObserverF
orTest; | 20 import org.chromium.chrome.browser.autofill.CardUnmaskPrompt.CardUnmaskObserverF
orTest; |
| 21 import org.chromium.chrome.browser.payments.PaymentAppFactory.PaymentAppFactoryA
ddition; |
20 import org.chromium.chrome.browser.payments.PaymentRequestImpl.PaymentRequestSer
viceObserverForTest; | 22 import org.chromium.chrome.browser.payments.PaymentRequestImpl.PaymentRequestSer
viceObserverForTest; |
21 import org.chromium.chrome.browser.payments.ui.EditorTextField; | 23 import org.chromium.chrome.browser.payments.ui.EditorTextField; |
22 import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSecti
on; | 24 import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSecti
on; |
23 import org.chromium.chrome.browser.payments.ui.PaymentRequestUI; | 25 import org.chromium.chrome.browser.payments.ui.PaymentRequestUI; |
24 import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.PaymentRequestOb
serverForTest; | 26 import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.PaymentRequestOb
serverForTest; |
25 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 27 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
26 import org.chromium.content.browser.ContentViewCore; | 28 import org.chromium.content.browser.ContentViewCore; |
27 import org.chromium.content.browser.test.util.CallbackHelper; | 29 import org.chromium.content.browser.test.util.CallbackHelper; |
28 import org.chromium.content.browser.test.util.Criteria; | 30 import org.chromium.content.browser.test.util.Criteria; |
29 import org.chromium.content.browser.test.util.CriteriaHelper; | 31 import org.chromium.content.browser.test.util.CriteriaHelper; |
30 import org.chromium.content.browser.test.util.DOMUtils; | 32 import org.chromium.content.browser.test.util.DOMUtils; |
31 import org.chromium.content_public.browser.WebContents; | 33 import org.chromium.content_public.browser.WebContents; |
| 34 import org.chromium.mojom.payments.PaymentItem; |
| 35 import org.json.JSONObject; |
32 | 36 |
| 37 import java.util.ArrayList; |
| 38 import java.util.HashSet; |
33 import java.util.List; | 39 import java.util.List; |
| 40 import java.util.Set; |
34 import java.util.concurrent.Callable; | 41 import java.util.concurrent.Callable; |
35 import java.util.concurrent.ExecutionException; | 42 import java.util.concurrent.ExecutionException; |
36 import java.util.concurrent.TimeoutException; | 43 import java.util.concurrent.TimeoutException; |
37 import java.util.concurrent.atomic.AtomicReference; | 44 import java.util.concurrent.atomic.AtomicReference; |
38 | 45 |
39 /** | 46 /** |
40 * A base integration test for payments. | 47 * A base integration test for payments. |
41 */ | 48 */ |
42 abstract class PaymentRequestTestBase extends ChromeActivityTestCaseBase<ChromeA
ctivity> | 49 abstract class PaymentRequestTestBase extends ChromeActivityTestCaseBase<ChromeA
ctivity> |
43 implements PaymentRequestObserverForTest, PaymentRequestServiceObserverF
orTest, | 50 implements PaymentRequestObserverForTest, PaymentRequestServiceObserverF
orTest, |
44 CardUnmaskObserverForTest { | 51 CardUnmaskObserverForTest { |
| 52 /** Flag for installing a payment app without instruments. */ |
| 53 protected static final int NO_INSTRUMENTS = 0; |
| 54 |
| 55 /** Flag for installing a payment app with instruments. */ |
| 56 protected static final int HAVE_INSTRUMENTS = 1; |
| 57 |
| 58 /** Flag for installing a fast payment app. */ |
| 59 protected static final int IMMEDIATE_RESPONSE = 0; |
| 60 |
| 61 /** Flag for installing a slow payment app. */ |
| 62 protected static final int DELAYED_RESPONSE = 1; |
| 63 |
45 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyForInput; | 64 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyForInput; |
46 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyToPay; | 65 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyToPay; |
47 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyToClose; | 66 protected final PaymentsCallbackHelper<PaymentRequestUI> mReadyToClose; |
48 protected final PaymentsCallbackHelper<PaymentRequestUI> mResultReady; | 67 protected final PaymentsCallbackHelper<PaymentRequestUI> mResultReady; |
49 protected final PaymentsCallbackHelper<CardUnmaskPrompt> mReadyForUnmaskInpu
t; | 68 protected final PaymentsCallbackHelper<CardUnmaskPrompt> mReadyForUnmaskInpu
t; |
50 protected final PaymentsCallbackHelper<CardUnmaskPrompt> mReadyToUnmask; | 69 protected final PaymentsCallbackHelper<CardUnmaskPrompt> mReadyToUnmask; |
51 protected final CallbackHelper mReadyToEdit; | 70 protected final CallbackHelper mReadyToEdit; |
52 protected final CallbackHelper mEditorValidationError; | 71 protected final CallbackHelper mEditorValidationError; |
53 protected final CallbackHelper mEditorTextUpdate; | 72 protected final CallbackHelper mEditorTextUpdate; |
54 protected final CallbackHelper mDismissed; | 73 protected final CallbackHelper mDismissed; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 protected String getAddressSectionLabel() throws ExecutionException { | 252 protected String getAddressSectionLabel() throws ExecutionException { |
234 return ThreadUtils.runOnUiThreadBlocking(new Callable<String>() { | 253 return ThreadUtils.runOnUiThreadBlocking(new Callable<String>() { |
235 @Override | 254 @Override |
236 public String call() { | 255 public String call() { |
237 return ((TextView) mUI.getShippingSummarySectionForTest().findVi
ewById( | 256 return ((TextView) mUI.getShippingSummarySectionForTest().findVi
ewById( |
238 R.id.payments_left_summary_label)).getText().toString(); | 257 R.id.payments_left_summary_label)).getText().toString(); |
239 } | 258 } |
240 }); | 259 }); |
241 } | 260 } |
242 | 261 |
| 262 /** Returns the label corresponding to the payment instrument at the specif
ied |index|. */ |
| 263 protected String getPaymentInstrumentLabel(final int index) throws Execution
Exception { |
| 264 return ThreadUtils.runOnUiThreadBlocking(new Callable<String>() { |
| 265 @Override |
| 266 public String call() { |
| 267 return ((OptionSection) mUI.getPaymentMethodSectionForTest()) |
| 268 .getOptionLabelsForTest(index).getText().toString(); |
| 269 } |
| 270 }); |
| 271 } |
| 272 |
243 /** | 273 /** |
244 * Returns the label corresponding to the contact detail suggestion at the
specified | 274 * Returns the label corresponding to the contact detail suggestion at the
specified |
245 * |suggestionIndex|. | 275 * |suggestionIndex|. |
246 */ | 276 */ |
247 protected String getContactDetailsSuggestionLabel(final int suggestionIndex) | 277 protected String getContactDetailsSuggestionLabel(final int suggestionIndex) |
248 throws ExecutionException { | 278 throws ExecutionException { |
249 assert (suggestionIndex < getNumberOfContactDetailSuggestions()); | |
250 | |
251 return ThreadUtils.runOnUiThreadBlocking(new Callable<String>() { | 279 return ThreadUtils.runOnUiThreadBlocking(new Callable<String>() { |
252 @Override | 280 @Override |
253 public String call() { | 281 public String call() { |
254 return ((OptionSection) mUI.getContactDetailsSectionForTest()) | 282 return ((OptionSection) mUI.getContactDetailsSectionForTest()) |
255 .getOptionLabelsForTest(suggestionIndex).getText().toStr
ing(); | 283 .getOptionLabelsForTest(suggestionIndex).getText().toStr
ing(); |
256 } | 284 } |
257 }); | 285 }); |
258 } | 286 } |
259 | 287 |
260 /** | 288 /** Returns the the number of payment instruments. */ |
261 * Returns the the number of contact detail suggestions, | 289 protected int getNumberOfPaymentInstruments() throws ExecutionException { |
262 */ | 290 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() { |
| 291 @Override |
| 292 public Integer call() { |
| 293 return ((OptionSection) mUI.getPaymentMethodSectionForTest()) |
| 294 .getNumberOfOptionLabelsForTest(); |
| 295 } |
| 296 }); |
| 297 } |
| 298 |
| 299 /** Returns the the number of contact detail suggestions. */ |
263 protected int getNumberOfContactDetailSuggestions() throws ExecutionExceptio
n { | 300 protected int getNumberOfContactDetailSuggestions() throws ExecutionExceptio
n { |
264 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() { | 301 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() { |
265 @Override | 302 @Override |
266 public Integer call() { | 303 public Integer call() { |
267 return ((OptionSection) mUI.getContactDetailsSectionForTest()) | 304 return ((OptionSection) mUI.getContactDetailsSectionForTest()) |
268 .getNumberOfOptionLabelsForTest(); | 305 .getNumberOfOptionLabelsForTest(); |
269 } | 306 } |
270 }); | 307 }); |
271 } | 308 } |
272 | 309 |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 * Called when the UI is ready for input. | 558 * Called when the UI is ready for input. |
522 * | 559 * |
523 * @param ui The UI that is ready for input. | 560 * @param ui The UI that is ready for input. |
524 */ | 561 */ |
525 public void notifyCalled(T target) { | 562 public void notifyCalled(T target) { |
526 ThreadUtils.assertOnUiThread(); | 563 ThreadUtils.assertOnUiThread(); |
527 mTarget = target; | 564 mTarget = target; |
528 notifyCalled(); | 565 notifyCalled(); |
529 } | 566 } |
530 } | 567 } |
| 568 |
| 569 /** |
| 570 * Installs a payment app for testing. |
| 571 * |
| 572 * @param instrumentPresence Whether the app has any payment instruments. Ei
ther NO_INSTRUMENTS |
| 573 * or HAVE_INSTRUMENTS. |
| 574 * @param responseSpeed How quickly the app will respond to "get instru
ments" query. Either |
| 575 * IMMEDIATE_RESPONSE or DELAYED_RESPONSE. |
| 576 */ |
| 577 protected void installPaymentApp(final int instrumentPresence, final int res
ponseSpeed) { |
| 578 PaymentAppFactory.setAdditionalFactory(new PaymentAppFactoryAddition() { |
| 579 @Override |
| 580 public List<PaymentApp> create(WebContents webContents) { |
| 581 List<PaymentApp> additionalApps = new ArrayList<>(); |
| 582 additionalApps.add(new BobPay(instrumentPresence, responseSpeed)
); |
| 583 return additionalApps; |
| 584 } |
| 585 }); |
| 586 } |
| 587 |
| 588 /** A payment app implementation for test. */ |
| 589 private static class BobPay implements PaymentApp { |
| 590 private final int mInstrumentPresence; |
| 591 private final int mResponseSpeed; |
| 592 |
| 593 BobPay(int instrumentPresence, int responseSpeed) { |
| 594 mInstrumentPresence = instrumentPresence; |
| 595 mResponseSpeed = responseSpeed; |
| 596 } |
| 597 |
| 598 @Override |
| 599 public void getInstruments(JSONObject details, final InstrumentsCallback |
| 600 instrumentsCallback) { |
| 601 final List<PaymentInstrument> instruments = new ArrayList<>(); |
| 602 if (mInstrumentPresence == HAVE_INSTRUMENTS) instruments.add(new Bob
PayInstrument()); |
| 603 Runnable instrumentsReady = new Runnable() { |
| 604 @Override |
| 605 public void run() { |
| 606 ThreadUtils.assertOnUiThread(); |
| 607 instrumentsCallback.onInstrumentsReady(BobPay.this, instrume
nts); |
| 608 } |
| 609 }; |
| 610 if (mResponseSpeed == IMMEDIATE_RESPONSE) { |
| 611 instrumentsReady.run(); |
| 612 } else { |
| 613 new Handler().postDelayed(instrumentsReady, 100); |
| 614 } |
| 615 } |
| 616 |
| 617 @Override |
| 618 public Set<String> getSupportedMethodNames() { |
| 619 Set<String> methodNames = new HashSet<>(); |
| 620 methodNames.add("https://bobpay.com"); |
| 621 return methodNames; |
| 622 } |
| 623 |
| 624 @Override |
| 625 public String getIdentifier() { |
| 626 return "https://bobpay.com"; |
| 627 } |
| 628 } |
| 629 |
| 630 /** A payment instrument implementation for test. */ |
| 631 private static class BobPayInstrument extends PaymentInstrument { |
| 632 BobPayInstrument() { |
| 633 super("https://bobpay.com", "Bob Pay", null, NO_ICON); |
| 634 } |
| 635 |
| 636 @Override |
| 637 public String getMethodName() { |
| 638 return "https://bobpay.com"; |
| 639 } |
| 640 |
| 641 @Override |
| 642 public void getDetails(String merchantName, String origin, PaymentItem t
otal, |
| 643 List<PaymentItem> cart, JSONObject details, DetailsCallback deta
ilsCallback) { |
| 644 detailsCallback.onInstrumentDetailsReady( |
| 645 "https://bobpay.com", "{\"transaction\": 1337}"); |
| 646 } |
| 647 |
| 648 @Override |
| 649 public void dismiss() {} |
| 650 } |
531 } | 651 } |
OLD | NEW |