Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / webstore_startup_installer_browsertest.cc
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.
4
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"
33 #include "url/gurl.h"
34
35 using content::WebContents;
36 using extensions::DictionaryBuilder;
37 using extensions::Extension;
38 using extensions::ExtensionBuilder;
39 using extensions::ListBuilder;
40
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";
47
48 class WebstoreStartupInstallerTest : public WebstoreInstallerTest {
49  public:
50   WebstoreStartupInstallerTest()
51       : WebstoreInstallerTest(
52             kWebstoreDomain,
53             kTestDataPath,
54             kCrxFilename,
55             kAppDomain,
56             kNonAppDomain) {}
57 };
58
59 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, Install) {
60   ExtensionInstallPrompt::g_auto_confirm_for_tests =
61       ExtensionInstallPrompt::ACCEPT;
62
63   ui_test_utils::NavigateToURL(
64       browser(), GenerateTestServerUrl(kAppDomain, "install.html"));
65
66   RunTest("runTest");
67
68   const extensions::Extension* extension =
69       extensions::ExtensionRegistry::Get(
70           browser()->profile())->enabled_extensions().GetByID(kTestExtensionId);
71   EXPECT_TRUE(extension);
72 }
73
74 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest,
75     InstallNotAllowedFromNonVerifiedDomains) {
76   ExtensionInstallPrompt::g_auto_confirm_for_tests =
77       ExtensionInstallPrompt::CANCEL;
78   ui_test_utils::NavigateToURL(
79       browser(),
80       GenerateTestServerUrl(kNonAppDomain, "install_non_verified_domain.html"));
81
82   RunTest("runTest1");
83   RunTest("runTest2");
84 }
85
86 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, FindLink) {
87   ui_test_utils::NavigateToURL(
88       browser(), GenerateTestServerUrl(kAppDomain, "find_link.html"));
89
90   RunTest("runTest");
91 }
92
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;
98
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(
106         browser(),
107         GenerateTestServerUrl(kAppDomain, "argument_validation.html"));
108     is_finished = !RunIndexedTest("runTest", i);
109   }
110 }
111
112 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, MultipleInstallCalls) {
113   ExtensionInstallPrompt::g_auto_confirm_for_tests =
114       ExtensionInstallPrompt::CANCEL;
115
116   ui_test_utils::NavigateToURL(
117       browser(),
118       GenerateTestServerUrl(kAppDomain, "multiple_install_calls.html"));
119   RunTest("runTest");
120 }
121
122 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerTest, InstallNotSupported) {
123   ExtensionInstallPrompt::g_auto_confirm_for_tests =
124       ExtensionInstallPrompt::CANCEL;
125   ui_test_utils::NavigateToURL(
126       browser(),
127       GenerateTestServerUrl(kAppDomain, "install_not_supported.html"));
128
129   ui_test_utils::WindowedTabAddedNotificationObserver observer(
130       content::NotificationService::AllSources());
131   RunTest("runTest");
132   observer.Wait();
133
134   // The inline install should fail, and a store-provided URL should be opened
135   // in a new tab.
136   WebContents* web_contents =
137       browser()->tab_strip_model()->GetActiveWebContents();
138   EXPECT_EQ(GURL("http://cws.com/show-me-the-money"), web_contents->GetURL());
139 }
140
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;
145
146   const GURL kInstallUrl = GenerateTestServerUrl(kAppDomain, "install.html");
147
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")
153           .Set("version", "1")
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))
159       .Build();
160   ASSERT_TRUE(hosted_app.get());
161
162   ExtensionService* extension_service =
163       extensions::ExtensionSystem::Get(browser()->profile())->
164           extension_service();
165
166   extension_service->AddExtension(hosted_app.get());
167   EXPECT_TRUE(extension_service->extensions()->Contains(hosted_app->id()));
168
169   ui_test_utils::NavigateToURL(browser(), kInstallUrl);
170
171   EXPECT_FALSE(extension_service->extensions()->Contains(kTestExtensionId));
172   RunTest("runTest");
173   EXPECT_TRUE(extension_service->extensions()->Contains(kTestExtensionId));
174 }
175
176 class WebstoreStartupInstallerSupervisedUsersTest
177     : public WebstoreStartupInstallerTest {
178  public:
179   // InProcessBrowserTest overrides:
180   void SetUpCommandLine(base::CommandLine* command_line) override {
181     WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
182     command_line->AppendSwitchASCII(switches::kSupervisedUserId, "asdf");
183   }
184 };
185
186 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallerSupervisedUsersTest,
187                        InstallProhibited) {
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))
192     return;
193 #endif
194
195   ExtensionInstallPrompt::g_auto_confirm_for_tests =
196       ExtensionInstallPrompt::ACCEPT;
197
198   ui_test_utils::NavigateToURL(
199       browser(), GenerateTestServerUrl(kAppDomain, "install_prohibited.html"));
200
201   RunTest("runTest");
202
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());
207 }
208
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 {
213  public:
214   void SetUpCommandLine(base::CommandLine* command_line) override {
215     WebstoreStartupInstallerTest::SetUpCommandLine(command_line);
216
217     GURL crx_url = GenerateTestServerUrl(
218         kWebstoreDomain, "malformed_extension.crx");
219     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
220         switches::kAppsGalleryUpdateURL, crx_url.spec());
221   }
222
223   void SetUpInProcessBrowserTestFixture() override {
224     WebstoreStartupInstallerTest::SetUpInProcessBrowserTestFixture();
225     extensions::ExtensionInstallUI::set_disable_failure_ui_for_tests();
226   }
227 };
228
229 IN_PROC_BROWSER_TEST_F(WebstoreStartupInstallUnpackFailureTest,
230     WebstoreStartupInstallUnpackFailureTest) {
231   ExtensionInstallPrompt::g_auto_confirm_for_tests =
232       ExtensionInstallPrompt::ACCEPT;
233
234   ui_test_utils::NavigateToURL(browser(),
235       GenerateTestServerUrl(kAppDomain, "install_unpack_failure.html"));
236
237   RunTest("runTest");
238 }
239
240 class CommandLineWebstoreInstall
241     : public WebstoreStartupInstallerTest,
242       public content::NotificationObserver,
243       public extensions::ExtensionRegistryObserver {
244  public:
245   CommandLineWebstoreInstall() : saw_install_(false), browser_open_count_(0) {}
246   ~CommandLineWebstoreInstall() override {}
247
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());
253   }
254
255   void TearDownOnMainThread() override {
256     extensions::ExtensionRegistry::Get(browser()->profile())
257         ->RemoveObserver(this);
258     WebstoreStartupInstallerTest::TearDownOnMainThread();
259   }
260
261   bool saw_install() { return saw_install_; }
262
263   int browser_open_count() { return browser_open_count_; }
264
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_;
271   }
272
273   void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
274                                   const extensions::Extension* extension,
275                                   bool is_update,
276                                   bool from_ephemeral,
277                                   const std::string& old_name) override {
278     EXPECT_EQ(extension->id(), kTestExtensionId);
279     saw_install_ = true;
280   }
281
282   content::NotificationRegistrar registrar_;
283
284   // Have we seen an installation notification for kTestExtensionId ?
285   bool saw_install_;
286
287   // How many NOTIFICATION_BROWSER_OPENED notifications have we seen?
288   int browser_open_count_;
289 };
290
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());
301 }