| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.payments; |
| 6 |
| 7 import android.support.v7.app.AlertDialog; |
| 8 import android.view.View; |
| 9 import android.widget.EditText; |
| 10 |
| 11 import org.chromium.base.ThreadUtils; |
| 12 import org.chromium.base.test.util.CommandLineFlags; |
| 13 import org.chromium.base.test.util.UrlUtils; |
| 14 import org.chromium.chrome.R; |
| 15 import org.chromium.chrome.browser.ChromeActivity; |
| 16 import org.chromium.chrome.browser.ChromeSwitches; |
| 17 import org.chromium.chrome.browser.autofill.CardUnmaskPrompt; |
| 18 import org.chromium.chrome.browser.payments.ui.PaymentRequestUI; |
| 19 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 20 import org.chromium.content.browser.ContentViewCore; |
| 21 import org.chromium.content.browser.test.util.Criteria; |
| 22 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 23 import org.chromium.content.browser.test.util.DOMUtils; |
| 24 import org.chromium.content_public.browser.WebContents; |
| 25 |
| 26 import java.util.concurrent.ExecutionException; |
| 27 import java.util.concurrent.TimeoutException; |
| 28 import java.util.concurrent.atomic.AtomicReference; |
| 29 |
| 30 /** |
| 31 * A base integration test for payments. |
| 32 */ |
| 33 @CommandLineFlags.Add({ChromeSwitches.EXPERIMENTAL_WEB_PLAFTORM_FEATURES}) |
| 34 abstract class PaymentRequestTestBase extends ChromeActivityTestCaseBase<ChromeA
ctivity> { |
| 35 private final AtomicReference<ContentViewCore> mViewCoreRef = |
| 36 new AtomicReference<ContentViewCore>(); |
| 37 private final AtomicReference<WebContents> mWebContentsRef = new AtomicRefer
ence<WebContents>(); |
| 38 private final String mTestFilePath; |
| 39 |
| 40 protected PaymentRequestTestBase(String testFileName) { |
| 41 super(ChromeActivity.class); |
| 42 mTestFilePath = UrlUtils.getIsolatedTestFilePath( |
| 43 String.format("chrome/test/data/android/payments/%s", testFileNa
me)); |
| 44 } |
| 45 |
| 46 @Override |
| 47 public void startMainActivity() throws InterruptedException {} |
| 48 |
| 49 protected abstract void onMainActivityStarted() |
| 50 throws InterruptedException, ExecutionException, TimeoutException; |
| 51 |
| 52 protected void triggerPaymentUI() |
| 53 throws InterruptedException, ExecutionException, TimeoutException { |
| 54 startMainActivityWithURL(mTestFilePath); |
| 55 onMainActivityStarted(); |
| 56 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 57 @Override |
| 58 public void run() { |
| 59 mViewCoreRef.set(getActivity().getCurrentContentViewCore()); |
| 60 mWebContentsRef.set(mViewCoreRef.get().getWebContents()); |
| 61 } |
| 62 }); |
| 63 assertWaitForPageScaleFactorMatch(1); |
| 64 DOMUtils.waitForNonZeroNodeBounds(mWebContentsRef.get(), "buy"); |
| 65 DOMUtils.clickNode(this, mViewCoreRef.get(), "buy"); |
| 66 } |
| 67 |
| 68 protected void clickClosePaymentUIButton() throws InterruptedException { |
| 69 clickPaymentUIButton("close", R.id.close_button); |
| 70 } |
| 71 |
| 72 protected void clickSecondaryPaymentUIButton() throws InterruptedException { |
| 73 clickPaymentUIButton("secondary", R.id.button_secondary); |
| 74 } |
| 75 |
| 76 protected void clickPrimaryPaymentUIButton() throws InterruptedException { |
| 77 clickPaymentUIButton("primary", R.id.button_primary); |
| 78 } |
| 79 |
| 80 private void clickPaymentUIButton(final String buttonName, final int resourc
eId) |
| 81 throws InterruptedException { |
| 82 CriteriaHelper.pollUiThread(new Criteria() { |
| 83 @Override |
| 84 public boolean isSatisfied() { |
| 85 PaymentRequestUI ui = PaymentRequestUI.getCurrentUIForTest(); |
| 86 if (ui == null) { |
| 87 updateFailureReason("Payment UI was not shown"); |
| 88 return false; |
| 89 } |
| 90 |
| 91 View button = ui.getDialogForTest().findViewById(resourceId); |
| 92 if (button == null) { |
| 93 updateFailureReason( |
| 94 String.format("Cannot find the %s button on payment
UI", buttonName)); |
| 95 return false; |
| 96 } |
| 97 |
| 98 if (!button.isEnabled()) { |
| 99 updateFailureReason( |
| 100 String.format("The %s button on payment UI is disabl
ed", buttonName)); |
| 101 return false; |
| 102 } |
| 103 |
| 104 ui.setShowResultDelayForTest(0); |
| 105 button.performClick(); |
| 106 return true; |
| 107 } |
| 108 }); |
| 109 } |
| 110 |
| 111 protected void waitForPaymentUIHidden() throws InterruptedException { |
| 112 CriteriaHelper.pollUiThread(new Criteria("Payment UI should be hidden")
{ |
| 113 @Override |
| 114 public boolean isSatisfied() { |
| 115 return PaymentRequestUI.getCurrentUIForTest() == null; |
| 116 } |
| 117 }); |
| 118 } |
| 119 |
| 120 protected void typeInCvc(final String cvc) throws InterruptedException { |
| 121 CriteriaHelper.pollUiThread(new Criteria() { |
| 122 @Override |
| 123 public boolean isSatisfied() { |
| 124 CardUnmaskPrompt prompt = CardUnmaskPrompt.getCurrentPromptForTe
st(); |
| 125 if (prompt == null) { |
| 126 updateFailureReason("CVC prompt is not shown"); |
| 127 return false; |
| 128 } |
| 129 |
| 130 EditText cvcInput = |
| 131 (EditText) prompt.getDialogForTest().findViewById(R.id.c
ard_unmask_input); |
| 132 if (cvcInput == null) { |
| 133 updateFailureReason("Cannot find CVC input on the prompt"); |
| 134 return false; |
| 135 } |
| 136 |
| 137 if (!cvcInput.isEnabled()) { |
| 138 updateFailureReason("CVC input is disabled"); |
| 139 return false; |
| 140 } |
| 141 |
| 142 cvcInput.setText(cvc); |
| 143 return true; |
| 144 } |
| 145 }); |
| 146 } |
| 147 |
| 148 protected void clickPrimaryCVCPromptButton() throws InterruptedException { |
| 149 CriteriaHelper.pollUiThread(new Criteria() { |
| 150 @Override |
| 151 public boolean isSatisfied() { |
| 152 CardUnmaskPrompt prompt = CardUnmaskPrompt.getCurrentPromptForTe
st(); |
| 153 if (prompt == null) { |
| 154 updateFailureReason("CVC prompt was not shown"); |
| 155 return false; |
| 156 } |
| 157 |
| 158 View positiveButton = |
| 159 prompt.getDialogForTest().getButton(AlertDialog.BUTTON_P
OSITIVE); |
| 160 if (positiveButton == null) { |
| 161 updateFailureReason("Cannot find the positive button on the
CVC prompt"); |
| 162 return false; |
| 163 } |
| 164 |
| 165 if (!positiveButton.isEnabled()) { |
| 166 updateFailureReason("The positive button the CVC prompt is d
isabled"); |
| 167 return false; |
| 168 } |
| 169 |
| 170 prompt.setShowResultDelayForTest(0); |
| 171 positiveButton.performClick(); |
| 172 return true; |
| 173 } |
| 174 }); |
| 175 } |
| 176 |
| 177 protected void expectResultContains(final String[] contents) throws Interrup
tedException { |
| 178 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
| 179 @Override |
| 180 public boolean isSatisfied() { |
| 181 try { |
| 182 String result = DOMUtils.getNodeContents(mWebContentsRef.get
(), "result"); |
| 183 if (result == null) { |
| 184 updateFailureReason("Cannot find 'result' node on test p
age"); |
| 185 return false; |
| 186 } |
| 187 for (int i = 0; i < contents.length; i++) { |
| 188 if (!result.contains(contents[i])) { |
| 189 updateFailureReason( |
| 190 String.format("Result should contain '%s'",
contents[i])); |
| 191 return false; |
| 192 } |
| 193 } |
| 194 return true; |
| 195 } catch (InterruptedException e1) { |
| 196 updateFailureReason(e1.getMessage()); |
| 197 return false; |
| 198 } catch (TimeoutException e2) { |
| 199 updateFailureReason(e2.getMessage()); |
| 200 return false; |
| 201 } |
| 202 } |
| 203 }); |
| 204 } |
| 205 } |
| OLD | NEW |