1 // Copyright 2013 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/strings/utf_string_conversions.h"
6 #include "chrome/browser/extensions/extension_install_prompt.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/tab_helper.h"
9 #include "chrome/browser/extensions/webstore_inline_installer.h"
10 #include "chrome/browser/extensions/webstore_inline_installer_factory.h"
11 #include "chrome/browser/extensions/webstore_installer_test.h"
12 #include "chrome/browser/extensions/webstore_standalone_installer.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/extension_system.h"
24 using content::WebContents
;
26 namespace extensions
{
30 const char kWebstoreDomain
[] = "cws.com";
31 const char kAppDomain
[] = "app.com";
32 const char kNonAppDomain
[] = "nonapp.com";
33 const char kTestExtensionId
[] = "ecglahbcnmdpdciemllbhojghbkagdje";
34 const char kTestDataPath
[] = "extensions/api_test/webstore_inline_install";
35 const char kCrxFilename
[] = "extension.crx";
39 class WebstoreInlineInstallerTest
: public WebstoreInstallerTest
{
41 WebstoreInlineInstallerTest()
42 : WebstoreInstallerTest(
50 class ProgrammableInstallPrompt
: public ExtensionInstallPrompt
{
52 explicit ProgrammableInstallPrompt(WebContents
* contents
)
53 : ExtensionInstallPrompt(contents
)
56 ~ProgrammableInstallPrompt() override
{}
58 void ConfirmStandaloneInstall(
60 const Extension
* extension
,
62 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt
) override
{
67 return delegate_
!= NULL
;
70 static void Accept() {
71 delegate_
->InstallUIProceed();
74 static void Reject() {
75 delegate_
->InstallUIAbort(true);
79 static Delegate
* delegate_
;
82 ExtensionInstallPrompt::Delegate
* ProgrammableInstallPrompt::delegate_
;
84 // Fake inline installer which creates a programmable prompt in place of
85 // the normal dialog UI.
86 class WebstoreInlineInstallerForTest
: public WebstoreInlineInstaller
{
88 WebstoreInlineInstallerForTest(WebContents
* contents
,
89 const std::string
& extension_id
,
90 const GURL
& requestor_url
,
91 const Callback
& callback
)
92 : WebstoreInlineInstaller(
96 base::Bind(DummyCallback
)),
97 programmable_prompt_(NULL
) {
100 scoped_ptr
<ExtensionInstallPrompt
> CreateInstallUI() override
{
101 programmable_prompt_
= new ProgrammableInstallPrompt(web_contents());
102 return make_scoped_ptr(programmable_prompt_
);
106 ~WebstoreInlineInstallerForTest() override
{}
108 friend class base::RefCountedThreadSafe
<WebstoreStandaloneInstaller
>;
110 static void DummyCallback(bool success
,
111 const std::string
& error
,
112 webstore_install::Result result
) {
115 ProgrammableInstallPrompt
* programmable_prompt_
;
118 class WebstoreInlineInstallerForTestFactory
:
119 public WebstoreInlineInstallerFactory
{
120 ~WebstoreInlineInstallerForTestFactory() override
{}
121 WebstoreInlineInstaller
* CreateInstaller(
122 WebContents
* contents
,
123 const std::string
& webstore_item_id
,
124 const GURL
& requestor_url
,
125 const WebstoreStandaloneInstaller::Callback
& callback
) override
{
126 return new WebstoreInlineInstallerForTest(
127 contents
, webstore_item_id
, requestor_url
, callback
);
131 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest
,
132 CloseTabBeforeInstallConfirmation
) {
133 GURL install_url
= GenerateTestServerUrl(kAppDomain
, "install.html");
134 ui_test_utils::NavigateToURL(browser(), install_url
);
135 WebContents
* web_contents
=
136 browser()->tab_strip_model()->GetActiveWebContents();
137 TabHelper
* tab_helper
= TabHelper::FromWebContents(web_contents
);
138 tab_helper
->SetWebstoreInlineInstallerFactoryForTests(
139 new WebstoreInlineInstallerForTestFactory());
140 RunTestAsync("runTest");
141 while (!ProgrammableInstallPrompt::Ready())
142 base::RunLoop().RunUntilIdle();
143 web_contents
->Close();
144 ProgrammableInstallPrompt::Accept();
147 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest
,
148 ShouldBlockInlineInstallFromPopupWindow
) {
150 GenerateTestServerUrl(kAppDomain
, "install_from_popup.html");
151 // Disable popup blocking for the test url.
152 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
153 ContentSettingsPattern::FromURL(install_url
),
154 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS
,
155 std::string(), CONTENT_SETTING_ALLOW
);
156 ui_test_utils::NavigateToURL(browser(), install_url
);
157 // The test page opens a popup which is a new |browser| window.
158 Browser
* popup_browser
= chrome::FindLastActiveWithProfile(
159 browser()->profile(), chrome::GetActiveDesktop());
160 WebContents
* popup_contents
=
161 popup_browser
->tab_strip_model()->GetActiveWebContents();
162 EXPECT_EQ(base::ASCIIToUTF16("POPUP"), popup_contents
->GetTitle());
163 RunTest(popup_contents
, "runTest");
166 // Ensure that inline-installing a disabled extension simply re-enables it.
167 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest
,
168 ReinstallDisabledExtension
) {
169 // Install an extension via inline install, and confirm it is successful.
170 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
171 ExtensionInstallPrompt::ACCEPT
;
172 ui_test_utils::NavigateToURL(
173 browser(), GenerateTestServerUrl(kAppDomain
, "install.html"));
175 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
176 ASSERT_TRUE(registry
->enabled_extensions().GetByID(kTestExtensionId
));
178 // Disable the extension.
179 ExtensionService
* extension_service
=
180 ExtensionSystem::Get(browser()->profile())->extension_service();
181 extension_service
->DisableExtension(kTestExtensionId
,
182 Extension::DISABLE_USER_ACTION
);
183 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kTestExtensionId
));
185 // Revisit the inline install site and reinstall the extension. It should
186 // simply be re-enabled, rather than try to install again.
187 ui_test_utils::NavigateToURL(
188 browser(), GenerateTestServerUrl(kAppDomain
, "install.html"));
190 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kTestExtensionId
));
191 // Since it was disabled by user action, the prompt should have just been the
192 // inline install prompt.
193 EXPECT_EQ(ExtensionInstallPrompt::INLINE_INSTALL_PROMPT
,
194 ExtensionInstallPrompt::g_last_prompt_type_for_tests
);
196 // Disable the extension due to a permissions increase.
197 extension_service
->DisableExtension(kTestExtensionId
,
198 Extension::DISABLE_PERMISSIONS_INCREASE
);
199 EXPECT_TRUE(registry
->disabled_extensions().GetByID(kTestExtensionId
));
200 ui_test_utils::NavigateToURL(
201 browser(), GenerateTestServerUrl(kAppDomain
, "install.html"));
203 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kTestExtensionId
));
204 // The displayed prompt should be for the permissions increase, versus a
205 // normal inline install prompt.
206 EXPECT_EQ(ExtensionInstallPrompt::RE_ENABLE_PROMPT
,
207 ExtensionInstallPrompt::g_last_prompt_type_for_tests
);
209 ExtensionInstallPrompt::g_last_prompt_type_for_tests
=
210 ExtensionInstallPrompt::UNSET_PROMPT_TYPE
;
211 ui_test_utils::NavigateToURL(
212 browser(), GenerateTestServerUrl(kAppDomain
, "install.html"));
214 // If the extension was already enabled, we should still display an inline
215 // install prompt (until we come up with something better).
216 EXPECT_EQ(ExtensionInstallPrompt::INLINE_INSTALL_PROMPT
,
217 ExtensionInstallPrompt::g_last_prompt_type_for_tests
);
220 class WebstoreInlineInstallerListenerTest
: public WebstoreInlineInstallerTest
{
222 WebstoreInlineInstallerListenerTest() {}
223 ~WebstoreInlineInstallerListenerTest() override
{}
226 void RunTest(const std::string
& file_name
) {
227 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
228 ExtensionInstallPrompt::ACCEPT
;
229 ui_test_utils::NavigateToURL(browser(),
230 GenerateTestServerUrl(kAppDomain
, file_name
));
231 WebstoreInstallerTest::RunTest("runTest");
235 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerListenerTest
,
236 InstallStageListenerTest
) {
237 RunTest("install_stage_listener.html");
240 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerListenerTest
,
241 DownloadProgressListenerTest
) {
242 RunTest("download_progress_listener.html");
245 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerListenerTest
, BothListenersTest
) {
246 RunTest("both_listeners.html");
249 } // namespace extensions