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 7#include "base/json/json_reader.h" 8#include "chrome/browser/extensions/extension_service_unittest.h" 9#include "chrome/browser/managed_mode/managed_user_service.h" 10#include "chrome/browser/managed_mode/managed_user_service_factory.h" 11#include "chrome/browser/themes/custom_theme_supplier.h" 12#include "chrome/browser/themes/theme_service_factory.h" 13#include "chrome/common/extensions/extension.h" 14#include "chrome/common/extensions/extension_manifest_constants.h" 15#include "chrome/common/pref_names.h" 16#include "chrome/test/base/testing_browser_process.h" 17#include "chrome/test/base/testing_profile.h" 18#include "chrome/test/base/testing_profile_manager.h" 19#include "testing/gtest/include/gtest/gtest.h" 20 21namespace theme_service_internal { 22 23class ThemeServiceTest : public ExtensionServiceTestBase { 24 public: 25 ThemeServiceTest() { 26 manager_.reset( 27 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); 28 } 29 virtual ~ThemeServiceTest() {} 30 31 scoped_refptr<extensions::Extension> MakeThemeExtension(base::FilePath path) { 32 DictionaryValue source; 33 source.SetString(extension_manifest_keys::kName, "theme"); 34 source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); 35 source.SetString(extension_manifest_keys::kUpdateURL, "http://foo.com"); 36 source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); 37 std::string error; 38 scoped_refptr<extensions::Extension> extension = 39 extensions::Extension::Create( 40 path, extensions::Manifest::EXTERNAL_PREF_DOWNLOAD, 41 source, extensions::Extension::NO_FLAGS, &error); 42 EXPECT_TRUE(extension.get()); 43 EXPECT_EQ("", error); 44 return extension; 45 } 46 47 virtual void SetUp() { 48 ExtensionServiceTestBase::SetUp(); 49 InitializeEmptyExtensionService(); 50 bool success = manager_->SetUp(); 51 ASSERT_TRUE(success); 52 } 53 54 const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) { 55 return theme_service->get_theme_supplier(); 56 } 57 58 TestingProfileManager* manager() { 59 return manager_.get(); 60 } 61 62 private: 63 scoped_ptr<TestingProfileManager> manager_; 64}; 65 66// Installs then uninstalls a theme and makes sure that the ThemeService 67// reverts to the default theme after the uninstall. 68TEST_F(ThemeServiceTest, ThemeInstallUninstall) { 69 base::ScopedTempDir temp_dir; 70 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 71 ThemeService* theme_service = 72 ThemeServiceFactory::GetForProfile(profile_.get()); 73 theme_service->UseDefaultTheme(); 74 scoped_refptr<extensions::Extension> extension = 75 MakeThemeExtension(temp_dir.path()); 76 service_->FinishInstallationForTest(extension.get()); 77 // Let ThemeService finish creating the theme pack. 78 base::MessageLoop::current()->RunUntilIdle(); 79 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 80 EXPECT_EQ(extension->id(), theme_service->GetThemeID()); 81 82 // Now unload the extension, should revert to the default theme. 83 service_->UnloadExtension(extension->id(), 84 extension_misc::UNLOAD_REASON_UNINSTALL); 85 EXPECT_TRUE(theme_service->UsingDefaultTheme()); 86} 87 88// Upgrades a theme and ensures that the ThemeService does not revert to the 89// default theme. 90TEST_F(ThemeServiceTest, ThemeUpgrade) { 91 base::ScopedTempDir temp_dir; 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 93 ThemeService* theme_service = 94 ThemeServiceFactory::GetForProfile(profile_.get()); 95 theme_service->UseDefaultTheme(); 96 scoped_refptr<extensions::Extension> extension = 97 MakeThemeExtension(temp_dir.path()); 98 service_->FinishInstallationForTest(extension.get()); 99 // Let ThemeService finish creating the theme pack. 100 base::MessageLoop::current()->RunUntilIdle(); 101 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 102 EXPECT_EQ(extension->id(), theme_service->GetThemeID()); 103 104 // Now unload the extension, should revert to the default theme. 105 service_->UnloadExtension(extension->id(), 106 extension_misc::UNLOAD_REASON_UPDATE); 107 EXPECT_FALSE(theme_service->UsingDefaultTheme()); 108} 109 110// Checks that managed users have their own default theme. 111TEST_F(ThemeServiceTest, ManagedUserThemeReplacesDefaultTheme) { 112 profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); 113 ThemeService* theme_service = 114 ThemeServiceFactory::GetForProfile(profile_.get()); 115 theme_service->UseDefaultTheme(); 116 EXPECT_TRUE(theme_service->UsingDefaultTheme()); 117 EXPECT_TRUE(get_theme_supplier(theme_service)); 118 EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(), 119 CustomThemeSupplier::MANAGED_USER_THEME); 120} 121 122TEST_F(ThemeServiceTest, ManagedUserThemeNewUser) { 123 TestingProfile* profile = manager()->CreateTestingProfile("mu"); 124 // Simulate the current initialization behavior: first the ThemeService is 125 // created, then the supervised user profile is initialized. 126 ThemeService* theme_service = 127 ThemeServiceFactory::GetForProfile(profile); 128 ManagedUserServiceFactory::GetForProfile(profile)->InitForTesting(); 129 EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(), 130 CustomThemeSupplier::MANAGED_USER_THEME); 131 manager()->DeleteTestingProfile("mu"); 132} 133 134#if defined(OS_LINUX) && !defined(OS_CHROMEOS) 135// Checks that managed users don't use the system theme even if it is the 136// default. The system theme is only available on Linux. 137TEST_F(ThemeServiceTest, ManagedUserThemeReplacesNativeTheme) { 138 profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); 139 profile_->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true); 140 ThemeService* theme_service = 141 ThemeServiceFactory::GetForProfile(profile_.get()); 142 theme_service->UseDefaultTheme(); 143 EXPECT_TRUE(theme_service->UsingDefaultTheme()); 144 EXPECT_TRUE(get_theme_supplier(theme_service)); 145 EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(), 146 CustomThemeSupplier::MANAGED_USER_THEME); 147} 148#endif 149 150}; // namespace theme_service_internal 151