Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / plugin_apitest.cc
blob05fd5c0a2c89064934523ded496a18799881b198
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/prefs/pref_service.h"
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/test_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/plugin_service.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/common/extension.h"
25 #include "net/base/filename_util.h"
27 using content::NavigationController;
28 using content::WebContents;
29 using extensions::Extension;
31 #if defined(OS_WIN)
32 // http://crbug.com/123851 : test flakily fails on win.
33 #define MAYBE_PluginLoadUnload DISABLED_PluginLoadUnload
34 #elif defined(OS_MACOSX) && defined(ADDRESS_SANITIZER)
35 // ExtensionBrowserTest.PluginLoadUnload started failing after the switch to
36 // dynamic ASan runtime library on Mac. See http://crbug.com/234591.
37 #define MAYBE_PluginLoadUnload DISABLED_PluginLoadUnload
38 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
39 // Timing out on ARM linux http://crbug.com/238460
40 #define MAYBE_PluginLoadUnload DISABLED_PluginLoadUnload
41 #else
42 #define MAYBE_PluginLoadUnload PluginLoadUnload
43 #endif
45 // Tests that a renderer's plugin list is properly updated when we load and
46 // unload an extension that contains a plugin.
47 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginLoadUnload) {
48 if (!content::PluginService::GetInstance()->NPAPIPluginsSupported())
49 return;
50 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize,
51 true);
53 base::FilePath extension_dir =
54 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins");
56 ui_test_utils::NavigateToURL(browser(),
57 net::FilePathToFileURL(extension_dir.AppendASCII("test.html")));
58 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
60 // With no extensions, the plugin should not be loaded.
61 bool result = false;
62 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
63 tab, "testPluginWorks()", &result));
64 EXPECT_FALSE(result);
66 ExtensionService* service = extensions::ExtensionSystem::Get(
67 browser()->profile())->extension_service();
68 extensions::ExtensionRegistry* registry =
69 extensions::ExtensionRegistry::Get(browser()->profile());
70 service->set_show_extensions_prompts(false);
71 const size_t size_before = registry->enabled_extensions().size();
72 const Extension* extension = LoadExtension(extension_dir);
73 ASSERT_TRUE(extension);
74 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
75 // Now the plugin should be in the cache.
76 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
77 tab, "testPluginWorks()", &result));
78 // We don't allow extension plugins to run on ChromeOS.
79 #if defined(OS_CHROMEOS)
80 EXPECT_FALSE(result);
81 #else
82 EXPECT_TRUE(result);
83 #endif
85 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
86 UnloadExtension(extension->id());
87 EXPECT_EQ(size_before, registry->enabled_extensions().size());
89 // Now the plugin should be unloaded, and the page should be broken.
91 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
92 tab, "testPluginWorks()", &result));
93 EXPECT_FALSE(result);
95 // If we reload the extension and page, it should work again.
97 ASSERT_TRUE(LoadExtension(extension_dir));
98 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
100 content::WindowedNotificationObserver observer(
101 content::NOTIFICATION_LOAD_STOP,
102 content::Source<NavigationController>(
103 &browser()->tab_strip_model()->GetActiveWebContents()->
104 GetController()));
105 chrome::Reload(browser(), CURRENT_TAB);
106 observer.Wait();
108 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
109 tab, "testPluginWorks()", &result));
110 // We don't allow extension plugins to run on ChromeOS.
111 #if defined(OS_CHROMEOS)
112 EXPECT_FALSE(result);
113 #else
114 EXPECT_TRUE(result);
115 #endif
118 #if defined(OS_MACOSX) && defined(ADDRESS_SANITIZER)
119 // ExtensionBrowserTest.PluginPrivate started failing after the switch to
120 // dynamic ASan runtime library on Mac. See http://crbug.com/234591.
121 #define MAYBE_PluginPrivate DISABLED_PluginPrivate
122 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
123 // Timing out on ARM linux http://crbug.com/238467
124 #define MAYBE_PluginPrivate DISABLED_PluginPrivate
125 #elif defined(OS_WIN) && defined(ARCH_CPU_X86_64)
126 // TODO(jschuh): Failing plugin tests. crbug.com/244653
127 #define MAYBE_PluginPrivate DISABLED_PluginPrivate
128 #else
129 #define MAYBE_PluginPrivate PluginPrivate
130 #endif
131 // Tests that private extension plugins are only visible to the extension.
132 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, MAYBE_PluginPrivate) {
133 #if defined(OS_WIN) && defined(USE_ASH)
134 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
135 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
136 switches::kAshBrowserTests))
137 return;
138 #endif
140 if (!content::PluginService::GetInstance()->NPAPIPluginsSupported())
141 return;
143 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize,
144 true);
146 base::FilePath extension_dir =
147 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins_private");
149 ExtensionService* service = extensions::ExtensionSystem::Get(
150 browser()->profile())->extension_service();
151 extensions::ExtensionRegistry* registry =
152 extensions::ExtensionRegistry::Get(browser()->profile());
153 service->set_show_extensions_prompts(false);
154 const size_t size_before = registry->enabled_extensions().size();
155 const Extension* extension = LoadExtension(extension_dir);
156 ASSERT_TRUE(extension);
157 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
159 // Load the test page through the extension URL, and the plugin should work.
160 ui_test_utils::NavigateToURL(browser(),
161 extension->GetResourceURL("test.html"));
162 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
163 bool result = false;
164 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
165 tab, "testPluginWorks()", &result));
166 // We don't allow extension plugins to run on ChromeOS.
167 #if defined(OS_CHROMEOS)
168 EXPECT_FALSE(result);
169 #else
170 // TODO(bauerb): This might wrongly fail if the plugin takes too long
171 // to load. Extension-private plugins don't appear in navigator.plugins, so
172 // we can't check for the plugin in Javascript.
173 EXPECT_TRUE(result);
174 #endif
176 // Regression test for http://crbug.com/131811: The plugin should be
177 // whitelisted for the extension (and only for the extension), so it should be
178 // loaded even if content settings are set to block plugins.
179 HostContentSettingsMapFactory::GetForProfile(browser()->profile())
180 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS,
181 CONTENT_SETTING_BLOCK);
182 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
183 tab, "testPluginWorks()", &result));
184 // We don't allow extension plugins to run on ChromeOS.
185 #if defined(OS_CHROMEOS)
186 EXPECT_FALSE(result);
187 #else
188 EXPECT_TRUE(result);
189 #endif
191 // Now load it through a file URL. The plugin should not load.
192 ui_test_utils::NavigateToURL(browser(),
193 net::FilePathToFileURL(extension_dir.AppendASCII("test.html")));
194 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
195 tab, "testPluginWorks()", &result));
196 EXPECT_FALSE(result);