File: extension_uninstall_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 (601 lines) | stat: -rw-r--r-- 24,826 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
// 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 <memory>

#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/browsertest_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/extensions/extensions_toolbar_container.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_registrar.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_urls.h"
#include "ui/views/layout/animating_layout_manager_test_util.h"
#include "ui/views/test/widget_test.h"

namespace {

const char kUninstallUrl[] = "https://www.google.com/";

const char kReportAbuseUrl[] =
    "https://chromewebstore.google.com/detail/cmcgleglgippmgippjjlefdmbndaebde/"
    "report?utm_source=chrome-remove-extension-dialog";

// A preference key storing the url loaded when an extension is uninstalled.
const char kUninstallUrlPrefKey[] = "uninstall_url";

scoped_refptr<const extensions::Extension> BuildTestExtension(
    const char* extension_name = "foo") {
  return extensions::ExtensionBuilder(extension_name).Build();
}

std::string GetActiveUrl(Browser* browser) {
  return browser->tab_strip_model()
      ->GetActiveWebContents()
      ->GetLastCommittedURL()
      .spec();
}

void SetUninstallURL(extensions::ExtensionPrefs* prefs,
                     const std::string& extension_id) {
  prefs->UpdateExtensionPref(extension_id, kUninstallUrlPrefKey,
                             base::Value(kUninstallUrl));
}

void CloseUninstallDialog(views::Widget* const bubble_widget) {
  views::test::WidgetDestroyedWaiter destroyed_waiter(bubble_widget);
  bubble_widget->CloseWithReason(
      views::Widget::ClosedReason::kCloseButtonClicked);
  destroyed_waiter.Wait();
}

class TestExtensionUninstallDialogDelegate
    : public extensions::ExtensionUninstallDialog::Delegate {
 public:
  explicit TestExtensionUninstallDialogDelegate(
      base::RepeatingClosure quit_closure)
      : quit_closure_(quit_closure) {}

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

  ~TestExtensionUninstallDialogDelegate() override = default;

  bool canceled() const { return canceled_; }
  const std::u16string& error() const { return error_; }

 private:
  void OnExtensionUninstallDialogClosed(bool did_start_uninstall,
                                        const std::u16string& error) override {
    ASSERT_FALSE(did_close_)
        << "OnExtensionUninstallDialogClosed() was called twice!";
    did_close_ = true;
    canceled_ = !did_start_uninstall;
    error_ = error;
    quit_closure_.Run();
  }

  base::RepeatingClosure quit_closure_;
  bool did_close_ = false;
  bool canceled_ = false;
  std::u16string error_;
};

}  // namespace

using ExtensionUninstallDialogViewBrowserTest = InProcessBrowserTest;

// Test that ExtensionUninstallDialog cancels the uninstall if the Window which
// is passed to ExtensionUninstallDialog::Create() is destroyed before
// ExtensionUninstallDialogDelegateView is created.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       TrackParentWindowDestruction) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());

  base::RunLoop run_loop;
  TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
      extensions::ExtensionUninstallDialog::Create(
          browser()->profile(), browser()->window()->GetNativeWindow(),
          &delegate));
  browser()->window()->Close();
  content::RunAllPendingInMessageLoop();

  dialog->ConfirmUninstall(extension.get(),
                           extensions::UNINSTALL_REASON_FOR_TESTING,
                           extensions::UNINSTALL_SOURCE_FOR_TESTING);
  run_loop.Run();
  EXPECT_TRUE(delegate.canceled());
}

// Test that ExtensionUninstallDialog cancels the uninstall if the Window which
// is passed to ExtensionUninstallDialog::Create() is destroyed after
// ExtensionUninstallDialogDelegateView is created.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       TrackParentWindowDestructionAfterViewCreation) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());

  base::RunLoop run_loop;
  TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
      extensions::ExtensionUninstallDialog::Create(
          browser()->profile(), browser()->window()->GetNativeWindow(),
          &delegate));
  content::RunAllPendingInMessageLoop();

  dialog->ConfirmUninstall(extension.get(),
                           extensions::UNINSTALL_REASON_FOR_TESTING,
                           extensions::UNINSTALL_SOURCE_FOR_TESTING);

  content::RunAllPendingInMessageLoop();

  // Kill parent window.
  browser()->window()->Close();
  run_loop.Run();
  EXPECT_TRUE(delegate.canceled());
}

// Tests uninstalling the extension while the dialog is active.
// Regression test for https://1200679.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       ExtensionUninstalledWhileDialogIsActive) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());

  base::RunLoop run_loop;
  TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
      extensions::ExtensionUninstallDialog::Create(
          browser()->profile(), browser()->window()->GetNativeWindow(),
          &delegate));

  dialog->ConfirmUninstall(extension.get(),
                           extensions::UNINSTALL_REASON_FOR_TESTING,
                           extensions::UNINSTALL_SOURCE_FOR_TESTING);
  // Wait for the icon to load and dialog to display.
  base::RunLoop().RunUntilIdle();

  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->UninstallExtension(extension->id(),
                           extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);

  run_loop.Run();
  // The dialog should be closed with an appropriate error.
  EXPECT_TRUE(delegate.canceled());
  EXPECT_EQ(u"Extension was removed before dialog closed.", delegate.error());

  // Explicitly destroy the dialog. ExtensionUninstallDialog's dtor will close
  // the view dialog if it's still open (which it shouldn't be), which will
  // guarantee that the check in the delegate for double-closed calls catches
  // any cases.
  dialog = nullptr;
}

// Test that we don't crash when uninstalling an extension from a web app
// window in Ash. Context: crbug.com/825554
// TODO(crbug.com/415937950): Fix and re-enable flaky test.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       DISABLED_WebAppWindowAshCrash) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());

  std::unique_ptr<web_app::OsIntegrationTestOverrideBlockingRegistration>
      faked_os_integration;
  {
    base::ScopedAllowBlockingForTesting blocking;
    faked_os_integration = std::make_unique<
        web_app::OsIntegrationTestOverrideBlockingRegistration>();
  }
  const GURL start_url = GURL("https://test.com/");
  auto web_app_info =
      web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
  web_app_info->scope = start_url;
  web_app_info->user_display_mode =
      web_app::mojom::UserDisplayMode::kStandalone;
  webapps::AppId app_id = web_app::test::InstallWebApp(browser()->profile(),
                                                       std::move(web_app_info));
  Browser* app_browser =
      web_app::LaunchWebAppBrowser(browser()->profile(), app_id);

  TestExtensionUninstallDialogDelegate delegate{base::DoNothing()};
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog;
  {
    base::RunLoop run_loop;
    dialog = extensions::ExtensionUninstallDialog::Create(
        app_browser->profile(), app_browser->window()->GetNativeWindow(),
        &delegate);
    run_loop.RunUntilIdle();
  }

  {
    base::RunLoop run_loop;
    dialog->ConfirmUninstall(extension.get(),
                             extensions::UNINSTALL_REASON_FOR_TESTING,
                             extensions::UNINSTALL_SOURCE_FOR_TESTING);
    run_loop.RunUntilIdle();
  }

  {
    base::ScopedAllowBlockingForTesting blocking;
    faked_os_integration.reset();
  }
}

class ParameterizedExtensionUninstallDialogViewBrowserTest
    : public InProcessBrowserTest,
      public testing::WithParamInterface<extensions::UninstallReason> {};

// Test that when the user clicks Uninstall on the ExtensionUninstallDialog the
// extension's uninstall url (when it is specified) should open and be the
// active tab.
IN_PROC_BROWSER_TEST_P(ParameterizedExtensionUninstallDialogViewBrowserTest,
                       EnsureExtensionUninstallURLIsActiveTabAfterUninstall) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());
  SetUninstallURL(extensions::ExtensionPrefs::Get(browser()->profile()),
                  extension->id());

  // Auto-confirm the uninstall dialog.
  extensions::ScopedTestDialogAutoConfirm auto_confirm(
      extensions::ScopedTestDialogAutoConfirm::ACCEPT);

  base::RunLoop run_loop;
  TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
      extensions::ExtensionUninstallDialog::Create(
          browser()->profile(), browser()->window()->GetNativeWindow(),
          &delegate));
  content::RunAllPendingInMessageLoop();

  extensions::UninstallReason uninstall_reason = GetParam();
  dialog->ConfirmUninstall(extension, uninstall_reason,
                           extensions::UNINSTALL_SOURCE_FOR_TESTING);

  content::RunAllPendingInMessageLoop();

  // There should be 2 tabs open: chrome://about and the extension's uninstall
  // url.
  EXPECT_EQ(2, browser()->tab_strip_model()->count());
  // This navigation can fail, since the uninstall url isn't hooked up to the
  // test server. That's fine, since we only care about the intended target,
  // which is valid.
  content::WaitForLoadStop(
      browser()->tab_strip_model()->GetActiveWebContents());
  // Verifying that the extension's uninstall url is the active tab.
  EXPECT_EQ(kUninstallUrl, GetActiveUrl(browser()));

  run_loop.Run();
  // The delegate should not be canceled because the user chose to uninstall
  // the extension, which should be successful.
  EXPECT_TRUE(!delegate.canceled());
}

// Test that when the user clicks the Report Abuse checkbox and clicks Uninstall
// on the ExtensionUninstallDialog, the extension's uninstall url (when it is
// specified) and the CWS Report Abuse survey are opened in the browser, also
// testing that the CWS survey is the active tab.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       EnsureCWSReportAbusePageIsActiveTabAfterUninstall) {
  scoped_refptr<const extensions::Extension> extension(BuildTestExtension());
  extensions::ExtensionRegistrar::Get(browser()->profile())
      ->AddExtension(extension.get());
  SetUninstallURL(extensions::ExtensionPrefs::Get(browser()->profile()),
                  extension->id());

  // Auto-confirm the uninstall dialog.
  extensions::ScopedTestDialogAutoConfirm auto_confirm(
      extensions::ScopedTestDialogAutoConfirm::ACCEPT_AND_OPTION);

  base::RunLoop run_loop;
  TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
      extensions::ExtensionUninstallDialog::Create(
          browser()->profile(), browser()->window()->GetNativeWindow(),
          &delegate));
  content::RunAllPendingInMessageLoop();

  dialog->ConfirmUninstall(extension,
                           // UNINSTALL_REASON_USER_INITIATED is used to trigger
                           // complete uninstallation.
                           extensions::UNINSTALL_REASON_USER_INITIATED,
                           extensions::UNINSTALL_SOURCE_FOR_TESTING);

  content::RunAllPendingInMessageLoop();
  // There should be 3 tabs open: chrome://about, the extension's uninstall url,
  // and the CWS Report Abuse survey.
  EXPECT_EQ(3, browser()->tab_strip_model()->count());
  // This navigation can fail, since the webstore report abuse url isn't hooked
  // up to the test server. That's fine, since we only care about the intended
  // target, which is valid.
  content::WaitForLoadStop(
      browser()->tab_strip_model()->GetActiveWebContents());
  // The CWS Report Abuse survey should be the active tab. We test this with the
  // actual string for the current "Report Abuse" page for the webstore, to be
  // explicit about what URL we are opening.
  EXPECT_EQ(kReportAbuseUrl, GetActiveUrl(browser()));
  // Similar to the scenario above, this navigation can fail. The uninstall url
  // isn't hooked up to our test server.
  content::WaitForLoadStop(browser()->tab_strip_model()->GetWebContentsAt(1));
  // Verifying that the extension's uninstall url was opened. It should not be
  // the active tab.
  EXPECT_EQ(kUninstallUrl, browser()
                               ->tab_strip_model()
                               ->GetWebContentsAt(1)
                               ->GetLastCommittedURL()
                               .spec());

  run_loop.Run();
  // The delegate should not be canceled because the user chose to uninstall the
  // extension, which should be successful.
  EXPECT_TRUE(!delegate.canceled());
}

// Tests the dialog is anchored in the correct place based on whether the
// extensions container is visible.
// Regression test for crbug.com/133249.
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewBrowserTest,
                       DialogAnchoredInCorrectPlace) {
  extensions::ExtensionRegistrar* extension_registrar =
      extensions::ExtensionRegistrar::Get(browser()->profile());

  scoped_refptr<const extensions::Extension> extensionA(
      BuildTestExtension("Extension A"));
  scoped_refptr<const extensions::Extension> extensionB(
      BuildTestExtension("Extension B"));
  extension_registrar->AddExtension(extensionA.get());
  extension_registrar->AddExtension(extensionB.get());

  // Extensions container should be visible since there are enabled
  // extensions.
  ExtensionsToolbarContainer* const container =
      browser()->GetBrowserView().toolbar()->extensions_container();
  ASSERT_TRUE(container->GetVisible());
  ASSERT_TRUE(container->GetViewForId(extensionA->id()));

  {
    base::RunLoop run_loop;
    TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
    std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
        extensions::ExtensionUninstallDialog::Create(
            browser()->profile(), browser()->window()->GetNativeWindow(),
            &delegate));

    dialog->ConfirmUninstall(extensionA.get(),
                             extensions::UNINSTALL_REASON_FOR_TESTING,
                             extensions::UNINSTALL_SOURCE_FOR_TESTING);
    // Wait for the icon to load and dialog to display.
    base::RunLoop().RunUntilIdle();

    // Dialog should be anchored to the container if the container is visible
    // and the extension has an action view.
    views::Widget* const bubble_widget =
        container->GetAnchoredWidgetForExtensionForTesting(extensionA->id());
    EXPECT_TRUE(bubble_widget);

    CloseUninstallDialog(bubble_widget);
    EXPECT_FALSE(
        container->GetAnchoredWidgetForExtensionForTesting(extensionA->id()));
  }

  // Disable the extension so it doesn't have an action view, but the container
  // is still visible.
  extension_registrar->DisableExtension(
      extensionA->id(), {extensions::disable_reason::DISABLE_USER_ACTION});
  ASSERT_TRUE(container->GetVisible());
  ASSERT_FALSE(container->GetViewForId(extensionA->id()));

  {
    base::RunLoop run_loop;
    TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
    std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
        extensions::ExtensionUninstallDialog::Create(
            browser()->profile(), browser()->window()->GetNativeWindow(),
            &delegate));

    dialog->ConfirmUninstall(extensionA.get(),
                             extensions::UNINSTALL_REASON_FOR_TESTING,
                             extensions::UNINSTALL_SOURCE_FOR_TESTING);
    base::RunLoop().RunUntilIdle();

    // Dialog should be anchored to the container if the container is visible
    // and the extension does not have an action view.
    views::Widget* const bubble_widget =
        container->GetAnchoredWidgetForExtensionForTesting(extensionA->id());
    EXPECT_TRUE(bubble_widget);

    CloseUninstallDialog(bubble_widget);
    EXPECT_FALSE(
        container->GetAnchoredWidgetForExtensionForTesting(extensionA->id()));
  }

  // Disable the second extension to have all extensions disable and the
  // container hidden.
  extension_registrar->DisableExtension(
      extensionB->id(), {extensions::disable_reason::DISABLE_USER_ACTION});
  views::test::WaitForAnimatingLayoutManager(container);
  ASSERT_FALSE(container->GetVisible());
  ASSERT_FALSE(container->GetViewForId(extensionB->id()));

  {
    base::RunLoop run_loop;
    TestExtensionUninstallDialogDelegate delegate(run_loop.QuitClosure());
    std::unique_ptr<extensions::ExtensionUninstallDialog> dialog(
        extensions::ExtensionUninstallDialog::Create(
            browser()->profile(), browser()->window()->GetNativeWindow(),
            &delegate));

    dialog->ConfirmUninstall(extensionA.get(),
                             extensions::UNINSTALL_REASON_FOR_TESTING,
                             extensions::UNINSTALL_SOURCE_FOR_TESTING);
    base::RunLoop().RunUntilIdle();

    // Dialog should be modal if the container is not visible, which means it is
    // not anchored to the container.
    views::Widget* const bubble_widget =
        container->GetAnchoredWidgetForExtensionForTesting(extensionA->id());
    EXPECT_FALSE(bubble_widget);
  }
}

class ExtensionUninstallDialogViewInteractiveBrowserTest
    : public DialogBrowserTest {
 public:
  enum UninstallMethod {
    MANUAL_UNINSTALL,
    UNINSTALL_BY_EXTENSION,
  };
  enum ExtensionOrigin {
    EXTENSION_LOCAL_SOURCE,
    EXTENSION_FROM_WEBSTORE,
  };
  void ShowUi(const std::string& name) override {
    extensions::ExtensionBuilder extension_builder("ExtensionForRemoval");
    if (extension_origin_ == EXTENSION_FROM_WEBSTORE) {
      extension_builder.SetManifestKey(
          "update_url", extension_urls::GetWebstoreUpdateUrl().spec());
    }

    extension_ = extension_builder.Build();
    extensions::ExtensionRegistrar::Get(browser()->profile())
        ->AddExtension(extension_.get());

    dialog_ = extensions::ExtensionUninstallDialog::Create(
        browser()->profile(), browser()->window()->GetNativeWindow(),
        &delegate_);
    if (uninstall_method_ == UNINSTALL_BY_EXTENSION) {
      triggering_extension_ =
          extensions::ExtensionBuilder("TestExtensionRemover").Build();
      dialog_->ConfirmUninstallByExtension(
          extension_.get(), triggering_extension_.get(),
          extensions::UNINSTALL_REASON_FOR_TESTING,
          extensions::UNINSTALL_SOURCE_FOR_TESTING);
    } else {
      dialog_->ConfirmUninstall(extension_.get(),
                                extensions::UNINSTALL_REASON_FOR_TESTING,
                                extensions::UNINSTALL_SOURCE_FOR_TESTING);
    }

    // The dialog shows when an icon update happens, run all pending messages to
    // make sure that the widget exists and is showing at the end of this call.
    content::RunAllPendingInMessageLoop();
  }

  void RunTest(UninstallMethod uninstall_method,
               ExtensionOrigin extension_origin) {
    uninstall_method_ = uninstall_method;
    extension_origin_ = extension_origin;

    ShowAndVerifyUi();
  }

 private:
  class TestDelegate : public extensions::ExtensionUninstallDialog::Delegate {
    void OnExtensionUninstallDialogClosed(
        bool did_start_uninstall,
        const std::u16string& error) override {}
  };

  void TearDownOnMainThread() override {
    // Dialog holds references to the profile, so it needs to tear down before
    // profiles are deleted.
    dialog_.reset();
  }

  scoped_refptr<const extensions::Extension> extension_;
  scoped_refptr<const extensions::Extension> triggering_extension_;
  TestDelegate delegate_;
  std::unique_ptr<extensions::ExtensionUninstallDialog> dialog_;

  UninstallMethod uninstall_method_;
  ExtensionOrigin extension_origin_;
};

#if BUILDFLAG(IS_WIN)
// TODO(crbug.com/40069124): Enable the test again.
#define MAYBE_InvokeUi_ManualUninstall DISABLED_InvokeUi_ManualUninstall
#else
#define MAYBE_InvokeUi_ManualUninstall InvokeUi_ManualUninstall
#endif
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewInteractiveBrowserTest,
                       MAYBE_InvokeUi_ManualUninstall) {
  RunTest(MANUAL_UNINSTALL, EXTENSION_LOCAL_SOURCE);
}

// TODO(crbug.com/40926539): Re-enable this test
#if BUILDFLAG(IS_WIN)
#define MAYBE_InvokeUi_ManualUninstallShowReportAbuse \
  DISABLED_InvokeUi_ManualUninstallShowReportAbuse
#else
#define MAYBE_InvokeUi_ManualUninstallShowReportAbuse \
  InvokeUi_ManualUninstallShowReportAbuse
#endif
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewInteractiveBrowserTest,
                       MAYBE_InvokeUi_ManualUninstallShowReportAbuse) {
  RunTest(MANUAL_UNINSTALL, EXTENSION_FROM_WEBSTORE);
}

#if BUILDFLAG(IS_WIN)
// TODO(crbug.com/40069124): Enable the test again.
#define MAYBE_InvokeUi_UninstallByExtension \
  DISABLED_InvokeUi_UninstallByExtension
#else
#define MAYBE_InvokeUi_UninstallByExtension InvokeUi_UninstallByExtension
#endif
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewInteractiveBrowserTest,
                       MAYBE_InvokeUi_UninstallByExtension) {
  RunTest(UNINSTALL_BY_EXTENSION, EXTENSION_LOCAL_SOURCE);
}

// TODO(crbug.com/40926539): Fix flakiness and re-enable this test.
#if BUILDFLAG(IS_WIN)
#define MAYBE_InvokeUi_UninstallByExtensionShowReportAbuse \
  DISABLED_InvokeUi_UninstallByExtensionShowReportAbuse
#else
#define MAYBE_InvokeUi_UninstallByExtensionShowReportAbuse \
  InvokeUi_UninstallByExtensionShowReportAbuse
#endif
IN_PROC_BROWSER_TEST_F(ExtensionUninstallDialogViewInteractiveBrowserTest,
                       MAYBE_InvokeUi_UninstallByExtensionShowReportAbuse) {
  RunTest(UNINSTALL_BY_EXTENSION, EXTENSION_FROM_WEBSTORE);
}

INSTANTIATE_TEST_SUITE_P(
    All,
    ParameterizedExtensionUninstallDialogViewBrowserTest,
    testing::Values(extensions::UNINSTALL_REASON_USER_INITIATED,
                    extensions::UNINSTALL_REASON_CHROME_WEBSTORE));