Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/themes/theme_service.h" |
| 6 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 7 | #include "base/json/json_reader.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_service_unittest.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 9 | #include "chrome/browser/themes/custom_theme_supplier.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "chrome/browser/themes/theme_service_factory.h" |
| 11 | #include "chrome/common/extensions/extension.h" |
| 12 | #include "chrome/common/extensions/extension_manifest_constants.h" |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 13 | #include "chrome/common/pref_names.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "chrome/test/base/testing_profile.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 17 | namespace theme_service_internal { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | class ThemeServiceTest : public ExtensionServiceTestBase { |
| 20 | public: |
| 21 | ThemeServiceTest() {} |
| 22 | virtual ~ThemeServiceTest() {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 24 | scoped_refptr<extensions::Extension> MakeThemeExtension(base::FilePath path) { |
| 25 | DictionaryValue source; |
| 26 | source.SetString(extension_manifest_keys::kName, "theme"); |
| 27 | source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); |
| 28 | source.SetString(extension_manifest_keys::kUpdateURL, "http://foo.com"); |
| 29 | source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); |
| 30 | std::string error; |
| 31 | scoped_refptr<extensions::Extension> extension = |
| 32 | extensions::Extension::Create( |
| 33 | path, extensions::Manifest::EXTERNAL_PREF_DOWNLOAD, |
| 34 | source, extensions::Extension::NO_FLAGS, &error); |
| 35 | EXPECT_TRUE(extension.get()); |
| 36 | EXPECT_EQ("", error); |
| 37 | return extension; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 38 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 39 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 40 | virtual void SetUp() { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 41 | ExtensionServiceTestBase::SetUp(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | InitializeEmptyExtensionService(); |
| 43 | } |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 44 | |
| 45 | const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) { |
| 46 | return theme_service->get_theme_supplier(); |
| 47 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | }; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 49 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | // Installs then uninstalls a theme and makes sure that the ThemeService |
| 51 | // reverts to the default theme after the uninstall. |
| 52 | TEST_F(ThemeServiceTest, ThemeInstallUninstall) { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 53 | base::ScopedTempDir temp_dir; |
| 54 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | ThemeService* theme_service = |
| 56 | ThemeServiceFactory::GetForProfile(profile_.get()); |
| 57 | theme_service->UseDefaultTheme(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 58 | scoped_refptr<extensions::Extension> extension = |
| 59 | MakeThemeExtension(temp_dir.path()); |
| 60 | service_->FinishInstallationForTest(extension.get()); |
| 61 | // Let ThemeService finish creating the theme pack. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 62 | base::MessageLoop::current()->RunUntilIdle(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 63 | EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 64 | EXPECT_EQ(extension->id(), theme_service->GetThemeID()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 65 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 66 | // Now unload the extension, should revert to the default theme. |
| 67 | service_->UnloadExtension(extension->id(), |
| 68 | extension_misc::UNLOAD_REASON_UNINSTALL); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 69 | EXPECT_TRUE(theme_service->UsingDefaultTheme()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 72 | // Upgrades a theme and ensures that the ThemeService does not revert to the |
| 73 | // default theme. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 74 | TEST_F(ThemeServiceTest, ThemeUpgrade) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 75 | base::ScopedTempDir temp_dir; |
| 76 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 77 | ThemeService* theme_service = |
| 78 | ThemeServiceFactory::GetForProfile(profile_.get()); |
| 79 | theme_service->UseDefaultTheme(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 80 | scoped_refptr<extensions::Extension> extension = |
| 81 | MakeThemeExtension(temp_dir.path()); |
| 82 | service_->FinishInstallationForTest(extension.get()); |
| 83 | // Let ThemeService finish creating the theme pack. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 84 | base::MessageLoop::current()->RunUntilIdle(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 85 | EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
| 86 | EXPECT_EQ(extension->id(), theme_service->GetThemeID()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 87 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 88 | // Now unload the extension, should revert to the default theme. |
| 89 | service_->UnloadExtension(extension->id(), |
| 90 | extension_misc::UNLOAD_REASON_UPDATE); |
| 91 | EXPECT_FALSE(theme_service->UsingDefaultTheme()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 92 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 93 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 94 | // Checks that managed users have their own default theme. |
| 95 | TEST_F(ThemeServiceTest, ManagedUserThemeReplacesDefaultTheme) { |
| 96 | profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); |
| 97 | ThemeService* theme_service = |
| 98 | ThemeServiceFactory::GetForProfile(profile_.get()); |
| 99 | theme_service->UseDefaultTheme(); |
| 100 | EXPECT_TRUE(theme_service->UsingDefaultTheme()); |
| 101 | EXPECT_TRUE(get_theme_supplier(theme_service)); |
| 102 | EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(), |
| 103 | CustomThemeSupplier::MANAGED_USER_THEME); |
| 104 | } |
| 105 | |
| 106 | #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 107 | // Checks that managed users don't use the system theme even if it is the |
| 108 | // default. The system theme is only available on Linux. |
| 109 | TEST_F(ThemeServiceTest, ManagedUserThemeReplacesNativeTheme) { |
| 110 | profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); |
| 111 | profile_->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true); |
| 112 | ThemeService* theme_service = |
| 113 | ThemeServiceFactory::GetForProfile(profile_.get()); |
| 114 | theme_service->UseDefaultTheme(); |
| 115 | EXPECT_TRUE(theme_service->UsingDefaultTheme()); |
| 116 | EXPECT_TRUE(get_theme_supplier(theme_service)); |
| 117 | EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(), |
| 118 | CustomThemeSupplier::MANAGED_USER_THEME); |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | }; // namespace theme_service_internal |