1 // Copyright (c) 2011 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.
4
5 #include "base/string_util.h"
6 #include "chrome/browser/extensions/extension_browsertest.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/themes/theme_service_factory.h"
12 #include "chrome/test/ui_test_utils.h"
13 #include "content/browser/tab_contents/tab_contents.h"
14
15 namespace {
16
17 } // namespace
18
19 class ExtensionInstallUIBrowserTest : public ExtensionBrowserTest {
20 public:
21 // Checks that a theme info bar is currently visible and issues an undo to
22 // revert to the previous theme.
VerifyThemeInfoBarAndUndoInstall()23 void VerifyThemeInfoBarAndUndoInstall() {
24 TabContents* tab_contents = browser()->GetSelectedTabContents();
25 ASSERT_TRUE(tab_contents);
26 ASSERT_EQ(1U, tab_contents->infobar_count());
27 ConfirmInfoBarDelegate* delegate =
28 tab_contents->GetInfoBarDelegateAt(0)->AsConfirmInfoBarDelegate();
29 ASSERT_TRUE(delegate);
30 delegate->Cancel();
31 ASSERT_EQ(0U, tab_contents->infobar_count());
32 }
33
GetTheme() const34 const Extension* GetTheme() const {
35 return ThemeServiceFactory::GetThemeForProfile(browser()->profile());
36 }
37 };
38
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,TestThemeInstallUndoResetsToDefault)39 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
40 TestThemeInstallUndoResetsToDefault) {
41 // Install theme once and undo to verify we go back to default theme.
42 FilePath theme_crx = PackExtension(test_data_dir_.AppendASCII("theme"));
43 ASSERT_TRUE(InstallExtensionWithUI(theme_crx, 1));
44 const Extension* theme = GetTheme();
45 ASSERT_TRUE(theme);
46 std::string theme_id = theme->id();
47 VerifyThemeInfoBarAndUndoInstall();
48 ASSERT_EQ(NULL, GetTheme());
49
50 // Set the same theme twice and undo to verify we go back to default theme.
51 // We set the |expected_change| to zero in these 'InstallExtensionWithUI'
52 // calls since the theme has already been installed above and this is an
53 // overinstall to set the active theme.
54 ASSERT_TRUE(InstallExtensionWithUI(theme_crx, 0));
55 theme = GetTheme();
56 ASSERT_TRUE(theme);
57 ASSERT_EQ(theme_id, theme->id());
58 ASSERT_TRUE(InstallExtensionWithUI(theme_crx, 0));
59 theme = GetTheme();
60 ASSERT_TRUE(theme);
61 ASSERT_EQ(theme_id, theme->id());
62 VerifyThemeInfoBarAndUndoInstall();
63 ASSERT_EQ(NULL, GetTheme());
64 }
65
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,TestThemeInstallUndoResetsToPreviousTheme)66 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
67 TestThemeInstallUndoResetsToPreviousTheme) {
68 // Install first theme.
69 FilePath theme_path = test_data_dir_.AppendASCII("theme");
70 ASSERT_TRUE(InstallExtensionWithUI(theme_path, 1));
71 const Extension* theme = GetTheme();
72 ASSERT_TRUE(theme);
73 std::string theme_id = theme->id();
74
75 // Then install second theme.
76 FilePath theme_path2 = test_data_dir_.AppendASCII("theme2");
77 ASSERT_TRUE(InstallExtensionWithUI(theme_path2, 1));
78 const Extension* theme2 = GetTheme();
79 ASSERT_TRUE(theme2);
80 EXPECT_FALSE(theme_id == theme2->id());
81
82 // Undo second theme will revert to first theme.
83 VerifyThemeInfoBarAndUndoInstall();
84 EXPECT_EQ(theme, GetTheme());
85 }
86
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,AppInstallConfirmation)87 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
88 AppInstallConfirmation) {
89 int num_tabs = browser()->tab_count();
90
91 FilePath app_dir = test_data_dir_.AppendASCII("app");
92 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir, 1,
93 browser()->profile()));
94
95 EXPECT_EQ(num_tabs + 1, browser()->tab_count());
96 TabContents* tab_contents = browser()->GetSelectedTabContents();
97 ASSERT_TRUE(tab_contents);
98 EXPECT_TRUE(StartsWithASCII(tab_contents->GetURL().spec(),
99 "chrome://newtab/#app-id=", // id changes
100 false));
101 }
102
IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,AppInstallConfirmation_Incognito)103 IN_PROC_BROWSER_TEST_F(ExtensionInstallUIBrowserTest,
104 AppInstallConfirmation_Incognito) {
105 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
106 Browser* incognito_browser = Browser::GetOrCreateTabbedBrowser(
107 incognito_profile);
108
109 int num_incognito_tabs = incognito_browser->tab_count();
110 int num_normal_tabs = browser()->tab_count();
111
112 FilePath app_dir = test_data_dir_.AppendASCII("app");
113 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(app_dir, 1,
114 incognito_profile));
115
116 EXPECT_EQ(num_incognito_tabs, incognito_browser->tab_count());
117 EXPECT_EQ(num_normal_tabs + 1, browser()->tab_count());
118 TabContents* tab_contents = browser()->GetSelectedTabContents();
119 ASSERT_TRUE(tab_contents);
120 EXPECT_TRUE(StartsWithASCII(tab_contents->GetURL().spec(),
121 "chrome://newtab/#app-id=", // id changes
122 false));
123 }
124