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
|
// Copyright 2013 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/themes/theme_service.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/test/theme_service_changed_waiter.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "ui/base/buildflags.h"
#include "ui/color/color_provider.h"
#include "ui/native_theme/native_theme.h"
#if BUILDFLAG(IS_LINUX)
#include "ui/linux/linux_ui.h"
#include "ui/linux/linux_ui_getter.h"
#endif
namespace {
// The ntp link color specified in the theme.
constexpr SkColor kThemeNtpLinkColor = SkColorSetRGB(36, 70, 0);
bool UsingCustomTheme(const ThemeService& theme_service) {
return !theme_service.UsingSystemTheme() &&
!theme_service.UsingDefaultTheme();
}
const ui::ColorProvider* GetColorProviderFor(Browser* browser) {
return browser->window()->GetColorProvider();
}
class ThemeServiceBrowserTest : public extensions::ExtensionBrowserTest {
public:
ThemeServiceBrowserTest() = default;
ThemeServiceBrowserTest(const ThemeServiceBrowserTest&) = delete;
ThemeServiceBrowserTest& operator=(const ThemeServiceBrowserTest&) = delete;
~ThemeServiceBrowserTest() override = default;
void SetUp() override {
extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
extensions::ExtensionBrowserTest::SetUp();
}
};
// Test that the theme is recreated from the extension when the data pack is
// unavailable or invalid (such as when the theme pack version is incremented).
// The PRE_ part of the test installs the theme and changes where Chrome looks
// for the theme data pack to make sure that Chrome does not find it.
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest, PRE_ThemeDataPackInvalid) {
Profile* profile = browser()->profile();
ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile);
// Test initial state.
EXPECT_FALSE(UsingCustomTheme(*theme_service));
EXPECT_NE(kThemeNtpLinkColor,
GetColorProviderFor(browser())->GetColor(kColorNewTabPageLink));
EXPECT_EQ(base::FilePath(),
profile->GetPrefs()->GetFilePath(prefs::kCurrentThemePackFilename));
test::ThemeServiceChangedWaiter waiter(theme_service);
InstallExtension(test_data_dir_.AppendASCII("theme"), 1);
waiter.WaitForThemeChanged();
// Check that the theme was installed.
EXPECT_TRUE(UsingCustomTheme(*theme_service));
EXPECT_EQ(kThemeNtpLinkColor,
GetColorProviderFor(browser())->GetColor(kColorNewTabPageLink));
EXPECT_NE(base::FilePath(),
profile->GetPrefs()->GetFilePath(prefs::kCurrentThemePackFilename));
// Change the theme data pack path to an invalid location such that second
// part of the test is forced to recreate the theme pack when the theme
// service is initialized.
profile->GetPrefs()->SetFilePath(prefs::kCurrentThemePackFilename,
base::FilePath());
}
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest, ThemeDataPackInvalid) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(browser()->profile());
EXPECT_TRUE(UsingCustomTheme(*theme_service));
EXPECT_EQ(kThemeNtpLinkColor,
GetColorProviderFor(browser())->GetColor(kColorNewTabPageLink));
}
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest, IncognitoTest) {
// This test relies on incognito being meaningfully different than default,
// which is not currently true in dark mode.
ui::NativeTheme::GetInstanceForNativeUi()->set_use_dark_colors(false);
// Should get a different ColorProvider for incognito and original windows.
Browser* incognito_browser = CreateIncognitoBrowser();
const auto* color_provider = GetColorProviderFor(browser());
const auto* otr_color_provider = GetColorProviderFor(incognito_browser);
EXPECT_NE(color_provider, otr_color_provider);
// And (some) colors should be different.
EXPECT_NE(color_provider->GetColor(kColorToolbar),
otr_color_provider->GetColor(kColorToolbar));
}
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest,
IncognitoCustomColor_WithAutoGeneratedTheme) {
Browser* incognito_browser = CreateIncognitoBrowser();
const SkColor original_color =
GetColorProviderFor(browser())->GetColor(kColorToolbar);
const SkColor original_otr_color =
GetColorProviderFor(incognito_browser)->GetColor(kColorToolbar);
ThemeServiceFactory::GetForProfile(profile())
->BuildAutogeneratedThemeFromColor(SkColorSetRGB(100, 100, 100));
// The custom theme should affect the normal window, but not the incognito
// window.
EXPECT_NE(original_color,
GetColorProviderFor(browser())->GetColor(kColorToolbar));
EXPECT_EQ(original_otr_color,
GetColorProviderFor(incognito_browser)->GetColor(kColorToolbar));
}
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest,
IncognitoCustomColor_WithExtensionOverride) {
Browser* incognito_browser = CreateIncognitoBrowser();
const SkColor original_color =
GetColorProviderFor(browser())->GetColor(kColorToolbar);
const SkColor original_otr_color =
GetColorProviderFor(incognito_browser)->GetColor(kColorToolbar);
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(browser()->profile());
test::ThemeServiceChangedWaiter waiter(theme_service);
InstallExtension(test_data_dir_.AppendASCII("theme_minimal/"), 1);
waiter.WaitForThemeChanged();
EXPECT_TRUE(theme_service->UsingExtensionTheme());
// The custom theme should affect the normal window, but not the incognito
// window.
EXPECT_NE(original_color,
GetColorProviderFor(browser())->GetColor(kColorToolbar));
EXPECT_EQ(original_otr_color,
GetColorProviderFor(incognito_browser)->GetColor(kColorToolbar));
}
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest, GetColorForToolbarButton) {
// This test relies on toolbar buttons having no tint, which is not currently
// true in dark mode when using the system theme.
ui::NativeTheme::GetInstanceForNativeUi()->set_use_dark_colors(false);
#if BUILDFLAG(IS_LINUX)
ui::LinuxUiGetter::set_instance(nullptr);
#endif
ui::NativeTheme::GetInstanceForNativeUi()->NotifyOnNativeThemeUpdated();
SkColor default_toolbar_button_color =
GetColorProviderFor(browser())->GetColor(kColorToolbarButtonIcon);
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(browser()->profile());
{
test::ThemeServiceChangedWaiter waiter(theme_service);
InstallExtension(
test_data_dir_.AppendASCII("theme_test_toolbar_button_color/"), 1);
waiter.WaitForThemeChanged();
}
// Should get a new color after installing a theme.
SkColor toolbar_button_explicit_color =
GetColorProviderFor(browser())->GetColor(kColorToolbarButtonIcon);
EXPECT_NE(toolbar_button_explicit_color, default_toolbar_button_color);
{
test::ThemeServiceChangedWaiter waiter(theme_service);
// ThemeService::OnThemeBuiltFromExtension will disable the previous theme
// but does so with tasks posted to the thread pool which will execute
// non-deterministically; thus, don't check the `expected_change` value.
InstallExtension(
test_data_dir_.AppendASCII("theme_test_toolbar_button_tint/"),
std::nullopt);
waiter.WaitForThemeChanged();
}
// Should get the color based on a tint.
SkColor toolbar_button_tinted_color =
GetColorProviderFor(browser())->GetColor(kColorToolbarButtonIcon);
EXPECT_NE(toolbar_button_tinted_color, default_toolbar_button_color);
EXPECT_NE(toolbar_button_tinted_color, toolbar_button_explicit_color);
}
// Test methods that involve resetting and updating multiple theme prefs. Ensure
// the final state is represented after only a single theme change event.
IN_PROC_BROWSER_TEST_F(ThemeServiceBrowserTest,
ThemeTransitionsEmitSingleNotification) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(browser()->profile());
// User color.
{
EXPECT_NE(SK_ColorGREEN, theme_service->GetUserColor());
test::ThemeServiceChangedWaiter waiter(theme_service);
theme_service->SetUserColor(SK_ColorGREEN);
waiter.WaitForThemeChanged();
EXPECT_EQ(SK_ColorGREEN, theme_service->GetUserColor());
}
// User color + color variant.
{
EXPECT_NE(SK_ColorRED, theme_service->GetUserColor());
EXPECT_NE(ui::mojom::BrowserColorVariant::kTonalSpot,
theme_service->GetBrowserColorVariant());
test::ThemeServiceChangedWaiter waiter(theme_service);
theme_service->SetUserColorAndBrowserColorVariant(
SK_ColorRED, ui::mojom::BrowserColorVariant::kTonalSpot);
waiter.WaitForThemeChanged();
EXPECT_EQ(SK_ColorRED, theme_service->GetUserColor());
EXPECT_EQ(ui::mojom::BrowserColorVariant::kTonalSpot,
theme_service->GetBrowserColorVariant());
}
// Grayscale.
{
EXPECT_FALSE(theme_service->GetIsGrayscale());
test::ThemeServiceChangedWaiter waiter(theme_service);
theme_service->SetIsGrayscale(true);
waiter.WaitForThemeChanged();
EXPECT_TRUE(theme_service->GetIsGrayscale());
}
}
} // namespace
|