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/extensions/extension_service.h"
8 #include "chrome/browser/extensions/webstore_installer_test.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/test_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "extensions/browser/extension_host.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/install/extension_install_ui.h"
24 #include "extensions/common/extension_builder.h"
25 #include "extensions/common/value_builder.h"
26 #include "net/dns/mock_host_resolver.h"
29 using content::WebContents
;
30 using extensions::DictionaryBuilder
;
31 using extensions::Extension
;
32 using extensions::ExtensionBuilder
;
33 using extensions::ListBuilder
;
35 const char kWebstoreDomain
[] = "cws.com";
36 const char kAppDomain
[] = "app.com";
37 const char kNonAppDomain
[] = "nonapp.com";
38 const char kTestExtensionId
[] = "ecglahbcnmdpdciemllbhojghbkagdje";
39 const char kTestDataPath
[] = "extensions/api_test/webstore_inline_install";
40 const char kCrxFilename
[] = "extension.crx";
42 class WebstoreStartupInstallerTest
: public WebstoreInstallerTest
{
44 WebstoreStartupInstallerTest()
45 : WebstoreInstallerTest(
53 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
, Install
) {
56 ui_test_utils::NavigateToURL(
57 browser(), GenerateTestServerUrl(kAppDomain
, "install.html"));
61 extensions::ExtensionRegistry
* registry
=
62 extensions::ExtensionRegistry::Get(browser()->profile());
63 const extensions::Extension
* extension
=
64 registry
->enabled_extensions().GetByID(kTestExtensionId
);
65 EXPECT_TRUE(extension
);
68 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
,
69 InstallNotAllowedFromNonVerifiedDomains
) {
71 ui_test_utils::NavigateToURL(
73 GenerateTestServerUrl(kNonAppDomain
, "install_non_verified_domain.html"));
79 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
, FindLink
) {
80 ui_test_utils::NavigateToURL(
81 browser(), GenerateTestServerUrl(kAppDomain
, "find_link.html"));
86 // Flakes on all platforms: http://crbug.com/95713, http://crbug.com/229947
87 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
,
88 DISABLED_ArgumentValidation
) {
91 // Each of these tests has to run separately, since one page/tab can
92 // only have one in-progress install request. These tests don't all pass
93 // callbacks to install, so they have no way to wait for the installation
94 // to complete before starting the next test.
95 bool is_finished
= false;
96 for (int i
= 0; !is_finished
; ++i
) {
97 ui_test_utils::NavigateToURL(
99 GenerateTestServerUrl(kAppDomain
, "argument_validation.html"));
100 is_finished
= !RunIndexedTest("runTest", i
);
104 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
, MultipleInstallCalls
) {
107 ui_test_utils::NavigateToURL(
109 GenerateTestServerUrl(kAppDomain
, "multiple_install_calls.html"));
113 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
, InstallNotSupported
) {
115 ui_test_utils::NavigateToURL(
117 GenerateTestServerUrl(kAppDomain
, "install_not_supported.html"));
119 ui_test_utils::WindowedTabAddedNotificationObserver
observer(
120 content::NotificationService::AllSources());
124 // The inline install should fail, and a store-provided URL should be opened
126 WebContents
* web_contents
=
127 browser()->tab_strip_model()->GetActiveWebContents();
128 EXPECT_EQ(GURL("http://cws.com/show-me-the-money"), web_contents
->GetURL());
131 // Regression test for http://crbug.com/144991.
132 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest
, InstallFromHostedApp
) {
135 const GURL kInstallUrl
= GenerateTestServerUrl(kAppDomain
, "install.html");
137 // We're forced to construct a hosted app dynamically because we need the
138 // app to run on a declared URL, but we don't know the port ahead of time.
139 scoped_refptr
<const Extension
> hosted_app
= ExtensionBuilder()
140 .SetManifest(DictionaryBuilder()
141 .Set("name", "hosted app")
143 .Set("app", DictionaryBuilder()
144 .Set("urls", ListBuilder().Append(kInstallUrl
.spec()))
145 .Set("launch", DictionaryBuilder()
146 .Set("web_url", kInstallUrl
.spec())))
147 .Set("manifest_version", 2))
149 ASSERT_TRUE(hosted_app
.get());
151 ExtensionService
* extension_service
=
152 extensions::ExtensionSystem::Get(browser()->profile())->
154 extensions::ExtensionRegistry
* registry
=
155 extensions::ExtensionRegistry::Get(browser()->profile());
157 extension_service
->AddExtension(hosted_app
.get());
158 EXPECT_TRUE(registry
->enabled_extensions().GetByID(hosted_app
->id()));
160 ui_test_utils::NavigateToURL(browser(), kInstallUrl
);
162 EXPECT_FALSE(registry
->enabled_extensions().GetByID(kTestExtensionId
));
164 EXPECT_TRUE(registry
->enabled_extensions().GetByID(kTestExtensionId
));
167 class WebstoreStartupInstallerSupervisedUsersTest
168 : public WebstoreStartupInstallerTest
{
170 // InProcessBrowserTest overrides:
171 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
172 WebstoreStartupInstallerTest::SetUpCommandLine(command_line
);
173 command_line
->AppendSwitchASCII(switches::kSupervisedUserId
, "asdf");
177 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerSupervisedUsersTest
,
179 #if defined(OS_WIN) && defined(USE_ASH)
180 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
181 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
182 switches::kAshBrowserTests
))
188 ui_test_utils::NavigateToURL(
189 browser(), GenerateTestServerUrl(kAppDomain
, "install_prohibited.html"));
193 // No error infobar should show up.
194 WebContents
* contents
= browser()->tab_strip_model()->GetActiveWebContents();
195 InfoBarService
* infobar_service
= InfoBarService::FromWebContents(contents
);
196 EXPECT_EQ(0u, infobar_service
->infobar_count());
199 // The unpack failure test needs to use a different install .crx, which is
200 // specified via a command-line flag, so it needs its own test subclass.
201 class WebstoreStartupInstallUnpackFailureTest
202 : public WebstoreStartupInstallerTest
{
204 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
205 WebstoreStartupInstallerTest::SetUpCommandLine(command_line
);
207 GURL crx_url
= GenerateTestServerUrl(
208 kWebstoreDomain
, "malformed_extension.crx");
209 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
210 switches::kAppsGalleryUpdateURL
, crx_url
.spec());
213 void SetUpInProcessBrowserTestFixture() override
{
214 WebstoreStartupInstallerTest::SetUpInProcessBrowserTestFixture();
215 extensions::ExtensionInstallUI::set_disable_failure_ui_for_tests();
219 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallUnpackFailureTest
,
220 WebstoreStartupInstallUnpackFailureTest
) {
223 ui_test_utils::NavigateToURL(browser(),
224 GenerateTestServerUrl(kAppDomain
, "install_unpack_failure.html"));