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/file_util.h"
7 #include "base/path_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/shell/common/shell_switches.h"
13 #include "content/shell/shell.h"
14 #include "content/test/content_browser_test.h"
15 #include "content/test/content_browser_test_utils.h"
16 #include "content/test/net/url_request_mock_http_job.h"
17 #include "ui/gfx/rect.h"
18 #include "webkit/plugins/plugin_switches.h"
21 #include "base/win/registry.h"
24 // TODO(jschuh): Finish plugins on Win64. crbug.com/180861
25 #if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
26 #define MAYBE(x) DISABLED_##x
34 void SetUrlRequestMock(const base::FilePath
& path
) {
35 URLRequestMockHTTPJob::AddUrlHandler(path
);
40 class PluginTest
: public ContentBrowserTest
{
44 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
45 // Some NPAPI tests schedule garbage collection to force object tear-down.
46 command_line
->AppendSwitchASCII(switches::kJavaScriptFlags
, "--expose_gc");
49 const testing::TestInfo
* const test_info
=
50 testing::UnitTest::GetInstance()->current_test_info();
51 if (strcmp(test_info
->name(), "MediaPlayerNew") == 0) {
52 // The installer adds our process names to the registry key below. Since
53 // the installer might not have run on this machine, add it manually.
54 base::win::RegKey regkey
;
55 if (regkey
.Open(HKEY_LOCAL_MACHINE
,
56 L
"Software\\Microsoft\\MediaPlayer\\ShimInclusionList",
57 KEY_WRITE
) == ERROR_SUCCESS
) {
58 regkey
.CreateKey(L
"BROWSER_TESTS.EXE", KEY_READ
);
60 } else if (strcmp(test_info
->name(), "MediaPlayerOld") == 0) {
61 // When testing the old WMP plugin, we need to force Chrome to not load
63 command_line
->AppendSwitch(switches::kUseOldWMPPlugin
);
64 } else if (strcmp(test_info
->name(), "FlashSecurity") == 0) {
65 command_line
->AppendSwitchASCII(switches::kTestSandbox
,
66 "security_tests.dll");
68 #elif defined(OS_MACOSX)
69 base::FilePath plugin_dir
;
70 PathService::Get(base::DIR_MODULE
, &plugin_dir
);
71 plugin_dir
= plugin_dir
.AppendASCII("plugins");
72 // The plugins directory isn't read by default on the Mac, so it needs to be
73 // explicitly registered.
74 command_line
->AppendSwitchPath(switches::kExtraPluginDir
, plugin_dir
);
78 virtual void SetUpOnMainThread() OVERRIDE
{
79 base::FilePath path
= GetTestFilePath("", "");
80 BrowserThread::PostTask(
81 BrowserThread::IO
, FROM_HERE
, base::Bind(&SetUrlRequestMock
, path
));
84 static void LoadAndWaitInWindow(Shell
* window
, const GURL
& url
) {
85 string16
expected_title(ASCIIToUTF16("OK"));
86 TitleWatcher
title_watcher(window
->web_contents(), expected_title
);
87 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
88 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("plugin_not_found"));
89 NavigateToURL(window
, url
);
90 string16 title
= title_watcher
.WaitAndGetTitle();
91 if (title
== ASCIIToUTF16("plugin_not_found")) {
92 const testing::TestInfo
* const test_info
=
93 testing::UnitTest::GetInstance()->current_test_info();
94 LOG(INFO
) << "PluginTest." << test_info
->name() <<
95 " not running because plugin not installed.";
97 EXPECT_EQ(expected_title
, title
);
101 void LoadAndWait(const GURL
& url
) {
102 LoadAndWaitInWindow(shell(), url
);
105 GURL
GetURL(const char* filename
) {
106 return GetTestUrl("npapi", filename
);
109 void NavigateAway() {
110 GURL url
= GetTestUrl("", "simple_page.html");
114 void TestPlugin(const char* filename
) {
115 base::FilePath path
= GetTestFilePath("plugin", filename
);
116 if (!file_util::PathExists(path
)) {
117 const testing::TestInfo
* const test_info
=
118 testing::UnitTest::GetInstance()->current_test_info();
119 LOG(INFO
) << "PluginTest." << test_info
->name() <<
120 " not running because test data wasn't found.";
124 GURL url
= GetTestUrl("plugin", filename
);
129 // Make sure that navigating away from a plugin referenced by JS doesn't
131 IN_PROC_BROWSER_TEST_F(PluginTest
, UnloadNoCrash
) {
132 LoadAndWait(GetURL("layout_test_plugin.html"));
136 // Tests if a plugin executing a self deleting script using NPN_GetURL
137 // works without crashing or hanging
138 // Flaky: http://crbug.com/59327
139 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(SelfDeletePluginGetUrl
)) {
140 LoadAndWait(GetURL("self_delete_plugin_geturl.html"));
143 // Tests if a plugin executing a self deleting script using Invoke
144 // works without crashing or hanging
145 // Flaky. See http://crbug.com/30702
146 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(SelfDeletePluginInvoke
)) {
147 LoadAndWait(GetURL("self_delete_plugin_invoke.html"));
150 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NPObjectReleasedOnDestruction
)) {
151 NavigateToURL(shell(), GetURL("npobject_released_on_destruction.html"));
155 // Test that a dialog is properly created when a plugin throws an
156 // exception. Should be run for in and out of process plugins, but
157 // the more interesting case is out of process, where we must route
158 // the exception to the correct renderer.
159 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NPObjectSetException
)) {
160 LoadAndWait(GetURL("npobject_set_exception.html"));
164 // Tests if a plugin executing a self deleting script in the context of
165 // a synchronous mouseup works correctly.
166 // This was never ported to Mac. The only thing remaining is to make
167 // SimulateMouseClick get to Mac plugins, currently it doesn't work.
168 IN_PROC_BROWSER_TEST_F(PluginTest
,
169 MAYBE(SelfDeletePluginInvokeInSynchronousMouseUp
)) {
170 NavigateToURL(shell(), GetURL("execute_script_delete_in_mouse_up.html"));
172 string16
expected_title(ASCIIToUTF16("OK"));
173 TitleWatcher
title_watcher(shell()->web_contents(), expected_title
);
174 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
175 SimulateMouseClick(shell()->web_contents(), 0,
176 WebKit::WebMouseEvent::ButtonLeft
);
177 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
181 // Flaky, http://crbug.com/60071.
182 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(GetURLRequest404Response
)) {
183 GURL
url(URLRequestMockHTTPJob::GetMockUrl(
184 base::FilePath().AppendASCII("npapi").
185 AppendASCII("plugin_url_request_404.html")));
189 // Tests if a plugin executing a self deleting script using Invoke with
190 // a modal dialog showing works without crashing or hanging
191 // Disabled, flakily exceeds timeout, http://crbug.com/46257.
192 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(SelfDeletePluginInvokeAlert
)) {
193 // Navigate asynchronously because if we waitd until it completes, there's a
194 // race condition where the alert can come up before we start watching for it.
195 shell()->LoadURL(GetURL("self_delete_plugin_invoke_alert.html"));
197 string16
expected_title(ASCIIToUTF16("OK"));
198 TitleWatcher
title_watcher(shell()->web_contents(), expected_title
);
199 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
201 WaitForAppModalDialog(shell());
203 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
206 // Test passing arguments to a plugin.
207 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(Arguments
)) {
208 LoadAndWait(GetURL("arguments.html"));
211 // Test invoking many plugins within a single page.
212 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(ManyPlugins
)) {
213 LoadAndWait(GetURL("many_plugins.html"));
216 // Test various calls to GetURL from a plugin.
217 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(GetURL
)) {
218 LoadAndWait(GetURL("geturl.html"));
221 // Test various calls to GetURL for javascript URLs with
222 // non NULL targets from a plugin.
223 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(GetJavaScriptURL
)) {
224 LoadAndWait(GetURL("get_javascript_url.html"));
227 // Test that calling GetURL with a javascript URL and target=_self
228 // works properly when the plugin is embedded in a subframe.
229 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(GetJavaScriptURL2
)) {
230 LoadAndWait(GetURL("get_javascript_url2.html"));
233 // Test is flaky on linux/cros/win builders. http://crbug.com/71904
234 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_GetURLRedirectNotification
) {
235 LoadAndWait(GetURL("geturl_redirect_notify.html"));
238 // Tests that identity is preserved for NPObjects passed from a plugin
240 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NPObjectIdentity
)) {
241 LoadAndWait(GetURL("npobject_identity.html"));
244 // Tests that if an NPObject is proxies back to its original process, the
245 // original pointer is returned and not a proxy. If this fails the plugin
247 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NPObjectProxy
)) {
248 LoadAndWait(GetURL("npobject_proxy.html"));
251 #if defined(OS_WIN) || defined(OS_MACOSX)
252 // Tests if a plugin executing a self deleting script in the context of
253 // a synchronous paint event works correctly
254 // http://crbug.com/44960
255 IN_PROC_BROWSER_TEST_F(PluginTest
,
256 MAYBE(SelfDeletePluginInvokeInSynchronousPaint
)) {
257 LoadAndWait(GetURL("execute_script_delete_in_paint.html"));
261 // Tests that if a plugin executes a self resizing script in the context of a
262 // synchronous paint, the plugin doesn't use deallocated memory.
263 // http://crbug.com/139462
264 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(ResizeDuringPaint
)) {
265 LoadAndWait(GetURL("resize_during_paint.html"));
268 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(SelfDeletePluginInNewStream
)) {
269 LoadAndWait(GetURL("self_delete_plugin_stream.html"));
272 // This test asserts on Mac in plugin_host in the NPNVWindowNPObject case.
273 #if !(defined(OS_MACOSX) && !defined(NDEBUG))
274 // If this test flakes use http://crbug.com/95558.
275 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(DeletePluginInDeallocate
)) {
276 LoadAndWait(GetURL("plugin_delete_in_deallocate.html"));
282 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(VerifyPluginWindowRect
)) {
283 LoadAndWait(GetURL("verify_plugin_window_rect.html"));
286 // Tests that creating a new instance of a plugin while another one is handling
287 // a paint message doesn't cause deadlock.
288 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(CreateInstanceInPaint
)) {
289 LoadAndWait(GetURL("create_instance_in_paint.html"));
292 // Tests that putting up an alert in response to a paint doesn't deadlock.
293 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_AlertInWindowMessage
) {
294 NavigateToURL(shell(), GetURL("alert_in_window_message.html"));
296 WaitForAppModalDialog(shell());
297 WaitForAppModalDialog(shell());
300 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(VerifyNPObjectLifetimeTest
)) {
301 LoadAndWait(GetURL("npobject_lifetime_test.html"));
304 // Tests that we don't crash or assert if NPP_New fails
305 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NewFails
)) {
306 LoadAndWait(GetURL("new_fails.html"));
309 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(SelfDeletePluginInNPNEvaluate
)) {
310 LoadAndWait(GetURL("execute_script_delete_in_npn_evaluate.html"));
313 IN_PROC_BROWSER_TEST_F(PluginTest
,
314 MAYBE(SelfDeleteCreatePluginInNPNEvaluate
)) {
315 LoadAndWait(GetURL("npn_plugin_delete_create_in_evaluate.html"));
320 // If this flakes, reopen http://crbug.com/17645
321 // As of 6 July 2011, this test is flaky on Windows (perhaps due to timing out).
322 #if !defined(OS_MACOSX)
323 // Disabled on Mac because the plugin side isn't implemented yet, see
324 // "TODO(port)" in plugin_javascript_open_popup.cc.
325 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(OpenPopupWindowWithPlugin
)) {
326 LoadAndWait(GetURL("get_javascript_open_popup_with_plugin.html"));
330 // Test checking the privacy mode is off.
331 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(PrivateDisabled
)) {
332 LoadAndWait(GetURL("private.html"));
335 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(ScheduleTimer
)) {
336 LoadAndWait(GetURL("schedule_timer.html"));
339 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(PluginThreadAsyncCall
)) {
340 LoadAndWait(GetURL("plugin_thread_async_call.html"));
343 // Test checking the privacy mode is on.
344 // If this flakes on Linux, use http://crbug.com/104380
345 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(PrivateEnabled
)) {
346 GURL url
= GetURL("private.html");
347 url
= GURL(url
.spec() + "?private");
348 LoadAndWaitInWindow(CreateOffTheRecordBrowser(), url
);
351 #if defined(OS_WIN) || defined(OS_MACOSX)
352 // Test a browser hang due to special case of multiple
353 // plugin instances indulged in sync calls across renderer.
354 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(MultipleInstancesSyncCalls
)) {
355 LoadAndWait(GetURL("multiple_instances_sync_calls.html"));
359 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(GetURLRequestFailWrite
)) {
360 GURL
url(URLRequestMockHTTPJob::GetMockUrl(
361 base::FilePath().AppendASCII("npapi").
362 AppendASCII("plugin_url_request_fail_write.html")));
367 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(EnsureScriptingWorksInDestroy
)) {
368 LoadAndWait(GetURL("ensure_scripting_works_in_destroy.html"));
371 // This test uses a Windows Event to signal to the plugin that it should crash
373 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(NoHangIfInitCrashes
)) {
374 HANDLE crash_event
= CreateEvent(NULL
, TRUE
, FALSE
, L
"TestPluginCrashOnInit");
375 SetEvent(crash_event
);
376 LoadAndWait(GetURL("no_hang_if_init_crashes.html"));
377 CloseHandle(crash_event
);
381 // If this flakes on Mac, use http://crbug.com/111508
382 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(PluginReferrerTest
)) {
383 GURL
url(URLRequestMockHTTPJob::GetMockUrl(
384 base::FilePath().AppendASCII("npapi").
385 AppendASCII("plugin_url_request_referrer_test.html")));
389 #if defined(OS_MACOSX)
390 // Test is flaky, see http://crbug.com/134515.
391 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_PluginConvertPointTest
) {
392 gfx::Rect
bounds(50, 50, 400, 400);
393 SetWindowBounds(shell()->window(), bounds
);
395 NavigateToURL(shell(), GetURL("convert_point.html"));
397 string16
expected_title(ASCIIToUTF16("OK"));
398 TitleWatcher
title_watcher(shell()->web_contents(), expected_title
);
399 title_watcher
.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
400 // TODO(stuartmorgan): When the automation system supports sending clicks,
401 // change the test to trigger on mouse-down rather than window focus.
403 // TODO: is this code still needed? It was here when it used to run in
405 //static_cast<WebContentsDelegate*>(shell())->
406 // ActivateContents(shell()->web_contents());
407 EXPECT_EQ(expected_title
, title_watcher
.WaitAndGetTitle());
411 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(Flash
)) {
412 TestPlugin("flash.html");
417 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_FlashSecurity
) {
418 TestPlugin("flash.html");
420 #endif // defined(OS_WIN)
423 // TODO(port) Port the following tests to platforms that have the required
425 // Flaky: http://crbug.com/55915
426 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(Quicktime
)) {
427 TestPlugin("quicktime.html");
430 // Disabled - http://crbug.com/44662
431 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(MediaPlayerNew
)) {
432 TestPlugin("wmp_new.html");
435 // http://crbug.com/4809
436 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_MediaPlayerOld
) {
437 TestPlugin("wmp_old.html");
440 // Disabled - http://crbug.com/44673
441 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(Real
)) {
442 TestPlugin("real.html");
445 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(FlashOctetStream
)) {
446 TestPlugin("flash-octet-stream.html");
450 // http://crbug.com/53926
451 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_FlashLayoutWhilePainting
) {
453 IN_PROC_BROWSER_TEST_F(PluginTest
, FlashLayoutWhilePainting
) {
455 TestPlugin("flash-layout-while-painting.html");
458 // http://crbug.com/8690
459 IN_PROC_BROWSER_TEST_F(PluginTest
, DISABLED_Java
) {
460 TestPlugin("Java.html");
463 IN_PROC_BROWSER_TEST_F(PluginTest
, MAYBE(Silverlight
)) {
464 TestPlugin("silverlight.html");
466 #endif // defined(OS_WIN)
468 } // namespace content