File: secure_payment_confirmation_dialog_view_browsertest.cc

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 (772 lines) | stat: -rw-r--r-- 29,721 bytes parent folder | download | duplicates (2)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/payments/secure_payment_confirmation_dialog_view.h"

#include <optional>
#include <utility>

#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_occlusion_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/payments/secure_payment_confirmation_views_util.h"
#include "chrome/browser/ui/views/payments/test_secure_payment_confirmation_payment_request_delegate.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/payments/core/sizes.h"
#include "components/strings/grit/components_strings.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features_generated.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/test/mock_input_event_activation_protector.h"
#include "ui/views/window/dialog_client_view.h"

namespace payments {
namespace {

const SkBitmap CreateIcon(SkColor color, int width, int height) {
  SkBitmap bitmap;
  bitmap.allocN32Pixels(width, height);
  bitmap.eraseColor(color);
  return bitmap;
}

const SkBitmap CreateMaxSizeIcon(SkColor color) {
  return CreateIcon(color, kSecurePaymentConfirmationIconMaximumWidthPx,
                    kSecurePaymentConfirmationIconHeightPx);
}

}  // namespace

class SecurePaymentConfirmationDialogViewTest
    : public DialogBrowserTest,
      public SecurePaymentConfirmationDialogView::ObserverForTest {
 public:
  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    content::WebContents* web_contents = GetActiveWebContents();
    CreateModel();

    test_delegate_ =
        std::make_unique<TestSecurePaymentConfirmationPaymentRequestDelegate>(
            web_contents->GetPrimaryMainFrame(), model_.GetWeakPtr(),
            GetWeakPtr());

    // TODO(crbug.com/40863331): Ideally, we'd expect the browser window to be
    // active here and could check that |IsBrowserWindowActivate()| returned
    // true, but on wayland, windows cannot be activated as they are on other
    // platforms.
    EXPECT_EQ(browser()->window()->IsActive(),
              test_delegate_->IsBrowserWindowActive());

    test_delegate_->ShowDialog(nullptr);
  }

  content::WebContents* GetActiveWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  base::WeakPtr<SecurePaymentConfirmationDialogViewTest> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

  virtual void CreateModel() {
    model_.set_title(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_VERIFY_PURCHASE));

    model_.set_merchant_label(
        l10n_util::GetStringUTF16(IDS_SECURE_PAYMENT_CONFIRMATION_STORE_LABEL));
    model_.set_merchant_name(std::optional<std::u16string>(u"Test Merchant"));
    model_.set_merchant_origin(std::optional<std::u16string>(u"merchant1.com"));

    model_.set_instrument_label(l10n_util::GetStringUTF16(
        IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME));
    model_.set_instrument_value(u"Mastercard ****4444");
    instrument_icon_ =
        std::make_unique<SkBitmap>(CreateMaxSizeIcon(SK_ColorBLUE));
    model_.set_instrument_icon(instrument_icon_.get());

    model_.set_total_label(
        l10n_util::GetStringUTF16(IDS_SECURE_PAYMENT_CONFIRMATION_TOTAL_LABEL));
    model_.set_total_value(u"$20.00 USD");

    model_.set_verify_button_label(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_VERIFY_BUTTON_LABEL));
    model_.set_cancel_button_label(l10n_util::GetStringUTF16(IDS_CANCEL));

    model_.set_opt_out_visible(false);
    model_.set_opt_out_label(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_OPT_OUT_LABEL));
    model_.set_opt_out_link_label(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_OPT_OUT_LINK_LABEL));
    model_.set_relying_party_id(u"relyingparty.com");

    // We set the network and issuer data into the model no matter what, so that
    // we can verify that disabling the runtime flag causes the code to ignore
    // the set data.
    model_.set_network_label(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_NETWORK_LABEL));
    model_.set_network_value(u"MasterCard");
    network_icon_ =
        std::make_unique<SkBitmap>(CreateMaxSizeIcon(SK_ColorGREEN));
    model_.set_network_icon(network_icon_.get());

    model_.set_issuer_label(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_ISSUER_LABEL));
    model_.set_issuer_value(u"ScotiaBank");
    issuer_icon_ = std::make_unique<SkBitmap>(CreateMaxSizeIcon(SK_ColorRED));
    model_.set_issuer_icon(issuer_icon_.get());
  }

  void InvokeSecurePaymentConfirmationUI() {
    content::WebContents* web_contents = GetActiveWebContents();

    test_delegate_ =
        std::make_unique<TestSecurePaymentConfirmationPaymentRequestDelegate>(
            web_contents->GetPrimaryMainFrame(), model_.GetWeakPtr(),
            GetWeakPtr());

    test_delegate_->ShowDialog(nullptr);

    // The web-modal dialog should be open.
    web_modal::WebContentsModalDialogManager*
        web_contents_modal_dialog_manager =
            web_modal::WebContentsModalDialogManager::FromWebContents(
                web_contents);
    EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());

    // By default, disable the input event protector for testing purposes.
    // However, see the AcceptButtonIgnoresAccidentalInputs test, which
    // explicitly checks that the protector is able to block unintended inputs.
    auto mock_input_protector =
        std::make_unique<views::MockInputEventActivationProtector>();
    EXPECT_CALL(*mock_input_protector, IsPossiblyUnintendedInteraction)
        .WillRepeatedly(testing::Return(false));
    test_delegate_->dialog_view()
        ->GetDialogClientView()
        ->SetInputProtectorForTesting(std::move(mock_input_protector));
  }

  views::View* GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID view_id) {
    return test_delegate_->dialog_view()->GetViewByID(
        static_cast<int>(view_id));
  }

  void ExpectLabelText(
      const std::u16string& text,
      SecurePaymentConfirmationDialogView::DialogViewID view_id) {
    EXPECT_EQ(text,
              static_cast<views::Label*>(GetViewByID(view_id))->GetText());
  }

  void ExpectIcon(const SkBitmap& expected_icon,
                  SecurePaymentConfirmationDialogView::DialogViewID view_id) {
    const SkBitmap& actual_icon =
        *(static_cast<views::ImageView*>(GetViewByID(view_id))
              ->GetImage()
              .bitmap());
    EXPECT_TRUE(cc::MatchesBitmap(expected_icon, actual_icon,
                                  cc::ExactPixelComparator()));
  }

  void ExpectOptOutText(views::View* view,
                        const std::u16string& relying_party_id,
                        const std::u16string& opt_out_link_label) {
    // To avoid overfitting, we check only that the opt-out label contains both
    // the relying party and the call-to-action text that is expected.
    std::string opt_out_text =
        base::UTF16ToUTF8(static_cast<views::StyledLabel*>(view)->GetText());
    EXPECT_THAT(opt_out_text,
                ::testing::HasSubstr(base::UTF16ToUTF8(relying_party_id)));
    EXPECT_THAT(opt_out_text,
                ::testing::HasSubstr(base::UTF16ToUTF8(opt_out_link_label)));
  }

  // Verify that the data displayed on the view matches what is expected given
  // the current `model_` state.
  void ExpectViewMatchesModel() {
    ASSERT_NE(test_delegate_->dialog_view(), nullptr);

    EXPECT_EQ(model_.verify_button_label(),
              test_delegate_->dialog_view()->GetDialogButtonLabel(
                  ui::mojom::DialogButton::kOk));

    EXPECT_EQ(model_.cancel_button_label(),
              test_delegate_->dialog_view()->GetDialogButtonLabel(
                  ui::mojom::DialogButton::kCancel));

    if (ShouldShowHeaderIcon()) {
      EXPECT_TRUE(GetViewByID(
          SecurePaymentConfirmationDialogView::DialogViewID::HEADER_ICON));
    } else {
      EXPECT_FALSE(GetViewByID(
          SecurePaymentConfirmationDialogView::DialogViewID::HEADER_ICON));
    }

    ASSERT_TRUE(test_delegate_->dialog_view()->GetBubbleFrameView());
    std::optional<double> progress =
        test_delegate_->dialog_view()->GetBubbleFrameView()->GetProgress();
    ASSERT_EQ(model_.progress_bar_visible(), progress.has_value());

    ExpectLabelText(GetExpectedTitleText(),
                    SecurePaymentConfirmationDialogView::DialogViewID::TITLE);

    ExpectViewMatchesModelForDescription();

    ExpectLabelText(
        model_.merchant_label(),
        SecurePaymentConfirmationDialogView::DialogViewID::MERCHANT_LABEL);
    ExpectLabelText(
        FormatMerchantLabel(model_.merchant_name(), model_.merchant_origin()),
        SecurePaymentConfirmationDialogView::DialogViewID::MERCHANT_VALUE);

    ExpectLabelText(
        model_.instrument_label(),
        SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_LABEL);
    ExpectLabelText(
        model_.instrument_value(),
        SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_VALUE);

    ASSERT_EQ(instrument_icon_.get(), model_.instrument_icon());
    const SkBitmap* expected_icon =
        instrument_icon_->drawsNothing()
            ? gfx::CreateVectorIcon(
                  kCreditCardIcon, kSecurePaymentConfirmationIconDefaultWidthPx,
                  test_delegate_->dialog_view()->GetColorProvider()->GetColor(
                      ui::kColorDialogForeground))
                  .bitmap()
            : model_.instrument_icon();
    ExpectIcon(
        *expected_icon,
        SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_ICON);

    ExpectLabelText(
        model_.total_label(),
        SecurePaymentConfirmationDialogView::DialogViewID::TOTAL_LABEL);
    ExpectLabelText(
        model_.total_value(),
        SecurePaymentConfirmationDialogView::DialogViewID::TOTAL_VALUE);

    // The Opt Out link always exists, but should only be visible if requested.
    views::View* opt_out_view =
        test_delegate_->dialog_view()->GetFootnoteViewForTesting();
    EXPECT_NE(opt_out_view, nullptr);
    EXPECT_EQ(opt_out_view->GetVisible(), model_.opt_out_visible());
    ExpectOptOutText(opt_out_view, model_.relying_party_id(),
                     model_.opt_out_link_label());

    ExpectViewMatchesModelForNetworkAndIssuerIcons();
  }

  virtual bool ShouldShowHeaderIcon() { return true; }

  virtual std::u16string GetExpectedTitleText() { return model_.title(); }

  virtual void ExpectViewMatchesModelForDescription() {
    // Without the flag enabled, the description should not be shown.
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::DESCRIPTION));
  }

  virtual void ExpectViewMatchesModelForNetworkAndIssuerIcons() {
    // Without the flag enabled, the network and issuer icons should not be
    // shown even though the data is set into the model. See
    // SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest for tests
    // that enable the feature.
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_LABEL));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_VALUE));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_ICON));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_LABEL));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_VALUE));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_ICON));
  }

  void ClickButton(views::View* button) {
    gfx::Point center(button->width() / 2, button->height() / 2);
    const ui::MouseEvent event(ui::EventType::kMousePressed, center, center,
                               ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
                               ui::EF_LEFT_MOUSE_BUTTON);
    button->OnMousePressed(event);
    button->OnMouseReleased(event);
  }

  // SecurePaymentConfirmationDialogView::ObserverForTest:
  void OnDialogClosed() override {
    dialog_closed_ = true;
    if (dialog_closed_callback_) {
      std::move(dialog_closed_callback_).Run();
    }
  }

  // SecurePaymentConfirmationDialogView::ObserverForTest:
  void OnConfirmButtonPressed() override { confirm_pressed_ = true; }
  void OnCancelButtonPressed() override { cancel_pressed_ = true; }
  void OnOptOutClicked() override { opt_out_clicked_ = true; }

 protected:
  std::unique_ptr<SkBitmap> instrument_icon_;
  std::unique_ptr<SkBitmap> network_icon_;
  std::unique_ptr<SkBitmap> issuer_icon_;

  SecurePaymentConfirmationModel model_;
  std::unique_ptr<TestSecurePaymentConfirmationPaymentRequestDelegate>
      test_delegate_;

  bool dialog_closed_ = false;
  bool confirm_pressed_ = false;
  bool cancel_pressed_ = false;
  bool opt_out_clicked_ = false;

  base::HistogramTester histogram_tester_;
  base::OnceClosure dialog_closed_callback_;

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

// Basic test that the view matches the model state.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       ViewMatchesModel) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();
}

// Test that clicking the 'Accept' button triggers the expected path and closes
// the dialog.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       AcceptButtonTest) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();

  ClickButton(test_delegate_->dialog_view()->GetOkButton());

  EXPECT_TRUE(confirm_pressed_);
  EXPECT_FALSE(cancel_pressed_);
  EXPECT_FALSE(opt_out_clicked_);
}

// Test that the 'Accept' button is protected against accidental inputs.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       AcceptButtonIgnoresAccidentalInputs) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();

  // Insert a mock input protector that will ignore the first input and then
  // accepts all subsequent inputs.
  auto mock_input_protector =
      std::make_unique<views::MockInputEventActivationProtector>();
  EXPECT_CALL(*mock_input_protector, IsPossiblyUnintendedInteraction)
      .WillOnce(testing::Return(true))
      .WillRepeatedly(testing::Return(false));
  test_delegate_->dialog_view()
      ->GetDialogClientView()
      ->SetInputProtectorForTesting(std::move(mock_input_protector));

  // Because of the input protector, the first press of the button should be
  // ignored.
  ClickButton(test_delegate_->dialog_view()->GetOkButton());
  EXPECT_FALSE(confirm_pressed_);

  // However a subsequent press should be accepted.
  ClickButton(test_delegate_->dialog_view()->GetOkButton());
  EXPECT_TRUE(confirm_pressed_);
}

// Test that clicking the 'Cancel' button triggers the expected path and closes
// the dialog.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       CancelButtonTest) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();

  ClickButton(test_delegate_->dialog_view()->GetCancelButton());

  EXPECT_TRUE(cancel_pressed_);
  EXPECT_FALSE(confirm_pressed_);
  EXPECT_FALSE(opt_out_clicked_);
}

// Test that the progress bar is visible in the view when requested by the
// model.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       ProgressBarVisible) {
  CreateModel();
  model_.set_progress_bar_visible(true);
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();
}

// Test that the view can be updated to show the progress bar after initial
// load.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       ShowProgressBar) {
  // Initially the model should be created with the progress bar hidden.
  CreateModel();
  ASSERT_FALSE(model_.progress_bar_visible());

  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  // Then update the model to have the bar be visible and check that the view
  // updates to match.
  model_.set_progress_bar_visible(true);
  ASSERT_TRUE(model_.progress_bar_visible());
  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();
}

// Check that the view updates to match model updates.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       OnModelUpdated) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  model_.set_title(u"Test Title");
  model_.set_merchant_label(u"Test merchant");
  model_.set_merchant_name(
      std::optional<std::u16string>(u"Test merchant value"));
  model_.set_merchant_origin(std::optional<std::u16string>(u"merchant2.com"));
  model_.set_instrument_label(u"Test instrument");
  model_.set_instrument_value(u"Test instrument value");
  model_.set_total_label(u"Test total");
  model_.set_total_value(u"Test total value");
  model_.set_verify_button_label(u"Test verify");
  model_.set_cancel_button_label(u"Test cancel");

  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();
}

// Test the two reasons an instrument icon is updated: The model's bitmap
// pointer changed, or the bitmap itself changed.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       InstrumentIconUpdated) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  // Avoid a dangling when replacing with a different icon.
  model_.set_instrument_icon(nullptr);

  // Change the bitmap pointer
  instrument_icon_ =
      std::make_unique<SkBitmap>(CreateMaxSizeIcon(SK_ColorGREEN));
  model_.set_instrument_icon(instrument_icon_.get());
  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();

  // Change the bitmap itself without touching the model's pointer
  *instrument_icon_ = CreateMaxSizeIcon(SK_ColorRED);
  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();
}

// Test that the web contents can be torn down whilst the dialog is visible, and
// that doing so should close the dialog.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       WebContentsClosed) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();

  GetActiveWebContents()->Close();

  EXPECT_TRUE(dialog_closed_);
  EXPECT_FALSE(confirm_pressed_);
  EXPECT_FALSE(cancel_pressed_);
  EXPECT_FALSE(opt_out_clicked_);
}

// TestBrowserUi-provided UI test.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       InvokeUi_default) {
  ShowAndVerifyUi();
}

// Test that the visible instrument icon is set correctly to the default icon if
// the model does not provide one.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       DefaultInstrumentIcon) {
  CreateModel();

  // Avoid a dangling when replacing with a different icon.
  model_.set_instrument_icon(nullptr);

  instrument_icon_ = std::make_unique<SkBitmap>();
  ASSERT_TRUE(instrument_icon_->drawsNothing());
  model_.set_instrument_icon(instrument_icon_.get());

  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();
}

// Test that the merchant label is formatted correctly based on the input
// name/origin provided in the model.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       MerchantLabelFormat) {
  CreateModel();

  // Both merchant name and origin
  model_.set_merchant_name(std::optional<std::u16string>(u"Test Merchant"));
  model_.set_merchant_origin(std::optional<std::u16string>(u"merchant.com"));
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();
  ExpectLabelText(
      u"Test Merchant (merchant.com)",
      SecurePaymentConfirmationDialogView::DialogViewID::MERCHANT_VALUE);

  // Only merchant name, no origin
  model_.set_merchant_name(std::optional<std::u16string>(u"Test Merchant 2"));
  model_.set_merchant_origin(std::optional<std::u16string>());
  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();
  ExpectLabelText(
      u"Test Merchant 2",
      SecurePaymentConfirmationDialogView::DialogViewID::MERCHANT_VALUE);

  // Only merchant origin, no name
  model_.set_merchant_name(std::optional<std::u16string>());
  model_.set_merchant_origin(std::optional<std::u16string>(u"merchant2.com"));
  test_delegate_->dialog_view()->OnModelUpdated();
  ExpectViewMatchesModel();
  ExpectLabelText(
      u"merchant2.com",
      SecurePaymentConfirmationDialogView::DialogViewID::MERCHANT_VALUE);
}

// Test that an oversized instrument icon is resized down to the maximum size.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       OversizedInstrumentIcon) {
  CreateModel();

  *instrument_icon_ =
      CreateIcon(SK_ColorRED, kSecurePaymentConfirmationIconMaximumWidthPx * 2,
                 kSecurePaymentConfirmationIconHeightPx * 2);

  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  views::ImageView* image_view = static_cast<views::ImageView*>(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_ICON));
  EXPECT_EQ(kSecurePaymentConfirmationIconMaximumWidthPx,
            image_view->GetImageBounds().width());
  EXPECT_EQ(kSecurePaymentConfirmationIconHeightPx,
            image_view->GetImageBounds().height());
}

// Test that an undersized instrument icon is resized up to the minimum size.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       UndersizedInstrumentIcon) {
  CreateModel();

  *instrument_icon_ =
      CreateIcon(SK_ColorRED, kSecurePaymentConfirmationIconDefaultWidthPx / 2,
                 kSecurePaymentConfirmationIconHeightPx / 2);

  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  views::ImageView* image_view = static_cast<views::ImageView*>(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_ICON));
  EXPECT_EQ(kSecurePaymentConfirmationIconDefaultWidthPx,
            image_view->GetImageBounds().width());
  EXPECT_EQ(kSecurePaymentConfirmationIconHeightPx,
            image_view->GetImageBounds().height());
}

// Test that a midsized instrument icon is not resized.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       MidsizedInstrumentIcon) {
  CreateModel();

  int width = (kSecurePaymentConfirmationIconDefaultWidthPx +
               kSecurePaymentConfirmationIconMaximumWidthPx) /
              2;
  *instrument_icon_ =
      CreateIcon(SK_ColorRED, width, kSecurePaymentConfirmationIconHeightPx);

  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  views::ImageView* image_view = static_cast<views::ImageView*>(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::INSTRUMENT_ICON));
  EXPECT_EQ(width, image_view->GetImageBounds().width());
  EXPECT_EQ(kSecurePaymentConfirmationIconHeightPx,
            image_view->GetImageBounds().height());
}

// Test that the opt-out link is only shown when explicitly requested, and that
// clicking it causes the expected path and closes the dialog.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       OptOutShownWhenRequested) {
  CreateModel();

  // Make sure that by default, opt-out wasn't requested. This means that every
  // other test is correctly testing the 'no opt out' path.
  ASSERT_FALSE(model_.opt_out_visible());

  model_.set_opt_out_visible(true);
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();

  // Now click the opt-out link, and verify that the dialog is closed via the
  // correct path.
  views::StyledLabel* opt_out_label = static_cast<views::StyledLabel*>(
      test_delegate_->dialog_view()->GetFootnoteViewForTesting());
  opt_out_label->ClickFirstLinkForTesting();

  EXPECT_FALSE(cancel_pressed_);
  EXPECT_FALSE(confirm_pressed_);
  EXPECT_TRUE(opt_out_clicked_);
}

// Occlusion by picture-in-picture video should dismiss the SPC authentication
// dialog.
IN_PROC_BROWSER_TEST_F(SecurePaymentConfirmationDialogViewTest,
                       PictureInPictureOcclusionClosesTheDialog) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();
  base::RunLoop run_loop;
  dialog_closed_callback_ = run_loop.QuitClosure();

  static_cast<PictureInPictureOcclusionObserver*>(test_delegate_->dialog_view())
      ->OnOcclusionStateChanged(/*occluded=*/true);

  run_loop.Run();
  EXPECT_TRUE(dialog_closed_);
}

// A variant of SecurePaymentConfirmationDialogViewTest that enables the network
// and issuer icons feature, and verifies the contents.
class SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest
    : public SecurePaymentConfirmationDialogViewTest {
 public:
  SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest() = default;

  void CreateModel() override {
    SecurePaymentConfirmationDialogViewTest::CreateModel();

    model_.set_title(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_INLINE_TITLE));
    model_.set_description(l10n_util::GetStringUTF16(
        IDS_SECURE_PAYMENT_CONFIRMATION_INLINE_DESCRIPTION));
  }

  bool ShouldShowHeaderIcon() override { return false; }

  std::u16string GetExpectedTitleText() override {
    return base::ReplaceStringPlaceholders(model_.title(),
                                           model_.relying_party_id(), nullptr);
  }

  void ExpectViewMatchesModelForDescription() override {
    ExpectLabelText(
        model_.description(),
        SecurePaymentConfirmationDialogView::DialogViewID::DESCRIPTION);
  }

  void ExpectViewMatchesModelForNetworkAndIssuerIcons() override {
    // For the inline view, there are no label or values for network/issuer, but
    // the icons should be present.
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_LABEL));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_VALUE));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_LABEL));
    EXPECT_FALSE(GetViewByID(
        SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_VALUE));

    ExpectIcon(*model_.network_icon(),
               SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_ICON);
    ExpectIcon(*model_.issuer_icon(),
               SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_ICON);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_{
      blink::features::kSecurePaymentConfirmationNetworkAndIssuerIcons};
};

// Variant of the main ViewMatchesModel test, which verifies that the title,
// description, and network/issuer icons are shown when the flag is enabled.
IN_PROC_BROWSER_TEST_F(
    SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest,
    ViewMatchesModel) {
  CreateModel();
  InvokeSecurePaymentConfirmationUI();
  ExpectViewMatchesModel();
}

// Tests that the network icon is not shown if it isn't in the model.
IN_PROC_BROWSER_TEST_F(
    SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest,
    NetworkIconNotShownIfNotPresent) {
  CreateModel();

  SkBitmap network_icon = SkBitmap();
  model_.set_network_icon(&network_icon);
  InvokeSecurePaymentConfirmationUI();

  // The title should still be present.
  ExpectLabelText(GetExpectedTitleText(),
                  SecurePaymentConfirmationDialogView::DialogViewID::TITLE);

  // The network icon should not be shown, but the issuer icon should still be
  // present.
  ASSERT_FALSE(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_ICON));
  ASSERT_TRUE(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_ICON));
}

// Tests that the issuer icon is not shown if it isn't in the model.
IN_PROC_BROWSER_TEST_F(
    SecurePaymentConfirmationDialogViewNetworkAndIssuerIconsTest,
    IssuerIconNotShownIfNotPresent) {
  CreateModel();

  SkBitmap issuer_icon = SkBitmap();
  model_.set_issuer_icon(&issuer_icon);
  InvokeSecurePaymentConfirmationUI();

  // The title should still be present.
  ExpectLabelText(GetExpectedTitleText(),
                  SecurePaymentConfirmationDialogView::DialogViewID::TITLE);

  // The issuer icon should not be shown, but the network icon should still be
  // present.
  ASSERT_FALSE(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::ISSUER_ICON));
  ASSERT_TRUE(GetViewByID(
      SecurePaymentConfirmationDialogView::DialogViewID::NETWORK_ICON));
}

}  // namespace payments