File: extension_install_dialog_view_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (909 lines) | stat: -rw-r--r-- 37,015 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
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Copyright 2014 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/extensions/extension_install_dialog_view.h"

#include <string>
#include <utility>

#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/message_formatter.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/extensions/extension_install_prompt_show_params.h"
#include "chrome/browser/extensions/extension_install_prompt_test_helper.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension_test_util.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_icon_manager.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/permissions/permission_message_provider.h"
#include "extensions/common/permissions/permissions_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

using extensions::PermissionIDSet;
using extensions::PermissionMessage;
using extensions::PermissionMessages;
using extensions::PermissionSet;

namespace {

void CloseAndWait(views::Widget* widget) {
  views::test::WidgetDestroyedWaiter waiter(widget);
  widget->Close();
  waiter.Wait();
}

}  // namespace

class ExtensionInstallDialogViewTestBase
    : public extensions::ExtensionBrowserTest {
 public:
  ExtensionInstallDialogViewTestBase(
      const ExtensionInstallDialogViewTestBase&) = delete;
  ExtensionInstallDialogViewTestBase& operator=(
      const ExtensionInstallDialogViewTestBase&) = delete;

 protected:
  ExtensionInstallDialogViewTestBase();

  void SetUpOnMainThread() override;

  // Creates and returns an install prompt of |prompt_type|.
  std::unique_ptr<ExtensionInstallPrompt::Prompt> CreatePrompt(
      ExtensionInstallPrompt::PromptType prompt_type);
  std::unique_ptr<ExtensionInstallPrompt::Prompt> CreatePrompt(
      ExtensionInstallPrompt::PromptType prompt_type,
      const extensions::Extension* extension);

  content::WebContents* web_contents() { return web_contents_; }

 private:
  raw_ptr<const extensions::Extension, AcrossTasksDanglingUntriaged> extension_;
  raw_ptr<content::WebContents, AcrossTasksDanglingUntriaged> web_contents_;
};

ExtensionInstallDialogViewTestBase::ExtensionInstallDialogViewTestBase()
    : extension_(nullptr), web_contents_(nullptr) {}

void ExtensionInstallDialogViewTestBase::SetUpOnMainThread() {
  extensions::ExtensionBrowserTest::SetUpOnMainThread();

  extension_ = LoadExtension(test_data_dir_.AppendASCII(
      "install_prompt/permissions_scrollbar_regression"));

  web_contents_ = browser()->tab_strip_model()->GetWebContentsAt(0);
}

std::unique_ptr<ExtensionInstallPrompt::Prompt>
ExtensionInstallDialogViewTestBase::CreatePrompt(
    ExtensionInstallPrompt::PromptType prompt_type) {
  return CreatePrompt(prompt_type, extension_);
}

std::unique_ptr<ExtensionInstallPrompt::Prompt>
ExtensionInstallDialogViewTestBase::CreatePrompt(
    ExtensionInstallPrompt::PromptType prompt_type,
    const extensions::Extension* extension) {
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt(
      new ExtensionInstallPrompt::Prompt(prompt_type));
  prompt->set_extension(extension);

  std::unique_ptr<extensions::ExtensionIconManager> icon_manager(
      new extensions::ExtensionIconManager());
  prompt->set_icon(icon_manager->GetIcon(extension->id()));

  return prompt;
}

class ScrollbarTest : public ExtensionInstallDialogViewTestBase {
 public:
  ScrollbarTest(const ScrollbarTest&) = delete;
  ScrollbarTest& operator=(const ScrollbarTest&) = delete;

 protected:
  ScrollbarTest() = default;

  bool IsScrollbarVisible(
      std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt);
};

bool ScrollbarTest::IsScrollbarVisible(
    std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt) {
  ExtensionInstallDialogView* dialog = new ExtensionInstallDialogView(
      std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
      base::DoNothing(), std::move(prompt));

  // Create the modal view around the install dialog view.
  views::Widget* modal = constrained_window::CreateBrowserModalDialogViews(
      dialog, web_contents()->GetTopLevelNativeWindow());
  modal->Show();
  content::RunAllTasksUntilIdle();

  // Check if the vertical scrollbar is visible.
  return dialog->scroll_view()->vertical_scroll_bar()->GetVisible();
}

// Tests that a scrollbar _is_ shown for an excessively long extension
// install prompt.
IN_PROC_BROWSER_TEST_F(ScrollbarTest, LongPromptScrollbar) {
  std::u16string permission_string(u"Test");
  PermissionMessages permissions;
  for (int i = 0; i < 20; i++) {
    permissions.emplace_back(permission_string, PermissionIDSet());
  }
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::PERMISSIONS_PROMPT);
  prompt->AddPermissionMessages(permissions);
  ASSERT_TRUE(IsScrollbarVisible(std::move(prompt)))
      << "Scrollbar is not visible";
}

// Tests that a scrollbar isn't shown for this regression case.
// See crbug.com/385570 for details.
// TODO(http://crbug.com/988934): Flaky on some Mac release bots.
#if BUILDFLAG(IS_MAC) && defined(NDEBUG)
#define MAYBE_ScrollbarRegression DISABLED_ScrollbarRegression
#else
#define MAYBE_ScrollbarRegression ScrollbarRegression
#endif
IN_PROC_BROWSER_TEST_F(ScrollbarTest, MAYBE_ScrollbarRegression) {
  std::u16string permission_string(
      u"Read and modify your data on *.facebook.com");
  PermissionMessages permissions;
  permissions.emplace_back(permission_string, PermissionIDSet());
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::PERMISSIONS_PROMPT);
  prompt->AddPermissionMessages(permissions);
  ASSERT_FALSE(IsScrollbarVisible(std::move(prompt))) << "Scrollbar is visible";
}

class ExtensionInstallDialogViewTest
    : public ExtensionInstallDialogViewTestBase {
 public:
  ExtensionInstallDialogViewTest() = default;
  ExtensionInstallDialogViewTest(const ExtensionInstallDialogViewTest&) =
      delete;
  ExtensionInstallDialogViewTest& operator=(
      const ExtensionInstallDialogViewTest&) = delete;

 protected:
  ExtensionInstallDialogView* CreateAndShowPrompt(
      ExtensionInstallPromptTestHelper* helper) {
    auto dialog = std::make_unique<ExtensionInstallDialogView>(
        std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
        helper->GetCallback(),
        CreatePrompt(ExtensionInstallPrompt::INSTALL_PROMPT));
    ExtensionInstallDialogView* delegate_view = dialog.get();

    views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
        dialog.release(), gfx::NativeWindow(),
        platform_util::GetViewForWindow(
            browser()->window()->GetNativeWindow()));
    modal_dialog->Show();

    return delegate_view;
  }
};

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest, NotifyDelegate) {
  {
    // User presses install. Note that we have to wait for the 0ms delay for the
    // install button to become enabled, hence the RunLoop later.
    ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view = CreateAndShowPrompt(&helper);
    base::RunLoop().RunUntilIdle();
    delegate_view->AcceptDialog();
    EXPECT_EQ(ExtensionInstallPrompt::Result::ACCEPTED, helper.result());
  }
  {
    // User presses cancel.
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view = CreateAndShowPrompt(&helper);
    delegate_view->CancelDialog();
    EXPECT_EQ(ExtensionInstallPrompt::Result::USER_CANCELED, helper.result());
  }
  {
    // Dialog is closed without the user explicitly choosing to proceed or
    // cancel.
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view = CreateAndShowPrompt(&helper);
    // Note that the close button isn't present, but the dialog can still be
    // closed this way via Esc.
    EXPECT_FALSE(delegate_view->ShouldShowCloseButton());
    CloseAndWait(delegate_view->GetWidget());
    // TODO(devlin): Should this be ABORTED?
    EXPECT_EQ(ExtensionInstallPrompt::Result::USER_CANCELED, helper.result());
  }
}

// Regression test for crbug.com/40059470. Tests that the originating web
// contents are activated and the installation prompt is shown there, if the
// user switches the tab after starting the installation.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest,
                       ActivateWebContentsOnTabChange) {
  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  content::WebContents* originating_web_contents =
      tab_strip_model->GetActiveWebContents();

  AddBlankTabAndShow(browser());
  ASSERT_NE(tab_strip_model->GetActiveWebContents(), originating_web_contents);

  base::RunLoop run_loop;
  auto show_dialog_callback =
      [&](std::unique_ptr<ExtensionInstallPromptShowParams> show_params,
          ExtensionInstallPrompt::DoneCallback done_callback,
          std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt) {
        // show_params_weak won't be dangling till the dialog is closed.
        ExtensionInstallPromptShowParams* show_params_weak = show_params.get();
        std::move(ExtensionInstallPrompt::GetDefaultShowDialogCallback())
            .Run(std::move(show_params), /*done_callback=*/base::DoNothing(),
                 std::move(prompt));

        // Ensure that show_params has the correct parent window and the
        // originating web contents are activated.
        EXPECT_TRUE(show_params_weak->GetParentWindow());
        EXPECT_EQ(show_params_weak->GetParentWindow(),
                  browser()->window()->GetNativeWindow());
        EXPECT_EQ(tab_strip_model->GetActiveWebContents(),
                  originating_web_contents);
        run_loop.Quit();
      };

  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("Test extension").Build();
  ExtensionInstallPrompt prompt(originating_web_contents);
  prompt.ShowDialog(/*install_callback=*/base::DoNothing(), extension.get(),
                    /*icon=*/nullptr,
                    base::BindLambdaForTesting(show_dialog_callback));
  run_loop.Run();
}

// Verifies that the "Add extension" button is disabled initially, but
// re-enabled after a short time delay.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest, InstallButtonDelay) {
  ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
  ExtensionInstallPromptTestHelper helper;
  ExtensionInstallDialogView* delegate_view = CreateAndShowPrompt(&helper);

  // Check that dialog is visible.
  EXPECT_TRUE(delegate_view->GetVisible());

  // Check initial button states.
  EXPECT_FALSE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kCancel));
  EXPECT_TRUE(delegate_view->GetInitiallyFocusedView()->HasFocus());

  // Check OK button state after timeout to verify that it is re-enabled.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  // Ensure default button (cancel) has focus.
  EXPECT_TRUE(delegate_view->GetInitiallyFocusedView()->HasFocus());
  CloseAndWait(delegate_view->GetWidget());
}

// Regression test for https://crbug.com/1201031: Ensures that while an
// ExtensionInstallDialogView is visible, it does not (and cannot) refer to its
// originator tab/WebContents after the tab's closure.
//
// Note that the tab's closure is not typically possible by user interaction.
// However, it can occur by other means:
//   a) chrome.tabs.remove() or
//   b) window.close() from devtools (See repro in crbug).
// This test uses CloseWebContentsAt to mimic that behavior.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewTest,
                       TabClosureClearsWebContentsFromDialogView) {
  ExtensionInstallPromptTestHelper helper;
  ExtensionInstallDialogView* delegate_view = CreateAndShowPrompt(&helper);
  TabStripModel* tab_strip_model = browser()->tab_strip_model();
  content::WebContents* originator_contents =
      tab_strip_model->GetActiveWebContents();
  ASSERT_TRUE(delegate_view->GetShowParamsForTesting());
  EXPECT_EQ(originator_contents,
            delegate_view->GetShowParamsForTesting()->GetParentWebContents());

  // Add a second tab.
  chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
  {
    // Close the first tab that results in install dialog moving to the second
    // tab.
    int tab1_idx = tab_strip_model->GetIndexOfWebContents(originator_contents);
    content::WebContentsDestroyedWatcher tab_destroyed_watcher(
        tab_strip_model->GetWebContentsAt(tab1_idx));
    int previous_tab_count = tab_strip_model->count();
    tab_strip_model->CloseWebContentsAt(tab1_idx, TabCloseTypes::CLOSE_NONE);
    EXPECT_EQ(previous_tab_count - 1, tab_strip_model->count());
    tab_destroyed_watcher.Wait();
  }

  class TabAddedObserver : public TabStripModelObserver {
   public:
    explicit TabAddedObserver(TabStripModel* tab_strip_model) {
      tab_strip_model->AddObserver(this);
    }

    void WaitForWebstoreTabAdded() { run_loop_.Run(); }

    // TabStripModelObserver:
    void OnTabStripModelChanged(
        TabStripModel* tab_strip_model,
        const TabStripModelChange& change,
        const TabStripSelectionChange& selection) override {
      if (change.type() != TabStripModelChange::kInserted) {
        return;
      }

      for (const auto& contents : change.GetInsert()->contents) {
        // Note: GetVisibleURL() is used instead of GetLastCommittedURL() for
        // simplicity's sake as this test doesn't serve webstore url and
        // the url doesn't commit.
        const GURL& url = contents.contents->GetVisibleURL();
        if (url.host() == extension_urls::GetNewWebstoreLaunchURL().host()) {
          run_loop_.Quit();
          return;
        }
      }
    }

   private:
    base::RunLoop run_loop_;
  };

  // The dialog remains visible even though |originator_contents| is gone. Note
  // that this doesn't seem quite intuitive, but this is how things are at the
  // moment. See crbug.com/1201031 for details.
  EXPECT_TRUE(delegate_view->GetVisible());

  EXPECT_EQ(nullptr,
            delegate_view->GetShowParamsForTesting()->GetParentWebContents());

  // Click webstore link.
  {
    TabAddedObserver observer(tab_strip_model);
    delegate_view->ClickLinkForTesting();
    observer.WaitForWebstoreTabAdded();
  }

  CloseAndWait(delegate_view->GetWidget());
}

class ExtensionInstallDialogViewInteractiveBrowserTest
    : public DialogBrowserTest {
 public:
  ExtensionInstallDialogViewInteractiveBrowserTest() {
    feature_list_.InitAndEnableFeature(
        extensions_features::kAllowWithholdingExtensionPermissionsOnInstall);
  }

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

  // DialogBrowserTest:
  void ShowUi(const std::string& name) override {
    extensions::ChromeTestExtensionLoader loader(browser()->profile());
    base::FilePath test_data_dir;
    base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
    scoped_refptr<const extensions::Extension> extension = loader.LoadExtension(
        test_data_dir.AppendASCII("extensions/uitest/long_name"));

    SkBitmap icon;
    // The dialog will downscale large images.
    icon.allocN32Pixels(800, 800);
    icon.eraseARGB(255, 128, 255, 128);

    auto prompt = std::make_unique<ExtensionInstallPrompt::Prompt>(type_);
    // The invoke UI tests can either directly set permission messages to easily
    // test different potential edge cases, or use a proper permission set which
    // goes through the standard flow to generate the messages.
    if (permission_set_) {
      prompt->AddPermissionSet(*permission_set_);
    } else {
      prompt->AddPermissionMessages(permission_messages_);
    }

    if (from_webstore_) {
      prompt->SetWebstoreData("69,420", true, 2.5, 37, "37");
    }

    ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
    auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
    auto install_prompt =
        std::make_unique<ExtensionInstallPrompt>(web_contents);
    install_prompt->ShowDialog(base::DoNothing(), extension.get(), &icon,
                               std::move(prompt),
                               ExtensionInstallPrompt::ShowDialogCallback());
    base::RunLoop().RunUntilIdle();
  }

  void set_from_webstore() { from_webstore_ = true; }

  void set_type(ExtensionInstallPrompt::PromptType type) { type_ = type; }

  void SetPermissionSet(std::unique_ptr<PermissionSet> permissions) {
    permission_set_ = std::move(permissions);
  }

  void AddPermission(std::string permission) {
    permission_messages_.emplace_back(base::ASCIIToUTF16(permission),
                                      PermissionIDSet());
  }

  void AddPermissionWithDetails(
      std::string main_permission,
      std::vector<std::u16string> detailed_permissions) {
    permission_messages_.emplace_back(base::ASCIIToUTF16(main_permission),
                                      PermissionIDSet(),
                                      std::move(detailed_permissions));
  }

 private:
  ExtensionInstallPrompt::PromptType type_ =
      ExtensionInstallPrompt::INSTALL_PROMPT;
  bool from_webstore_ = false;
  std::unique_ptr<PermissionSet> permission_set_;
  PermissionMessages permission_messages_;

  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_Simple) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_External) {
  set_type(ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT);
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_ExternalWithPermission) {
  set_type(ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT);
  AddPermission("Example permission");
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_ReEnable) {
  set_type(ExtensionInstallPrompt::RE_ENABLE_PROMPT);
  AddPermission("Example permission");
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_MultilinePermission) {
  AddPermission(
      "In the shade of the house, in the sunshine of the riverbank "
      "near the boats, in the shade of the Sal-wood forest, in the "
      "shade of the fig tree is where Siddhartha grew up");
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_ManyPermissions) {
  for (int i = 0; i < 20; i++) {
    AddPermission("Example permission");
  }
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_DetailedPermission) {
  AddPermissionWithDetails("Example header permission",
                           {u"Detailed permission 1", u"Detailed permission 2",
                            u"Very very very very very very long detailed "
                            u"permission that wraps to a new line"});
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_WithWithholdingOption) {
  // The permissions withholding UI requires a proper permission set to be used,
  // as it checks for host permissions to determine if it should be shown.
  auto permissions = std::make_unique<PermissionSet>(
      extensions::APIPermissionSet(), extensions::ManifestPermissionSet(),
      extensions::URLPatternSet(
          {URLPattern(extensions::Extension::kValidHostPermissionSchemes,
                      "http://example.com/*")}),
      extensions::URLPatternSet());
  SetPermissionSet(std::move(permissions));
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewInteractiveBrowserTest,
                       InvokeUi_AllInfoTypes) {
  AddPermission("Example permission");
  AddPermissionWithDetails(
      "This permission has details",
      {u"Detailed permission 1", u"Detailed permission 2"});
  ShowAndVerifyUi();
}

class ExtensionInstallDialogViewOnUninstallationTest
    : public ExtensionInstallDialogViewTest {
 public:
  ExtensionInstallDialogViewOnUninstallationTest() = default;
  ExtensionInstallDialogViewOnUninstallationTest(
      const ExtensionInstallDialogViewOnUninstallationTest&) = delete;
  ExtensionInstallDialogViewOnUninstallationTest& operator=(
      const ExtensionInstallDialogViewOnUninstallationTest&) = delete;

 protected:
  void UninstallExtension(const std::string& extension_id);
};

void ExtensionInstallDialogViewOnUninstallationTest::UninstallExtension(
    const std::string& extension_id) {
  extensions::TestExtensionRegistryObserver observer(
      extensions::ExtensionRegistry::Get(browser()->profile()), extension_id);
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->UninstallExtension(
          extension_id,
          extensions::UninstallReason::UNINSTALL_REASON_FOR_TESTING, nullptr);
  observer.WaitForExtensionUninstalled();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewOnUninstallationTest,
                       UninstallingExtensionClosesDialog) {
  const extensions::Extension* extension =
      LoadExtension(test_data_dir_.AppendASCII(
          "install_prompt/dialog_on_uninstall/same_extension"));
  ASSERT_TRUE(extension);
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::REPAIR_PROMPT, extension);
  ExtensionInstallDialogView* dialog = new ExtensionInstallDialogView(
      std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
      base::DoNothing(), std::move(prompt));

  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      dialog, gfx::NativeWindow(),
      platform_util::GetViewForWindow(browser()->window()->GetNativeWindow()));
  ASSERT_TRUE(modal_dialog);
  views::test::WidgetDestroyedWaiter dialog_observer(modal_dialog);
  modal_dialog->Show();
  EXPECT_FALSE(modal_dialog->IsClosed());
  UninstallExtension(extension->id());
  dialog_observer.Wait();
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewOnUninstallationTest,
                       UninstallingOtherExtensionDoesNotCloseDialog) {
  const extensions::Extension* extension =
      LoadExtension(test_data_dir_.AppendASCII(
          "install_prompt/dialog_on_uninstall/same_extension"));
  const extensions::Extension* other_extension =
      LoadExtension(test_data_dir_.AppendASCII(
          "install_prompt/dialog_on_uninstall/other_extension"));
  ASSERT_TRUE(extension);
  ASSERT_TRUE(other_extension);
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::REPAIR_PROMPT, extension);
  ExtensionInstallDialogView* dialog = new ExtensionInstallDialogView(
      std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
      base::DoNothing(), std::move(prompt));

  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      dialog, gfx::NativeWindow(),
      platform_util::GetViewForWindow(browser()->window()->GetNativeWindow()));
  ASSERT_TRUE(modal_dialog);
  views::test::WidgetDestroyedWaiter dialog_observer(modal_dialog);
  modal_dialog->Show();
  EXPECT_FALSE(modal_dialog->IsClosed());
  UninstallExtension(other_extension->id());
  ASSERT_TRUE(modal_dialog);
  modal_dialog->Close();
  dialog_observer.Wait();
}

class ExtensionInstallDialogRatingsSectionTest
    : public ExtensionInstallDialogViewTest {
 public:
  ExtensionInstallDialogRatingsSectionTest() = default;

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

  void TestRatingsSectionA11y(int num_ratings,
                              double average_rating,
                              const std::string& expected_text);
};

void ExtensionInstallDialogRatingsSectionTest::TestRatingsSectionA11y(
    int num_ratings,
    double average_rating,
    const std::string& expected_text) {
  SCOPED_TRACE(base::StringPrintf(
      "Testing with %d ratings, %f average rating. Expected text: '%s'.",
      num_ratings, average_rating, expected_text.c_str()));
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::REPAIR_PROMPT);
  prompt->SetWebstoreData("1,234", true, average_rating, num_ratings,
                          base::NumberToString(num_ratings));

  ExtensionInstallDialogView* dialog = new ExtensionInstallDialogView(
      std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
      base::DoNothing(), std::move(prompt));

  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      dialog, gfx::NativeWindow(),
      platform_util::GetViewForWindow(browser()->window()->GetNativeWindow()));
  modal_dialog->Show();

  views::View* rating_view = modal_dialog->non_client_view()->GetViewByID(
      ExtensionInstallDialogView::kRatingsViewId);
  ASSERT_TRUE(rating_view);
  {
    ui::AXNodeData node_data;
    rating_view->GetViewAccessibility().GetAccessibleNodeData(&node_data);
    EXPECT_EQ(ax::mojom::Role::kStaticText, node_data.role);
    EXPECT_EQ(expected_text,
              node_data.GetStringAttribute(ax::mojom::StringAttribute::kName));

    if (num_ratings == 0) {
      EXPECT_EQ(rating_view->GetViewAccessibility().GetCachedName(),
                l10n_util::GetStringUTF16(
                    IDS_EXTENSION_PROMPT_NO_RATINGS_ACCESSIBLE_TEXT));
    } else {
      EXPECT_EQ(rating_view->GetViewAccessibility().GetCachedName(),
                base::i18n::MessageFormatter::FormatWithNumberedArgs(
                    l10n_util::GetStringUTF16(
                        IDS_EXTENSION_PROMPT_RATING_ACCESSIBLE_TEXT),
                    average_rating, num_ratings));
    }
  }

  for (views::View* child : rating_view->children()) {
    ui::AXNodeData node_data;
    child->GetViewAccessibility().GetAccessibleNodeData(&node_data);
    EXPECT_EQ(ax::mojom::Role::kNone, node_data.role);
  }

  CloseAndWait(modal_dialog);
}

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogRatingsSectionTest,
                       RatingsSectionA11y) {
  TestRatingsSectionA11y(400, 3.297, "Rated 3.3 by 400 users.");
  TestRatingsSectionA11y(1, 1.0, "Rated 1.0 by one user.");
  TestRatingsSectionA11y(0, 0.0, "Not yet rated by any users.");
}

class ExtensionInstallDialogWithWithholdPermissionsUI
    : public ExtensionInstallDialogViewTestBase {
 public:
  ExtensionInstallDialogWithWithholdPermissionsUI() {
    scoped_feature_list_.InitAndEnableFeature(
        extensions_features::kAllowWithholdingExtensionPermissionsOnInstall);
  }

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

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Verifies that some UI is displayed in the extra view when withholding
// permissions on installation.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogWithWithholdPermissionsUI,
                       ShowsWithholdUI) {
  ExtensionInstallPromptTestHelper helper;
  std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
      CreatePrompt(ExtensionInstallPrompt::INSTALL_PROMPT);
  // Add a permission to the prompt with example.com as an explicit pattern.
  PermissionSet permissions(
      extensions::APIPermissionSet(), extensions::ManifestPermissionSet(),
      extensions::URLPatternSet(
          {URLPattern(extensions::Extension::kValidHostPermissionSchemes,
                      "http://example.com/*")}),
      extensions::URLPatternSet());
  prompt->AddPermissionSet(permissions);
  auto dialog = std::make_unique<ExtensionInstallDialogView>(
      std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
      helper.GetCallback(), std::move(prompt));
  views::BubbleDialogDelegateView* delegate_view = dialog.get();

  views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
      dialog.release(), gfx::NativeWindow(),
      platform_util::GetViewForWindow(browser()->window()->GetNativeWindow()));
  modal_dialog->Show();

  const views::View* const extra_view = delegate_view->GetExtraView();
  EXPECT_TRUE(extra_view);
  EXPECT_EQ("Checkbox", extra_view->GetClassName());

  CloseAndWait(delegate_view->GetWidget());
}

class ExtensionInstallDialogViewRequestTest
    : public ExtensionInstallDialogViewTestBase {
 public:
  ExtensionInstallDialogViewRequestTest() = default;
  ExtensionInstallDialogViewRequestTest(
      const ExtensionInstallDialogViewRequestTest&) = delete;
  ExtensionInstallDialogViewRequestTest& operator=(
      const ExtensionInstallDialogViewRequestTest&) = delete;

 protected:
  ExtensionInstallDialogView* CreateAndShowRequestPrompt(
      ExtensionInstallPromptTestHelper* helper) {
    PermissionMessages permissions;
    permissions.emplace_back(u"Permission message", PermissionIDSet());
    std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt =
        CreatePrompt(ExtensionInstallPrompt::EXTENSION_REQUEST_PROMPT);
    prompt->AddPermissionMessages(permissions);

    auto dialog = std::make_unique<ExtensionInstallDialogView>(
        std::make_unique<ExtensionInstallPromptShowParams>(web_contents()),
        helper->GetCallback(), std::move(prompt));
    ExtensionInstallDialogView* delegate_view = dialog.get();

    views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
        dialog.release(), gfx::NativeWindow(),
        platform_util::GetViewForWindow(
            browser()->window()->GetNativeWindow()));
    modal_dialog->Show();

    return delegate_view;
  }
};

IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewRequestTest, NotifyDelegate) {
  base::HistogramTester histogram_tester;
  {
    // User presses "Send". Note that we have to wait for the 0ms delay for the
    // "Send" button to become enabled, hence the RunLoop later.
    ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view =
        CreateAndShowRequestPrompt(&helper);
    EXPECT_TRUE(delegate_view->IsJustificationFieldVisibleForTesting());

    base::RunLoop().RunUntilIdle();
    delegate_view->AcceptDialog();
    EXPECT_EQ(ExtensionInstallPrompt::Result::ACCEPTED, helper.result());
    EXPECT_EQ(std::string(), helper.justification());
  }
  {
    // User presses cancel.
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view =
        CreateAndShowRequestPrompt(&helper);
    EXPECT_TRUE(delegate_view->IsJustificationFieldVisibleForTesting());
    delegate_view->CancelDialog();
    EXPECT_EQ(ExtensionInstallPrompt::Result::USER_CANCELED, helper.result());
    EXPECT_EQ(std::string(), helper.justification());
  }
  {
    // Dialog is closed without the user explicitly choosing to proceed or
    // cancel.
    ExtensionInstallPromptTestHelper helper;
    ExtensionInstallDialogView* delegate_view =
        CreateAndShowRequestPrompt(&helper);
    EXPECT_TRUE(delegate_view->IsJustificationFieldVisibleForTesting());
    // Note that the close button isn't present, but the dialog can still be
    // closed this way using Esc.
    EXPECT_FALSE(delegate_view->ShouldShowCloseButton());
    CloseAndWait(delegate_view->GetWidget());
    // TODO(devlin): Should this be ABORTED?
    EXPECT_EQ(ExtensionInstallPrompt::Result::USER_CANCELED, helper.result());
    EXPECT_EQ(std::string(), helper.justification());
  }
}

// Verifies that the "Send" button is disabled initially, but re-enabled after a
// short time delay.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewRequestTest,
                       RequestButtonDelay) {
  ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
  ExtensionInstallPromptTestHelper helper;
  ExtensionInstallDialogView* delegate_view =
      CreateAndShowRequestPrompt(&helper);

  // Check that dialog and justification textfield are visible.
  EXPECT_TRUE(delegate_view->GetVisible());
  EXPECT_TRUE(delegate_view->IsJustificationFieldVisibleForTesting());

  // Check initial button states.
  EXPECT_FALSE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kCancel));

  // Check OK button state after timeout to verify that it is re-enabled.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  CloseAndWait(delegate_view->GetWidget());
}

// Verifies that the "Send" button is disabled when the justification text
// exceeds the limit.
IN_PROC_BROWSER_TEST_F(ExtensionInstallDialogViewRequestTest,
                       SendButtonDisabledWhenJustificationExceedsLimit) {
  ExtensionInstallDialogView::SetInstallButtonDelayForTesting(0);
  ExtensionInstallPromptTestHelper helper;
  ExtensionInstallDialogView* delegate_view =
      CreateAndShowRequestPrompt(&helper);

  // Check that dialog and justification textfield are visible.
  EXPECT_TRUE(delegate_view->GetVisible());
  ASSERT_TRUE(delegate_view->IsJustificationFieldVisibleForTesting());

  // Check OK button state after timeout to verify that it is re-enabled.
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  // Add long justification and verify that OK button is disabled.
  delegate_view->SetJustificationTextForTesting(
      u"I really, really, really, really, really, really, really, really, "
      u"really, really, really, really, really, really, really, really, "
      u"really, really, really, really, really, really, really, really, "
      u"really, really, really, really, really, really, really, really, "
      u"really, really, really, really, really need this extension. Pretty "
      u"please!");
  EXPECT_FALSE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  // Add short justificastion and verify that OK button is enabled.
  delegate_view->SetJustificationTextForTesting(u"I need it now.");
  EXPECT_TRUE(
      delegate_view->IsDialogButtonEnabled(ui::mojom::DialogButton::kOk));

  CloseAndWait(delegate_view->GetWidget());
}