// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.payments;
import androidx.test.filters.MediumTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.autofill.AutofillTestHelper;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.payments.PaymentRequestTestRule.AppPresence;
import org.chromium.chrome.browser.payments.PaymentRequestTestRule.FactorySpeed;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.R;
import org.chromium.components.autofill.AutofillProfile;
import org.chromium.components.payments.Event2;
import java.util.concurrent.TimeoutException;
/** A payment integration test for a merchant that requests contact details. */
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class PaymentRequestContactDetailsTest {
@Rule
public PaymentRequestTestRule mPaymentRequestTestRule =
new PaymentRequestTestRule("payment_request_contact_details_test.html");
@Before
public void setUp() throws TimeoutException {
AutofillTestHelper helper = new AutofillTestHelper();
// The user has valid payer name, phone number and email address on disk.
helper.setProfile(
AutofillProfile.builder()
.setFullName("Jon Doe")
.setCompanyName("Google")
.setStreetAddress("340 Main St")
.setRegion("CA")
.setLocality("Los Angeles")
.setPostalCode("90291")
.setCountryCode("US")
.setPhoneNumber("555-555-5555")
.setEmailAddress("[email protected]")
.setLanguageCode("en-US")
.build());
// Add the same profile but with a different address.
helper.setProfile(
AutofillProfile.builder()
.setFullName("")
.setCompanyName("Google")
.setStreetAddress("999 Main St")
.setRegion("CA")
.setLocality("Los Angeles")
.setPostalCode("90291")
.setCountryCode("US")
.setPhoneNumber("555-555-5555")
.setEmailAddress("[email protected]")
.setLanguageCode("en-US")
.build());
// Add the same profile but without a phone number.
helper.setProfile(
AutofillProfile.builder()
.setFullName("Jon Doe")
.setCompanyName("Google")
.setStreetAddress("340 Main St")
.setRegion("CA")
.setLocality("Los Angeles")
.setPostalCode("90291")
.setCountryCode("US")
.setPhoneNumber("") /* empty phone_number */
.setEmailAddress("[email protected]")
.setLanguageCode("en-US")
.build());
// Add the same profile but without an email.
helper.setProfile(
AutofillProfile.builder()
.setFullName("Jon Doe")
.setCompanyName("Google")
.setStreetAddress("340 Main St")
.setRegion("CA")
.setLocality("Los Angeles")
.setPostalCode("90291")
.setCountryCode("US")
.setPhoneNumber("555-555-5555")
.setEmailAddress("") /* emailAddress */
.setLanguageCode("en-US")
.build());
// Add the same profile but without a name.
helper.setProfile(
AutofillProfile.builder()
.setFullName("")
.setCompanyName("Google")
.setStreetAddress("340 Main St")
.setRegion("CA")
.setLocality("Los Angeles")
.setPostalCode("90291")
.setCountryCode("US")
.setPhoneNumber("555-555-5555")
.setEmailAddress("[email protected]")
.setLanguageCode("en-US")
.build());
mPaymentRequestTestRule.addPaymentAppFactory(
AppPresence.HAVE_APPS, FactorySpeed.FAST_FACTORY);
}
/** Provide the existing valid payer name, phone number and email address to the merchant. */
@Test
@MediumTest
@Feature({"Payments"})
public void testPay() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.button_primary, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"Jon Doe", "+15555555555", "[email protected]"});
}
/** Attempt to add invalid contact information and cancel the transaction. */
@Test
@MediumTest
@Feature({"Payments"})
public void testAddInvalidContactAndCancel() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_add_option_button, mPaymentRequestTestRule.getReadyToEdit());
mPaymentRequestTestRule.setTextInEditorAndWait(
new String[] {"", "+++", "jane.jones"},
mPaymentRequestTestRule.getEditorTextUpdate());
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.editor_dialog_done_button, mPaymentRequestTestRule.getEditorValidationError());
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.payments_edit_cancel_button, mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.close_button, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"User closed the Payment Request UI."});
}
/** Add new payer name, phone number and email address and provide that to the merchant. */
@Test
@MediumTest
@Feature({"Payments"})
public void testAddContactAndPay() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_add_option_button, mPaymentRequestTestRule.getReadyToEdit());
mPaymentRequestTestRule.setTextInEditorAndWait(
new String[] {"Jane Jones", "999-999-9999", "[email protected]"},
mPaymentRequestTestRule.getEditorTextUpdate());
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.editor_dialog_done_button, mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.button_primary, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"Jane Jones", "+19999999999", "[email protected]"});
}
/** Quickly pressing on "add contact info" and then [X] should not crash. */
@Test
@MediumTest
@Feature({"Payments"})
public void testQuickAddContactAndCloseShouldNotCrash() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
// Quickly press on "add contact info" and then [X].
int callCount = mPaymentRequestTestRule.getReadyToEdit().getCallCount();
ThreadUtils.runOnUiThreadBlocking(
() -> {
mPaymentRequestTestRule
.getPaymentRequestUI()
.getContactDetailsSectionForTest()
.findViewById(R.id.payments_add_option_button)
.performClick();
mPaymentRequestTestRule
.getPaymentRequestUI()
.getDialogForTest()
.findViewById(R.id.close_button)
.performClick();
});
mPaymentRequestTestRule.getReadyToEdit().waitForCallback(callCount);
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.payments_edit_cancel_button, mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.close_button, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"User closed the Payment Request UI."});
}
/** Quickly pressing on [X] and then "add contact info" should not crash. */
@Test
@MediumTest
@Feature({"Payments"})
public void testQuickCloseAndAddContactShouldNotCrash() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
// Quickly press on [X] and then "add contact info."
int callCount = mPaymentRequestTestRule.getDismissed().getCallCount();
ThreadUtils.runOnUiThreadBlocking(
() -> {
mPaymentRequestTestRule
.getPaymentRequestUI()
.getDialogForTest()
.findViewById(R.id.close_button)
.performClick();
mPaymentRequestTestRule
.getPaymentRequestUI()
.getContactDetailsSectionForTest()
.findViewById(R.id.payments_add_option_button)
.performClick();
});
mPaymentRequestTestRule.getDismissed().waitForCallback(callCount);
mPaymentRequestTestRule.expectResultContains(
new String[] {"User closed the Payment Request UI."});
}
/** Test that going into the editor and cancelling will leave the row checked. */
@Test
@MediumTest
@Feature({"Payments"})
public void testEditContactAndCancelEditorShouldKeepContactSelected() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
mPaymentRequestTestRule.expectContactDetailsRowIsSelected(0);
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_open_editor_pencil_button, mPaymentRequestTestRule.getReadyToEdit());
// Cancel the editor.
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.payments_edit_cancel_button, mPaymentRequestTestRule.getReadyToPay());
// Expect the row to still be selected in the Contact Details section.
mPaymentRequestTestRule.expectContactDetailsRowIsSelected(0);
}
/** Test that going into the "add" flow and cancelling will leave existing row checked. */
@Test
@MediumTest
@Feature({"Payments"})
public void testAddContactAndCancelEditorShouldKeepContactSelected() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
mPaymentRequestTestRule.expectContactDetailsRowIsSelected(0);
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_add_option_button, mPaymentRequestTestRule.getReadyToEdit());
// Cancel the editor.
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.payments_edit_cancel_button, mPaymentRequestTestRule.getReadyToPay());
// Expect the existing row to still be selected in the Contact Details section.
mPaymentRequestTestRule.expectContactDetailsRowIsSelected(0);
}
/** Quickly pressing on "add contact info" and then "cancel" should not crash. */
@Test
@MediumTest
@Feature({"Payments"})
public void testQuickAddContactAndCancelShouldNotCrash() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
// Quickly press on "add contact info" and then "cancel."
int callCount = mPaymentRequestTestRule.getReadyToEdit().getCallCount();
ThreadUtils.runOnUiThreadBlocking(
() -> {
mPaymentRequestTestRule
.getPaymentRequestUI()
.getContactDetailsSectionForTest()
.findViewById(R.id.payments_add_option_button)
.performClick();
mPaymentRequestTestRule
.getPaymentRequestUI()
.getDialogForTest()
.findViewById(R.id.button_secondary)
.performClick();
});
mPaymentRequestTestRule.getReadyToEdit().waitForCallback(callCount);
mPaymentRequestTestRule.clickInEditorAndWait(
R.id.payments_edit_cancel_button, mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.close_button, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"User closed the Payment Request UI."});
}
/** Quickly pressing on "cancel" and then "add contact info" should not crash. */
@Test
@MediumTest
@DisabledTest(message = "crbug.com/1182234")
@Feature({"Payments"})
public void testQuickCancelAndAddContactShouldNotCrash() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
// Quickly press on "cancel" and then "add contact info."
int callCount = mPaymentRequestTestRule.getDismissed().getCallCount();
ThreadUtils.runOnUiThreadBlocking(
() -> {
mPaymentRequestTestRule
.getPaymentRequestUI()
.getDialogForTest()
.findViewById(R.id.button_secondary)
.performClick();
mPaymentRequestTestRule
.getPaymentRequestUI()
.getContactDetailsSectionForTest()
.findViewById(R.id.payments_add_option_button)
.performClick();
});
mPaymentRequestTestRule.getDismissed().waitForCallback(callCount);
mPaymentRequestTestRule.expectResultContains(
new String[] {"User closed the Payment Request UI."});
}
/**
* Makes sure that suggestions that are equal to or subsets of other suggestions are not shown
* to the user.
*/
@Test
@MediumTest
@Feature({"Payments"})
public void testSuggestionsDeduped() throws TimeoutException {
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
Assert.assertEquals(1, mPaymentRequestTestRule.getNumberOfContactDetailSuggestions());
}
/**
* Test that ending a payment request that requires the user's email address, phone number and
* name results in the appropriate metric being logged in PaymentRequest.Events.
*/
@Test
@MediumTest
@DisabledTest(message = "crbug.com/1182234")
@Feature({"Payments"})
public void testPaymentRequestEventsMetric() throws TimeoutException {
// Start and complete the Payment Request.
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickAndWait(
R.id.button_primary, mPaymentRequestTestRule.getDismissed());
mPaymentRequestTestRule.expectResultContains(
new String[] {"Jon Doe", "+15555555555", "[email protected]"});
int expectedSample =
Event2.SHOWN
| Event2.COMPLETED
| Event2.PAY_CLICKED
| Event2.HAD_INITIAL_FORM_OF_PAYMENT
| Event2.REQUEST_PAYER_DATA
| Event2.REQUEST_METHOD_BASIC_CARD
| Event2.REQUEST_METHOD_OTHER
| Event2.SELECTED_OTHER;
Assert.assertEquals(
1,
RecordHistogram.getHistogramValueCountForTesting(
"PaymentRequest.Events2", expectedSample));
}
/**
* Test that requesting contact details in an incognito window does not crash. Previously the
* helper text ("Cards and addresses are from...") would try to fetch the signed-in user in
* incognito and hit a null-deference in doing so.
*
* <p>See https://crbug.com/1311352
*/
@Test
@MediumTest
@Feature({"Payments"})
public void testPaymentRequestIncognitoMode() throws TimeoutException {
// Open the test page in an incognito window.
mPaymentRequestTestRule.newIncognitoTabFromMenu();
mPaymentRequestTestRule.loadUrl(
mPaymentRequestTestRule
.getTestServer()
.getURL(
"/components/test/data/payments/payment_request_contact_details_test.html"));
mPaymentRequestTestRule.setObserversAndWaitForInitialPageLoad();
// Trigger the PaymentRequest, and expand the contact info section to show the text. This is
// where the code would previously crash.
mPaymentRequestTestRule.triggerUIAndWait("buy", mPaymentRequestTestRule.getReadyToPay());
mPaymentRequestTestRule.clickInContactInfoAndWait(
R.id.payments_section, mPaymentRequestTestRule.getReadyForInput());
// Close the dialog.
mPaymentRequestTestRule.clickAndWait(
R.id.button_primary, mPaymentRequestTestRule.getDismissed());
}
}