blob: 4168f965971655ab3981d5091569a709bd04de1d [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 Murdochbb1529c2013-08-08 10:24:53 +01007#include "base/json/json_reader.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include "chrome/browser/extensions/extension_service_unittest.h"
Ben Murdoch558790d2013-07-30 15:19:42 +01009#include "chrome/browser/themes/custom_theme_supplier.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000010#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 Murdoch558790d2013-07-30 15:19:42 +010013#include "chrome/common/pref_names.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#include "chrome/test/base/testing_profile.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015#include "testing/gtest/include/gtest/gtest.h"
16
Ben Murdoch558790d2013-07-30 15:19:42 +010017namespace theme_service_internal {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019class ThemeServiceTest : public ExtensionServiceTestBase {
20 public:
21 ThemeServiceTest() {}
22 virtual ~ThemeServiceTest() {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023
Ben Murdochbb1529c2013-08-08 10:24:53 +010024 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)2a99a7e2013-03-28 15:31:22 +000038 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000039
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040 virtual void SetUp() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010041 ExtensionServiceTestBase::SetUp();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000042 InitializeEmptyExtensionService();
43 }
Ben Murdoch558790d2013-07-30 15:19:42 +010044
45 const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) {
46 return theme_service->get_theme_supplier();
47 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000048};
Torne (Richard Coles)58218062012-11-14 11:43:16 +000049
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000050// Installs then uninstalls a theme and makes sure that the ThemeService
51// reverts to the default theme after the uninstall.
52TEST_F(ThemeServiceTest, ThemeInstallUninstall) {
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010053 base::ScopedTempDir temp_dir;
54 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000055 ThemeService* theme_service =
56 ThemeServiceFactory::GetForProfile(profile_.get());
57 theme_service->UseDefaultTheme();
Ben Murdochbb1529c2013-08-08 10:24:53 +010058 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)90dce4d2013-05-29 14:40:03 +010062 base::MessageLoop::current()->RunUntilIdle();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063 EXPECT_FALSE(theme_service->UsingDefaultTheme());
Ben Murdochbb1529c2013-08-08 10:24:53 +010064 EXPECT_EQ(extension->id(), theme_service->GetThemeID());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010065
Ben Murdochbb1529c2013-08-08 10:24:53 +010066 // Now unload the extension, should revert to the default theme.
67 service_->UnloadExtension(extension->id(),
68 extension_misc::UNLOAD_REASON_UNINSTALL);
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010069 EXPECT_TRUE(theme_service->UsingDefaultTheme());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010070}
71
Ben Murdochbb1529c2013-08-08 10:24:53 +010072// Upgrades a theme and ensures that the ThemeService does not revert to the
73// default theme.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010074TEST_F(ThemeServiceTest, ThemeUpgrade) {
Ben Murdochbb1529c2013-08-08 10:24:53 +010075 base::ScopedTempDir temp_dir;
76 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010077 ThemeService* theme_service =
78 ThemeServiceFactory::GetForProfile(profile_.get());
79 theme_service->UseDefaultTheme();
Ben Murdochbb1529c2013-08-08 10:24:53 +010080 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)a36e5922013-08-05 13:57:33 +010084 base::MessageLoop::current()->RunUntilIdle();
Ben Murdochbb1529c2013-08-08 10:24:53 +010085 EXPECT_FALSE(theme_service->UsingDefaultTheme());
86 EXPECT_EQ(extension->id(), theme_service->GetThemeID());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010087
Ben Murdochbb1529c2013-08-08 10:24:53 +010088 // 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)58218062012-11-14 11:43:16 +000092}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000093
Ben Murdoch558790d2013-07-30 15:19:42 +010094// Checks that managed users have their own default theme.
95TEST_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.
109TEST_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