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.
5 #include "base/command_line.h"
6 #include "base/strings/string_util.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/themes/theme_service.h"
14 #include "chrome/browser/themes/theme_service_factory.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/test/base/test_switches.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/infobars/core/confirm_infobar_delegate.h"
24 #include "components/infobars/core/infobar.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "extensions/browser/app_sorting.h"
28 #include "extensions/browser/extension_prefs.h"
29 #include "extensions/common/id_util.h"
31 using content::WebContents
;
32 using extensions::AppSorting
;
33 using extensions::Extension
;
35 class ExtensionInstallUIBrowserTest
: public ExtensionBrowserTest
{
37 // Checks that a theme info bar is currently visible and issues an undo to
38 // revert to the previous theme.
39 void VerifyThemeInfoBarAndUndoInstall() {
40 WebContents
* web_contents
=
41 browser()->tab_strip_model()->GetActiveWebContents();
42 ASSERT_TRUE(web_contents
);
43 InfoBarService
* infobar_service
=
44 InfoBarService::FromWebContents(web_contents
);
45 ASSERT_EQ(1U, infobar_service
->infobar_count());
46 ConfirmInfoBarDelegate
* delegate
=
47 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
48 ASSERT_TRUE(delegate
);
50 ASSERT_EQ(0U, infobar_service
->infobar_count());
53 // Install the given theme from the data dir and verify expected name.
54 void InstallThemeAndVerify(const char* theme_name
,
55 const std::string
& expected_name
) {
56 // If there is already a theme installed, the current theme should be
57 // disabled and the new one installed + enabled.
58 int expected_change
= GetTheme() ? 0 : 1;
59 const base::FilePath theme_path
= test_data_dir_
.AppendASCII(theme_name
);
60 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_path
, expected_change
,
62 const Extension
* theme
= GetTheme();
64 ASSERT_EQ(theme
->name(), expected_name
);
67 const Extension
* GetTheme() const {
68 return ThemeServiceFactory::GetThemeForProfile(browser()->profile());
73 // Fails consistently on bot chromium.chromiumos \ Linux.
74 // See: http://crbug.com/120647.
75 #define MAYBE_TestThemeInstallUndoResetsToDefault DISABLED_TestThemeInstallUndoResetsToDefault
77 #define MAYBE_TestThemeInstallUndoResetsToDefault TestThemeInstallUndoResetsToDefault
80 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
81 MAYBE_TestThemeInstallUndoResetsToDefault
) {
82 #if defined(OS_WIN) && defined(USE_ASH)
83 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
84 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
88 // Install theme once and undo to verify we go back to default theme.
89 base::FilePath theme_crx
= PackExtension(test_data_dir_
.AppendASCII("theme"));
90 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 1, browser()));
91 const Extension
* theme
= GetTheme();
93 std::string theme_id
= theme
->id();
94 VerifyThemeInfoBarAndUndoInstall();
95 ASSERT_EQ(NULL
, GetTheme());
97 // Set the same theme twice and undo to verify we go back to default theme.
98 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
101 ASSERT_EQ(theme_id
, theme
->id());
102 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
105 ASSERT_EQ(theme_id
, theme
->id());
106 VerifyThemeInfoBarAndUndoInstall();
107 ASSERT_EQ(NULL
, GetTheme());
110 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
111 TestThemeInstallUndoResetsToPreviousTheme
) {
112 #if defined(OS_WIN) && defined(USE_ASH)
113 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
114 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
118 // Install first theme.
119 InstallThemeAndVerify("theme", "camo theme");
120 const Extension
* theme
= GetTheme();
121 std::string theme_id
= theme
->id();
123 // Then install second theme.
124 InstallThemeAndVerify("theme2", "snowflake theme");
125 const Extension
* theme2
= GetTheme();
126 EXPECT_FALSE(theme_id
== theme2
->id());
128 // Undo second theme will revert to first theme.
129 VerifyThemeInfoBarAndUndoInstall();
130 EXPECT_EQ(theme
, GetTheme());
133 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
135 InstallThemeAndVerify("theme", "camo theme");
137 // Reset to default theme.
138 ThemeServiceFactory::GetForProfile(browser()->profile())->UseDefaultTheme();
139 ASSERT_FALSE(GetTheme());
142 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
143 TestInstallThemeInFullScreen
) {
144 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FULLSCREEN
));
145 InstallThemeAndVerify("theme", "camo theme");
148 // TODO(samarth): remove along with NTP4 code.
149 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
150 DISABLED_AppInstallConfirmation
) {
151 int num_tabs
= browser()->tab_strip_model()->count();
153 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
154 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1, browser()));
156 if (NewTabUI::ShouldShowApps()) {
157 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
158 WebContents
* web_contents
=
159 browser()->tab_strip_model()->GetActiveWebContents();
160 ASSERT_TRUE(web_contents
);
161 EXPECT_TRUE(StartsWithASCII(web_contents
->GetURL().spec(),
162 "chrome://newtab/", false));
164 // TODO(xiyuan): Figure out how to test extension installed bubble?
168 // TODO(samarth): remove along with NTP4 code.
169 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
170 DISABLED_AppInstallConfirmation_Incognito
) {
171 Browser
* incognito_browser
= CreateIncognitoBrowser();
173 int num_incognito_tabs
= incognito_browser
->tab_strip_model()->count();
174 int num_normal_tabs
= browser()->tab_strip_model()->count();
176 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
177 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1,
180 EXPECT_EQ(num_incognito_tabs
,
181 incognito_browser
->tab_strip_model()->count());
182 if (NewTabUI::ShouldShowApps()) {
183 EXPECT_EQ(num_normal_tabs
+ 1, browser()->tab_strip_model()->count());
184 WebContents
* web_contents
=
185 browser()->tab_strip_model()->GetActiveWebContents();
186 ASSERT_TRUE(web_contents
);
187 EXPECT_TRUE(StartsWithASCII(web_contents
->GetURL().spec(),
188 "chrome://newtab/", false));
190 // TODO(xiyuan): Figure out how to test extension installed bubble?
194 class NewTabUISortingBrowserTest
: public ExtensionInstallUIBrowserTest
,
195 public content::NotificationObserver
{
197 NewTabUISortingBrowserTest() {}
199 virtual void Observe(int type
,
200 const content::NotificationSource
& source
,
201 const content::NotificationDetails
& details
) OVERRIDE
{
202 if (type
!= chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
) {
203 observer_
->Observe(type
, source
, details
);
206 const std::string
* id
= content::Details
<const std::string
>(details
).ptr();
208 last_reordered_extension_id_
= *id
;
212 std::string last_reordered_extension_id_
;
213 content::NotificationRegistrar registrar_
;
216 DISALLOW_COPY_AND_ASSIGN(NewTabUISortingBrowserTest
);
219 // TODO(samarth): remove along with NTP4 code.
220 IN_PROC_BROWSER_TEST_F(NewTabUISortingBrowserTest
,
221 DISABLED_ReorderDuringInstall
) {
222 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
223 ExtensionService
* service
= extensions::ExtensionSystem::Get(
224 browser()->profile())->extension_service();
225 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
226 const std::string app_id
= extensions::id_util::GenerateIdForPath(app_dir
);
228 const extensions::Extension
* webstore_extension
=
229 service
->GetInstalledExtension(extension_misc::kWebStoreAppId
);
230 EXPECT_TRUE(webstore_extension
);
231 AppSorting
* sorting
=
232 extensions::ExtensionPrefs::Get(browser()->profile())->app_sorting();
234 // Register for notifications in the same way as AppLauncherHandler.
235 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
,
236 content::Source
<AppSorting
>(sorting
));
237 // ExtensionAppItem calls this when an app install starts.
238 sorting
->EnsureValidOrdinals(app_id
, syncer::StringOrdinal());
239 // Vefify the app is not actually installed yet.
240 EXPECT_FALSE(service
->GetInstalledExtension(app_id
));
241 // Move the test app from the end to be before the web store.
242 sorting
->OnExtensionMoved(
243 app_id
, std::string(), extension_misc::kWebStoreAppId
);
244 EXPECT_EQ(app_id
, last_reordered_extension_id_
);
246 // Now install the app.
247 const extensions::Extension
* test_app
= LoadExtension(app_dir
);
248 ASSERT_TRUE(test_app
);
249 EXPECT_TRUE(service
->GetInstalledExtension(app_id
));
250 EXPECT_EQ(app_id
, test_app
->id());