OLD | NEW |
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/json/json_reader.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/extension_service_unittest.h" | 10 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 11 #include "chrome/browser/extensions/unpacked_installer.h" |
9 #include "chrome/browser/themes/theme_service_factory.h" | 12 #include "chrome/browser/themes/theme_service_factory.h" |
| 13 #include "chrome/common/chrome_paths.h" |
10 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
11 #include "chrome/common/extensions/extension_manifest_constants.h" | |
12 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "content/public/test/test_utils.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
14 | 18 |
15 namespace { | 19 namespace { |
16 | 20 |
17 class ThemeServiceTest : public ExtensionServiceTestBase { | 21 class ThemeServiceTest : public ExtensionServiceTestBase { |
18 public: | 22 public: |
19 ThemeServiceTest() {} | 23 ThemeServiceTest() {} |
20 virtual ~ThemeServiceTest() {} | 24 virtual ~ThemeServiceTest() {} |
21 | 25 |
22 scoped_refptr<extensions::Extension> MakeThemeExtension(base::FilePath path) { | 26 // Moves a minimal theme to |temp_dir_path| and unpacks it from that |
23 DictionaryValue source; | 27 // directory. |
24 source.SetString(extension_manifest_keys::kName, "theme"); | 28 std::string LoadUnpackedThemeAt(base::FilePath temp_dir) { |
25 source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); | 29 base::FilePath dst_manifest_path = temp_dir.AppendASCII("manifest.json"); |
26 source.SetString(extension_manifest_keys::kUpdateURL, "http://foo.com"); | 30 base::FilePath test_data_dir; |
27 source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); | 31 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
28 std::string error; | 32 base::FilePath src_manifest_path = |
29 scoped_refptr<extensions::Extension> extension = | 33 test_data_dir.AppendASCII("extensions/theme_empty/manifest.json"); |
30 extensions::Extension::Create( | 34 EXPECT_TRUE(base::CopyFile(src_manifest_path, dst_manifest_path)); |
31 path, extensions::Manifest::EXTERNAL_PREF_DOWNLOAD, | 35 |
32 source, extensions::Extension::NO_FLAGS, &error); | 36 scoped_refptr<extensions::UnpackedInstaller> installer( |
33 EXPECT_TRUE(extension.get()); | 37 extensions::UnpackedInstaller::Create(service_)); |
34 EXPECT_EQ("", error); | 38 content::WindowedNotificationObserver observer( |
35 return extension; | 39 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 40 content::Source<Profile>(profile_.get())); |
| 41 installer->Load(temp_dir); |
| 42 observer.Wait(); |
| 43 |
| 44 std::string extension_id = |
| 45 content::Details<extensions::Extension>(observer.details())->id(); |
| 46 |
| 47 // Let the ThemeService finish creating the theme pack. |
| 48 base::MessageLoop::current()->RunUntilIdle(); |
| 49 |
| 50 return extension_id; |
| 51 } |
| 52 |
| 53 // Update the theme with |extension_id|. |
| 54 void UpdateUnpackedTheme(const std::string& extension_id) { |
| 55 int updated_notification = service_->IsExtensionEnabled(extension_id) ? |
| 56 chrome::NOTIFICATION_EXTENSION_LOADED : |
| 57 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED; |
| 58 |
| 59 const base::FilePath& path = |
| 60 service_->GetInstalledExtension(extension_id)->path(); |
| 61 |
| 62 scoped_refptr<extensions::UnpackedInstaller> installer( |
| 63 extensions::UnpackedInstaller::Create(service_)); |
| 64 content::WindowedNotificationObserver observer(updated_notification, |
| 65 content::Source<Profile>(profile_.get())); |
| 66 installer->Load(path); |
| 67 observer.Wait(); |
| 68 |
| 69 // Let the ThemeService finish creating the theme pack. |
| 70 base::MessageLoop::current()->RunUntilIdle(); |
36 } | 71 } |
37 | 72 |
38 virtual void SetUp() { | 73 virtual void SetUp() { |
39 ExtensionServiceTestBase::SetUp(); | 74 ExtensionServiceTestBase::SetUp(); |
40 InitializeEmptyExtensionService(); | 75 InitializeEmptyExtensionService(); |
41 } | 76 } |
42 }; | 77 }; |
43 | 78 |
44 // Installs then uninstalls a theme and makes sure that the ThemeService | 79 // Installs then uninstalls a theme and makes sure that the ThemeService |
45 // reverts to the default theme after the uninstall. | 80 // reverts to the default theme after the uninstall. |
46 TEST_F(ThemeServiceTest, ThemeInstallUninstall) { | 81 TEST_F(ThemeServiceTest, ThemeInstallUninstall) { |
47 base::ScopedTempDir temp_dir; | |
48 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
49 ThemeService* theme_service = | 82 ThemeService* theme_service = |
50 ThemeServiceFactory::GetForProfile(profile_.get()); | 83 ThemeServiceFactory::GetForProfile(profile_.get()); |
51 theme_service->UseDefaultTheme(); | 84 theme_service->UseDefaultTheme(); |
52 scoped_refptr<extensions::Extension> extension = | 85 // Let the ThemeService uninstall unused themes. |
53 MakeThemeExtension(temp_dir.path()); | |
54 service_->FinishInstallationForTest(extension.get()); | |
55 // Let ThemeService finish creating the theme pack. | |
56 base::MessageLoop::current()->RunUntilIdle(); | 86 base::MessageLoop::current()->RunUntilIdle(); |
| 87 |
| 88 base::ScopedTempDir temp_dir; |
| 89 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 90 const std::string& extension_id = LoadUnpackedThemeAt(temp_dir.path()); |
57 EXPECT_FALSE(theme_service->UsingDefaultTheme()); | 91 EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
58 EXPECT_EQ(extension->id(), theme_service->GetThemeID()); | 92 EXPECT_EQ(extension_id, theme_service->GetThemeID()); |
59 | 93 |
60 // Now unload the extension, should revert to the default theme. | 94 // Now uninstall the extension, should revert to the default theme. |
61 service_->UnloadExtension(extension->id(), | 95 service_->UninstallExtension(extension_id, false, NULL); |
62 extension_misc::UNLOAD_REASON_UNINSTALL); | |
63 EXPECT_TRUE(theme_service->UsingDefaultTheme()); | 96 EXPECT_TRUE(theme_service->UsingDefaultTheme()); |
64 } | 97 } |
65 | 98 |
66 // Upgrades a theme and ensures that the ThemeService does not revert to the | 99 // Test that a theme extension is disabled when not in use. A theme may be |
67 // default theme. | 100 // installed but not in use if it there is an infobar to revert to the previous |
68 TEST_F(ThemeServiceTest, ThemeUpgrade) { | 101 // theme. |
69 base::ScopedTempDir temp_dir; | 102 TEST_F(ThemeServiceTest, DisableUnusedTheme) { |
70 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
71 ThemeService* theme_service = | 103 ThemeService* theme_service = |
72 ThemeServiceFactory::GetForProfile(profile_.get()); | 104 ThemeServiceFactory::GetForProfile(profile_.get()); |
73 theme_service->UseDefaultTheme(); | 105 theme_service->UseDefaultTheme(); |
74 scoped_refptr<extensions::Extension> extension = | 106 // Let the ThemeService uninstall unused themes. |
75 MakeThemeExtension(temp_dir.path()); | |
76 service_->FinishInstallationForTest(extension.get()); | |
77 // Let ThemeService finish creating the theme pack. | |
78 base::MessageLoop::current()->RunUntilIdle(); | 107 base::MessageLoop::current()->RunUntilIdle(); |
| 108 |
| 109 base::ScopedTempDir temp_dir1; |
| 110 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir()); |
| 111 base::ScopedTempDir temp_dir2; |
| 112 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); |
| 113 |
| 114 // 1) Installing a theme should disable the previously active theme. |
| 115 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.path()); |
79 EXPECT_FALSE(theme_service->UsingDefaultTheme()); | 116 EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
80 EXPECT_EQ(extension->id(), theme_service->GetThemeID()); | 117 EXPECT_EQ(extension1_id, theme_service->GetThemeID()); |
| 118 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id)); |
81 | 119 |
82 // Now unload the extension, should revert to the default theme. | 120 // Show an infobar to prevent the current theme from being uninstalled. |
83 service_->UnloadExtension(extension->id(), | 121 theme_service->OnInfobarDisplayed(); |
84 extension_misc::UNLOAD_REASON_UPDATE); | 122 |
| 123 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.path()); |
| 124 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); |
| 125 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id)); |
| 126 EXPECT_TRUE(service_->GetExtensionById(extension1_id, |
| 127 ExtensionService::INCLUDE_DISABLED)); |
| 128 |
| 129 // 2) Enabling a disabled theme extension should swap the current theme. |
| 130 service_->EnableExtension(extension1_id); |
| 131 base::MessageLoop::current()->RunUntilIdle(); |
| 132 EXPECT_EQ(extension1_id, theme_service->GetThemeID()); |
| 133 EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id)); |
| 134 EXPECT_TRUE(service_->GetExtensionById(extension2_id, |
| 135 ExtensionService::INCLUDE_DISABLED)); |
| 136 |
| 137 // 3) Using SetTheme() with a disabled theme should enable and set the |
| 138 // theme. This is the case when the user reverts to the previous theme |
| 139 // via an infobar. |
| 140 const extensions::Extension* extension2 = |
| 141 service_->GetInstalledExtension(extension2_id); |
| 142 theme_service->SetTheme(extension2); |
| 143 base::MessageLoop::current()->RunUntilIdle(); |
| 144 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); |
| 145 EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id)); |
| 146 EXPECT_TRUE(service_->GetExtensionById(extension1_id, |
| 147 ExtensionService::INCLUDE_DISABLED)); |
| 148 |
| 149 // 4) Disabling the current theme extension should revert to the default theme |
| 150 // and uninstall any installed theme extensions. |
| 151 theme_service->OnInfobarDestroyed(); |
85 EXPECT_FALSE(theme_service->UsingDefaultTheme()); | 152 EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
| 153 service_->DisableExtension(extension2_id, |
| 154 extensions::Extension::DISABLE_USER_ACTION); |
| 155 base::MessageLoop::current()->RunUntilIdle(); |
| 156 EXPECT_TRUE(theme_service->UsingDefaultTheme()); |
| 157 EXPECT_FALSE(service_->GetInstalledExtension(extension1_id)); |
| 158 EXPECT_FALSE(service_->GetInstalledExtension(extension2_id)); |
| 159 } |
| 160 |
| 161 // Test the ThemeService's behavior when a theme is upgraded. |
| 162 TEST_F(ThemeServiceTest, ThemeUpgrade) { |
| 163 // Setup. |
| 164 ThemeService* theme_service = |
| 165 ThemeServiceFactory::GetForProfile(profile_.get()); |
| 166 theme_service->UseDefaultTheme(); |
| 167 // Let the ThemeService uninstall unused themes. |
| 168 base::MessageLoop::current()->RunUntilIdle(); |
| 169 |
| 170 theme_service->OnInfobarDisplayed(); |
| 171 |
| 172 base::ScopedTempDir temp_dir1; |
| 173 ASSERT_TRUE(temp_dir1.CreateUniqueTempDir()); |
| 174 base::ScopedTempDir temp_dir2; |
| 175 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir()); |
| 176 |
| 177 const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.path()); |
| 178 const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.path()); |
| 179 |
| 180 // Test the initial state. |
| 181 EXPECT_TRUE(service_->GetExtensionById(extension1_id, |
| 182 ExtensionService::INCLUDE_DISABLED)); |
| 183 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); |
| 184 |
| 185 // 1) Upgrading the current theme should not revert to the default theme. |
| 186 content::WindowedNotificationObserver theme_change_observer( |
| 187 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 188 content::Source<ThemeService>(theme_service)); |
| 189 UpdateUnpackedTheme(extension2_id); |
| 190 |
| 191 // The ThemeService should have sent an theme change notification even though |
| 192 // the id of the current theme did not change. |
| 193 theme_change_observer.Wait(); |
| 194 |
| 195 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); |
| 196 EXPECT_TRUE(service_->GetExtensionById(extension1_id, |
| 197 ExtensionService::INCLUDE_DISABLED)); |
| 198 |
| 199 // 2) Upgrading a disabled theme should not change the current theme. |
| 200 UpdateUnpackedTheme(extension1_id); |
| 201 EXPECT_EQ(extension2_id, theme_service->GetThemeID()); |
| 202 EXPECT_TRUE(service_->GetExtensionById(extension1_id, |
| 203 ExtensionService::INCLUDE_DISABLED)); |
86 } | 204 } |
87 | 205 |
88 }; // namespace | 206 }; // namespace |
OLD | NEW |