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/crx_file/id_util.h"
24 #include "components/infobars/core/confirm_infobar_delegate.h"
25 #include "components/infobars/core/infobar.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "extensions/browser/app_sorting.h"
29 #include "extensions/browser/extension_prefs.h"
30 #include "extensions/common/constants.h"
32 using content::WebContents
;
33 using extensions::AppSorting
;
34 using extensions::Extension
;
36 class ExtensionInstallUIBrowserTest
: public ExtensionBrowserTest
{
38 // Checks that a theme info bar is currently visible and issues an undo to
39 // revert to the previous theme.
40 void VerifyThemeInfoBarAndUndoInstall() {
41 WebContents
* web_contents
=
42 browser()->tab_strip_model()->GetActiveWebContents();
43 ASSERT_TRUE(web_contents
);
44 InfoBarService
* infobar_service
=
45 InfoBarService::FromWebContents(web_contents
);
46 ASSERT_EQ(1U, infobar_service
->infobar_count());
47 ConfirmInfoBarDelegate
* delegate
=
48 infobar_service
->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
49 ASSERT_TRUE(delegate
);
51 ASSERT_EQ(0U, infobar_service
->infobar_count());
54 // Install the given theme from the data dir and verify expected name.
55 void InstallThemeAndVerify(const char* theme_name
,
56 const std::string
& expected_name
) {
57 // If there is already a theme installed, the current theme should be
58 // disabled and the new one installed + enabled.
59 int expected_change
= GetTheme() ? 0 : 1;
60 const base::FilePath theme_path
= test_data_dir_
.AppendASCII(theme_name
);
61 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_path
, expected_change
,
63 const Extension
* theme
= GetTheme();
65 ASSERT_EQ(theme
->name(), expected_name
);
68 const Extension
* GetTheme() const {
69 return ThemeServiceFactory::GetThemeForProfile(browser()->profile());
74 // Fails consistently on bot chromium.chromiumos \ Linux.
75 // See: http://crbug.com/120647.
76 #define MAYBE_TestThemeInstallUndoResetsToDefault DISABLED_TestThemeInstallUndoResetsToDefault
78 #define MAYBE_TestThemeInstallUndoResetsToDefault TestThemeInstallUndoResetsToDefault
81 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
82 MAYBE_TestThemeInstallUndoResetsToDefault
) {
83 #if defined(OS_WIN) && defined(USE_ASH)
84 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
85 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
86 switches::kAshBrowserTests
))
90 // Install theme once and undo to verify we go back to default theme.
91 base::FilePath theme_crx
= PackExtension(test_data_dir_
.AppendASCII("theme"));
92 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 1, browser()));
93 const Extension
* theme
= GetTheme();
95 std::string theme_id
= theme
->id();
96 VerifyThemeInfoBarAndUndoInstall();
97 ASSERT_EQ(NULL
, GetTheme());
99 // Set the same theme twice and undo to verify we go back to default theme.
100 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
103 ASSERT_EQ(theme_id
, theme
->id());
104 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(theme_crx
, 0, browser()));
107 ASSERT_EQ(theme_id
, theme
->id());
108 VerifyThemeInfoBarAndUndoInstall();
109 ASSERT_EQ(NULL
, GetTheme());
112 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
113 TestThemeInstallUndoResetsToPreviousTheme
) {
114 #if defined(OS_WIN) && defined(USE_ASH)
115 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
116 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
117 switches::kAshBrowserTests
))
121 // Install first theme.
122 InstallThemeAndVerify("theme", "camo theme");
123 const Extension
* theme
= GetTheme();
124 std::string theme_id
= theme
->id();
126 // Then install second theme.
127 InstallThemeAndVerify("theme2", "snowflake theme");
128 const Extension
* theme2
= GetTheme();
129 EXPECT_FALSE(theme_id
== theme2
->id());
131 // Undo second theme will revert to first theme.
132 VerifyThemeInfoBarAndUndoInstall();
133 EXPECT_EQ(theme
, GetTheme());
136 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
138 InstallThemeAndVerify("theme", "camo theme");
140 // Reset to default theme.
141 ThemeServiceFactory::GetForProfile(browser()->profile())->UseDefaultTheme();
142 ASSERT_FALSE(GetTheme());
145 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
146 TestInstallThemeInFullScreen
) {
147 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FULLSCREEN
));
148 InstallThemeAndVerify("theme", "camo theme");
151 // TODO(samarth): remove along with NTP4 code.
152 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
153 DISABLED_AppInstallConfirmation
) {
154 int num_tabs
= browser()->tab_strip_model()->count();
156 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
157 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1, browser()));
159 if (NewTabUI::ShouldShowApps()) {
160 EXPECT_EQ(num_tabs
+ 1, browser()->tab_strip_model()->count());
161 WebContents
* web_contents
=
162 browser()->tab_strip_model()->GetActiveWebContents();
163 ASSERT_TRUE(web_contents
);
164 EXPECT_TRUE(base::StartsWithASCII(web_contents
->GetURL().spec(),
165 "chrome://newtab/", false));
167 // TODO(xiyuan): Figure out how to test extension installed bubble?
171 // TODO(samarth): remove along with NTP4 code.
172 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
173 DISABLED_AppInstallConfirmation_Incognito
) {
174 Browser
* incognito_browser
= CreateIncognitoBrowser();
176 int num_incognito_tabs
= incognito_browser
->tab_strip_model()->count();
177 int num_normal_tabs
= browser()->tab_strip_model()->count();
179 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
180 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1,
183 EXPECT_EQ(num_incognito_tabs
,
184 incognito_browser
->tab_strip_model()->count());
185 if (NewTabUI::ShouldShowApps()) {
186 EXPECT_EQ(num_normal_tabs
+ 1, browser()->tab_strip_model()->count());
187 WebContents
* web_contents
=
188 browser()->tab_strip_model()->GetActiveWebContents();
189 ASSERT_TRUE(web_contents
);
190 EXPECT_TRUE(base::StartsWithASCII(web_contents
->GetURL().spec(),
191 "chrome://newtab/", false));
193 // TODO(xiyuan): Figure out how to test extension installed bubble?
197 class NewTabUISortingBrowserTest
: public ExtensionInstallUIBrowserTest
,
198 public content::NotificationObserver
{
200 NewTabUISortingBrowserTest() {}
202 void Observe(int type
,
203 const content::NotificationSource
& source
,
204 const content::NotificationDetails
& details
) override
{
205 if (type
!= chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
) {
206 observer_
->Observe(type
, source
, details
);
209 const std::string
* id
= content::Details
<const std::string
>(details
).ptr();
211 last_reordered_extension_id_
= *id
;
215 std::string last_reordered_extension_id_
;
216 content::NotificationRegistrar registrar_
;
219 DISALLOW_COPY_AND_ASSIGN(NewTabUISortingBrowserTest
);
222 // TODO(samarth): remove along with NTP4 code.
223 IN_PROC_BROWSER_TEST_F(NewTabUISortingBrowserTest
,
224 DISABLED_ReorderDuringInstall
) {
225 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
226 ExtensionService
* service
= extensions::ExtensionSystem::Get(
227 browser()->profile())->extension_service();
228 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
229 const std::string app_id
= crx_file::id_util::GenerateIdForPath(app_dir
);
231 const extensions::Extension
* webstore_extension
=
232 service
->GetInstalledExtension(extensions::kWebStoreAppId
);
233 EXPECT_TRUE(webstore_extension
);
234 AppSorting
* sorting
=
235 extensions::ExtensionPrefs::Get(browser()->profile())->app_sorting();
237 // Register for notifications in the same way as AppLauncherHandler.
239 chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
,
240 content::Source
<AppSorting
>(sorting
));
241 // ExtensionAppItem calls this when an app install starts.
242 sorting
->EnsureValidOrdinals(app_id
, syncer::StringOrdinal());
243 // Vefify the app is not actually installed yet.
244 EXPECT_FALSE(service
->GetInstalledExtension(app_id
));
245 // Move the test app from the end to be before the web store.
246 sorting
->OnExtensionMoved(
247 app_id
, std::string(), extensions::kWebStoreAppId
);
248 EXPECT_EQ(app_id
, last_reordered_extension_id_
);
250 // Now install the app.
251 const extensions::Extension
* test_app
= LoadExtension(app_dir
);
252 ASSERT_TRUE(test_app
);
253 EXPECT_TRUE(service
->GetInstalledExtension(app_id
));
254 EXPECT_EQ(app_id
, test_app
->id());