Fix build break
[chromium-blink-merge.git] / chrome / browser / plugins / plugin_prefs_unittest.cc
blobad1aa8e77437fcc8304efb3e14761dbd857b7454
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 "chrome/browser/plugins/plugin_prefs.h"
7 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "content/public/browser/plugin_service.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/plugins/npapi/mock_plugin_list.h"
19 #include "webkit/plugins/webplugininfo.h"
21 using content::BrowserThread;
22 using content::PluginService;
24 namespace {
26 void CanEnablePluginCallback(const base::Closure& quit_closure,
27 bool expected_can_change,
28 bool did_change) {
29 EXPECT_EQ(expected_can_change, did_change);
30 quit_closure.Run();
33 base::FilePath GetComponentUpdatedPepperFlashPath(
34 const base::FilePath::StringType& version) {
35 base::FilePath path;
36 EXPECT_TRUE(PathService::Get(
37 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &path));
38 path = path.Append(version);
39 path = path.Append(chrome::kPepperFlashPluginFilename);
40 return path;
43 base::FilePath GetBundledPepperFlashPath() {
44 base::FilePath path;
45 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path));
46 return path;
49 } // namespace
51 class PluginPrefsTest : public ::testing::Test {
52 public:
53 virtual void SetUp() OVERRIDE {
54 plugin_prefs_ = new PluginPrefs();
57 void SetPolicyEnforcedPluginPatterns(
58 const std::set<string16>& disabled,
59 const std::set<string16>& disabled_exceptions,
60 const std::set<string16>& enabled) {
61 plugin_prefs_->SetPolicyEnforcedPluginPatterns(
62 disabled, disabled_exceptions, enabled);
65 protected:
66 void EnablePluginSynchronously(bool enabled,
67 const base::FilePath& path,
68 bool expected_can_change) {
69 base::RunLoop run_loop;
70 plugin_prefs_->EnablePlugin(
71 enabled, path,
72 base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(),
73 expected_can_change));
74 run_loop.Run();
77 scoped_refptr<PluginPrefs> plugin_prefs_;
80 TEST_F(PluginPrefsTest, DisabledByPolicy) {
81 std::set<string16> disabled_plugins;
82 disabled_plugins.insert(ASCIIToUTF16("Disable this!"));
83 disabled_plugins.insert(ASCIIToUTF16("*Google*"));
84 SetPolicyEnforcedPluginPatterns(disabled_plugins,
85 std::set<string16>(),
86 std::set<string16>());
88 EXPECT_EQ(PluginPrefs::NO_POLICY,
89 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
90 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
91 plugin_prefs_->PolicyStatusForPlugin(
92 ASCIIToUTF16("Disable this!")));
93 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
94 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth")));
97 TEST_F(PluginPrefsTest, EnabledByPolicy) {
98 std::set<string16> enabled_plugins;
99 enabled_plugins.insert(ASCIIToUTF16("Enable that!"));
100 enabled_plugins.insert(ASCIIToUTF16("PDF*"));
101 SetPolicyEnforcedPluginPatterns(std::set<string16>(),
102 std::set<string16>(),
103 enabled_plugins);
105 EXPECT_EQ(PluginPrefs::NO_POLICY,
106 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
107 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
108 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!")));
109 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
110 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader")));
113 TEST_F(PluginPrefsTest, EnabledAndDisabledByPolicy) {
114 const string16 k42(ASCIIToUTF16("42"));
115 const string16 kEnabled(ASCIIToUTF16("Enabled"));
116 const string16 kEnabled2(ASCIIToUTF16("Enabled 2"));
117 const string16 kEnabled3(ASCIIToUTF16("Enabled 3"));
118 const string16 kException(ASCIIToUTF16("Exception"));
119 const string16 kException2(ASCIIToUTF16("Exception 2"));
120 const string16 kGoogleMars(ASCIIToUTF16("Google Mars"));
121 const string16 kGoogleEarth(ASCIIToUTF16("Google Earth"));
123 std::set<string16> disabled_plugins;
124 std::set<string16> disabled_plugins_exceptions;
125 std::set<string16> enabled_plugins;
127 disabled_plugins.insert(kEnabled);
128 disabled_plugins_exceptions.insert(kEnabled);
129 enabled_plugins.insert(kEnabled);
131 disabled_plugins_exceptions.insert(kException);
133 disabled_plugins.insert(kEnabled2);
134 enabled_plugins.insert(kEnabled2);
136 disabled_plugins.insert(kException2);
137 disabled_plugins_exceptions.insert(kException2);
139 disabled_plugins_exceptions.insert(kEnabled3);
140 enabled_plugins.insert(kEnabled3);
142 SetPolicyEnforcedPluginPatterns(disabled_plugins,
143 disabled_plugins_exceptions,
144 enabled_plugins);
146 EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42));
148 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
149 plugin_prefs_->PolicyStatusForPlugin(kEnabled));
150 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
151 plugin_prefs_->PolicyStatusForPlugin(kEnabled2));
152 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
153 plugin_prefs_->PolicyStatusForPlugin(kEnabled3));
155 EXPECT_EQ(PluginPrefs::NO_POLICY,
156 plugin_prefs_->PolicyStatusForPlugin(kException));
157 EXPECT_EQ(PluginPrefs::NO_POLICY,
158 plugin_prefs_->PolicyStatusForPlugin(kException2));
160 disabled_plugins.clear();
161 disabled_plugins_exceptions.clear();
162 enabled_plugins.clear();
164 disabled_plugins.insert(ASCIIToUTF16("*"));
165 disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*"));
166 enabled_plugins.insert(kGoogleEarth);
168 SetPolicyEnforcedPluginPatterns(disabled_plugins,
169 disabled_plugins_exceptions,
170 enabled_plugins);
172 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
173 plugin_prefs_->PolicyStatusForPlugin(kGoogleEarth));
174 EXPECT_EQ(PluginPrefs::NO_POLICY,
175 plugin_prefs_->PolicyStatusForPlugin(kGoogleMars));
176 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
177 plugin_prefs_->PolicyStatusForPlugin(k42));
180 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) {
181 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService.
183 MessageLoop message_loop;
184 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
185 webkit::npapi::MockPluginList plugin_list;
186 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list);
187 PluginService::GetInstance()->Init();
188 plugin_prefs_->SetPluginListForTesting(&plugin_list);
190 string16 component_updated_plugin_name(
191 ASCIIToUTF16("Component-updated Pepper Flash"));
192 webkit::WebPluginInfo component_updated_plugin_1(
193 component_updated_plugin_name,
194 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.227")),
195 ASCIIToUTF16("11.3.31.227"),
196 ASCIIToUTF16(""));
197 webkit::WebPluginInfo component_updated_plugin_2(
198 component_updated_plugin_name,
199 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.228")),
200 ASCIIToUTF16("11.3.31.228"),
201 ASCIIToUTF16(""));
202 webkit::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"),
203 GetBundledPepperFlashPath(),
204 ASCIIToUTF16("11.3.31.229"),
205 ASCIIToUTF16(""));
207 plugin_list.AddPluginToLoad(component_updated_plugin_1);
208 plugin_list.AddPluginToLoad(component_updated_plugin_2);
209 plugin_list.AddPluginToLoad(bundled_plugin);
211 // Set the state of any of the three plugins will affect the others.
212 EnablePluginSynchronously(true, component_updated_plugin_1.path, true);
213 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
214 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
215 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
217 EnablePluginSynchronously(false, bundled_plugin.path, true);
218 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
219 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
220 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
222 EnablePluginSynchronously(true, component_updated_plugin_2.path, true);
223 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
224 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
225 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
227 std::set<string16> disabled_plugins;
228 disabled_plugins.insert(component_updated_plugin_name);
229 SetPolicyEnforcedPluginPatterns(disabled_plugins,
230 std::set<string16>(),
231 std::set<string16>());
233 // Policy settings should be respected.
234 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
235 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
236 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
238 EnablePluginSynchronously(false, bundled_plugin.path, true);
239 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
241 // Trying to change the state of a policy-enforced plugin should not take
242 // effect. And it shouldn't change the state of other plugins either, even if
243 // they are not restricted by any policy.
244 EnablePluginSynchronously(true, component_updated_plugin_1.path, false);
245 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
246 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
247 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
249 EnablePluginSynchronously(true, bundled_plugin.path, true);
250 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
251 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
252 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
254 plugin_prefs_->SetPluginListForTesting(NULL);
255 PluginService::GetInstance()->SetPluginListForTesting(NULL);