This source file includes following definitions.
- registry_
- LoadUnpackedThemeAt
- UpdateUnpackedTheme
- SetUp
- get_theme_supplier
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TEST_F
- TEST_F
#include "chrome/browser/themes/theme_service.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/managed_mode/managed_user_service.h"
#include "chrome/browser/managed_mode/managed_user_service_factory.h"
#include "chrome/browser/themes/custom_theme_supplier.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "testing/gtest/include/gtest/gtest.h"
using extensions::ExtensionRegistry;
namespace theme_service_internal {
class ThemeServiceTest : public ExtensionServiceTestBase {
public:
ThemeServiceTest() : is_managed_(false),
registry_(NULL) {}
virtual ~ThemeServiceTest() {}
std::string LoadUnpackedThemeAt(const base::FilePath& temp_dir) {
base::FilePath dst_manifest_path = temp_dir.AppendASCII("manifest.json");
base::FilePath test_data_dir;
EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
base::FilePath src_manifest_path =
test_data_dir.AppendASCII("extensions/theme_minimal/manifest.json");
EXPECT_TRUE(base::CopyFile(src_manifest_path, dst_manifest_path));
scoped_refptr<extensions::UnpackedInstaller> installer(
extensions::UnpackedInstaller::Create(service_));
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_.get()));
installer->Load(temp_dir);
observer.Wait();
std::string extension_id =
content::Details<extensions::Extension>(observer.details())->id();
base::MessageLoop::current()->RunUntilIdle();
return extension_id;
}
void UpdateUnpackedTheme(const std::string& extension_id) {
int updated_notification = service_->IsExtensionEnabled(extension_id) ?
chrome::NOTIFICATION_EXTENSION_LOADED :
chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED;
const base::FilePath& path =
service_->GetInstalledExtension(extension_id)->path();
scoped_refptr<extensions::UnpackedInstaller> installer(
extensions::UnpackedInstaller::Create(service_));
content::WindowedNotificationObserver observer(updated_notification,
content::Source<Profile>(profile_.get()));
installer->Load(path);
observer.Wait();
base::MessageLoop::current()->RunUntilIdle();
}
virtual void SetUp() {
ExtensionServiceTestBase::SetUp();
ExtensionServiceTestBase::ExtensionServiceInitParams params =
CreateDefaultInitParams();
params.profile_is_managed = is_managed_;
InitializeExtensionService(params);
service_->Init();
registry_ = ExtensionRegistry::Get(profile_.get());
ASSERT_TRUE(registry_);
}
const CustomThemeSupplier* get_theme_supplier(ThemeService* theme_service) {
return theme_service->get_theme_supplier();
}
protected:
bool is_managed_;
ExtensionRegistry* registry_;
};
TEST_F(ThemeServiceTest, ThemeInstallUninstall) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(profile_.get());
theme_service->UseDefaultTheme();
base::MessageLoop::current()->RunUntilIdle();
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const std::string& extension_id = LoadUnpackedThemeAt(temp_dir.path());
EXPECT_FALSE(theme_service->UsingDefaultTheme());
EXPECT_EQ(extension_id, theme_service->GetThemeID());
service_->UninstallExtension(extension_id, false, NULL);
EXPECT_TRUE(theme_service->UsingDefaultTheme());
}
TEST_F(ThemeServiceTest, DisableUnusedTheme) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(profile_.get());
theme_service->UseDefaultTheme();
base::MessageLoop::current()->RunUntilIdle();
base::ScopedTempDir temp_dir1;
ASSERT_TRUE(temp_dir1.CreateUniqueTempDir());
base::ScopedTempDir temp_dir2;
ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.path());
EXPECT_FALSE(theme_service->UsingDefaultTheme());
EXPECT_EQ(extension1_id, theme_service->GetThemeID());
EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id));
theme_service->OnInfobarDisplayed();
const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.path());
EXPECT_EQ(extension2_id, theme_service->GetThemeID());
EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id));
EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
ExtensionRegistry::DISABLED));
service_->EnableExtension(extension1_id);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(extension1_id, theme_service->GetThemeID());
EXPECT_TRUE(service_->IsExtensionEnabled(extension1_id));
EXPECT_TRUE(registry_->GetExtensionById(extension2_id,
ExtensionRegistry::DISABLED));
const extensions::Extension* extension2 =
service_->GetInstalledExtension(extension2_id);
theme_service->SetTheme(extension2);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(extension2_id, theme_service->GetThemeID());
EXPECT_TRUE(service_->IsExtensionEnabled(extension2_id));
EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
ExtensionRegistry::DISABLED));
theme_service->OnInfobarDestroyed();
EXPECT_FALSE(theme_service->UsingDefaultTheme());
service_->DisableExtension(extension2_id,
extensions::Extension::DISABLE_USER_ACTION);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(theme_service->UsingDefaultTheme());
EXPECT_FALSE(service_->GetInstalledExtension(extension1_id));
EXPECT_FALSE(service_->GetInstalledExtension(extension2_id));
}
TEST_F(ThemeServiceTest, ThemeUpgrade) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(profile_.get());
theme_service->UseDefaultTheme();
base::MessageLoop::current()->RunUntilIdle();
theme_service->OnInfobarDisplayed();
base::ScopedTempDir temp_dir1;
ASSERT_TRUE(temp_dir1.CreateUniqueTempDir());
base::ScopedTempDir temp_dir2;
ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
const std::string& extension1_id = LoadUnpackedThemeAt(temp_dir1.path());
const std::string& extension2_id = LoadUnpackedThemeAt(temp_dir2.path());
EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
ExtensionRegistry::DISABLED));
EXPECT_EQ(extension2_id, theme_service->GetThemeID());
content::WindowedNotificationObserver theme_change_observer(
chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>(theme_service));
UpdateUnpackedTheme(extension2_id);
theme_change_observer.Wait();
EXPECT_EQ(extension2_id, theme_service->GetThemeID());
EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
ExtensionRegistry::DISABLED));
UpdateUnpackedTheme(extension1_id);
EXPECT_EQ(extension2_id, theme_service->GetThemeID());
EXPECT_TRUE(registry_->GetExtensionById(extension1_id,
ExtensionRegistry::DISABLED));
}
class ThemeServiceManagedUserTest : public ThemeServiceTest {
public:
ThemeServiceManagedUserTest() {}
virtual ~ThemeServiceManagedUserTest() {}
virtual void SetUp() OVERRIDE {
is_managed_ = true;
ThemeServiceTest::SetUp();
}
};
TEST_F(ThemeServiceManagedUserTest, ManagedUserThemeReplacesDefaultTheme) {
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(profile_.get());
theme_service->UseDefaultTheme();
EXPECT_TRUE(theme_service->UsingDefaultTheme());
EXPECT_TRUE(get_theme_supplier(theme_service));
EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(),
CustomThemeSupplier::MANAGED_USER_THEME);
}
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
TEST_F(ThemeServiceManagedUserTest, ManagedUserThemeReplacesNativeTheme) {
profile_->GetPrefs()->SetBoolean(prefs::kUsesSystemTheme, true);
ThemeService* theme_service =
ThemeServiceFactory::GetForProfile(profile_.get());
theme_service->UseDefaultTheme();
EXPECT_TRUE(theme_service->UsingDefaultTheme());
EXPECT_TRUE(get_theme_supplier(theme_service));
EXPECT_EQ(get_theme_supplier(theme_service)->get_theme_type(),
CustomThemeSupplier::MANAGED_USER_THEME);
}
#endif
};