1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/test/theme_service_changed_waiter.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/crx_file/id_util.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/app_sorting.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
using content::WebContents;
using extensions::AppSorting;
using extensions::Extension;
class ExtensionInstallUIBrowserTest : public extensions::ExtensionBrowserTest {
public:
ExtensionInstallUIBrowserTest() = default;
ExtensionInstallUIBrowserTest(const ExtensionInstallUIBrowserTest&) = delete;
ExtensionInstallUIBrowserTest& operator=(
const ExtensionInstallUIBrowserTest&) = delete;
~ExtensionInstallUIBrowserTest() override = default;
// Checks that a theme info bar is currently visible and issues an undo to
// revert to the previous theme.
void VerifyThemeInfoBarAndUndoInstall() {
WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
infobars::ContentInfoBarManager* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(web_contents);
ASSERT_EQ(1U, infobar_manager->infobars().size());
ConfirmInfoBarDelegate* delegate =
infobar_manager->infobars()[0]->delegate()->AsConfirmInfoBarDelegate();
ASSERT_TRUE(delegate);
delegate->Cancel();
WaitForThemeChange();
ASSERT_EQ(0U, infobar_manager->infobars().size());
}
// Install the given theme from the data dir and verify expected name.
void InstallThemeAndVerify(const char* theme_name,
const std::string& expected_name) {
const base::FilePath theme_path = test_data_dir_.AppendASCII(theme_name);
const bool theme_exists = GetTheme();
// Themes install asynchronously so we must check the number of enabled
// extensions after theme install completes.
size_t num_before = extensions::ExtensionRegistry::Get(profile())
->enabled_extensions()
.size();
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_path, 1));
WaitForThemeChange();
size_t num_after = extensions::ExtensionRegistry::Get(profile())
->enabled_extensions()
.size();
// If a theme was already installed, we're just swapping one for another, so
// no change in extension count.
EXPECT_EQ(num_before + (theme_exists ? 0 : 1), num_after);
const Extension* theme = GetTheme();
ASSERT_TRUE(theme);
ASSERT_EQ(theme->name(), expected_name);
}
const Extension* GetTheme() const {
return ThemeServiceFactory::GetThemeForProfile(browser()->profile());
}
void WaitForThemeChange() {
test::ThemeServiceChangedWaiter waiter(
ThemeServiceFactory::GetForProfile(browser()->profile()));
waiter.WaitForThemeChanged();
}
};
// Fails on Linux and Windows (http://crbug.com/580907).
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
DISABLED_TestThemeInstallUndoResetsToDefault) {
// Install theme once and undo to verify we go back to default theme.
base::FilePath theme_crx = PackExtension(test_data_dir_.AppendASCII("theme"));
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx, 1));
WaitForThemeChange();
const Extension* theme = GetTheme();
ASSERT_TRUE(theme);
std::string theme_id = theme->id();
VerifyThemeInfoBarAndUndoInstall();
ASSERT_EQ(nullptr, GetTheme());
// Set the same theme twice and undo to verify we go back to default theme.
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx, 0));
WaitForThemeChange();
theme = GetTheme();
ASSERT_TRUE(theme);
ASSERT_EQ(theme_id, theme->id());
ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx, 0));
WaitForThemeChange();
theme = GetTheme();
ASSERT_TRUE(theme);
ASSERT_EQ(theme_id, theme->id());
VerifyThemeInfoBarAndUndoInstall();
ASSERT_EQ(nullptr, GetTheme());
}
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
TestThemeInstallUndoResetsToPreviousTheme) {
// Install first theme.
InstallThemeAndVerify("theme", "camo theme");
const Extension* theme = GetTheme();
std::string theme_id = theme->id();
// Then install second theme.
InstallThemeAndVerify("theme2", "snowflake theme");
const Extension* theme2 = GetTheme();
EXPECT_NE(theme_id, theme2->id());
// Undo second theme will revert to first theme.
VerifyThemeInfoBarAndUndoInstall();
EXPECT_EQ(theme, GetTheme());
}
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
TestThemeReset) {
InstallThemeAndVerify("theme", "camo theme");
// Reset to default theme.
ThemeServiceFactory::GetForProfile(browser()->profile())->UseDefaultTheme();
ASSERT_FALSE(GetTheme());
}
// Flaky (http://crbug.com/851252).
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
DISABLED_TestInstallThemeInFullScreen) {
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FULLSCREEN));
InstallThemeAndVerify("theme", "camo theme");
}
|