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::StartsWith(web_contents
->GetURL().spec(),
166 base::CompareCase::INSENSITIVE_ASCII
));
168 // TODO(xiyuan): Figure out how to test extension installed bubble?
172 // TODO(samarth): remove along with NTP4 code.
173 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest
,
174 DISABLED_AppInstallConfirmation_Incognito
) {
175 Browser
* incognito_browser
= CreateIncognitoBrowser();
177 int num_incognito_tabs
= incognito_browser
->tab_strip_model()->count();
178 int num_normal_tabs
= browser()->tab_strip_model()->count();
180 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
181 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir
, 1,
184 EXPECT_EQ(num_incognito_tabs
,
185 incognito_browser
->tab_strip_model()->count());
186 if (NewTabUI::ShouldShowApps()) {
187 EXPECT_EQ(num_normal_tabs
+ 1, browser()->tab_strip_model()->count());
188 WebContents
* web_contents
=
189 browser()->tab_strip_model()->GetActiveWebContents();
190 ASSERT_TRUE(web_contents
);
191 EXPECT_TRUE(base::StartsWith(web_contents
->GetURL().spec(),
193 base::CompareCase::INSENSITIVE_ASCII
));
195 // TODO(xiyuan): Figure out how to test extension installed bubble?
199 class NewTabUISortingBrowserTest
: public ExtensionInstallUIBrowserTest
,
200 public content::NotificationObserver
{
202 NewTabUISortingBrowserTest() {}
204 void Observe(int type
,
205 const content::NotificationSource
& source
,
206 const content::NotificationDetails
& details
) override
{
207 if (type
!= chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
) {
208 observer_
->Observe(type
, source
, details
);
211 const std::string
* id
= content::Details
<const std::string
>(details
).ptr();
213 last_reordered_extension_id_
= *id
;
217 std::string last_reordered_extension_id_
;
218 content::NotificationRegistrar registrar_
;
221 DISALLOW_COPY_AND_ASSIGN(NewTabUISortingBrowserTest
);
224 // TODO(samarth): remove along with NTP4 code.
225 IN_PROC_BROWSER_TEST_F(NewTabUISortingBrowserTest
,
226 DISABLED_ReorderDuringInstall
) {
227 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL
));
228 ExtensionService
* service
= extensions::ExtensionSystem::Get(
229 browser()->profile())->extension_service();
230 base::FilePath app_dir
= test_data_dir_
.AppendASCII("app");
231 const std::string app_id
= crx_file::id_util::GenerateIdForPath(app_dir
);
233 const extensions::Extension
* webstore_extension
=
234 service
->GetInstalledExtension(extensions::kWebStoreAppId
);
235 EXPECT_TRUE(webstore_extension
);
236 AppSorting
* sorting
=
237 extensions::ExtensionPrefs::Get(browser()->profile())->app_sorting();
239 // Register for notifications in the same way as AppLauncherHandler.
241 chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
,
242 content::Source
<AppSorting
>(sorting
));
243 // ExtensionAppItem calls this when an app install starts.
244 sorting
->EnsureValidOrdinals(app_id
, syncer::StringOrdinal());
245 // Vefify the app is not actually installed yet.
246 EXPECT_FALSE(service
->GetInstalledExtension(app_id
));
247 // Move the test app from the end to be before the web store.
248 sorting
->OnExtensionMoved(
249 app_id
, std::string(), extensions::kWebStoreAppId
);
250 EXPECT_EQ(app_id
, last_reordered_extension_id_
);
252 // Now install the app.
253 const extensions::Extension
* test_app
= LoadExtension(app_dir
);
254 ASSERT_TRUE(test_app
);
255 EXPECT_TRUE(service
->GetInstalledExtension(app_id
));
256 EXPECT_EQ(app_id
, test_app
->id());