File: payment_request_browsertest_base.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (333 lines) | stat: -rw-r--r-- 13,504 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_BROWSERTEST_BASE_H_
#define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_BROWSERTEST_BASE_H_

#include <iosfwd>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog_view.h"
#include "chrome/browser/ui/views/payments/test_chrome_payment_request_delegate.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager_observer.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/test_utils/test_event_waiter.h"
#include "components/payments/content/payment_request.h"
#include "components/payments/core/const_csp_checker.h"
#include "components/sync/test/test_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/browser/web_contents_observer.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/mojom/payments/payment_request.mojom-forward.h"

namespace autofill {
class AddressDataManager;
class AutofillProfile;
class CreditCard;
class PaymentsDataManager;
}  // namespace autofill

namespace content {
class RenderFrameHost;
class WebContents;
}  // namespace content

namespace payments {

enum class DialogViewID;

ACTION_P(QuitMessageLoop, loop) {
  loop->Quit();
}

class PersonalDataLoadedObserverMock
    : public autofill::PersonalDataManagerObserver {
 public:
  PersonalDataLoadedObserverMock();
  ~PersonalDataLoadedObserverMock() override;

  MOCK_METHOD(void, OnPersonalDataChanged, (), (override));
};

// Base class for any interactive PaymentRequest test that will need to open
// the UI and interact with it.
class PaymentRequestBrowserTestBase
    : public InProcessBrowserTest,
      public PaymentRequest::ObserverForTest,
      public PaymentRequestDialogView::ObserverForTest,
      public content::WebContentsObserver {
 public:
  // Various events that can be waited on by the EventWaiter.
  enum DialogEvent : int {
    DIALOG_OPENED,
    DIALOG_CLOSED,
    ORDER_SUMMARY_OPENED,
    PAYMENT_METHOD_OPENED,
    SHIPPING_ADDRESS_SECTION_OPENED,
    SHIPPING_OPTION_SECTION_OPENED,
    CREDIT_CARD_EDITOR_OPENED,
    SHIPPING_ADDRESS_EDITOR_OPENED,
    CONTACT_INFO_EDITOR_OPENED,
    BACK_NAVIGATION,
    BACK_TO_PAYMENT_SHEET_NAVIGATION,
    CONTACT_INFO_OPENED,
    EDITOR_VIEW_UPDATED,
    CAN_MAKE_PAYMENT_CALLED,
    CAN_MAKE_PAYMENT_RETURNED,
    HAS_ENROLLED_INSTRUMENT_CALLED,
    HAS_ENROLLED_INSTRUMENT_RETURNED,
    ERROR_MESSAGE_SHOWN,
    SPEC_DONE_UPDATING,
    CVC_PROMPT_SHOWN,
    NOT_SUPPORTED_ERROR,
    ABORT_CALLED,
    PROCESSING_SPINNER_SHOWN,
    PROCESSING_SPINNER_HIDDEN,
    PAYMENT_HANDLER_WINDOW_OPENED,
    PAYMENT_HANDLER_TITLE_SET,
  };

  PaymentRequestBrowserTestBase(const PaymentRequestBrowserTestBase&) = delete;
  PaymentRequestBrowserTestBase& operator=(
      const PaymentRequestBrowserTestBase&) = delete;

  base::WeakPtr<PaymentRequestBrowserTestBase> GetWeakPtr();

 protected:
  PaymentRequestBrowserTestBase();
  ~PaymentRequestBrowserTestBase() override;

  void SetUpCommandLine(base::CommandLine* command_line) override;
  void SetUpOnMainThread() override;

  // Test will open a browser window to |file_path| (relative to
  // components/test/data/payments).
  void NavigateTo(const std::string& file_path);
  void NavigateTo(const std::string& hostname, const std::string& file_path);

  void SetIncognito();
  void SetInvalidSsl();
  void SetBrowserWindowInactive();

  // PaymentRequest::ObserverForTest:
  void OnCanMakePaymentCalled() override;
  void OnCanMakePaymentReturned() override;
  void OnHasEnrolledInstrumentCalled() override;
  void OnHasEnrolledInstrumentReturned() override;
  void OnNotSupportedError() override;
  void OnConnectionTerminated() override;
  void OnPayCalled() override;
  void OnAbortCalled() override;

  // PaymentRequestDialogView::ObserverForTest:
  void OnDialogOpened() override;
  void OnDialogClosed() override;
  void OnOrderSummaryOpened() override;
  void OnPaymentMethodOpened() override;
  void OnShippingAddressSectionOpened() override;
  void OnShippingOptionSectionOpened() override;
  void OnShippingAddressEditorOpened() override;
  void OnContactInfoEditorOpened() override;
  void OnBackNavigation() override;
  void OnBackToPaymentSheetNavigation() override;
  void OnContactInfoOpened() override;
  void OnEditorViewUpdated() override;
  void OnErrorMessageShown() override;
  void OnSpecDoneUpdating() override;
  void OnProcessingSpinnerShown() override;
  void OnProcessingSpinnerHidden() override;
  void OnPaymentHandlerWindowOpened() override;
  void OnPaymentHandlerTitleSet() override;

  void InstallPaymentApp(const std::string& hostname,
                         const std::string& service_worker_filename,
                         std::string* url_method_output);

  void InstallPaymentAppWithoutIcon(const std::string& hostname,
                                    const std::string& service_worker_filename,
                                    std::string* url_method_output);

  // Will call JavaScript to invoke the PaymentRequest dialog and verify that
  // it's open and ready for input.
  void InvokePaymentRequestUI();
  void InvokePaymentRequestUIWithJs(const std::string& script);

  // Will expect that all strings in |expected_strings| are present in output.
  void ExpectBodyContains(const std::vector<std::string>& expected_strings);

  // Utility functions that will click on Dialog views and wait for the
  // associated action to happen.
  void OpenOrderSummaryScreen();
  void OpenPaymentMethodScreen();
  void OpenShippingAddressSectionScreen();
  void OpenShippingOptionSectionScreen();
  void OpenContactInfoScreen();
  void OpenCreditCardEditorScreen();
  void OpenShippingAddressEditorScreen();
  void OpenContactInfoEditorScreen();
  void ClickOnBackArrow();
  void ClickOnCancel();

  content::WebContents* GetActiveWebContents();

  // Convenience method to get all the `PaymentRequest`s that are still alive.
  const std::vector<PaymentRequest*> GetPaymentRequests();

  autofill::PersonalDataManager* GetDataManager();
  autofill::AddressDataManager* address_data_manager();
  autofill::PaymentsDataManager* payments_data_manager();
  // Adds the various models to the database, waiting until the personal data
  // manager notifies that they are added.
  // NOTE: If no use_count is specified on the models and multiple items are
  // inserted, the order in which they are returned is undefined, since they
  // are added close to each other.
  void AddAutofillProfile(const autofill::AutofillProfile& profile);
  void AddCreditCard(const autofill::CreditCard& card);

  void CreatePaymentRequestForTest(
      mojo::PendingReceiver<payments::mojom::PaymentRequest> receiver,
      content::RenderFrameHost* render_frame_host);

  // Click on a view from within the dialog and waits for an observed event
  // to be observed.
  void ClickOnDialogViewAndWait(DialogViewID view_id,
                                bool wait_for_animation = true);
  void ClickOnDialogViewAndWait(DialogViewID view_id,
                                PaymentRequestDialogView* dialog_view,
                                bool wait_for_animation = true);
  void ClickOnDialogViewAndWait(views::View* view,
                                bool wait_for_animation = true);
  void ClickOnDialogViewAndWait(views::View* view,
                                PaymentRequestDialogView* dialog_view,
                                bool wait_for_animation = true);
  void ClickOnChildInListViewAndWait(size_t child_index,
                                     size_t total_num_children,
                                     DialogViewID list_view_id,
                                     bool wait_for_animation = true);

  // Click on a view from within the dialog. Does NOT wait for asynchronous
  // effects including any animation. Most use-cases should use
  // ClickOnDialogViewAndWait instead.
  void ClickOnDialogView(views::View* view);

  // Returns profile label values under |parent_view|.
  std::vector<std::u16string> GetProfileLabelValues(
      DialogViewID parent_view_id);
  // Returns the shipping option labels under |parent_view_id|.
  std::vector<std::u16string> GetShippingOptionLabelValues(
      DialogViewID parent_view_id);

  // TODO(crbug.com/40182225): Remove remaining test usage and delete these.
  void OpenCVCPromptWithCVC(const std::u16string& cvc,
                            PaymentRequestDialogView* dialog_view);
  void PayWithCreditCard(const std::u16string& cvc);

  void RetryPaymentRequest(const std::string& validation_errors,
                           PaymentRequestDialogView* dialog_view);
  void RetryPaymentRequest(const std::string& validation_errors,
                           const DialogEvent& dialog_event,
                           PaymentRequestDialogView* dialog_view);

  // Returns whether a given view is visible in the current (or given) dialog.
  bool IsViewVisible(DialogViewID view_id) const;
  bool IsViewVisible(DialogViewID view_id, views::View* dialog_view) const;

  // Getting/setting the |value| in the textfield of a given |type|.
  std::u16string_view GetEditorTextfieldValue(autofill::FieldType type);
  void SetEditorTextfieldValue(const std::u16string& value,
                               autofill::FieldType type);
  // Getting/setting the |value| in the combobox of a given |type|.
  std::u16string GetComboboxValue(autofill::FieldType type);
  void SetComboboxValue(const std::u16string& value, autofill::FieldType type);
  // Special case for the billing address since the interesting value is not
  // the visible one accessible directly on the base combobox model.
  void SelectBillingAddress(const std::string& billing_address_id);

  // Whether the editor textfield/combobox for the given |type| is currently in
  // an invalid state.
  bool IsEditorTextfieldInvalid(autofill::FieldType type);
  bool IsEditorComboboxInvalid(autofill::FieldType type);

  bool IsPayButtonEnabled();

  std::u16string_view GetPrimaryButtonLabel() const;

  // Sets proper animation delegates and waits for animation to finish.
  void WaitForAnimation();
  void WaitForAnimation(PaymentRequestDialogView* dialog_view);

  // Returns child of dialog_view() with passed-in `id`.
  views::View* GetByDialogViewID(DialogViewID id) const;

  // Returns child of `parent` with passed-in `id`.
  views::View* GetChildByDialogViewID(views::View* parent,
                                      DialogViewID id) const;

  // Returns the text of the Label or StyledLabel with the specific |view_id|
  // that is a child of the Payment Request dialog view.
  std::u16string_view GetLabelText(DialogViewID view_id);
  std::u16string_view GetLabelText(DialogViewID view_id,
                                   views::View* dialog_view);
  std::u16string_view GetStyledLabelText(DialogViewID view_id);
  // Returns the error label text associated with a given field |type|.
  std::u16string_view GetErrorLabelForType(autofill::FieldType type);

  net::EmbeddedTestServer* https_server() { return https_server_.get(); }

  PaymentRequestDialogView* dialog_view() const {
    return delegate_->dialog_view();
  }

  void SetRegionDataLoader(autofill::RegionDataLoader* region_data_loader) {
    delegate_->OverrideRegionDataLoader(region_data_loader);
  }

  // Sets the value of the payments.can_make_payment_enabled pref.
  void SetCanMakePaymentEnabledPref(bool can_make_payment_enabled);

  // Resets the event waiter for a given |event| or |event_sequence|.
  void ResetEventWaiter(DialogEvent event);
  void ResetEventWaiterForSequence(std::list<DialogEvent> event_sequence);
  // Resets the event waiter for the events that trigger when opening a dialog.
  void ResetEventWaiterForDialogOpened();
  // Wait for the event(s) passed to ResetEventWaiter*() to occur.
  [[nodiscard]] testing::AssertionResult WaitForObservedEvent();

  // Return a weak pointer to a Content Security Policy (CSP) checker for tests.
  base::WeakPtr<CSPChecker> GetCSPCheckerForTests();

 private:
  std::unique_ptr<autofill::EventWaiter<DialogEvent>> event_waiter_;
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  // Weak, owned by the PaymentRequest object.
  raw_ptr<TestChromePaymentRequestDelegate, AcrossTasksDanglingUntriaged>
      delegate_ = nullptr;
  syncer::TestSyncService sync_service_;
  sync_preferences::TestingPrefServiceSyncable prefs_;
  bool is_incognito_ = false;
  bool is_valid_ssl_ = true;
  bool is_browser_window_active_ = true;
  std::vector<base::WeakPtr<PaymentRequest>> requests_;
  ConstCSPChecker const_csp_checker_{/*allow=*/true};

  base::WeakPtrFactory<PaymentRequestBrowserTestBase> weak_ptr_factory_{this};
};

}  // namespace payments

std::ostream& operator<<(
    std::ostream& out,
    payments::PaymentRequestBrowserTestBase::DialogEvent event);

#endif  // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_BROWSERTEST_BASE_H_