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 "content/renderer/browser_plugin/browser_plugin_browsertest.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" |
| 10 #include "content/common/browser_plugin_messages.h" |
| 11 #include "content/public/common/content_constants.h" |
| 12 #include "content/renderer/browser_plugin/browser_plugin.h" |
| 13 #include "content/renderer/browser_plugin/mock_browser_plugin.h" |
| 14 #include "content/renderer/browser_plugin/mock_browser_plugin_manager.h" |
| 15 #include "content/renderer/render_thread_impl.h" |
| 16 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 17 #include "skia/ext/platform_canvas.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| 21 |
| 22 namespace { |
| 23 const char kHTMLForBrowserPluginObject[] = |
| 24 "<object id='browserplugin' width='640px' height='480px'" |
| 25 " src='foo' type='%s'>"; |
| 26 |
| 27 std::string GetHTMLForBrowserPluginObject() { |
| 28 return StringPrintf(kHTMLForBrowserPluginObject, |
| 29 content::kBrowserPluginNewMimeType); |
| 30 } |
| 31 |
| 32 } |
| 33 |
| 34 namespace content { |
| 35 namespace browser_plugin { |
| 36 |
| 37 BrowserPluginTest::BrowserPluginTest() {} |
| 38 |
| 39 void BrowserPluginTest::SetUp() { |
| 40 GetContentClient()->set_renderer_for_testing(&content_renderer_client_); |
| 41 content::RenderViewTest::SetUp(); |
| 42 browser_plugin_manager_.reset(new MockBrowserPluginManager()); |
| 43 } |
| 44 |
| 45 void BrowserPluginTest::TearDown() { |
| 46 browser_plugin_manager_->Cleanup(); |
| 47 content::RenderViewTest::TearDown(); |
| 48 } |
| 49 |
| 50 std::string BrowserPluginTest::ExecuteScriptAndReturnString( |
| 51 const std::string& script) { |
| 52 v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue( |
| 53 WebKit::WebScriptSource(WebKit::WebString::fromUTF8(script.c_str()))); |
| 54 if (value.IsEmpty() || !value->IsString()) |
| 55 return std::string(); |
| 56 |
| 57 v8::Local<v8::String> v8_str = value->ToString(); |
| 58 int length = v8_str->Utf8Length() + 1; |
| 59 scoped_array<char> str(new char[length]); |
| 60 v8_str->WriteUtf8(str.get(), length); |
| 61 return str.get(); |
| 62 } |
| 63 |
| 64 // This test verifies that an initial resize occurs when we instantiate the |
| 65 // browser plugin. This test also verifies that the browser plugin is waiting |
| 66 // for a BrowserPluginMsg_UpdateRect in response. We issue an UpdateRect, and |
| 67 // we observe an UpdateRect_ACK, with the resize_pending_ reset, indiciating |
| 68 // that the BrowserPlugin is not waiting for any more UpdateRects to |
| 69 // satisfy its resize request. |
| 70 TEST_F(BrowserPluginTest, InitialResize) { |
| 71 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 72 // Verify that the information based on ResizeGuest is correct, and |
| 73 // use its TransportDIB::Id to paint. |
| 74 const IPC::Message* msg = |
| 75 browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 76 BrowserPluginHostMsg_ResizeGuest::ID); |
| 77 ASSERT_TRUE(msg); |
| 78 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 79 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 80 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 81 int instance_id = resize_params.a; |
| 82 int width = resize_params.c; |
| 83 int height = resize_params.d; |
| 84 bool resize_pending = resize_params.e; |
| 85 EXPECT_EQ(640, width); |
| 86 EXPECT_EQ(480, height); |
| 87 // Verify that the browser plugin wasn't already waiting on a resize when this |
| 88 // resize happened. |
| 89 EXPECT_EQ(false, resize_pending); |
| 90 |
| 91 MockBrowserPlugin* browser_plugin = |
| 92 static_cast<MockBrowserPlugin*>( |
| 93 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 94 ASSERT_TRUE(browser_plugin); |
| 95 // Now the browser plugin is expecting a UpdateRect resize. |
| 96 EXPECT_TRUE(browser_plugin->resize_pending_); |
| 97 |
| 98 // Send the BrowserPlugin an UpdateRect equal to its container size. |
| 99 // That should clear the resize_pending_ flag. |
| 100 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 101 update_rect_params.view_size = gfx::Size(640, 480); |
| 102 update_rect_params.scale_factor = 1.0f; |
| 103 update_rect_params.is_resize_ack = true; |
| 104 browser_plugin->UpdateRect(0, update_rect_params); |
| 105 EXPECT_FALSE(browser_plugin->resize_pending_); |
| 106 } |
| 107 |
| 108 // Verify that the src attribute on the browser plugin works as expected. |
| 109 TEST_F(BrowserPluginTest, SrcAttribute) { |
| 110 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 111 // Verify that we're reporting the correct URL to navigate to based on the |
| 112 // src attribute. |
| 113 { |
| 114 const IPC::Message* msg = |
| 115 browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 116 BrowserPluginHostMsg_NavigateOrCreateGuest::ID); |
| 117 ASSERT_TRUE(msg); |
| 118 |
| 119 int instance_id; |
| 120 long long frame_id; |
| 121 std::string src; |
| 122 BrowserPluginHostMsg_NavigateOrCreateGuest::Read( |
| 123 msg, |
| 124 &instance_id, |
| 125 &frame_id, |
| 126 &src); |
| 127 EXPECT_EQ("foo", src); |
| 128 } |
| 129 |
| 130 browser_plugin_manager()->sink().ClearMessages(); |
| 131 // Navigate to bar and observe the associated |
| 132 // BrowserPluginHostMsg_NavigateOrCreateGuest message. |
| 133 // Verify that the src attribute is updated as well. |
| 134 ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'"); |
| 135 { |
| 136 const IPC::Message* msg = |
| 137 browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 138 BrowserPluginHostMsg_NavigateOrCreateGuest::ID); |
| 139 ASSERT_TRUE(msg); |
| 140 |
| 141 int instance_id; |
| 142 long long frame_id; |
| 143 std::string src; |
| 144 BrowserPluginHostMsg_NavigateOrCreateGuest::Read( |
| 145 msg, |
| 146 &instance_id, |
| 147 &frame_id, |
| 148 &src); |
| 149 EXPECT_EQ("bar", src); |
| 150 std::string src_value = |
| 151 ExecuteScriptAndReturnString( |
| 152 "document.getElementById('browserplugin').src"); |
| 153 EXPECT_EQ("bar", src_value); |
| 154 } |
| 155 } |
| 156 |
| 157 TEST_F(BrowserPluginTest, ResizeFlowControl) { |
| 158 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 159 browser_plugin_manager()->sink().ClearMessages(); |
| 160 |
| 161 // Resize the browser plugin three times. |
| 162 ExecuteJavaScript("document.getElementById('browserplugin').width = '641px'"); |
| 163 ProcessPendingMessages(); |
| 164 ExecuteJavaScript("document.getElementById('browserplugin').width = '642px'"); |
| 165 ProcessPendingMessages(); |
| 166 ExecuteJavaScript("document.getElementById('browserplugin').width = '643px'"); |
| 167 ProcessPendingMessages(); |
| 168 |
| 169 // Expect to see three messsages in the sink. |
| 170 EXPECT_EQ(3u, browser_plugin_manager()->sink().message_count()); |
| 171 const IPC::Message* msg = |
| 172 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 173 BrowserPluginHostMsg_ResizeGuest::ID); |
| 174 ASSERT_TRUE(msg); |
| 175 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 176 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 177 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 178 int instance_id = resize_params.a; |
| 179 int width = resize_params.c; |
| 180 int height = resize_params.d; |
| 181 bool resize_pending = resize_params.e; |
| 182 EXPECT_EQ(641, width); |
| 183 EXPECT_EQ(480, height); |
| 184 // This indicates that the BrowserPlugin has sent out a previous resize |
| 185 // request but has not yet received an UpdateRect for that request. |
| 186 // We send this resize regardless to update the damage buffer in the |
| 187 // browser process, so it's ready when the guest sends the appropriate |
| 188 // UpdateRect. |
| 189 EXPECT_TRUE(resize_pending); |
| 190 |
| 191 MockBrowserPlugin* browser_plugin = |
| 192 static_cast<MockBrowserPlugin*>( |
| 193 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 194 ASSERT_TRUE(browser_plugin); |
| 195 { |
| 196 // We send a stale UpdateRect to the BrowserPlugin. |
| 197 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 198 update_rect_params.view_size = gfx::Size(640, 480); |
| 199 update_rect_params.scale_factor = 1.0f; |
| 200 update_rect_params.is_resize_ack = true; |
| 201 browser_plugin->UpdateRect(0, update_rect_params); |
| 202 // This tells us that the BrowserPlugin is still expecting another |
| 203 // UpdateRect with the most recent size. |
| 204 EXPECT_TRUE(browser_plugin->resize_pending_); |
| 205 } |
| 206 { |
| 207 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 208 update_rect_params.view_size = gfx::Size(643, 480); |
| 209 update_rect_params.scale_factor = 1.0f; |
| 210 update_rect_params.is_resize_ack = true; |
| 211 browser_plugin->UpdateRect(0, update_rect_params); |
| 212 // The BrowserPlugin has finally received an UpdateRect that satisifes |
| 213 // its current size, and so it is happy. |
| 214 EXPECT_FALSE(browser_plugin->resize_pending_); |
| 215 } |
| 216 } |
| 217 |
| 218 TEST_F(BrowserPluginTest, GuestCrash) { |
| 219 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 220 |
| 221 // Grab the BrowserPlugin's instance ID from its resize message. |
| 222 const IPC::Message* msg = |
| 223 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 224 BrowserPluginHostMsg_ResizeGuest::ID); |
| 225 ASSERT_TRUE(msg); |
| 226 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 227 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 228 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 229 int instance_id = resize_params.a; |
| 230 |
| 231 MockBrowserPlugin* browser_plugin = |
| 232 static_cast<MockBrowserPlugin*>( |
| 233 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 234 ASSERT_TRUE(browser_plugin); |
| 235 |
| 236 WebKit::WebCursorInfo cursor_info; |
| 237 // Send an event and verify that the event is deported. |
| 238 browser_plugin->handleInputEvent(WebKit::WebMouseEvent(), |
| 239 cursor_info); |
| 240 EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 241 BrowserPluginHostMsg_HandleInputEvent::ID)); |
| 242 browser_plugin_manager()->sink().ClearMessages(); |
| 243 |
| 244 // Pretend that the guest has crashed |
| 245 browser_plugin->GuestCrashed(); |
| 246 // Send an event and verify that events are no longer deported. |
| 247 browser_plugin->handleInputEvent(WebKit::WebMouseEvent(), |
| 248 cursor_info); |
| 249 EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 250 BrowserPluginHostMsg_HandleInputEvent::ID)); |
| 251 |
| 252 // Navigate and verify that the guest_crashed_ flag has been reset. |
| 253 browser_plugin->SetSrcAttribute("bar"); |
| 254 EXPECT_FALSE(browser_plugin->guest_crashed_); |
| 255 |
| 256 } |
| 257 |
| 258 TEST_F(BrowserPluginTest, RemovePlugin) { |
| 259 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 260 EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 261 BrowserPluginHostMsg_PluginDestroyed::ID)); |
| 262 ExecuteJavaScript("x = document.getElementById('browserplugin'); " |
| 263 "x.parentNode.removeChild(x);"); |
| 264 ProcessPendingMessages(); |
| 265 EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 266 BrowserPluginHostMsg_PluginDestroyed::ID)); |
| 267 } |
| 268 |
| 269 TEST_F(BrowserPluginTest, CustomEvents) { |
| 270 const char* kAddEventListener = |
| 271 "var url;" |
| 272 "function nav(u) {" |
| 273 " url = u;" |
| 274 "}" |
| 275 "document.getElementById('browserplugin')." |
| 276 " addEventListener('navigation', nav);"; |
| 277 const char* kRemoveEventListener = |
| 278 "document.getElementById('browserplugin')." |
| 279 " removeEventListener('navigation', nav);"; |
| 280 const char* kGoogleURL = "http://www.google.com/"; |
| 281 const char* kGoogleNewsURL = "http://news.google.com/"; |
| 282 |
| 283 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 284 ExecuteJavaScript(kAddEventListener); |
| 285 // Grab the BrowserPlugin's instance ID from its resize message. |
| 286 const IPC::Message* msg = |
| 287 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 288 BrowserPluginHostMsg_ResizeGuest::ID); |
| 289 ASSERT_TRUE(msg); |
| 290 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 291 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 292 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 293 int instance_id = resize_params.a; |
| 294 |
| 295 MockBrowserPlugin* browser_plugin = |
| 296 static_cast<MockBrowserPlugin*>( |
| 297 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 298 ASSERT_TRUE(browser_plugin); |
| 299 |
| 300 browser_plugin->DidNavigate(GURL(kGoogleURL)); |
| 301 EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url")); |
| 302 |
| 303 ExecuteJavaScript(kRemoveEventListener); |
| 304 browser_plugin->DidNavigate(GURL(kGoogleNewsURL)); |
| 305 // The URL variable should not change because we've removed the event |
| 306 // listener. |
| 307 EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url")); |
| 308 } |
| 309 |
| 310 } |
| 311 } |
OLD | NEW |