File: secure_payment_confirmation_dialog_view_browsertest.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (609 lines) | stat: -rw-r--r-- 23,178 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
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
// 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/strings/string_util.h"
#include "base/test/metrics/histogram_tester.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 "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();
  }

  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");
  }

  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));

    EXPECT_TRUE(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(model_.title(),
                    SecurePaymentConfirmationDialogView::DialogViewID::TITLE);

    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());
  }

  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_;

  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_);
}

}  // namespace payments