Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 1 | // 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 Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 8 | #include "base/test/scoped_feature_list.h" |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 9 | #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 Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 17 | #if defined(OS_ANDROID) |
| 18 | #include "components/payments/content/android/payment_feature_list.h" |
| 19 | #endif // OS_ANDROID |
| 20 | |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 21 | namespace payments { |
| 22 | namespace { |
| 23 | |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 24 | constexpr char kOriginalPrice[] = "5.00"; |
| 25 | constexpr char kDiscountPrice[] = "4.00"; |
| 26 | |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 27 | class PaymentHandlerCapabilitiesTest |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 28 | : public PaymentRequestPlatformBrowserTestBase, |
| 29 | public testing::WithParamInterface<bool> { |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 30 | public: |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 31 | #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 Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 44 | for (const auto& app : test_controller()->app_descriptions()) { |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 45 | auto iter = expected_app_totals_.find(app.sublabel); |
| 46 | ASSERT_NE(expected_app_totals_.end(), iter) |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 47 | << "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 Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 53 | |
| 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 Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 71 | }; |
| 72 | |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 73 | // Only Andorid has the scroll to expand payment handler feature. |
| 74 | INSTANTIATE_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. |
| 86 | IN_PROC_BROWSER_TEST_P(PaymentHandlerCapabilitiesTest, TwoApps) { |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 87 | 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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 100 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 101 | 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 Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 107 | ExpectModifiedPriceForOrigin("alicepay.com", kDiscountPrice); |
| 108 | ExpectPriceNotModifiedForOrigin("bobpay.com", kOriginalPrice); |
Rouslan Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 109 | |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 110 | ExpectAppTotals(); |
| 111 | } |
| 112 | |
| 113 | // A "basic-card" modifier without any networks will apply to a payment handler |
| 114 | // that does not declare its capabilities. |
| 115 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 123 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 124 | 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. |
| 137 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 145 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 146 | 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. |
| 159 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 167 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 168 | 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. |
| 180 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 188 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 189 | 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. |
| 201 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 210 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 211 | 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. |
| 224 | IN_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 Sharify | fd677ff | 2020-07-08 19:01:52 | [diff] [blame] | 233 | ResetEventWaiterForSingleEvent(TestEvent::kAppListReady); |
Rouslan Solomakhin | 575238d6 | 2020-06-04 17:06:53 | [diff] [blame] | 234 | 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 Solomakhin | 94e0ab24 | 2020-06-03 00:38:35 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | } // namespace |
| 245 | } // namespace payments |