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