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/confirm_infobar_delegate.h"
12 #include "chrome/browser/infobars/infobar.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/themes/theme_service.h"
16 #include "chrome/browser/themes/theme_service_factory.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_commands.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/test/base/test_switches.h"
24 #include "chrome/test/base/ui_test_utils.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/common/id_util.h"
30 using content::WebContents
;
31 using extensions::AppSorting
;
32 using extensions::Extension
;
34 class ExtensionInstallUIBrowserTest
: public ExtensionBrowserTest
{
36 // Checks that a theme info bar is currently visible and issues an undo to
37 // revert to the previous theme.
38 void VerifyThemeInfoBarAndUndoInstall() {
39 WebContents
* web_contents
=
40 browser()->tab_strip_model()->GetActiveWebContents();
41 ASSERT_TRUE(web_contents
);
42 InfoBarService
* infobar_service
=
43 InfoBarService::FromWebContents(web_contents
);
44 ASSERT_EQ(1U, infobar_service
->infobar_count());
45 ConfirmInfoBarDelegate
* delegate
=
46 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
47 ASSERT_TRUE(delegate
);
49 ASSERT_EQ(0U, infobar_service
->infobar_count());
52 // Install the given theme from the data dir and verify expected name.
53 void InstallThemeAndVerify(const char* theme_name
,
54 const std::string
& expected_name
) {
55 // If there is already a theme installed, the current theme should be
56 // disabled and the new one installed + enabled.
57 int expected_change
= GetTheme() ? 0 : 1;
58 const base::FilePath theme_path
= test_data_dir_
.AppendASCII(theme_name
);
59 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_path
, expected_change
,
61 const Extension
* theme
= GetTheme();
63 ASSERT_EQ(theme
->name(), expected_name
);
66 const Extension
* GetTheme() const {
67 return ThemeServiceFactory::GetThemeForProfile(browser()->profile());
72 // Fails consistently on bot chromium.chromiumos \ Linux.
73 // See: http://crbug.com/120647.
74 #define MAYBE_TestThemeInstallUndoResetsToDefault DISABLED_TestThemeInstallUndoResetsToDefault
76 #define MAYBE_TestThemeInstallUndoResetsToDefault TestThemeInstallUndoResetsToDefault
79 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
80 MAYBE_TestThemeInstallUndoResetsToDefault
) {
81 #if defined(OS_WIN) && defined(USE_ASH)
82 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
83 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
87 // Install theme once and undo to verify we go back to default theme.
88 base::FilePath theme_crx
= PackExtension(test_data_dir_
.AppendASCII("theme"));
89 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 1, browser()));
90 const Extension
* theme
= GetTheme();
92 std::string theme_id
= theme
->id();
93 VerifyThemeInfoBarAndUndoInstall();
94 ASSERT_EQ(NULL
, GetTheme());
96 // Set the same theme twice and undo to verify we go back to default theme.
97 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
100 ASSERT_EQ(theme_id
, theme
->id());
101 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
104 ASSERT_EQ(theme_id
, theme
->id());
105 VerifyThemeInfoBarAndUndoInstall();
106 ASSERT_EQ(NULL
, GetTheme());
109 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
110 TestThemeInstallUndoResetsToPreviousTheme
) {
111 #if defined(OS_WIN) && defined(USE_ASH)
112 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
113 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
117 // Install first theme.
118 InstallThemeAndVerify("theme", "camo theme");
119 const Extension
* theme
= GetTheme();
120 std::string theme_id
= theme
->id();
122 // Then install second theme.
123 InstallThemeAndVerify("theme2", "snowflake theme");
124 const Extension
* theme2
= GetTheme();
125 EXPECT_FALSE(theme_id
== theme2
->id());
127 // Undo second theme will revert to first theme.
128 VerifyThemeInfoBarAndUndoInstall();
129 EXPECT_EQ(theme
, GetTheme());
132 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
134 InstallThemeAndVerify("theme", "camo theme");
136 // Reset to default theme.
137 ThemeServiceFactory::GetForProfile(browser()->profile())->UseDefaultTheme();
138 ASSERT_FALSE(GetTheme());
141 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
142 TestInstallThemeInFullScreen
) {
143 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FULLSCREEN
));
144 InstallThemeAndVerify("theme", "camo theme");
147 // TODO(samarth): remove along with NTP4 code.
148 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
149 DISABLED_AppInstallConfirmation
) {
150 int num_tabs
= browser()->tab_strip_model()->count();
152 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
153 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1, browser()));
155 if (NewTabUI::ShouldShowApps()) {
156 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
157 WebContents
* web_contents
=
158 browser()->tab_strip_model()->GetActiveWebContents();
159 ASSERT_TRUE(web_contents
);
160 EXPECT_TRUE(StartsWithASCII(web_contents
->GetURL().spec(),
161 "chrome://newtab/", false));
163 // TODO(xiyuan): Figure out how to test extension installed bubble?
167 // TODO(samarth): remove along with NTP4 code.
168 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
169 DISABLED_AppInstallConfirmation_Incognito
) {
170 Browser
* incognito_browser
= CreateIncognitoBrowser();
172 int num_incognito_tabs
= incognito_browser
->tab_strip_model()->count();
173 int num_normal_tabs
= browser()->tab_strip_model()->count();
175 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
176 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1,
179 EXPECT_EQ(num_incognito_tabs
,
180 incognito_browser
->tab_strip_model()->count());
181 if (NewTabUI::ShouldShowApps()) {
182 EXPECT_EQ(num_normal_tabs
+ 1, browser()->tab_strip_model()->count());
183 WebContents
* web_contents
=
184 browser()->tab_strip_model()->GetActiveWebContents();
185 ASSERT_TRUE(web_contents
);
186 EXPECT_TRUE(StartsWithASCII(web_contents
->GetURL().spec(),
187 "chrome://newtab/", false));
189 // TODO(xiyuan): Figure out how to test extension installed bubble?
193 class NewTabUISortingBrowserTest
: public ExtensionInstallUIBrowserTest
,
194 public content::NotificationObserver
{
196 NewTabUISortingBrowserTest() {}
198 virtual void Observe(int type
,
199 const content::NotificationSource
& source
,
200 const content::NotificationDetails
& details
) OVERRIDE
{
201 if (type
!= chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
) {
202 observer_
->Observe(type
, source
, details
);
205 const std::string
* id
= content::Details
<const std::string
>(details
).ptr();
207 last_reordered_extension_id_
= *id
;
211 std::string last_reordered_extension_id_
;
212 content::NotificationRegistrar registrar_
;
215 DISALLOW_COPY_AND_ASSIGN(NewTabUISortingBrowserTest
);
218 // TODO(samarth): remove along with NTP4 code.
219 IN_PROC_BROWSER_TEST_F(NewTabUISortingBrowserTest
,
220 DISABLED_ReorderDuringInstall
) {
221 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
222 ExtensionService
* service
= extensions::ExtensionSystem::Get(
223 browser()->profile())->extension_service();
224 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
225 const std::string app_id
= extensions::id_util::GenerateIdForPath(app_dir
);
227 const extensions::Extension
* webstore_extension
=
228 service
->GetInstalledExtension(extension_misc::kWebStoreAppId
);
229 EXPECT_TRUE(webstore_extension
);
230 AppSorting
* sorting
= service
->extension_prefs()->app_sorting();
232 // Register for notifications in the same way as AppLauncherHandler.
233 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
,
234 content::Source
<AppSorting
>(sorting
));
235 // ExtensionAppItem calls this when an app install starts.
236 sorting
->EnsureValidOrdinals(app_id
, syncer::StringOrdinal());
237 // Vefify the app is not actually installed yet.
238 EXPECT_FALSE(service
->GetInstalledExtension(app_id
));
239 // Move the test app from the end to be before the web store.
240 service
->OnExtensionMoved(app_id
,
242 extension_misc::kWebStoreAppId
);
243 EXPECT_EQ(app_id
, last_reordered_extension_id_
);
245 // Now install the app.
246 const extensions::Extension
* test_app
= LoadExtension(app_dir
);
247 ASSERT_TRUE(test_app
);
248 EXPECT_TRUE(service
->GetInstalledExtension(app_id
));
249 EXPECT_EQ(app_id
, test_app
->id());