File: password_dialog_view_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (646 lines) | stat: -rw-r--r-- 26,884 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
// Copyright 2016 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/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/passwords/account_chooser_dialog_view.h"
#include "chrome/browser/ui/views/passwords/auto_signin_first_run_dialog_view.h"
#include "chrome/browser/ui/views/passwords/credential_leak_dialog_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/password_manager/core/browser/mock_password_form_manager_for_ui.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/ui_base_switches.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"

using net::test_server::BasicHttpResponse;
using net::test_server::HttpRequest;
using net::test_server::HttpResponse;
using password_manager::CredentialLeakFlags;
using password_manager::CredentialLeakType;
using ::testing::Field;
using ::testing::ReturnRef;

namespace {

// ManagePasswordsUIController subclass to capture the dialog instance
class TestManagePasswordsUIController : public ManagePasswordsUIController {
 public:
  explicit TestManagePasswordsUIController(content::WebContents* web_contents);

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

  void OnDialogHidden() override;
  AccountChooserPrompt* CreateAccountChooser(
      CredentialManagerDialogController* controller) override;
  AutoSigninFirstRunPrompt* CreateAutoSigninPrompt(
      CredentialManagerDialogController* controller) override;
  std::unique_ptr<CredentialLeakPrompt> CreateCredentialLeakPrompt(
      CredentialLeakDialogController* controller) override;

  AccountChooserDialogView* current_account_chooser() const {
    return static_cast<AccountChooserDialogView*>(current_account_chooser_);
  }

  AutoSigninFirstRunDialogView* current_autosignin_prompt() const {
    return static_cast<AutoSigninFirstRunDialogView*>(
        current_autosignin_prompt_);
  }

  views::Widget* current_credential_leak_widget() const {
    return current_credential_leak_prompt_->GetWidgetForTesting();
  }

  MOCK_METHOD(void, OnDialogClosed, (), ());

 private:
  raw_ptr<AccountChooserPrompt, AcrossTasksDanglingUntriaged>
      current_account_chooser_;
  raw_ptr<AutoSigninFirstRunPrompt, AcrossTasksDanglingUntriaged>
      current_autosignin_prompt_;
  raw_ptr<CredentialLeakPrompt, AcrossTasksDanglingUntriaged>
      current_credential_leak_prompt_;
};

TestManagePasswordsUIController::TestManagePasswordsUIController(
    content::WebContents* web_contents)
    : ManagePasswordsUIController(web_contents),
      current_account_chooser_(nullptr),
      current_autosignin_prompt_(nullptr),
      current_credential_leak_prompt_(nullptr) {
  // Attach TestManagePasswordsUIController to |web_contents| so the default
  // ManagePasswordsUIController isn't created.
  // Do not silently replace an existing ManagePasswordsUIController because it
  // unregisters itself in WebContentsDestroyed().
  EXPECT_FALSE(web_contents->GetUserData(UserDataKey()));
  web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
}

void TestManagePasswordsUIController::OnDialogHidden() {
  ManagePasswordsUIController::OnDialogHidden();
  OnDialogClosed();
}

AccountChooserPrompt* TestManagePasswordsUIController::CreateAccountChooser(
    CredentialManagerDialogController* controller) {
  current_account_chooser_ =
      ManagePasswordsUIController::CreateAccountChooser(controller);
  return current_account_chooser_;
}

AutoSigninFirstRunPrompt*
TestManagePasswordsUIController::CreateAutoSigninPrompt(
    CredentialManagerDialogController* controller) {
  current_autosignin_prompt_ =
      ManagePasswordsUIController::CreateAutoSigninPrompt(controller);
  return current_autosignin_prompt_;
}

std::unique_ptr<CredentialLeakPrompt>
TestManagePasswordsUIController::CreateCredentialLeakPrompt(
    CredentialLeakDialogController* controller) {
  auto current_credential_leak_prompt =
      ManagePasswordsUIController::CreateCredentialLeakPrompt(controller);
  current_credential_leak_prompt_ = current_credential_leak_prompt.get();
  return current_credential_leak_prompt;
}

std::unique_ptr<password_manager::PasswordFormManagerForUI> WrapFormInManager(
    const password_manager::PasswordForm* form) {
  auto submitted_manager =
      std::make_unique<password_manager::MockPasswordFormManagerForUI>();
  ON_CALL(*submitted_manager, GetPendingCredentials)
      .WillByDefault(ReturnRef(*form));
  return submitted_manager;
}

class PasswordDialogViewTest : public DialogBrowserTest {
 public:
  // DialogBrowserTest:
  void SetUpOnMainThread() override;
  void ShowUi(const std::string& name) override;

  void SetupChooseCredentials(
      std::vector<std::unique_ptr<password_manager::PasswordForm>>
          local_credentials,
      const url::Origin& origin);

  content::WebContents* SetupTabWithTestController(Browser* browser);

  TestManagePasswordsUIController* controller() const { return controller_; }

  ChromePasswordManagerClient* client() const {
    return ChromePasswordManagerClient::FromWebContents(
        browser()->tab_strip_model()->GetActiveWebContents());
  }

  MOCK_METHOD(void,
              OnChooseCredential,
              (const password_manager::PasswordForm*),
              ());
  MOCK_METHOD(void, OnIconRequestDone, (), ());

  // Called on the server background thread.
  std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
    std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse);
    if (request.relative_url == "/icon.png") {
      OnIconRequestDone();
    }
    return std::move(response);
  }

 private:
  raw_ptr<TestManagePasswordsUIController, AcrossTasksDanglingUntriaged>
      controller_;
};

void PasswordDialogViewTest::SetUpOnMainThread() {
#if BUILDFLAG(IS_MAC)
  // On non-Mac platforms, animations are globally disabled during tests; on
  // Mac they are generally not, but these tests are dramatically slower and
  // flakier with animations.
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kDisableModalAnimations);
#endif
  SetupTabWithTestController(browser());
}

void PasswordDialogViewTest::SetupChooseCredentials(
    std::vector<std::unique_ptr<password_manager::PasswordForm>>
        local_credentials,
    const url::Origin& origin) {
  client()->PromptUserToChooseCredentials(
      std::move(local_credentials), origin,
      base::BindOnce(&PasswordDialogViewTest::OnChooseCredential,
                     base::Unretained(this)));
  EXPECT_EQ(password_manager::ui::CREDENTIAL_REQUEST_STATE,
            controller()->GetState());
}

content::WebContents* PasswordDialogViewTest::SetupTabWithTestController(
    Browser* browser) {
  // Open a new tab with modified ManagePasswordsUIController.
  content::WebContents* tab =
      browser->tab_strip_model()->GetActiveWebContents();
  std::unique_ptr<content::WebContents> new_tab = content::WebContents::Create(
      content::WebContents::CreateParams(tab->GetBrowserContext()));
  content::WebContents* raw_new_tab = new_tab.get();
  EXPECT_TRUE(raw_new_tab);

  // ManagePasswordsUIController needs ChromePasswordManagerClient for logging
  // and ChromePasswordManagerClient needs ChromeAutofillClient.
  autofill::ChromeAutofillClient::CreateForWebContents(raw_new_tab);
  ChromePasswordManagerClient::CreateForWebContents(raw_new_tab);
  EXPECT_TRUE(ChromePasswordManagerClient::FromWebContents(raw_new_tab));
  controller_ = new TestManagePasswordsUIController(raw_new_tab);
  browser->tab_strip_model()->AppendWebContents(std::move(new_tab), true);

  // Navigate to a Web URL.
  EXPECT_NO_FATAL_FAILURE(EXPECT_TRUE(
      ui_test_utils::NavigateToURL(browser, GURL("http://www.google.com"))));
  EXPECT_EQ(controller_,
            ManagePasswordsUIController::FromWebContents(raw_new_tab));
  return raw_new_tab;
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAccountChooserWithMultipleCredentialsReturnEmpty) {
  // Set up the test server to handle the form icon request.
  embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
      &PasswordDialogViewTest::HandleRequest, base::Unretained(this)));
  ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.icon_url = GURL("broken url");
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));
  form.icon_url = embedded_test_server()->GetURL("/icon.png");
  form.display_name = u"Peter Pan";
  form.federation_origin =
      url::SchemeHostPort(GURL("https://google.com/federation"));
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  // Prepare to capture the network request.
  EXPECT_CALL(*this, OnIconRequestDone());
  embedded_test_server()->StartAcceptingConnections();

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));
  ASSERT_TRUE(controller()->current_account_chooser());
  AccountChooserDialogView* dialog = controller()->current_account_chooser();
  EXPECT_CALL(*this, OnChooseCredential(nullptr));
  EXPECT_CALL(*controller(), OnDialogClosed());
  dialog->GetWidget()->Close();
  base::RunLoop().RunUntilIdle();

  EXPECT_FALSE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(
    PasswordDialogViewTest,
    PopupAccountChooserWithMultipleCredentialsReturnNonEmpty) {
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.icon_url = GURL("broken url");
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));
  GURL icon_url("https://google.com/icon.png");
  form.icon_url = icon_url;
  form.display_name = u"Peter Pan";
  form.federation_origin =
      url::SchemeHostPort(GURL("https://google.com/federation"));
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));
  ASSERT_TRUE(controller()->current_account_chooser());

  // After picking a credential, we should pass it back to the caller via the
  // callback, but we should not pop up the autosignin prompt as there were
  // multiple credentials available.
  EXPECT_CALL(*this, OnChooseCredential(testing::Pointee(form)));
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
  controller()->ChooseCredential(
      form, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);
  EXPECT_FALSE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAccountChooserWithSingleCredentialReturnEmpty) {
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));

  EXPECT_TRUE(controller()->current_account_chooser());
  AccountChooserDialogView* dialog = controller()->current_account_chooser();
  EXPECT_CALL(*this, OnChooseCredential(nullptr));
  EXPECT_CALL(*controller(), OnDialogClosed());
  dialog->GetWidget()->Close();
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAccountChooserWithSingleCredentialClickSignIn) {
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));

  EXPECT_TRUE(controller()->current_account_chooser());
  views::BubbleDialogDelegateView* dialog =
      controller()->current_account_chooser();
  views::test::WidgetDestroyedWaiter bubble_observer(dialog->GetWidget());
  EXPECT_CALL(*this, OnChooseCredential(testing::Pointee(form)));
  dialog->Accept();
  bubble_observer.Wait();
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAccountChooserWithSingleCredentialReturnNonEmpty) {
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));

  EXPECT_TRUE(controller()->current_account_chooser());

  // After picking a credential, we should pass it back to the caller via the
  // callback, and pop up the autosignin prompt iff we should show it.
  EXPECT_CALL(*this, OnChooseCredential(testing::Pointee(form)));
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
  controller()->ChooseCredential(
      form, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);

  EXPECT_TRUE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAccountChooserWithDisabledAutoSignin) {
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  SetupChooseCredentials(std::move(local_credentials),
                         url::Origin::Create(origin));

  EXPECT_TRUE(controller()->current_account_chooser());

  // After picking a credential, we should pass it back to the caller via the
  // callback, and pop up the autosignin prompt iff we should show it.
  EXPECT_CALL(*this, OnChooseCredential(testing::Pointee(form)));
  browser()->profile()->GetPrefs()->SetBoolean(
      password_manager::prefs::kCredentialsEnableAutosignin, false);
  controller()->ChooseCredential(
      form, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);

  // The first run experience isn't shown because the setting is off.
  EXPECT_FALSE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, PopupAccountChooserInIncognito) {
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
  EXPECT_TRUE(browser()->profile()->GetPrefs()->GetBoolean(
      password_manager::prefs::kCredentialsEnableAutosignin));
  GURL origin("https://example.com");
  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;
  local_credentials.push_back(
      std::make_unique<password_manager::PasswordForm>(form));

  Browser* incognito = CreateIncognitoBrowser();
  content::WebContents* tab = SetupTabWithTestController(incognito);
  ChromePasswordManagerClient* client =
      ChromePasswordManagerClient::FromWebContents(tab);
  client->PromptUserToChooseCredentials(
      std::move(local_credentials), url::Origin::Create(origin),
      base::BindOnce(&PasswordDialogViewTest::OnChooseCredential,
                     base::Unretained(this)));
  EXPECT_EQ(password_manager::ui::CREDENTIAL_REQUEST_STATE,
            controller()->GetState());
  EXPECT_TRUE(controller()->current_account_chooser());

  EXPECT_CALL(*this, OnChooseCredential(testing::Pointee(form)));
  controller()->ChooseCredential(
      form, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);

  // The first run experience isn't shown because of Incognito.
  EXPECT_FALSE(controller()->current_autosignin_prompt());
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, EscCancelsAutoSigninPrompt) {
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
  controller()->OnPromptEnableAutoSignin();
  ASSERT_TRUE(controller()->current_autosignin_prompt());
  EXPECT_EQ(password_manager::ui::INACTIVE_STATE, controller()->GetState());
  AutoSigninFirstRunDialogView* dialog =
      controller()->current_autosignin_prompt();
  views::test::WidgetDestroyedWaiter bubble_observer(dialog->GetWidget());
  ui::Accelerator esc(ui::VKEY_ESCAPE, 0);
  EXPECT_CALL(*controller(), OnDialogClosed());
  EXPECT_TRUE(dialog->GetWidget()->client_view()->AcceleratorPressed(esc));
  bubble_observer.Wait();
  content::RunAllPendingInMessageLoop();
  base::RunLoop().RunUntilIdle();
  testing::Mock::VerifyAndClearExpectations(controller());
  EXPECT_FALSE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, PopupCredentialsLeakedPrompt) {
  CredentialLeakType leak_type = CredentialLeakFlags::kPasswordSaved |
                                 CredentialLeakFlags::kPasswordUsedOnOtherSites;
  controller()->OnCredentialLeak(password_manager::LeakedPasswordDetails(
      leak_type, GURL("https://example.com"), u"Eve", u"qwerty",
      /*in_account_store=*/false));
  ASSERT_TRUE(controller()->current_credential_leak_widget());
  EXPECT_EQ(password_manager::ui::INACTIVE_STATE, controller()->GetState());
  views::Widget* dialog = controller()->current_credential_leak_widget();
  views::test::WidgetDestroyedWaiter bubble_observer(dialog);
  ui::Accelerator esc(ui::VKEY_ESCAPE, 0);
  EXPECT_TRUE(dialog->client_view()->AcceleratorPressed(esc));
  bubble_observer.Wait();
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest,
                       PopupAutoSigninPromptAfterBlockedZeroclick) {
  EXPECT_TRUE(
      password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          browser()->profile()->GetPrefs()));

  GURL origin("https://example.com");
  password_manager::PasswordForm form;
  form.url = origin;
  form.username_value = u"peter@pan.test";
  form.password_value = u"I can fly!";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;

  // Successful login alone will not prompt:
  client()->NotifySuccessfulLoginWithExistingPassword(WrapFormInManager(&form));
  ASSERT_FALSE(controller()->current_autosignin_prompt());

  // Blocked automatic sign-in will not prompt:
  std::unique_ptr<password_manager::PasswordForm> blocked_form(
      new password_manager::PasswordForm(form));
  client()->NotifyUserCouldBeAutoSignedIn(std::move(blocked_form));
  ASSERT_FALSE(controller()->current_autosignin_prompt());

  // Successful login with a distinct form after block will not prompt:
  blocked_form = std::make_unique<password_manager::PasswordForm>(form);
  client()->NotifyUserCouldBeAutoSignedIn(std::move(blocked_form));
  form.username_value = u"notpeter@pan.test";
  client()->NotifySuccessfulLoginWithExistingPassword(WrapFormInManager(&form));
  ASSERT_FALSE(controller()->current_autosignin_prompt());

  // Successful login with the same form after block will not prompt if auto
  // sign-in is off:
  browser()->profile()->GetPrefs()->SetBoolean(
      password_manager::prefs::kCredentialsEnableAutosignin, false);
  blocked_form = std::make_unique<password_manager::PasswordForm>(form);
  client()->NotifyUserCouldBeAutoSignedIn(std::move(blocked_form));
  client()->NotifySuccessfulLoginWithExistingPassword(WrapFormInManager(&form));
  ASSERT_FALSE(controller()->current_autosignin_prompt());
  browser()->profile()->GetPrefs()->SetBoolean(
      password_manager::prefs::kCredentialsEnableAutosignin, true);

  // Successful login with the same form after block will *prompt:
  blocked_form = std::make_unique<password_manager::PasswordForm>(form);
  client()->NotifyUserCouldBeAutoSignedIn(std::move(blocked_form));
  client()->NotifySuccessfulLoginWithExistingPassword(WrapFormInManager(&form));
  ASSERT_TRUE(controller()->current_autosignin_prompt());
}

// DialogBrowserTest methods for interactive dialog invocation.
void PasswordDialogViewTest::ShowUi(const std::string& name) {
  if (name == "AutoSigninFirstRun") {
    controller()->OnPromptEnableAutoSignin();
    return;
  }

  GURL origin("https://example.com");
  if (name == "CredentialLeak") {
    CredentialLeakType leak_type =
        CredentialLeakFlags::kPasswordSaved |
        CredentialLeakFlags::kPasswordUsedOnOtherSites;

    controller()->OnCredentialLeak(password_manager::LeakedPasswordDetails(
        leak_type, origin, u"Eve", u"qwerty",
        /*in_account_store=*/false));
    return;
  }

  std::vector<std::unique_ptr<password_manager::PasswordForm>>
      local_credentials;
  password_manager::PasswordForm form;
  form.url = origin;
  form.display_name = u"Peter Pan";
  form.username_value = u"peter@pan.test";
  form.match_type = password_manager::PasswordForm::MatchType::kExact;

  if (name == "PopupAutoSigninPrompt") {
    form.icon_url = GURL("broken url");
    local_credentials.push_back(
        std::make_unique<password_manager::PasswordForm>(form));
    form.icon_url = GURL("https://google.com/icon.png");
    form.display_name = u"Peter";
    form.federation_origin =
        url::SchemeHostPort(GURL("https://google.com/federation"));
    local_credentials.push_back(
        std::make_unique<password_manager::PasswordForm>(form));
    controller()->OnAutoSignin(std::move(local_credentials),
                               url::Origin::Create(origin));
    EXPECT_EQ(password_manager::ui::AUTO_SIGNIN_STATE,
              controller()->GetState());
  } else if (base::StartsWith(name, "PopupAccountChooserWith",
                              base::CompareCase::SENSITIVE)) {
    local_credentials.push_back(
        std::make_unique<password_manager::PasswordForm>(form));
    if (name == "PopupAccountChooserWithMultipleCredentialClickSignIn") {
      form.icon_url = GURL("https://google.com/icon.png");
      form.display_name = u"Tinkerbell";
      form.username_value = u"tinkerbell@pan.test";
      form.federation_origin =
          url::SchemeHostPort(GURL("https://google.com/neverland"));
      local_credentials.push_back(
          std::make_unique<password_manager::PasswordForm>(form));
      form.display_name = u"James Hook";
      form.username_value = u"james@pan.test";
      form.federation_origin =
          url::SchemeHostPort(GURL("https://google.com/jollyroger"));
      local_credentials.push_back(
          std::make_unique<password_manager::PasswordForm>(form));
      form.display_name = u"Wendy Darling";
      form.username_value = u"wendy@pan.test";
      form.federation_origin =
          url::SchemeHostPort(GURL("https://google.com/london"));
      local_credentials.push_back(
          std::make_unique<password_manager::PasswordForm>(form));
    }
    SetupChooseCredentials(std::move(local_credentials),
                           url::Origin::Create(origin));
  } else {
    ADD_FAILURE() << "Unknown dialog type";
    return;
  }
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, InvokeUi_AutoSigninFirstRun) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, InvokeUi_CredentialLeak) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(PasswordDialogViewTest, InvokeUi_PopupAutoSigninPrompt) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(
    PasswordDialogViewTest,
    InvokeUi_PopupAccountChooserWithSingleCredentialClickSignIn) {
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(
    PasswordDialogViewTest,
    InvokeUi_PopupAccountChooserWithMultipleCredentialClickSignIn) {
  ShowAndVerifyUi();
}

}  // namespace