summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/signin/signin_ui_util_browsertest.cc
blob: 299e45539d68b79afdbb5f7180b8edc694113b14 (plain)
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SIGNIN_SIGNIN_UI_UTIL_BROWSERTEST_CC_
#define CHROME_BROWSER_SIGNIN_SIGNIN_UI_UTIL_BROWSERTEST_CC_

#include "chrome/browser/signin/signin_ui_util.h"

#include "base/callback_helpers.h"
#include "base/test/bind.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/signin/public/base/signin_buildflags.h"
#include "content/public/test/browser_test.h"

#if !BUILDFLAG(ENABLE_DICE_SUPPORT)
#error This file only contains DICE browser tests for now.
#endif

namespace signin_ui_util {

class DiceSigninUiUtilBrowserTest : public InProcessBrowserTest {
 public:
  DiceSigninUiUtilBrowserTest() = default;
  ~DiceSigninUiUtilBrowserTest() override = default;

  Profile* CreateProfile() {
    Profile* new_profile = nullptr;
    base::RunLoop run_loop;
    ProfileManager::CreateMultiProfileAsync(
        u"test_profile", /*icon_index=*/0, /*is_hidden=*/false,
        base::BindLambdaForTesting(
            [&new_profile, &run_loop](Profile* profile,
                                      Profile::CreateStatus status) {
              ASSERT_NE(status, Profile::CREATE_STATUS_LOCAL_FAIL);
              if (status == Profile::CREATE_STATUS_INITIALIZED) {
                new_profile = profile;
                run_loop.Quit();
              }
            }));
    run_loop.Run();
    return new_profile;
  }

 private:
};

// Tests that `ShowExtensionSigninPrompt()` doesn't crash when it cannot create
// a new browser. Regression test for https://crbug.com/1273370.
IN_PROC_BROWSER_TEST_F(DiceSigninUiUtilBrowserTest,
                       ShowExtensionSigninPrompt_NoBrowser) {
  Profile* new_profile = CreateProfile();

  // New profile should not have any browser windows.
  EXPECT_FALSE(chrome::FindBrowserWithProfile(new_profile));

  ShowExtensionSigninPrompt(new_profile, /*enable_sync=*/true,
                            /*email_hint=*/std::string());
  // `ShowExtensionSigninPrompt()` creates a new browser.
  Browser* browser = chrome::FindBrowserWithProfile(new_profile);
  ASSERT_TRUE(browser);
  EXPECT_EQ(1, browser->tab_strip_model()->count());

  // Profile deletion closes the browser.
  g_browser_process->profile_manager()->ScheduleProfileForDeletion(
      new_profile->GetPath(), base::DoNothing());
  ui_test_utils::WaitForBrowserToClose(browser);
  EXPECT_FALSE(chrome::FindBrowserWithProfile(new_profile));

  // `ShowExtensionSigninPrompt()` does nothing for deleted profile.
  ShowExtensionSigninPrompt(new_profile, /*enable_sync=*/true,
                            /*email_hint=*/std::string());
  EXPECT_FALSE(chrome::FindBrowserWithProfile(new_profile));
}

}  // namespace signin_ui_util

#endif  // CHROME_BROWSER_SIGNIN_SIGNIN_UI_UTIL_BROWSERTEST_CC_