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 androidx.test.filters.MediumTest;
8 
9 import org.junit.ClassRule;
10 import org.junit.Rule;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 
14 import org.chromium.base.test.util.CommandLineFlags;
15 import org.chromium.base.test.util.Feature;
16 import org.chromium.chrome.browser.flags.ChromeSwitches;
17 import org.chromium.chrome.browser.payments.PaymentRequestTestRule.AppPresence;
18 import org.chromium.chrome.browser.payments.PaymentRequestTestRule.AppSpeed;
19 import org.chromium.chrome.browser.payments.PaymentRequestTestRule.FactorySpeed;
20 import org.chromium.chrome.browser.payments.PaymentRequestTestRule.TestFactory;
21 import org.chromium.chrome.browser.payments.PaymentRequestTestRule.TestPay;
22 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
23 import org.chromium.content_public.browser.test.util.TestThreadUtils;
24 import org.chromium.ui.test.util.DisableAnimationsTestRule;
25 
26 import java.util.concurrent.TimeoutException;
27 
28 /**
29  * A payment integration test for a merchant that requests payment via Bob Pay.
30  */
31 @RunWith(ChromeJUnit4ClassRunner.class)
32 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
33 public class PaymentRequestPaymentAppTest {
34     // Disable animations to reduce flakiness.
35     @ClassRule
36     public static DisableAnimationsTestRule sNoAnimationsRule = new DisableAnimationsTestRule();
37 
38     @Rule
39     public PaymentRequestTestRule mPaymentRequestTestRule =
40             new PaymentRequestTestRule("payment_request_bobpay_test.html");
41 
42     /** If no payment methods are supported, reject the show() promise. */
43     @Test
44     @MediumTest
45     @Feature({"Payments"})
testNoSupportedPaymentMethods()46     public void testNoSupportedPaymentMethods() throws TimeoutException {
47         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getShowFailed());
48         mPaymentRequestTestRule.expectResultContains(
49                 new String[] {"show() rejected", "The payment method", "not supported"});
50     }
51 
52     /**
53      * If Bob Pay factory does not have any apps, reject the show() promise. Here Bob Pay factory
54      * responds to Chrome immediately.
55      */
56     @Test
57     @MediumTest
58     @Feature({"Payments"})
testNoAppsInFastBobPayFactory()59     public void testNoAppsInFastBobPayFactory() throws TimeoutException {
60         mPaymentRequestTestRule.addPaymentAppFactory(
61                 AppPresence.NO_APPS, FactorySpeed.FAST_FACTORY);
62         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getShowFailed());
63         mPaymentRequestTestRule.expectResultContains(
64                 new String[] {"show() rejected", "The payment method", "not supported"});
65     }
66 
67     /**
68      * If Bob Pay factory does not have any apps, reject the show() promise. Here Bob Pay factory
69      * responds to Chrome after a slight delay.
70      */
71     @Test
72     @MediumTest
73     @Feature({"Payments"})
testNoAppsInSlowBobPayFactory()74     public void testNoAppsInSlowBobPayFactory() throws TimeoutException {
75         mPaymentRequestTestRule.addPaymentAppFactory(
76                 AppPresence.NO_APPS, FactorySpeed.SLOW_FACTORY);
77         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getShowFailed());
78         mPaymentRequestTestRule.expectResultContains(
79                 new String[] {"show() rejected", "The payment method", "not supported"});
80     }
81 
82     /** If the factory creates more payment apps after the UI has been dismissed, don't crash. */
83     @Test
84     @MediumTest
85     @Feature({"Payments"})
testAppsCreatedAfterDismissShouldNotCrash()86     public void testAppsCreatedAfterDismissShouldNotCrash() throws TimeoutException {
87         TestFactory factory = mPaymentRequestTestRule.addPaymentAppFactory(
88                 AppPresence.HAVE_APPS, FactorySpeed.FAST_FACTORY);
89         mPaymentRequestTestRule.openPageAndClickNodeAndWait(
90                 "buy", mPaymentRequestTestRule.getDismissed());
91         TestThreadUtils.runOnUiThreadBlocking(() -> {
92             factory.getDelegateForTest().onPaymentAppCreated(
93                     new TestPay("https://bobpay.com", AppSpeed.FAST_APP));
94             factory.getDelegateForTest().onPaymentAppCreated(
95                     new TestPay("https://alicepay.com", AppSpeed.FAST_APP));
96         });
97         mPaymentRequestTestRule.expectResultContains(new String[] {"\"transaction\": 1337"});
98     }
99 
100     /** If the factory calls into delegate after the UI has been dismissed, don't crash. */
101     @Test
102     @MediumTest
103     @Feature({"Payments"})
testFactoryActivityAfterDismissShouldNotCrash()104     public void testFactoryActivityAfterDismissShouldNotCrash() throws TimeoutException {
105         TestFactory factory = mPaymentRequestTestRule.addPaymentAppFactory(
106                 AppPresence.HAVE_APPS, FactorySpeed.FAST_FACTORY);
107         mPaymentRequestTestRule.openPageAndClickNodeAndWait(
108                 "buy", mPaymentRequestTestRule.getDismissed());
109         TestThreadUtils.runOnUiThreadBlocking(() -> {
110             factory.getDelegateForTest().onCanMakePaymentCalculated(true);
111             factory.getDelegateForTest().onDoneCreatingPaymentApps(factory);
112         });
113         mPaymentRequestTestRule.expectResultContains(new String[] {"\"transaction\": 1337"});
114     }
115 
116     /**
117      * If Bob Pay is supported and installed, user should be able to pay with it. Here Bob Pay
118      * factory responds to Chrome immediately.
119      */
120     @Test
121     @MediumTest
122     @Feature({"Payments"})
testPayViaFastBobPayFactory()123     public void testPayViaFastBobPayFactory() throws TimeoutException {
124         mPaymentRequestTestRule.addPaymentAppFactory(
125                 AppPresence.HAVE_APPS, FactorySpeed.FAST_FACTORY);
126         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getDismissed());
127         mPaymentRequestTestRule.expectResultContains(
128                 new String[] {"https://bobpay.com", "\"transaction\"", "1337"});
129     }
130 
131     /**
132      * If Bob Pay is supported and installed, user should be able to pay with it. Here Bob Pay
133      * factory responds to Chrome after a slight delay.
134      */
135     @Test
136     @MediumTest
137     @Feature({"Payments"})
testPayViaSlowBobPayFactory()138     public void testPayViaSlowBobPayFactory() throws TimeoutException {
139         mPaymentRequestTestRule.addPaymentAppFactory(
140                 AppPresence.HAVE_APPS, FactorySpeed.SLOW_FACTORY);
141         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getDismissed());
142         mPaymentRequestTestRule.expectResultContains(
143                 new String[] {"https://bobpay.com", "\"transaction\"", "1337"});
144     }
145 
146     /**
147      * Test payment with a Bob Pay that is created with a delay, but responds immediately to
148      * invokePaymentApp().
149      */
150     @Test
151     @MediumTest
152     @Feature({"Payments"})
testPayViaDelayedFastBobPay()153     public void testPayViaDelayedFastBobPay() throws TimeoutException {
154         mPaymentRequestTestRule.addPaymentAppFactory("https://bobpay.com", AppPresence.HAVE_APPS,
155                 FactorySpeed.FAST_FACTORY, AppSpeed.FAST_APP);
156         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getDismissed());
157         mPaymentRequestTestRule.expectResultContains(
158                 new String[] {"https://bobpay.com", "\"transaction\"", "1337"});
159     }
160 
161     /**
162      * Test payment with a Bob Pay that is created with a delay, and responds slowly to
163      * invokePaymentApp().
164      */
165     @Test
166     @MediumTest
167     @Feature({"Payments"})
testPayViaDelayedSlowBobPay()168     public void testPayViaDelayedSlowBobPay() throws TimeoutException {
169         mPaymentRequestTestRule.addPaymentAppFactory("https://bobpay.com", AppPresence.HAVE_APPS,
170                 FactorySpeed.SLOW_FACTORY, AppSpeed.SLOW_APP);
171         mPaymentRequestTestRule.openPageAndClickBuyAndWait(mPaymentRequestTestRule.getDismissed());
172         mPaymentRequestTestRule.expectResultContains(
173                 new String[] {"https://bobpay.com", "\"transaction\"", "1337"});
174     }
175 }
176