OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/memory/singleton.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "content/browser/browser_plugin/browser_plugin_guest.h" |
| 9 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
| 10 #include "content/browser/browser_plugin/test_browser_plugin_embedder.h" |
| 11 #include "content/browser/browser_plugin/test_browser_plugin_guest.h" |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 13 #include "content/browser/web_contents/web_contents_impl.h" |
| 14 #include "content/common/view_messages.h" |
| 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/notification_types.h" |
| 17 #include "content/public/test/test_utils.h" |
| 18 #include "content/shell/shell.h" |
| 19 #include "content/test/content_browser_test_utils.h" |
| 20 #include "content/test/content_browser_test.h" |
| 21 #include "ipc/ipc_message.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 23 |
| 24 using WebKit::WebInputEvent; |
| 25 using WebKit::WebMouseEvent; |
| 26 using content::BrowserPluginEmbedder; |
| 27 using content::BrowserPluginGuest; |
| 28 using content::BrowserPluginHostFactory; |
| 29 |
| 30 template<typename T> struct DefaultSingletonTraits; |
| 31 |
| 32 namespace content { |
| 33 |
| 34 // Test factory for creating test instances of BrowserPluginEmbedder and |
| 35 // BrowserPluginGuest. |
| 36 class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { |
| 37 public: |
| 38 virtual BrowserPluginGuest* CreateBrowserPluginGuest( |
| 39 int instance_id, |
| 40 WebContentsImpl* web_contents, |
| 41 RenderViewHost* render_view_host) OVERRIDE { |
| 42 return new TestBrowserPluginGuest(instance_id, |
| 43 web_contents, |
| 44 render_view_host); |
| 45 } |
| 46 |
| 47 // Also keeps track of number of instances created. |
| 48 virtual BrowserPluginEmbedder* CreateBrowserPluginEmbedder( |
| 49 WebContentsImpl* web_contents, |
| 50 RenderViewHost* render_view_host) OVERRIDE { |
| 51 embedder_instance_count_++; |
| 52 if (message_loop_runner_) |
| 53 message_loop_runner_->Quit(); |
| 54 |
| 55 return new TestBrowserPluginEmbedder(web_contents, render_view_host); |
| 56 } |
| 57 |
| 58 // Singleton getter. |
| 59 static TestBrowserPluginHostFactory* GetInstance() { |
| 60 return Singleton<TestBrowserPluginHostFactory>::get(); |
| 61 } |
| 62 |
| 63 // Waits for at least one embedder to be created in the test. |
| 64 void WaitForEmbedderCreation() { |
| 65 // Check if already have created instance. |
| 66 if (embedder_instance_count_ > 0) |
| 67 return; |
| 68 // Wait otherwise. |
| 69 message_loop_runner_ = new MessageLoopRunner(); |
| 70 message_loop_runner_->Run(); |
| 71 } |
| 72 |
| 73 private: |
| 74 // For Singleton. |
| 75 friend struct DefaultSingletonTraits<TestBrowserPluginHostFactory>; |
| 76 |
| 77 TestBrowserPluginHostFactory() : embedder_instance_count_(0) {} |
| 78 virtual ~TestBrowserPluginHostFactory() {} |
| 79 |
| 80 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 81 int embedder_instance_count_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginHostFactory); |
| 84 }; |
| 85 |
| 86 class BrowserPluginHostTest : public ContentBrowserTest { |
| 87 public: |
| 88 BrowserPluginHostTest() {} |
| 89 |
| 90 virtual void SetUp() OVERRIDE { |
| 91 // Override factory to create tests instances of BrowserPlugin*. |
| 92 content::BrowserPluginEmbedder::set_factory_for_testing( |
| 93 TestBrowserPluginHostFactory::GetInstance()); |
| 94 content::BrowserPluginGuest::set_factory_for_testing( |
| 95 TestBrowserPluginHostFactory::GetInstance()); |
| 96 |
| 97 ContentBrowserTest::SetUp(); |
| 98 } |
| 99 virtual void TearDown() OVERRIDE { |
| 100 content::BrowserPluginEmbedder::set_factory_for_testing(NULL); |
| 101 content::BrowserPluginGuest::set_factory_for_testing(NULL); |
| 102 |
| 103 ContentBrowserTest::TearDown(); |
| 104 } |
| 105 }; |
| 106 |
| 107 // This test loads a guest that has infinite loop, therefore it hangs the guest |
| 108 // and eventually gets killed. |
| 109 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateGuest) { |
| 110 ASSERT_TRUE(test_server()->Start()); |
| 111 GURL test_url(test_server()->GetURL( |
| 112 "files/browser_plugin_infinite_loop.html")); |
| 113 NavigateToURL(shell(), test_url); |
| 114 |
| 115 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( |
| 116 shell()->web_contents()); |
| 117 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 118 embedder_web_contents->GetRenderViewHost()); |
| 119 |
| 120 test_url = test_server()->GetURL( |
| 121 "files/browser_plugin_infinite_loop_child.html"); |
| 122 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 123 StringPrintf("SetSrc('%s');", test_url.spec().c_str()))); |
| 124 |
| 125 // Wait to make sure embedder is created/attached to WebContents. |
| 126 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation(); |
| 127 |
| 128 TestBrowserPluginEmbedder* test_embedder = |
| 129 static_cast<TestBrowserPluginEmbedder*>( |
| 130 embedder_web_contents->GetBrowserPluginEmbedder()); |
| 131 DCHECK(test_embedder); |
| 132 test_embedder->WaitForGuestAdded(); |
| 133 |
| 134 // Verify that we have exactly one guest. |
| 135 const ContainerInstanceMap& instance_map = test_embedder-> |
| 136 guests_for_testing(); |
| 137 EXPECT_EQ(1u, instance_map.size()); |
| 138 |
| 139 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( |
| 140 instance_map.begin()->second); |
| 141 |
| 142 // Wait for the guest to send an UpdateRectMsg, meaning it is ready. |
| 143 test_guest->WaitForUpdateRectMsg(); |
| 144 |
| 145 WebKit::WebMouseEvent mouse_event; |
| 146 mouse_event.type = WebInputEvent::MouseDown; |
| 147 mouse_event.button = WebMouseEvent::ButtonMiddle; |
| 148 mouse_event.x = 35; |
| 149 mouse_event.y = 35; |
| 150 mouse_event.globalX = 35; |
| 151 mouse_event.globalY = 35; |
| 152 |
| 153 IPC::Message* input_message = new ViewMsg_HandleInputEvent( |
| 154 embedder_web_contents->GetRenderViewHost()->GetRoutingID()); |
| 155 input_message->WriteData(reinterpret_cast<const char*>(&mouse_event), |
| 156 sizeof(WebKit::WebMouseEvent)); |
| 157 embedder_web_contents->GetRenderViewHost()->Send(input_message); |
| 158 |
| 159 // Expect the guest to crash. |
| 160 test_guest->WaitForCrashed(); |
| 161 } |
| 162 |
| 163 } |
OLD | NEW |