blob: 660afc775b5b59325ee851a12990ad76fc0e7a20 [file] [log] [blame]
Rouslan Solomakhin94e0ab242020-06-03 00:38:351// Copyright 2020 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#include <map>
6#include <string>
7
Rouslan Solomakhin575238d62020-06-04 17:06:538#include "base/test/scoped_feature_list.h"
Rouslan Solomakhin94e0ab242020-06-03 00:38:359#include "build/build_config.h"
10#include "chrome/test/payments/payment_request_platform_browsertest_base.h"
11#include "chrome/test/payments/payment_request_test_controller.h"
12#include "chrome/test/payments/test_event_waiter.h"
13#include "content/public/test/browser_test.h"
14#include "content/public/test/browser_test_utils.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
Rouslan Solomakhin575238d62020-06-04 17:06:5317#if defined(OS_ANDROID)
18#include "components/payments/content/android/payment_feature_list.h"
19#endif // OS_ANDROID
20
Rouslan Solomakhin94e0ab242020-06-03 00:38:3521namespace payments {
22namespace {
23
Rouslan Solomakhin575238d62020-06-04 17:06:5324constexpr char kOriginalPrice[] = "5.00";
25constexpr char kDiscountPrice[] = "4.00";
26
Rouslan Solomakhin94e0ab242020-06-03 00:38:3527class PaymentHandlerCapabilitiesTest
Rouslan Solomakhin575238d62020-06-04 17:06:5328 : public PaymentRequestPlatformBrowserTestBase,
29 public testing::WithParamInterface<bool> {
Rouslan Solomakhin94e0ab242020-06-03 00:38:3530 public:
Rouslan Solomakhin575238d62020-06-04 17:06:5331#if defined(OS_ANDROID)
32 PaymentHandlerCapabilitiesTest() {
33 if (GetParam()) {
34 features_.InitAndEnableFeature(android::kScrollToExpandPaymentHandler);
35 } else {
36 features_.InitAndDisableFeature(android::kScrollToExpandPaymentHandler);
37 }
38 }
39#endif // OS_ANDROID
40
41 void ExpectAppTotals() {
42 EXPECT_EQ(expected_app_totals_.size(),
43 test_controller()->app_descriptions().size());
Rouslan Solomakhin94e0ab242020-06-03 00:38:3544 for (const auto& app : test_controller()->app_descriptions()) {
Rouslan Solomakhin575238d62020-06-04 17:06:5345 auto iter = expected_app_totals_.find(app.sublabel);
46 ASSERT_NE(expected_app_totals_.end(), iter)
Rouslan Solomakhin94e0ab242020-06-03 00:38:3547 << "Origin \"" << app.sublabel << "\" was not expected.";
48 EXPECT_EQ(iter->second, app.total)
49 << app.sublabel << " should have a total of \"" << iter->second
50 << "\", but \"" << app.total << "\" was found instead.";
51 }
52 }
Rouslan Solomakhin575238d62020-06-04 17:06:5353
54 void ExpectModifiedPriceForOrigin(const std::string& origin,
55 const std::string& value) {
56 // Android payment apps pre-format the modified value.
57#if defined(OS_ANDROID)
58 expected_app_totals_[origin] = "$" + value;
59#else
60 expected_app_totals_[origin] = "USD " + value;
61#endif // OS_ANDROID
62 }
63
64 void ExpectPriceNotModifiedForOrigin(const std::string& origin,
65 const std::string& value) {
66 expected_app_totals_[origin] = "USD " + value;
67 }
68
69 std::map<std::string, std::string> expected_app_totals_;
70 base::test::ScopedFeatureList features_;
Rouslan Solomakhin94e0ab242020-06-03 00:38:3571};
72
Rouslan Solomakhin575238d62020-06-04 17:06:5373// Only Andorid has the scroll to expand payment handler feature.
74INSTANTIATE_TEST_SUITE_P(ScrollToExpand,
75 PaymentHandlerCapabilitiesTest,
76 ::testing::Values(
77#if defined(OS_ANDROID)
78 // "Scroll to expand" payment handler is an
79 // Android-only feature.
80 true,
81#endif // OS_ANDROID
82 false));
83
84// Modified price should be displayed for the payment handler with the matching
85// capabilities.
86IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest, TwoApps) {
Rouslan Solomakhin94e0ab242020-06-03 00:38:3587 NavigateTo("alicepay.com", "/payment_handler_installer.html");
88 EXPECT_EQ(
89 "success",
90 content::EvalJs(GetActiveWebContents(),
91 "installWithCapabilities('alicepay.com/app1/app.js', "
92 "'basic-card', {supportedNetworks: ['visa']})"));
93 NavigateTo("bobpay.com", "/payment_handler_installer.html");
94 EXPECT_EQ(
95 "success",
96 content::EvalJs(GetActiveWebContents(),
97 "installWithCapabilities('bobpay.com/app1/app.js', "
98 "'basic-card', {supportedNetworks: ['mastercard']})"));
99
Sahel Sharifyfd677ff2020-07-08 19:01:52100 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin94e0ab242020-06-03 00:38:35101 NavigateTo("test.com",
102 "/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
103 EXPECT_TRUE(
104 content::ExecJs(GetActiveWebContents(), "visaSupportedNetwork()"));
105 WaitForObservedEvent();
106
Rouslan Solomakhin575238d62020-06-04 17:06:53107 ExpectModifiedPriceForOrigin("alicepay.com", kDiscountPrice);
108 ExpectPriceNotModifiedForOrigin("bobpay.com", kOriginalPrice);
Rouslan Solomakhin94e0ab242020-06-03 00:38:35109
Rouslan Solomakhin575238d62020-06-04 17:06:53110 ExpectAppTotals();
111}
112
113// A "basic-card" modifier without any networks will apply to a payment handler
114// that does not declare its capabilities.
115IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
116 AllCardsModifierMatchesAppWithoutCapabilities) {
117 NavigateTo("bobpay.com", "/payment_handler_installer.html");
118 EXPECT_EQ("success",
119 content::EvalJs(GetActiveWebContents(),
120 "installWithCapabilities('bobpay.com/app1/app.js', "
121 "'basic-card', {})"));
122
Sahel Sharifyfd677ff2020-07-08 19:01:52123 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53124 NavigateTo("test.com",
125 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
126 "data_test.html");
127 EXPECT_TRUE(
128 content::ExecJs(GetActiveWebContents(), "buyWithAllCardsModifier()"));
129 WaitForObservedEvent();
130
131 ExpectModifiedPriceForOrigin("bobpay.com", kDiscountPrice);
132 ExpectAppTotals();
133}
134
135// A "basic-card" modifier without any networks will apply to a payment handler
136// with visa capabilities.
137IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
138 AllCardsModifierMatchesVisaCapabilities) {
139 NavigateTo("bobpay.com", "/payment_handler_installer.html");
140 EXPECT_EQ("success",
141 content::EvalJs(GetActiveWebContents(),
142 "installWithCapabilities('bobpay.com/app1/app.js', "
143 "'basic-card', {supportedNetworks: ['visa']})"));
144
Sahel Sharifyfd677ff2020-07-08 19:01:52145 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53146 NavigateTo("test.com",
147 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
148 "data_test.html");
149 EXPECT_TRUE(
150 content::ExecJs(GetActiveWebContents(), "buyWithAllCardsModifier()"));
151 WaitForObservedEvent();
152
153 ExpectModifiedPriceForOrigin("bobpay.com", kDiscountPrice);
154 ExpectAppTotals();
155}
156
157// A "basic-card" modifier with visa network will not apply to a payment handler
158// that does not declare its capabilities.
159IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
160 VisaCardsModifierDoesNotMatchAppWithoutCapabilities) {
161 NavigateTo("bobpay.com", "/payment_handler_installer.html");
162 EXPECT_EQ("success",
163 content::EvalJs(GetActiveWebContents(),
164 "installWithCapabilities('bobpay.com/app1/app.js', "
165 "'basic-card', {})"));
166
Sahel Sharifyfd677ff2020-07-08 19:01:52167 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53168 NavigateTo("test.com",
169 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
170 "data_test.html");
171 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "buyWithVisaModifier()"));
172 WaitForObservedEvent();
173
174 ExpectPriceNotModifiedForOrigin("bobpay.com", kOriginalPrice);
175 ExpectAppTotals();
176}
177
178// A "basic-card" modifier with visa network will apply to a payment handler
179// with visa capabilities.
180IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
181 VisaCardsModifierMatchesVisaCapabilities) {
182 NavigateTo("bobpay.com", "/payment_handler_installer.html");
183 EXPECT_EQ("success",
184 content::EvalJs(GetActiveWebContents(),
185 "installWithCapabilities('bobpay.com/app1/app.js', "
186 "'basic-card', {supportedNetworks: ['visa']})"));
187
Sahel Sharifyfd677ff2020-07-08 19:01:52188 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53189 NavigateTo("test.com",
190 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
191 "data_test.html");
192 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "buyWithVisaModifier()"));
193 WaitForObservedEvent();
194
195 ExpectModifiedPriceForOrigin("bobpay.com", kDiscountPrice);
196 ExpectAppTotals();
197}
198
199// A "basic-card" modifier without any networks will apply to a payment handler
200// with mastercard capabilities.
201IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
202 AllCardsModifierMatchesMastercardCapabilities) {
203 NavigateTo("bobpay.com", "/payment_handler_installer.html");
204 EXPECT_EQ(
205 "success",
206 content::EvalJs(GetActiveWebContents(),
207 "installWithCapabilities('bobpay.com/app1/app.js', "
208 "'basic-card', {supportedNetworks: ['mastercard']})"));
209
Sahel Sharifyfd677ff2020-07-08 19:01:52210 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53211 NavigateTo("test.com",
212 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
213 "data_test.html");
214 EXPECT_TRUE(
215 content::ExecJs(GetActiveWebContents(), "buyWithAllCardsModifier()"));
216 WaitForObservedEvent();
217
218 ExpectModifiedPriceForOrigin("bobpay.com", kDiscountPrice);
219 ExpectAppTotals();
220}
221
222// A "basic-card" modifier with visa network will not apply to a payment handler
223// with mastercard capabilities.
224IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest,
225 VisaCardsModifierDoesNotMatchMastercardCapabilities) {
226 NavigateTo("bobpay.com", "/payment_handler_installer.html");
227 EXPECT_EQ(
228 "success",
229 content::EvalJs(GetActiveWebContents(),
230 "installWithCapabilities('bobpay.com/app1/app.js', "
231 "'basic-card', {supportedNetworks: ['mastercard']})"));
232
Sahel Sharifyfd677ff2020-07-08 19:01:52233 ResetEventWaiterForSingleEvent(TestEvent::kAppListReady);
Rouslan Solomakhin575238d62020-06-04 17:06:53234 NavigateTo("test.com",
235 "/payment_request_bobpay_and_basic_card_with_modifier_optional_"
236 "data_test.html");
237 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "buyWithVisaModifier()"));
238 WaitForObservedEvent();
239
240 ExpectPriceNotModifiedForOrigin("bobpay.com", kOriginalPrice);
241 ExpectAppTotals();
Rouslan Solomakhin94e0ab242020-06-03 00:38:35242}
243
244} // namespace
245} // namespace payments