Chromium Code Reviews| Index: content/browser/browser_plugin/browser_plugin_host_browsertest.cc |
| diff --git a/content/browser/browser_plugin/browser_plugin_host_browsertest.cc b/content/browser/browser_plugin/browser_plugin_host_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3cd3230657f2a57f0e3d36164a67a65df294475b |
| --- /dev/null |
| +++ b/content/browser/browser_plugin/browser_plugin_host_browsertest.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/singleton.h" |
| +#include "base/run_loop.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "content/browser/browser_plugin/browser_plugin_guest.h" |
| +#include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
| +#include "content/browser/browser_plugin/test_browser_plugin_embedder.h" |
| +#include "content/browser/browser_plugin/test_browser_plugin_guest.h" |
| +#include "content/browser/renderer_host/render_view_host_impl.h" |
| +#include "content/browser/web_contents/web_contents_impl.h" |
| +#include "content/common/view_messages.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "content/shell/shell.h" |
| +#include "content/test/content_browser_test_utils.h" |
| +#include "content/test/content_browser_test.h" |
| +#include "ipc/ipc_message.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| + |
| +using WebKit::WebInputEvent; |
| +using WebKit::WebMouseEvent; |
| +using content::BrowserPluginEmbedder; |
| +using content::BrowserPluginGuest; |
| +using content::BrowserPluginHostFactory; |
| + |
| +template<typename T> struct DefaultSingletonTraits; |
|
awong
2012/09/06 00:23:27
This is a .cc file so you should just #include the
lazyboy
2012/09/06 04:33:53
Done.
|
| + |
| +namespace content { |
| + |
| +// Test factory for creating test instances of BrowserPluginEmbedder and |
| +// BrowserPluginGuest. |
| +class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { |
| + public: |
| + virtual BrowserPluginGuest* CreateBrowserPluginGuest( |
| + int instance_id, |
| + WebContentsImpl* web_contents, |
| + RenderViewHost* render_view_host) OVERRIDE { |
| + return new TestBrowserPluginGuest(instance_id, |
| + web_contents, |
| + render_view_host); |
| + } |
| + |
| + // Also keeps track of number of instances created. |
| + virtual BrowserPluginEmbedder* CreateBrowserPluginEmbedder( |
| + WebContentsImpl* web_contents, |
| + RenderViewHost* render_view_host) OVERRIDE { |
| + embedder_instance_count_++; |
| + if (message_loop_runner_) |
| + message_loop_runner_->Quit(); |
| + |
| + return new TestBrowserPluginEmbedder(web_contents, render_view_host); |
| + } |
| + |
| + // Singleton getter. |
| + static TestBrowserPluginHostFactory* GetInstance() { |
| + return Singleton<TestBrowserPluginHostFactory>::get(); |
| + } |
| + |
| + // Waits for at least one embedder to be created in the test. |
| + void WaitForEmbedderCreation() { |
| + // Check if already have created instance. |
| + if (embedder_instance_count_ > 0) |
| + return; |
| + // Wait otherwise. |
| + message_loop_runner_ = new MessageLoopRunner(); |
| + message_loop_runner_->Run(); |
| + } |
| + |
| + private: |
| + // For Singleton. |
| + friend struct DefaultSingletonTraits<TestBrowserPluginHostFactory>; |
| + |
| + TestBrowserPluginHostFactory() : embedder_instance_count_(0) {} |
| + virtual ~TestBrowserPluginHostFactory() {} |
| + |
| + scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| + int embedder_instance_count_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginHostFactory); |
| +}; |
| + |
| +class BrowserPluginHostTest : public ContentBrowserTest { |
| + public: |
| + BrowserPluginHostTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + // Override factory to create tests instances of BrowserPlugin*. |
| + content::BrowserPluginEmbedder::set_factory_for_testing( |
| + TestBrowserPluginHostFactory::GetInstance()); |
| + content::BrowserPluginGuest::set_factory_for_testing( |
| + TestBrowserPluginHostFactory::GetInstance()); |
| + |
| + ContentBrowserTest::SetUp(); |
| + } |
| + virtual void TearDown() OVERRIDE { |
| + content::BrowserPluginEmbedder::set_factory_for_testing(NULL); |
| + content::BrowserPluginGuest::set_factory_for_testing(NULL); |
| + |
| + ContentBrowserTest::TearDown(); |
| + } |
| +}; |
| + |
| +// This test loads a guest that has infinite loop, therefore it hangs the guest |
| +// and eventually gets killed. |
| +IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateGuest) { |
| + ASSERT_TRUE(test_server()->Start()); |
| + GURL test_url(test_server()->GetURL( |
| + "files/browser_plugin_infinite_loop.html")); |
| + NavigateToURL(shell(), test_url); |
| + |
| + WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( |
| + shell()->web_contents()); |
| + RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| + embedder_web_contents->GetRenderViewHost()); |
| + |
| + test_url = test_server()->GetURL( |
| + "files/browser_plugin_infinite_loop_child.html"); |
| + rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| + StringPrintf("SetSrc('%s');", test_url.spec().c_str()))); |
| + |
| + // Wait to make sure embedder is created/attached to WebContents. |
| + TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation(); |
| + |
| + TestBrowserPluginEmbedder* test_embedder = |
| + static_cast<TestBrowserPluginEmbedder*>( |
| + embedder_web_contents->GetBrowserPluginEmbedder()); |
| + DCHECK(test_embedder); |
|
awong
2012/09/06 00:23:27
Prefer ASSERT_TRUE to DCHECK in unittests. Otherwi
lazyboy
2012/09/06 04:33:53
Done.
|
| + test_embedder->WaitForGuestAdded(); |
| + |
| + // Verify that we have exactly one guest. |
| + const ContainerInstanceMap& instance_map = test_embedder-> |
| + guests_for_testing(); |
| + EXPECT_EQ(1u, instance_map.size()); |
| + |
| + TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( |
| + instance_map.begin()->second); |
| + |
| + // Wait for the guest to send an UpdateRectMsg, meaning it is ready. |
| + test_guest->WaitForUpdateRectMsg(); |
| + |
| + WebKit::WebMouseEvent mouse_event; |
| + mouse_event.type = WebInputEvent::MouseDown; |
| + mouse_event.button = WebMouseEvent::ButtonMiddle; |
| + mouse_event.x = 35; |
| + mouse_event.y = 35; |
| + mouse_event.globalX = 35; |
| + mouse_event.globalY = 35; |
| + |
| + IPC::Message* input_message = new ViewMsg_HandleInputEvent( |
| + embedder_web_contents->GetRenderViewHost()->GetRoutingID()); |
| + input_message->WriteData(reinterpret_cast<const char*>(&mouse_event), |
| + sizeof(WebKit::WebMouseEvent)); |
| + embedder_web_contents->GetRenderViewHost()->Send(input_message); |
| + |
| + // Expect the guest to crash. |
| + test_guest->WaitForCrashed(); |
| +} |
| + |
| +} |