base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / chrome_plugin_browsertest.cc
blob795cfdaefa2d4fe48fddd04ff7cc58eaffa21d40
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/kill.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::KillProcess(iter.GetData().handle, 0, true);
175 found = true;
177 ASSERT_TRUE(found) << "Didn't find Flash process!";
178 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
181 static void GetPluginsInfoCallback(
182 std::vector<content::WebPluginInfo>* rv,
183 const base::Closure& quit_task,
184 const std::vector<content::WebPluginInfo>& plugins) {
185 *rv = plugins;
186 quit_task.Run();
189 static void CountPluginProcesses(int* count, const base::Closure& quit_task) {
190 for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
191 if (iter.GetData().process_type == content::PROCESS_TYPE_PLUGIN ||
192 iter.GetData().process_type == content::PROCESS_TYPE_PPAPI_PLUGIN) {
193 (*count)++;
196 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
200 // Tests a bunch of basic scenarios with Flash.
201 // This test fails under ASan on Mac, see http://crbug.com/147004.
202 // It fails elsewhere, too. See http://crbug.com/152071.
203 IN_PROC_BROWSER_TEST_F(ChromePluginTest, DISABLED_Flash) {
204 // Official builds always have bundled Flash.
205 #if !defined(OFFICIAL_BUILD)
206 std::vector<base::FilePath> flash_paths;
207 GetFlashPath(&flash_paths);
208 if (flash_paths.empty()) {
209 LOG(INFO) << "Test not running because couldn't find Flash.";
210 return;
212 #endif
214 GURL url = GetURL("flash.html");
215 EnsureFlashProcessCount(0);
217 // Try a single tab.
218 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
219 EnsureFlashProcessCount(1);
220 Profile* profile = browser()->profile();
221 // Try another tab.
222 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateBrowser(profile), url, true));
223 // Try an incognito window.
224 ASSERT_NO_FATAL_FAILURE(LoadAndWait(CreateIncognitoBrowser(), url, true));
225 EnsureFlashProcessCount(1);
227 // Now kill Flash process and verify it reloads.
228 CrashFlash();
229 EnsureFlashProcessCount(0);
231 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
232 EnsureFlashProcessCount(1);
234 // Now try disabling it.
235 EnableFlash(false, profile);
236 CrashFlash();
238 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, false));
239 EnsureFlashProcessCount(0);
241 // Now enable it again.
242 EnableFlash(true, profile);
243 ASSERT_NO_FATAL_FAILURE(LoadAndWait(browser(), url, true));
244 EnsureFlashProcessCount(1);
247 #if defined(OFFICIAL_BUILD)
248 // Verify that the official builds have the known set of plugins.
249 IN_PROC_BROWSER_TEST_F(ChromePluginTest, InstalledPlugins) {
250 const char* expected[] = {
251 "Chrome PDF Viewer",
252 "Shockwave Flash",
253 "Native Client",
254 "Chrome Remote Desktop Viewer",
257 std::vector<content::WebPluginInfo> plugins = GetPlugins();
258 for (size_t i = 0; i < arraysize(expected); ++i) {
259 size_t j = 0;
260 for (; j < plugins.size(); ++j) {
261 if (plugins[j].name == base::ASCIIToUTF16(expected[i]))
262 break;
264 ASSERT_TRUE(j != plugins.size()) << "Didn't find " << expected[i];
267 #endif