Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/themes/theme_service_unittest.cc

Issue 2799003002: Unpack theme data from extensions off of UI thread. (Closed)
Patch Set: fix gtk case Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/themes/theme_service.h" 5 #include "chrome/browser/themes/theme_service.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/test/scoped_task_environment.h"
13 #include "build/build_config.h" 14 #include "build/build_config.h"
14 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_service.h" 16 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_service_test_base.h" 17 #include "chrome/browser/extensions/extension_service_test_base.h"
17 #include "chrome/browser/extensions/unpacked_installer.h" 18 #include "chrome/browser/extensions/unpacked_installer.h"
18 #include "chrome/browser/themes/custom_theme_supplier.h" 19 #include "chrome/browser/themes/custom_theme_supplier.h"
19 #include "chrome/browser/themes/theme_properties.h" 20 #include "chrome/browser/themes/theme_properties.h"
20 #include "chrome/browser/themes/theme_service_factory.h" 21 #include "chrome/browser/themes/theme_service_factory.h"
21 #include "chrome/common/chrome_paths.h" 22 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/features.h" 23 #include "chrome/common/features.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 test_data_dir.AppendASCII("extensions/theme_minimal/manifest.json"); 71 test_data_dir.AppendASCII("extensions/theme_minimal/manifest.json");
71 EXPECT_TRUE(base::CopyFile(src_manifest_path, dst_manifest_path)); 72 EXPECT_TRUE(base::CopyFile(src_manifest_path, dst_manifest_path));
72 73
73 scoped_refptr<extensions::UnpackedInstaller> installer( 74 scoped_refptr<extensions::UnpackedInstaller> installer(
74 extensions::UnpackedInstaller::Create(service_)); 75 extensions::UnpackedInstaller::Create(service_));
75 extensions::TestExtensionRegistryObserver observer( 76 extensions::TestExtensionRegistryObserver observer(
76 ExtensionRegistry::Get(profile())); 77 ExtensionRegistry::Get(profile()));
77 installer->Load(temp_dir); 78 installer->Load(temp_dir);
78 std::string extension_id = observer.WaitForExtensionLoaded()->id(); 79 std::string extension_id = observer.WaitForExtensionLoaded()->id();
79 80
80 // Let the ThemeService finish creating the theme pack. 81 WaitForThemeInstall();
81 base::RunLoop().RunUntilIdle();
82 82
83 return extension_id; 83 return extension_id;
84 } 84 }
85 85
86 // Update the theme with |extension_id|. 86 // Update the theme with |extension_id|.
87 void UpdateUnpackedTheme(const std::string& extension_id) { 87 void UpdateUnpackedTheme(const std::string& extension_id) {
88 const base::FilePath& path = 88 const base::FilePath& path =
89 service_->GetInstalledExtension(extension_id)->path(); 89 service_->GetInstalledExtension(extension_id)->path();
90 90
91 scoped_refptr<extensions::UnpackedInstaller> installer( 91 scoped_refptr<extensions::UnpackedInstaller> installer(
(...skipping 12 matching lines...) Expand all
104 } 104 }
105 105
106 // Let the ThemeService finish creating the theme pack. 106 // Let the ThemeService finish creating the theme pack.
107 base::RunLoop().RunUntilIdle(); 107 base::RunLoop().RunUntilIdle();
108 } 108 }
109 109
110 const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) { 110 const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) {
111 return theme_service->get_theme_supplier(); 111 return theme_service->get_theme_supplier();
112 } 112 }
113 113
114 void WaitForThemeInstall() {
115 content::WindowedNotificationObserver theme_change_observer(
116 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
117 content::Source<ThemeService>(
118 ThemeServiceFactory::GetForProfile(profile())));
119 theme_change_observer.Wait();
120 }
121
114 // Alpha blends a non-opaque foreground color against an opaque background. 122 // Alpha blends a non-opaque foreground color against an opaque background.
115 // This is not the same as color_utils::AlphaBlend() since it gets the opacity 123 // This is not the same as color_utils::AlphaBlend() since it gets the opacity
116 // from the foreground color and then does not blend the two colors' alpha 124 // from the foreground color and then does not blend the two colors' alpha
117 // values together. 125 // values together.
118 static SkColor AlphaBlend(SkColor foreground, SkColor background) { 126 static SkColor AlphaBlend(SkColor foreground, SkColor background) {
119 return color_utils::AlphaBlend(SkColorSetA(foreground, SK_AlphaOPAQUE), 127 return color_utils::AlphaBlend(SkColorSetA(foreground, SK_AlphaOPAQUE),
120 background, SkColorGetA(foreground)); 128 background, SkColorGetA(foreground));
121 } 129 }
122 130
123 // Returns the separator color as the opaque result of blending it atop the 131 // Returns the separator color as the opaque result of blending it atop the
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 EXPECT_TRUE(theme_service->UsingDefaultTheme()); 164 EXPECT_TRUE(theme_service->UsingDefaultTheme());
157 } 165 }
158 166
159 // Test that a theme extension is disabled when not in use. A theme may be 167 // Test that a theme extension is disabled when not in use. A theme may be
160 // installed but not in use if it there is an infobar to revert to the previous 168 // installed but not in use if it there is an infobar to revert to the previous
161 // theme. 169 // theme.
162 TEST_F(ThemeServiceTest, DisableUnusedTheme) { 170 TEST_F(ThemeServiceTest, DisableUnusedTheme) {
163 ThemeService* theme_service = 171 ThemeService* theme_service =
164 ThemeServiceFactory::GetForProfile(profile_.get()); 172 ThemeServiceFactory::GetForProfile(profile_.get());
165 theme_service->UseDefaultTheme(); 173 theme_service->UseDefaultTheme();
166 // Let the ThemeService uninstall unused themes.
167 base::RunLoop().RunUntilIdle();
168 174
169 base::ScopedTempDir temp_dir1; 175 base::ScopedTempDir temp_dir1;
170 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir()); 176 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir());
171 base::ScopedTempDir temp_dir2; 177 base::ScopedTempDir temp_dir2;
172 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); 178 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
173 179
174 // 1) Installing a theme should disable the previously active theme. 180 // 1) Installing a theme should disable the previously active theme.
175 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.GetPath()); 181 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.GetPath());
176 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 182 EXPECT_FALSE(theme_service->UsingDefaultTheme());
177 EXPECT_EQ(extension1_id, theme_service->GetThemeID()); 183 EXPECT_EQ(extension1_id, theme_service->GetThemeID());
178 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id)); 184 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id));
179 185
180 // Show an infobar to prevent the current theme from being uninstalled. 186 // Show an infobar to prevent the current theme from being uninstalled.
181 theme_service->OnInfobarDisplayed(); 187 theme_service->OnInfobarDisplayed();
182 188
183 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.GetPath()); 189 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.GetPath());
184 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); 190 EXPECT_EQ(extension2_id, theme_service->GetThemeID());
185 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id)); 191 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id));
186 EXPECT_TRUE(registry_->GetExtensionById(extension1_id, 192 EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
187 ExtensionRegistry::DISABLED)); 193 ExtensionRegistry::DISABLED));
188 194
189 // 2) Enabling a disabled theme extension should swap the current theme. 195 // 2) Enabling a disabled theme extension should swap the current theme.
190 service_->EnableExtension(extension1_id); 196 service_->EnableExtension(extension1_id);
191 base::RunLoop().RunUntilIdle(); 197 WaitForThemeInstall();
192 EXPECT_EQ(extension1_id, theme_service->GetThemeID()); 198 EXPECT_EQ(extension1_id, theme_service->GetThemeID());
193 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id)); 199 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id));
194 EXPECT_TRUE(registry_->GetExtensionById(extension2_id, 200 EXPECT_TRUE(registry_->GetExtensionById(extension2_id,
195 ExtensionRegistry::DISABLED)); 201 ExtensionRegistry::DISABLED));
196 202
197 // 3) Using SetTheme() with a disabled theme should enable and set the 203 // 3) Using RevertToTheme() with a disabled theme should enable and set the
198 // theme. This is the case when the user reverts to the previous theme 204 // theme. This is the case when the user reverts to the previous theme
199 // via an infobar. 205 // via an infobar.
200 const extensions::Extension* extension2 = 206 const extensions::Extension* extension2 =
201 service_->GetInstalledExtension(extension2_id); 207 service_->GetInstalledExtension(extension2_id);
202 theme_service->SetTheme(extension2); 208 theme_service->RevertToTheme(extension2);
203 base::RunLoop().RunUntilIdle(); 209 WaitForThemeInstall();
204 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); 210 EXPECT_EQ(extension2_id, theme_service->GetThemeID());
205 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id)); 211 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id));
206 EXPECT_TRUE(registry_->GetExtensionById(extension1_id, 212 EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
207 ExtensionRegistry::DISABLED)); 213 ExtensionRegistry::DISABLED));
208 214
209 // 4) Disabling the current theme extension should revert to the default theme 215 // 4) Disabling the current theme extension should revert to the default theme
210 // and uninstall any installed theme extensions. 216 // and uninstall any installed theme extensions.
211 theme_service->OnInfobarDestroyed(); 217 theme_service->OnInfobarDestroyed();
212 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 218 EXPECT_FALSE(theme_service->UsingDefaultTheme());
213 service_->DisableExtension(extension2_id, 219 service_->DisableExtension(extension2_id,
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir()); 339 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir());
334 base::ScopedTempDir temp_dir2; 340 base::ScopedTempDir temp_dir2;
335 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); 341 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
336 342
337 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.GetPath()); 343 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.GetPath());
338 ASSERT_EQ(extension1_id, theme_service->GetThemeID()); 344 ASSERT_EQ(extension1_id, theme_service->GetThemeID());
339 345
340 // Show an infobar. 346 // Show an infobar.
341 theme_service->OnInfobarDisplayed(); 347 theme_service->OnInfobarDisplayed();
342 348
343 // Install another theme. Emulate the infobar destroying itself (and 349 // Install another theme. The first extension shouldn't be uninstalled yet as
344 // causing unused themes to be uninstalled) as a result of the 350 // it should be possible to revert to it. Emulate the infobar destroying
345 // NOTIFICATION_BROWSER_THEME_CHANGED notification. 351 // itself as a result of the NOTIFICATION_BROWSER_THEME_CHANGED notification.
346 { 352 {
347 InfobarDestroyerOnThemeChange destroyer(profile_.get()); 353 InfobarDestroyerOnThemeChange destroyer(profile_.get());
348 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.GetPath()); 354 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.GetPath());
349 ASSERT_EQ(extension2_id, theme_service->GetThemeID()); 355 EXPECT_EQ(extension2_id, theme_service->GetThemeID());
350 ASSERT_FALSE(service_->GetInstalledExtension(extension1_id));
351 } 356 }
352 357
358 auto* extension1 = service_->GetInstalledExtension(extension1_id);
359 ASSERT_TRUE(extension1);
360
353 // Check that it is possible to reinstall extension1. 361 // Check that it is possible to reinstall extension1.
354 ASSERT_EQ(extension1_id, LoadUnpackedThemeAt(temp_dir1.GetPath())); 362 ThemeServiceFactory::GetForProfile(profile_.get())->RevertToTheme(extension1);
363 WaitForThemeInstall();
355 EXPECT_EQ(extension1_id, theme_service->GetThemeID()); 364 EXPECT_EQ(extension1_id, theme_service->GetThemeID());
356 } 365 }
357 366
358 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) 367 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
359 class ThemeServiceSupervisedUserTest : public ThemeServiceTest { 368 class ThemeServiceSupervisedUserTest : public ThemeServiceTest {
360 public: 369 public:
361 ThemeServiceSupervisedUserTest() {} 370 ThemeServiceSupervisedUserTest() {}
362 ~ThemeServiceSupervisedUserTest() override {} 371 ~ThemeServiceSupervisedUserTest() override {}
363 372
364 void SetUp() override { 373 void SetUp() override {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 // it should still be lighter than the tab color. 506 // it should still be lighter than the tab color.
498 separator_color = GetSeparatorColor(tab_color, SK_ColorWHITE); 507 separator_color = GetSeparatorColor(tab_color, SK_ColorWHITE);
499 separator_luminance = color_utils::GetRelativeLuminance(separator_color); 508 separator_luminance = color_utils::GetRelativeLuminance(separator_color);
500 EXPECT_LT(separator_luminance, 1); 509 EXPECT_LT(separator_luminance, 1);
501 EXPECT_GT(separator_luminance, tab_luminance); 510 EXPECT_GT(separator_luminance, tab_luminance);
502 } 511 }
503 } 512 }
504 #endif // !defined(OS_MACOSX) 513 #endif // !defined(OS_MACOSX)
505 514
506 }; // namespace theme_service_internal 515 }; // namespace theme_service_internal
OLDNEW
« no previous file with comments | « chrome/browser/themes/theme_service_browsertest.cc ('k') | chrome/browser/ui/extensions/extension_install_ui_default.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698