ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chrome_plugin_browsertest.cc
blob92c8364ee383d53e7489a9a9c186af6da1a6edef
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 <vector>
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/process/process.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/plugins/plugin_prefs.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/browser_child_process_host_iterator.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/child_process_data.h"
25 #include "content/public/browser/plugin_service.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/content_constants.h"
28 #include "content/public/common/content_paths.h"
29 #include "content/public/common/process_type.h"
30 #include "content/public/common/webplugininfo.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "content/public/test/test_utils.h"
33 #include "net/base/filename_util.h"
35 #if defined(OS_WIN)
36 #include "ui/aura/window.h"
37 #include "ui/aura/window_tree_host.h"
38 #endif
40 using content::BrowserThread;
42 namespace {
44 class CallbackBarrier : public base::RefCountedThreadSafe<CallbackBarrier> {
45 public:
46 explicit CallbackBarrier(const base::Closure& target_callback)
47 : target_callback_(target_callback),
48 outstanding_callbacks_(0),
49 did_enable_(true) {
52 base::Callback<void(bool)> CreateCallback() {
53 outstanding_callbacks_++;
54 return base::Bind(&CallbackBarrier::MayRunTargetCallback, this);
57 private:
58 friend class base::RefCountedThreadSafe<CallbackBarrier>;
60 ~CallbackBarrier() {
61 EXPECT_TRUE(target_callback_.is_null());
64 void MayRunTargetCallback(bool did_enable) {
65 EXPECT_GT(outstanding_callbacks_, 0);
66 did_enable_ = did_enable_ && did_enable;
67 if (--outstanding_callbacks_ == 0) {
68 EXPECT_TRUE(did_enable_);
69 target_callback_.Run();
70 target_callback_.Reset();
74 base::Closure target_callback_;
75 int outstanding_callbacks_;
76 bool did_enable_;
79 } // namespace
81 class ChromePluginTest : public InProcessBrowserTest {
82 protected:
83 ChromePluginTest() {}
85 static GURL GetURL(const char* filename) {
86 base::FilePath path;
87 PathService::Get(content::DIR_TEST_DATA, &path);
88 path = path.AppendASCII("plugin").AppendASCII(filename);
89 CHECK(base::PathExists(path));
90 return net::FilePathToFileURL(path);
93 static void LoadAndWait(Browser* window, const GURL& url, bool pass) {
94 content::WebContents* web_contents =
95 window->tab_strip_model()->GetActiveWebContents();
96 base::string16 expected_title(
97 base::ASCIIToUTF16(pass ? "OK" : "plugin_not_found"));
98 content::TitleWatcher title_watcher(web_contents, expected_title);
99 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
100 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16(
101 pass ? "plugin_not_found" : "OK"));
102 ui_test_utils::NavigateToURL(window, url);
103 ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
106 static void CrashFlash() {
107 scoped_refptr<content::MessageLoopRunner> runner =
108 new content::MessageLoopRunner;
109 BrowserThread::PostTask(
110 BrowserThread::IO,
111 FROM_HERE,
112 base::Bind(&CrashFlashInternal, runner->QuitClosure()));
113 runner->Run();
116 static void GetFlashPath(std::vector<base::FilePath>* paths) {
117 paths->clear();
118 std::vector<content::WebPluginInfo> plugins = GetPlugins();
119 for (std::vector<content::WebPluginInfo>::const_iterator it =
120 plugins.begin(); it != plugins.end(); ++it) {
121 if (it->name == base::ASCIIToUTF16(content::kFlashPluginName))
122 paths->push_back(it->path);
126 static std::vector<content::WebPluginInfo> GetPlugins() {
127 std::vector<content::WebPluginInfo> plugins;
128 scoped_refptr<content::MessageLoopRunner> runner =
129 new content::MessageLoopRunner;
130 content::PluginService::GetInstance()->GetPlugins(
131 base::Bind(&GetPluginsInfoCallback, &plugins, runner->QuitClosure()));
132 runner->Run();
133 return plugins;
136 static void EnableFlash(bool enable, Profile* profile) {
137 std::vector<base::FilePath> paths;
138 GetFlashPath(&paths);
139 ASSERT_FALSE(paths.empty());
141 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile).get();
142 scoped_refptr<content::MessageLoopRunner> runner =
143 new content::MessageLoopRunner;
144 scoped_refptr<CallbackBarrier> callback_barrier(
145 new CallbackBarrier(runner->QuitClosure()));
146 for (std::vector<base::FilePath>::iterator iter = paths.begin();
147 iter != paths.end(); ++iter) {
148 plugin_prefs->EnablePlugin(enable, *iter,
149 callback_barrier->CreateCallback());
151 runner->Run();
154 static void EnsureFlashProcessCount(int expected) {
155 int actual = 0;
156 scoped_refptr<content::MessageLoopRunner> runner =
157 new content::MessageLoopRunner;
158 BrowserThread::PostTask(
159 BrowserThread::IO,
160 FROM_HERE,
161 base::Bind(&CountPluginProcesses, &actual, runner->QuitClosure()));
162 runner->Run();
163 ASSERT_EQ(expected, actual);
166 private:
167 static void CrashFlashInternal(const base::Closure& quit_task) {
168 bool found = false;
169 for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
170 if (iter.GetData().process_type != content::PROCESS_TYPE_PLUGIN &&
171 iter.GetData().process_type != content::PROCESS_TYPE_PPAPI_PLUGIN) {
172 continue;
174 base::Process process = base::Process::DeprecatedGetProcessFromHandle(
175 iter.GetData().handle);
176 process.Terminate(0, true);
177 found = true;
179 ASSERT_TRUE(found) << "Didn't find Flash process!";
180 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
183 static void GetPluginsInfoCallback(
184 std::vector<content::WebPluginInfo>* rv,
185 const base::Closure& quit_task,
186 const std::vector<content::WebPluginInfo>& plugins) {
187 *rv = plugins;
188 quit_task.Run();
191 static void CountPluginProcesses(int* count, const base::Closure& quit_task) {
192 for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
193 if (iter.GetData().process_type == content::PROCESS_TYPE_PLUGIN ||
194 iter.GetData().process_type == content::PROCESS_TYPE_PPAPI_PLUGIN) {
195 (*count)++;
198 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
202 // Tests a bunch of basic scenarios with Flash.
203 // This test fails under ASan on Mac, see http://crbug.com/147004.
204 // It fails elsewhere, too. See http://crbug.com/152071.
205 IN_PROC_BROWSER_TEST_F(ChromePluginTest, DISABLED_Flash) {
206 // Official builds always have bundled Flash.
207 #if !defined(OFFICIAL_BUILD)
208 std::vector<base::FilePath> flash_paths;
209 GetFlashPath(&flash_paths);
210 if (flash_paths.empty()) {
211 LOG(INFO) << "Test not running because couldn't find Flash.";
212 return;
214 #endif
216 GURL url = GetURL("flash.html");
217 EnsureFlashProcessCount(0);
219 // Try a single tab.
220 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
221 EnsureFlashProcessCount(1);
222 Profile* profile = browser()->profile();
223 // Try another tab.
224 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateBrowser(profile), url, true));
225 // Try an incognito window.
226 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateIncognitoBrowser(), url, true));
227 EnsureFlashProcessCount(1);
229 // Now kill Flash process and verify it reloads.
230 CrashFlash();
231 EnsureFlashProcessCount(0);
233 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
234 EnsureFlashProcessCount(1);
236 // Now try disabling it.
237 EnableFlash(false, profile);
238 CrashFlash();
240 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, false));
241 EnsureFlashProcessCount(0);
243 // Now enable it again.
244 EnableFlash(true, profile);
245 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
246 EnsureFlashProcessCount(1);
249 #if defined(OFFICIAL_BUILD)
250 // Verify that the official builds have the known set of plugins.
251 IN_PROC_BROWSER_TEST_F(ChromePluginTest, InstalledPlugins) {
252 const char* expected[] = {
253 "Chrome PDF Viewer",
254 "Shockwave Flash",
255 "Native Client",
258 std::vector<content::WebPluginInfo> plugins = GetPlugins();
259 for (size_t i = 0; i < arraysize(expected); ++i) {
260 size_t j = 0;
261 for (; j < plugins.size(); ++j) {
262 if (plugins[j].name == base::ASCIIToUTF16(expected[i]))
263 break;
265 ASSERT_TRUE(j != plugins.size()) << "Didn't find " << expected[i];
268 #endif