1 // Copyright 2017 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 <vector>
6 
7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/scoped_feature_list.h"
10 #include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h"
11 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h"
12 #include "components/autofill/core/browser/autofill_test_utils.h"
13 #include "components/payments/core/features.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/browser_test.h"
17 #include "content/public/test/browser_test_utils.h"
18 
19 namespace payments {
20 
21 class PaymentRequestModifiersTest : public PaymentRequestBrowserTestBase {
22  protected:
PaymentRequestModifiersTest()23   PaymentRequestModifiersTest() {}
24 
SetUpCommandLine(base::CommandLine * command_line)25   void SetUpCommandLine(base::CommandLine* command_line) override {
26     PaymentRequestBrowserTestBase::SetUpCommandLine(command_line);
27     command_line->AppendSwitch(
28         switches::kEnableExperimentalWebPlatformFeatures);
29   }
30 
SetUpInProcessBrowserTestFixture()31   void SetUpInProcessBrowserTestFixture() override {
32     PaymentRequestBrowserTestBase::SetUpInProcessBrowserTestFixture();
33 
34     feature_list_.InitWithFeatures(
35         {
36             features::kWebPaymentsModifiers,
37             features::kReturnGooglePayInBasicCard,
38         },
39         {});
40   }
41 
GetLineCount()42   size_t GetLineCount() {
43     auto* top = dialog_view()->view_stack_for_testing()->top();
44     const auto* content =
45         top->GetViewByID(static_cast<int>(DialogViewID::CONTENT_VIEW));
46     return content->children().size();
47   }
48 
49  private:
50   base::test::ScopedFeatureList feature_list_;
51 
52   DISALLOW_COPY_AND_ASSIGN(PaymentRequestModifiersTest);
53 };
54 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,NoModifierAppliedIfNoSelectedInstrument)55 IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,
56                        NoModifierAppliedIfNoSelectedInstrument) {
57   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
58   InvokePaymentRequestUI();
59   OpenOrderSummaryScreen();
60 
61   EXPECT_EQ(base::ASCIIToUTF16("$5.00"),
62             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
63   // There's only the total line.
64   EXPECT_EQ(1u, GetLineCount());
65 }
66 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,ModifierAppliedIfApplicableSelectedInstrumentWithoutNetwork)67 IN_PROC_BROWSER_TEST_F(
68     PaymentRequestModifiersTest,
69     ModifierAppliedIfApplicableSelectedInstrumentWithoutNetwork) {
70   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
71   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
72   AddAutofillProfile(profile);
73   autofill::CreditCard card(
74       autofill::test::GetMaskedServerCard());  // Mastercard card.
75   card.set_billing_address_id(profile.guid());
76   AddCreditCard(card);
77 
78   InvokePaymentRequestUI();
79   OpenOrderSummaryScreen();
80 
81   EXPECT_EQ(base::ASCIIToUTF16("$4.00"),
82             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
83   // A line for the discount and one for the total.
84   EXPECT_EQ(2u, GetLineCount());
85 }
86 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,ModifierAppliedIfApplicableSelectedInstrumentWithMatchingNetwork)87 IN_PROC_BROWSER_TEST_F(
88     PaymentRequestModifiersTest,
89     ModifierAppliedIfApplicableSelectedInstrumentWithMatchingNetwork) {
90   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
91   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
92   AddAutofillProfile(profile);
93   autofill::CreditCard card(
94       autofill::test::GetMaskedServerCard());  // Mastercard card.
95   card.set_billing_address_id(profile.guid());
96   AddCreditCard(card);
97 
98   ResetEventWaiterForDialogOpened();
99   content::WebContents* web_contents = GetActiveWebContents();
100   const std::string click_buy_button_js =
101       "(function() { "
102       "document.getElementById('mastercard_supported_network'"
103       ").click(); })();";
104   ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js));
105   WaitForObservedEvent();
106   // The web-modal dialog should be open.
107   web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
108       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
109   EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
110 
111   OpenOrderSummaryScreen();
112 
113   EXPECT_EQ(base::ASCIIToUTF16("$4.00"),
114             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
115   // A line for the discount and one for the total.
116   EXPECT_EQ(2u, GetLineCount());
117 }
118 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,ModifierNotAppliedIfSelectedInstrumentWithoutMatchingNetwork)119 IN_PROC_BROWSER_TEST_F(
120     PaymentRequestModifiersTest,
121     ModifierNotAppliedIfSelectedInstrumentWithoutMatchingNetwork) {
122   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
123   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
124   AddAutofillProfile(profile);
125   autofill::CreditCard card(
126       autofill::test::GetMaskedServerCard());  // Mastercard card.
127   card.set_billing_address_id(profile.guid());
128   AddCreditCard(card);
129 
130   ResetEventWaiterForDialogOpened();
131   content::WebContents* web_contents = GetActiveWebContents();
132   const std::string click_buy_button_js =
133       "(function() { "
134       "document.getElementById('visa_supported_network')."
135       "click(); })();";
136   ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js));
137   WaitForObservedEvent();
138   // The web-modal dialog should be open.
139   web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
140       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
141   EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
142 
143   OpenOrderSummaryScreen();
144 
145   EXPECT_EQ(base::ASCIIToUTF16("$5.00"),
146             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
147   // There's only the total line.
148   EXPECT_EQ(1u, GetLineCount());
149 }
150 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,ModifierAppliedToBasicCardWithoutTypeOrNetwork)151 IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,
152                        ModifierAppliedToBasicCardWithoutTypeOrNetwork) {
153   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
154   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
155   AddAutofillProfile(profile);
156   autofill::CreditCard card(autofill::test::GetCreditCard());  // Visa card.
157   card.set_billing_address_id(profile.guid());
158   AddCreditCard(card);
159 
160   InvokePaymentRequestUI();
161   OpenOrderSummaryScreen();
162 
163   EXPECT_EQ(base::ASCIIToUTF16("$4.00"),
164             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
165   // There's the total line and the discount line.
166   EXPECT_EQ(2u, GetLineCount());
167 }
168 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,ModifierAppliedToUnknownTypeWithMatchingNetwork)169 IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,
170                        ModifierAppliedToUnknownTypeWithMatchingNetwork) {
171   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
172   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
173   AddAutofillProfile(profile);
174   autofill::CreditCard card(autofill::test::GetCreditCard());  // Visa card.
175   // Change to Mastercard to match the test case.
176   card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
177                   base::ASCIIToUTF16("5555555555554444"));
178   card.set_billing_address_id(profile.guid());
179   AddCreditCard(card);
180 
181   ResetEventWaiterForDialogOpened();
182   content::WebContents* web_contents = GetActiveWebContents();
183   const std::string click_buy_button_js =
184       "(function() { "
185       "document.getElementById('mastercard_any_supported_type')."
186       "click(); })();";
187   ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js));
188   WaitForObservedEvent();
189   // The web-modal dialog should be open.
190   web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
191       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
192   EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
193 
194   OpenOrderSummaryScreen();
195 
196   EXPECT_EQ(base::ASCIIToUTF16("$4.00"),
197             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
198   // There's the total line and the discount line.
199   EXPECT_EQ(2u, GetLineCount());
200 }
201 
IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,NoTotalInModifierDoesNotCrash)202 IN_PROC_BROWSER_TEST_F(PaymentRequestModifiersTest,
203                        NoTotalInModifierDoesNotCrash) {
204   NavigateTo("/payment_request_bobpay_and_basic_card_with_modifiers_test.html");
205   autofill::AutofillProfile profile(autofill::test::GetFullProfile());
206   AddAutofillProfile(profile);
207   autofill::CreditCard card(autofill::test::GetCreditCard());  // Visa card.
208   // Change to Mastercard to match the test case.
209   card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
210                   base::ASCIIToUTF16("5555555555554444"));
211   card.set_billing_address_id(profile.guid());
212   AddCreditCard(card);
213 
214   ResetEventWaiterForDialogOpened();
215   content::WebContents* web_contents = GetActiveWebContents();
216   const std::string click_buy_button_js =
217       "(function() { document.getElementById('no_total').click(); })();";
218   ASSERT_TRUE(content::ExecuteScript(web_contents, click_buy_button_js));
219   WaitForObservedEvent();
220   // The web-modal dialog should be open.
221   web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
222       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
223   EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
224 
225   OpenOrderSummaryScreen();
226 
227   // The price is the global total, because the modifier does not have total.
228   EXPECT_EQ(base::ASCIIToUTF16("$5.00"),
229             GetLabelText(DialogViewID::ORDER_SUMMARY_TOTAL_AMOUNT_LABEL));
230   // Only global total is available.
231   EXPECT_EQ(1u, GetLineCount());
232 }
233 
234 }  // namespace payments
235