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/scoped_observer.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_install_prompt.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/startup_helper.h"
11 #include "chrome/browser/extensions/webstore_installer_test.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/test_switches.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "extensions/browser/extension_host.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/extension_registry_observer.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/browser/install/extension_install_ui.h"
30 #include "extensions/common/extension_builder.h"
31 #include "extensions/common/value_builder.h"
32 #include "net/dns/mock_host_resolver.h"
35 using content::WebContents;
36 using extensions::DictionaryBuilder;
37 using extensions::Extension;
38 using extensions::ExtensionBuilder;
39 using extensions::ListBuilder;
41 const char kWebstoreDomain[] = "cws.com";
42 const char kAppDomain[] = "app.com";
43 const char kNonAppDomain[] = "nonapp.com";
44 const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje";
45 const char kTestDataPath[] = "extensions/api_test/webstore_inline_install";
46 const char kCrxFilename[] = "extension.crx";
48 class WebstoreStartupInstallerTest : public WebstoreInstallerTest {
50 WebstoreStartupInstallerTest()
51 : WebstoreInstallerTest(
59 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, Install) {
60 ExtensionInstallPrompt::g_auto_confirm_for_tests =
61 ExtensionInstallPrompt::ACCEPT;
63 ui_test_utils::NavigateToURL(
64 browser(), GenerateTestServerUrl(kAppDomain, "install.html"));
68 const extensions::Extension* extension =
69 extensions::ExtensionRegistry::Get(
70 browser()->profile())->enabled_extensions().GetByID(kTestExtensionId);
71 EXPECT_TRUE(extension);
74 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest,
75 InstallNotAllowedFromNonVerifiedDomains) {
76 ExtensionInstallPrompt::g_auto_confirm_for_tests =
77 ExtensionInstallPrompt::CANCEL;
78 ui_test_utils::NavigateToURL(
80 GenerateTestServerUrl(kNonAppDomain, "install_non_verified_domain.html"));
86 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, FindLink) {
87 ui_test_utils::NavigateToURL(
88 browser(), GenerateTestServerUrl(kAppDomain, "find_link.html"));
93 // Flakes on all platforms: http://crbug.com/95713, http://crbug.com/229947
94 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest,
95 DISABLED_ArgumentValidation) {
96 ExtensionInstallPrompt::g_auto_confirm_for_tests =
97 ExtensionInstallPrompt::CANCEL;
99 // Each of these tests has to run separately, since one page/tab can
100 // only have one in-progress install request. These tests don't all pass
101 // callbacks to install, so they have no way to wait for the installation
102 // to complete before starting the next test.
103 bool is_finished = false;
104 for (int i = 0; !is_finished; ++i) {
105 ui_test_utils::NavigateToURL(
107 GenerateTestServerUrl(kAppDomain, "argument_validation.html"));
108 is_finished = !RunIndexedTest("runTest", i);
112 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, MultipleInstallCalls) {
113 ExtensionInstallPrompt::g_auto_confirm_for_tests =
114 ExtensionInstallPrompt::CANCEL;
116 ui_test_utils::NavigateToURL(
118 GenerateTestServerUrl(kAppDomain, "multiple_install_calls.html"));
122 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, InstallNotSupported) {
123 ExtensionInstallPrompt::g_auto_confirm_for_tests =
124 ExtensionInstallPrompt::CANCEL;
125 ui_test_utils::NavigateToURL(
127 GenerateTestServerUrl(kAppDomain, "install_not_supported.html"));
129 ui_test_utils::WindowedTabAddedNotificationObserver observer(
130 content::NotificationService::AllSources());
134 // The inline install should fail, and a store-provided URL should be opened
136 WebContents* web_contents =
137 browser()->tab_strip_model()->GetActiveWebContents();
138 EXPECT_EQ(GURL("http://cws.com/show-me-the-money"), web_contents->GetURL());
141 // Regression test for http://crbug.com/144991.
142 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, InstallFromHostedApp) {
143 ExtensionInstallPrompt::g_auto_confirm_for_tests =
144 ExtensionInstallPrompt::ACCEPT;
146 const GURL kInstallUrl = GenerateTestServerUrl(kAppDomain, "install.html");
148 // We're forced to construct a hosted app dynamically because we need the
149 // app to run on a declared URL, but we don't know the port ahead of time.
150 scoped_refptr<const Extension> hosted_app = ExtensionBuilder()
151 .SetManifest(DictionaryBuilder()
152 .Set("name", "hosted app")
154 .Set("app", DictionaryBuilder()
155 .Set("urls", ListBuilder().Append(kInstallUrl.spec()))
156 .Set("launch", DictionaryBuilder()
157 .Set("web_url", kInstallUrl.spec())))
158 .Set("manifest_version", 2))
160 ASSERT_TRUE(hosted_app.get());
162 ExtensionService* extension_service =
163 extensions::ExtensionSystem::Get(browser()->profile())->
166 extension_service->AddExtension(hosted_app.get());
167 EXPECT_TRUE(extension_service->extensions()->Contains(hosted_app->id()));
169 ui_test_utils::NavigateToURL(browser(), kInstallUrl);
171 EXPECT_FALSE(extension_service->extensions()->Contains(kTestExtensionId));
173 EXPECT_TRUE(extension_service->extensions()->Contains(kTestExtensionId));
176 class WebstoreStartupInstallerSupervisedUsersTest
177 : public WebstoreStartupInstallerTest {
179 // InProcessBrowserTest overrides:
180 void SetUpCommandLine(base::CommandLine* command_line) override {
181 WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
182 command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
186 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerSupervisedUsersTest,
188 #if defined(OS_WIN) && defined(USE_ASH)
189 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
190 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
191 switches::kAshBrowserTests))
195 ExtensionInstallPrompt::g_auto_confirm_for_tests =
196 ExtensionInstallPrompt::ACCEPT;
198 ui_test_utils::NavigateToURL(
199 browser(), GenerateTestServerUrl(kAppDomain, "install_prohibited.html"));
203 // No error infobar should show up.
204 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
205 InfoBarService* infobar_service = InfoBarService::FromWebContents(contents);
206 EXPECT_EQ(0u, infobar_service->infobar_count());
209 // The unpack failure test needs to use a different install .crx, which is
210 // specified via a command-line flag, so it needs its own test subclass.
211 class WebstoreStartupInstallUnpackFailureTest
212 : public WebstoreStartupInstallerTest {
214 void SetUpCommandLine(base::CommandLine* command_line) override {
215 WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
217 GURL crx_url = GenerateTestServerUrl(
218 kWebstoreDomain, "malformed_extension.crx");
219 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
220 switches::kAppsGalleryUpdateURL, crx_url.spec());
223 void SetUpInProcessBrowserTestFixture() override {
224 WebstoreStartupInstallerTest::SetUpInProcessBrowserTestFixture();
225 extensions::ExtensionInstallUI::set_disable_failure_ui_for_tests();
229 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallUnpackFailureTest,
230 WebstoreStartupInstallUnpackFailureTest) {
231 ExtensionInstallPrompt::g_auto_confirm_for_tests =
232 ExtensionInstallPrompt::ACCEPT;
234 ui_test_utils::NavigateToURL(browser(),
235 GenerateTestServerUrl(kAppDomain, "install_unpack_failure.html"));
240 class CommandLineWebstoreInstall
241 : public WebstoreStartupInstallerTest,
242 public content::NotificationObserver,
243 public extensions::ExtensionRegistryObserver {
245 CommandLineWebstoreInstall() : saw_install_(false), browser_open_count_(0) {}
246 ~CommandLineWebstoreInstall() override {}
248 void SetUpOnMainThread() override {
249 WebstoreStartupInstallerTest::SetUpOnMainThread();
250 extensions::ExtensionRegistry::Get(browser()->profile())->AddObserver(this);
251 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
252 content::NotificationService::AllSources());
255 void TearDownOnMainThread() override {
256 extensions::ExtensionRegistry::Get(browser()->profile())
257 ->RemoveObserver(this);
258 WebstoreStartupInstallerTest::TearDownOnMainThread();
261 bool saw_install() { return saw_install_; }
263 int browser_open_count() { return browser_open_count_; }
265 // NotificationObserver interface.
266 void Observe(int type,
267 const content::NotificationSource& source,
268 const content::NotificationDetails& details) override {
269 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_OPENED);
270 ++browser_open_count_;
273 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
274 const extensions::Extension* extension,
277 const std::string& old_name) override {
278 EXPECT_EQ(extension->id(), kTestExtensionId);
282 content::NotificationRegistrar registrar_;
284 // Have we seen an installation notification for kTestExtensionId ?
287 // How many NOTIFICATION_BROWSER_OPENED notifications have we seen?
288 int browser_open_count_;
291 IN_PROC_BROWSER_TEST_F(CommandLineWebstoreInstall, CannotInstallNonEphemeral) {
292 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
293 command_line->AppendSwitchASCII(
294 switches::kInstallEphemeralAppFromWebstore, kTestExtensionId);
295 ExtensionInstallPrompt::g_auto_confirm_for_tests =
296 ExtensionInstallPrompt::ACCEPT;
297 extensions::StartupHelper helper;
298 EXPECT_FALSE(helper.InstallEphemeralApp(*command_line, browser()->profile()));
299 EXPECT_FALSE(saw_install());
300 EXPECT_EQ(0, browser_open_count());