Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / webstore_startup_installer_browsertest.cc
blob2c142b4ed441d0d588e1119bf8553ec5c32907c5
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_service.h"
9 #include "chrome/browser/extensions/startup_helper.h"
10 #include "chrome/browser/extensions/webstore_installer_test.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/test_switches.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "extensions/browser/extension_host.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_registry_observer.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/install/extension_install_ui.h"
29 #include "extensions/common/extension_builder.h"
30 #include "extensions/common/value_builder.h"
31 #include "net/dns/mock_host_resolver.h"
32 #include "url/gurl.h"
34 using content::WebContents;
35 using extensions::DictionaryBuilder;
36 using extensions::Extension;
37 using extensions::ExtensionBuilder;
38 using extensions::ListBuilder;
40 const char kWebstoreDomain[] = "cws.com";
41 const char kAppDomain[] = "app.com";
42 const char kNonAppDomain[] = "nonapp.com";
43 const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje";
44 const char kTestDataPath[] = "extensions/api_test/webstore_inline_install";
45 const char kCrxFilename[] = "extension.crx";
47 class WebstoreStartupInstallerTest : public WebstoreInstallerTest {
48 public:
49 WebstoreStartupInstallerTest()
50 : WebstoreInstallerTest(
51 kWebstoreDomain,
52 kTestDataPath,
53 kCrxFilename,
54 kAppDomain,
55 kNonAppDomain) {}
58 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, Install) {
59 AutoAcceptInstall();
61 ui_test_utils::NavigateToURL(
62 browser(), GenerateTestServerUrl(kAppDomain, "install.html"));
64 RunTest("runTest");
66 extensions::ExtensionRegistry* registry =
67 extensions::ExtensionRegistry::Get(browser()->profile());
68 const extensions::Extension* extension =
69 registry->enabled_extensions().GetByID(kTestExtensionId);
70 EXPECT_TRUE(extension);
73 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest,
74 InstallNotAllowedFromNonVerifiedDomains) {
75 AutoCancelInstall();
76 ui_test_utils::NavigateToURL(
77 browser(),
78 GenerateTestServerUrl(kNonAppDomain, "install_non_verified_domain.html"));
80 RunTest("runTest1");
81 RunTest("runTest2");
84 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, FindLink) {
85 ui_test_utils::NavigateToURL(
86 browser(), GenerateTestServerUrl(kAppDomain, "find_link.html"));
88 RunTest("runTest");
91 // Flakes on all platforms: http://crbug.com/95713, http://crbug.com/229947
92 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest,
93 DISABLED_ArgumentValidation) {
94 AutoCancelInstall();
96 // Each of these tests has to run separately, since one page/tab can
97 // only have one in-progress install request. These tests don't all pass
98 // callbacks to install, so they have no way to wait for the installation
99 // to complete before starting the next test.
100 bool is_finished = false;
101 for (int i = 0; !is_finished; ++i) {
102 ui_test_utils::NavigateToURL(
103 browser(),
104 GenerateTestServerUrl(kAppDomain, "argument_validation.html"));
105 is_finished = !RunIndexedTest("runTest", i);
109 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, MultipleInstallCalls) {
110 AutoCancelInstall();
112 ui_test_utils::NavigateToURL(
113 browser(),
114 GenerateTestServerUrl(kAppDomain, "multiple_install_calls.html"));
115 RunTest("runTest");
118 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, InstallNotSupported) {
119 AutoCancelInstall();
120 ui_test_utils::NavigateToURL(
121 browser(),
122 GenerateTestServerUrl(kAppDomain, "install_not_supported.html"));
124 ui_test_utils::WindowedTabAddedNotificationObserver observer(
125 content::NotificationService::AllSources());
126 RunTest("runTest");
127 observer.Wait();
129 // The inline install should fail, and a store-provided URL should be opened
130 // in a new tab.
131 WebContents* web_contents =
132 browser()->tab_strip_model()->GetActiveWebContents();
133 EXPECT_EQ(GURL("http://cws.com/show-me-the-money"), web_contents->GetURL());
136 // Regression test for http://crbug.com/144991.
137 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, InstallFromHostedApp) {
138 AutoAcceptInstall();
140 const GURL kInstallUrl = GenerateTestServerUrl(kAppDomain, "install.html");
142 // We're forced to construct a hosted app dynamically because we need the
143 // app to run on a declared URL, but we don't know the port ahead of time.
144 scoped_refptr<const Extension> hosted_app = ExtensionBuilder()
145 .SetManifest(DictionaryBuilder()
146 .Set("name", "hosted app")
147 .Set("version", "1")
148 .Set("app", DictionaryBuilder()
149 .Set("urls", ListBuilder().Append(kInstallUrl.spec()))
150 .Set("launch", DictionaryBuilder()
151 .Set("web_url", kInstallUrl.spec())))
152 .Set("manifest_version", 2))
153 .Build();
154 ASSERT_TRUE(hosted_app.get());
156 ExtensionService* extension_service =
157 extensions::ExtensionSystem::Get(browser()->profile())->
158 extension_service();
159 extensions::ExtensionRegistry* registry =
160 extensions::ExtensionRegistry::Get(browser()->profile());
162 extension_service->AddExtension(hosted_app.get());
163 EXPECT_TRUE(registry->enabled_extensions().GetByID(hosted_app->id()));
165 ui_test_utils::NavigateToURL(browser(), kInstallUrl);
167 EXPECT_FALSE(registry->enabled_extensions().GetByID(kTestExtensionId));
168 RunTest("runTest");
169 EXPECT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId));
172 class WebstoreStartupInstallerSupervisedUsersTest
173 : public WebstoreStartupInstallerTest {
174 public:
175 // InProcessBrowserTest overrides:
176 void SetUpCommandLine(base::CommandLine* command_line) override {
177 WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
178 command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
182 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerSupervisedUsersTest,
183 InstallProhibited) {
184 #if defined(OS_WIN) && defined(USE_ASH)
185 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
186 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
187 switches::kAshBrowserTests))
188 return;
189 #endif
191 AutoAcceptInstall();
193 ui_test_utils::NavigateToURL(
194 browser(), GenerateTestServerUrl(kAppDomain, "install_prohibited.html"));
196 RunTest("runTest");
198 // No error infobar should show up.
199 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
200 InfoBarService* infobar_service = InfoBarService::FromWebContents(contents);
201 EXPECT_EQ(0u, infobar_service->infobar_count());
204 // The unpack failure test needs to use a different install .crx, which is
205 // specified via a command-line flag, so it needs its own test subclass.
206 class WebstoreStartupInstallUnpackFailureTest
207 : public WebstoreStartupInstallerTest {
208 public:
209 void SetUpCommandLine(base::CommandLine* command_line) override {
210 WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
212 GURL crx_url = GenerateTestServerUrl(
213 kWebstoreDomain, "malformed_extension.crx");
214 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
215 switches::kAppsGalleryUpdateURL, crx_url.spec());
218 void SetUpInProcessBrowserTestFixture() override {
219 WebstoreStartupInstallerTest::SetUpInProcessBrowserTestFixture();
220 extensions::ExtensionInstallUI::set_disable_failure_ui_for_tests();
224 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallUnpackFailureTest,
225 WebstoreStartupInstallUnpackFailureTest) {
226 AutoAcceptInstall();
228 ui_test_utils::NavigateToURL(browser(),
229 GenerateTestServerUrl(kAppDomain, "install_unpack_failure.html"));
231 RunTest("runTest");
234 class CommandLineWebstoreInstall
235 : public WebstoreStartupInstallerTest,
236 public content::NotificationObserver,
237 public extensions::ExtensionRegistryObserver {
238 public:
239 CommandLineWebstoreInstall() : saw_install_(false), browser_open_count_(0) {}
240 ~CommandLineWebstoreInstall() override {}
242 void SetUpOnMainThread() override {
243 WebstoreStartupInstallerTest::SetUpOnMainThread();
244 extensions::ExtensionRegistry::Get(browser()->profile())->AddObserver(this);
245 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_OPENED,
246 content::NotificationService::AllSources());
249 void TearDownOnMainThread() override {
250 extensions::ExtensionRegistry::Get(browser()->profile())
251 ->RemoveObserver(this);
252 WebstoreStartupInstallerTest::TearDownOnMainThread();
255 bool saw_install() { return saw_install_; }
257 int browser_open_count() { return browser_open_count_; }
259 // NotificationObserver interface.
260 void Observe(int type,
261 const content::NotificationSource& source,
262 const content::NotificationDetails& details) override {
263 DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_OPENED);
264 ++browser_open_count_;
267 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
268 const extensions::Extension* extension,
269 bool is_update,
270 bool from_ephemeral,
271 const std::string& old_name) override {
272 EXPECT_EQ(extension->id(), kTestExtensionId);
273 saw_install_ = true;
276 content::NotificationRegistrar registrar_;
278 // Have we seen an installation notification for kTestExtensionId ?
279 bool saw_install_;
281 // How many NOTIFICATION_BROWSER_OPENED notifications have we seen?
282 int browser_open_count_;
285 IN_PROC_BROWSER_TEST_F(CommandLineWebstoreInstall, CannotInstallNonEphemeral) {
286 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
287 command_line->AppendSwitchASCII(
288 switches::kInstallEphemeralAppFromWebstore, kTestExtensionId);
289 AutoAcceptInstall();
290 extensions::StartupHelper helper;
291 EXPECT_FALSE(helper.InstallEphemeralApp(*command_line, browser()->profile()));
292 EXPECT_FALSE(saw_install());
293 EXPECT_EQ(0, browser_open_count());