#include "chrome/browser/ui/views/profiles/avatar_toolbar_button.h"
#include <optional>
#include <string>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/with_feature_override.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/util/managed_browser_utils.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/profiles/profile_colors_util.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/profile_destruction_waiter.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/management/management_service.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/accounts_mutator.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/primary_account_mutator.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/test/test_sync_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "google_apis/gaia/core_account_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/mojom/themes.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_profile.h"
#include "components/user_manager/user_names.h"
#endif
namespace {
ui::mojom::BrowserColorVariant kColorVariant = …;
const gfx::Image kSignedInImage = …;
const char kSignedInImageUrl[] = …;
enum class ColorThemeType { … };
std::unique_ptr<KeyedService> TestingSyncFactoryFunction(
content::BrowserContext* context) { … }
class ProfileLoader { … };
}
class AvatarToolbarButtonBrowserTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, IncognitoWindowCount) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, GuestWindowCount) { … }
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
class AvatarToolbarButtonAshBrowserTest
: public AvatarToolbarButtonBrowserTest {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(ash::switches::kGuestSession);
command_line->AppendSwitchASCII(ash::switches::kLoginUser,
user_manager::kGuestUserName);
command_line->AppendSwitchASCII(ash::switches::kLoginProfile,
TestingProfile::kTestUserProfileDir);
command_line->AppendSwitch(switches::kIncognito);
}
};
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonAshBrowserTest, GuestSession) {
Profile* guest_profile = browser()->profile();
ASSERT_TRUE(guest_profile->IsGuestSession());
AvatarToolbarButton* avatar_button = GetAvatarToolbarButton(browser());
EXPECT_TRUE(avatar_button->GetVisible());
EXPECT_FALSE(avatar_button->GetEnabled());
EXPECT_EQ(avatar_button->GetText(),
l10n_util::GetPluralStringFUTF16(IDS_AVATAR_BUTTON_GUEST, 1));
Browser* browser_2 = CreateBrowser(guest_profile);
AvatarToolbarButton* avatar_button_2 = GetAvatarToolbarButton(browser_2);
EXPECT_TRUE(avatar_button_2->GetVisible());
EXPECT_FALSE(avatar_button_2->GetEnabled());
EXPECT_EQ(avatar_button->GetText(),
l10n_util::GetPluralStringFUTF16(IDS_AVATAR_BUTTON_GUEST, 1));
EXPECT_EQ(avatar_button_2->GetText(),
l10n_util::GetPluralStringFUTF16(IDS_AVATAR_BUTTON_GUEST, 1));
}
#endif
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, DefaultBrowser) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, IncognitoBrowser) { … }
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, SigninBrowser) {
CreateIncognitoBrowser(browser()->profile());
Profile::OTRProfileID profile_id(
Profile::OTRProfileID::CreateUniqueForCaptivePortal());
Browser* browser1 = Browser::Create(Browser::CreateParams(
browser()->profile()->GetOffTheRecordProfile(profile_id,
true),
true));
AddBlankTabAndShow(browser1);
AvatarToolbarButton* avatar = GetAvatarToolbarButton(browser1);
ASSERT_TRUE(avatar);
EXPECT_TRUE(avatar->GetVisible());
EXPECT_FALSE(avatar->GetEnabled());
}
#endif
class AvatarToolbarButtonBrowserTestWithExplicitBrowserSignin
: public base::test::WithFeatureOverride,
public AvatarToolbarButtonBrowserTest { … };
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonBrowserTestWithExplicitBrowserSignin,
ShowNameOnSigninThenSync) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonBrowserTestWithExplicitBrowserSignin,
ShowNameOnSync) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonBrowserTestWithExplicitBrowserSignin,
OpenNewBrowserWhileNameIsShown) { … }
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(…);
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
ShowNameDoesNotAppearOnNewBrowserIfNotShowing) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, SyncPaused) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, SyncError) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
SyncPausedThenExplicitText) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
ExplicitTextThenSyncPause) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
ShowExplicitTextAndHide) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
ShowExplicitTextAndDefaultHide) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
ShowExplicitTextTwiceAndHide) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, SignInOutIconEffect) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, SignedInChangeIcon) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
PRE_SignedInWithNewSessionKeepIcon) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
SignedInWithNewSessionKeepIcon) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest, TooltipText) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonBrowserTest,
EnableSyncWithSyncDisabled) { … }
#endif
class AvatarToolbarButtonProfileColorBrowserTest
: public AvatarToolbarButtonBrowserTest,
public testing::WithParamInterface<ColorThemeType> { … };
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonProfileColorBrowserTest,
PRE_AutogeneratedTheme) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonProfileColorBrowserTest,
AutogeneratedTheme) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonProfileColorBrowserTest,
DefaultTheme) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonProfileColorBrowserTest,
UpdateThemeOnBrowserUpdate) { … }
IN_PROC_BROWSER_TEST_P(AvatarToolbarButtonProfileColorBrowserTest,
ProfileColorsUpdateOnColorSchemeChange) { … }
INSTANTIATE_TEST_SUITE_P(…);
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
class AvatarToolbarButtonEnterpriseBadgingBrowserTest
: public AvatarToolbarButtonBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkProfileTextBadging) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkProfileTextBadgingUpdating) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBadgeOnTransientModeTimesOut) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBadgeOnTransientModeTimesOutToNonTransient) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBadgeOnNonTransientModeDoesNotTimesOut) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBrowserShowsBadgeWithLabelPresets) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkNewBrowserShowsBadge) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBadgeNonTransientModeAndSyncPause) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
WorkBadgeTransientModeAndSyncPause) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonEnterpriseBadgingBrowserTest,
DecliningManagementShouldRemoveWorkBadge) { … }
class AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest
: public AvatarToolbarButtonBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
SigninPausedFromExternalErrorThenReauth) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
SigninPausedFromWebSignout) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
SigninPausedFromWebSignoutThenRestartChrome) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
SigninPausedDelayEndedNoBrowser) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
SigninPausedThenSignout) { … }
IN_PROC_BROWSER_TEST_F(AvatarToolbarButtonWithExplicitBrowserSigninBrowserTest,
AccessibilityLabels) { … }
#endif