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 BrowserPluginHostMsg_ResizeGuest_Params params(resize_params.b); |
| 83 EXPECT_EQ(640, params.width); |
| 84 EXPECT_EQ(480, params.height); |
| 85 // Verify that the browser plugin wasn't already waiting on a resize when this |
| 86 // resize happened. |
| 87 EXPECT_EQ(false, params.resize_pending); |
| 88 |
| 89 MockBrowserPlugin* browser_plugin = |
| 90 static_cast<MockBrowserPlugin*>( |
| 91 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 92 ASSERT_TRUE(browser_plugin); |
| 93 // Now the browser plugin is expecting a UpdateRect resize. |
| 94 EXPECT_TRUE(browser_plugin->resize_pending_); |
| 95 |
| 96 // Send the BrowserPlugin an UpdateRect equal to its container size. |
| 97 // That should clear the resize_pending_ flag. |
| 98 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 99 update_rect_params.view_size = gfx::Size(640, 480); |
| 100 update_rect_params.scale_factor = 1.0f; |
| 101 update_rect_params.is_resize_ack = true; |
| 102 browser_plugin->UpdateRect(0, update_rect_params); |
| 103 EXPECT_FALSE(browser_plugin->resize_pending_); |
| 104 } |
| 105 |
| 106 // Verify that the src attribute on the browser plugin works as expected. |
| 107 TEST_F(BrowserPluginTest, SrcAttribute) { |
| 108 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 109 // Verify that we're reporting the correct URL to navigate to based on the |
| 110 // src attribute. |
| 111 { |
| 112 const IPC::Message* msg = |
| 113 browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 114 BrowserPluginHostMsg_NavigateOrCreateGuest::ID); |
| 115 ASSERT_TRUE(msg); |
| 116 |
| 117 int instance_id; |
| 118 long long frame_id; |
| 119 std::string src; |
| 120 BrowserPluginHostMsg_NavigateOrCreateGuest::Read( |
| 121 msg, |
| 122 &instance_id, |
| 123 &frame_id, |
| 124 &src); |
| 125 EXPECT_EQ("foo", src); |
| 126 } |
| 127 |
| 128 browser_plugin_manager()->sink().ClearMessages(); |
| 129 // Navigate to bar and observe the associated |
| 130 // BrowserPluginHostMsg_NavigateOrCreateGuest message. |
| 131 // Verify that the src attribute is updated as well. |
| 132 ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'"); |
| 133 { |
| 134 const IPC::Message* msg = |
| 135 browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 136 BrowserPluginHostMsg_NavigateOrCreateGuest::ID); |
| 137 ASSERT_TRUE(msg); |
| 138 |
| 139 int instance_id; |
| 140 long long frame_id; |
| 141 std::string src; |
| 142 BrowserPluginHostMsg_NavigateOrCreateGuest::Read( |
| 143 msg, |
| 144 &instance_id, |
| 145 &frame_id, |
| 146 &src); |
| 147 EXPECT_EQ("bar", src); |
| 148 std::string src_value = |
| 149 ExecuteScriptAndReturnString( |
| 150 "document.getElementById('browserplugin').src"); |
| 151 EXPECT_EQ("bar", src_value); |
| 152 } |
| 153 } |
| 154 |
| 155 TEST_F(BrowserPluginTest, ResizeFlowControl) { |
| 156 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 157 browser_plugin_manager()->sink().ClearMessages(); |
| 158 |
| 159 // Resize the browser plugin three times. |
| 160 ExecuteJavaScript("document.getElementById('browserplugin').width = '641px'"); |
| 161 ProcessPendingMessages(); |
| 162 ExecuteJavaScript("document.getElementById('browserplugin').width = '642px'"); |
| 163 ProcessPendingMessages(); |
| 164 ExecuteJavaScript("document.getElementById('browserplugin').width = '643px'"); |
| 165 ProcessPendingMessages(); |
| 166 |
| 167 // Expect to see three messsages in the sink. |
| 168 EXPECT_EQ(3u, browser_plugin_manager()->sink().message_count()); |
| 169 const IPC::Message* msg = |
| 170 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 171 BrowserPluginHostMsg_ResizeGuest::ID); |
| 172 ASSERT_TRUE(msg); |
| 173 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 174 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 175 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 176 int instance_id = resize_params.a; |
| 177 BrowserPluginHostMsg_ResizeGuest_Params params(resize_params.b); |
| 178 EXPECT_EQ(641, params.width); |
| 179 EXPECT_EQ(480, params.height); |
| 180 // This indicates that the BrowserPlugin has sent out a previous resize |
| 181 // request but has not yet received an UpdateRect for that request. |
| 182 // We send this resize regardless to update the damage buffer in the |
| 183 // browser process, so it's ready when the guest sends the appropriate |
| 184 // UpdateRect. |
| 185 EXPECT_TRUE(params.resize_pending); |
| 186 |
| 187 MockBrowserPlugin* browser_plugin = |
| 188 static_cast<MockBrowserPlugin*>( |
| 189 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 190 ASSERT_TRUE(browser_plugin); |
| 191 { |
| 192 // We send a stale UpdateRect to the BrowserPlugin. |
| 193 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 194 update_rect_params.view_size = gfx::Size(640, 480); |
| 195 update_rect_params.scale_factor = 1.0f; |
| 196 update_rect_params.is_resize_ack = true; |
| 197 browser_plugin->UpdateRect(0, update_rect_params); |
| 198 // This tells us that the BrowserPlugin is still expecting another |
| 199 // UpdateRect with the most recent size. |
| 200 EXPECT_TRUE(browser_plugin->resize_pending_); |
| 201 } |
| 202 { |
| 203 BrowserPluginMsg_UpdateRect_Params update_rect_params; |
| 204 update_rect_params.view_size = gfx::Size(643, 480); |
| 205 update_rect_params.scale_factor = 1.0f; |
| 206 update_rect_params.is_resize_ack = true; |
| 207 browser_plugin->UpdateRect(0, update_rect_params); |
| 208 // The BrowserPlugin has finally received an UpdateRect that satisifes |
| 209 // its current size, and so it is happy. |
| 210 EXPECT_FALSE(browser_plugin->resize_pending_); |
| 211 } |
| 212 } |
| 213 |
| 214 TEST_F(BrowserPluginTest, GuestCrash) { |
| 215 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 216 |
| 217 // Grab the BrowserPlugin's instance ID from its resize message. |
| 218 const IPC::Message* msg = |
| 219 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 220 BrowserPluginHostMsg_ResizeGuest::ID); |
| 221 ASSERT_TRUE(msg); |
| 222 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 223 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 224 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 225 int instance_id = resize_params.a; |
| 226 |
| 227 MockBrowserPlugin* browser_plugin = |
| 228 static_cast<MockBrowserPlugin*>( |
| 229 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 230 ASSERT_TRUE(browser_plugin); |
| 231 |
| 232 WebKit::WebCursorInfo cursor_info; |
| 233 // Send an event and verify that the event is deported. |
| 234 browser_plugin->handleInputEvent(WebKit::WebMouseEvent(), |
| 235 cursor_info); |
| 236 EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 237 BrowserPluginHostMsg_HandleInputEvent::ID)); |
| 238 browser_plugin_manager()->sink().ClearMessages(); |
| 239 |
| 240 // Pretend that the guest has crashed |
| 241 browser_plugin->GuestCrashed(); |
| 242 // Send an event and verify that events are no longer deported. |
| 243 browser_plugin->handleInputEvent(WebKit::WebMouseEvent(), |
| 244 cursor_info); |
| 245 EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 246 BrowserPluginHostMsg_HandleInputEvent::ID)); |
| 247 |
| 248 // Navigate and verify that the guest_crashed_ flag has been reset. |
| 249 browser_plugin->SetSrcAttribute("bar"); |
| 250 EXPECT_FALSE(browser_plugin->guest_crashed_); |
| 251 |
| 252 } |
| 253 |
| 254 TEST_F(BrowserPluginTest, RemovePlugin) { |
| 255 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 256 EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 257 BrowserPluginHostMsg_PluginDestroyed::ID)); |
| 258 ExecuteJavaScript("x = document.getElementById('browserplugin'); " |
| 259 "x.parentNode.removeChild(x);"); |
| 260 ProcessPendingMessages(); |
| 261 EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching( |
| 262 BrowserPluginHostMsg_PluginDestroyed::ID)); |
| 263 } |
| 264 |
| 265 TEST_F(BrowserPluginTest, CustomEvents) { |
| 266 const char* kAddEventListener = |
| 267 "var url;" |
| 268 "function nav(u) {" |
| 269 " url = u;" |
| 270 "}" |
| 271 "document.getElementById('browserplugin')." |
| 272 " addEventListener('navigation', nav);"; |
| 273 const char* kRemoveEventListener = |
| 274 "document.getElementById('browserplugin')." |
| 275 " removeEventListener('navigation', nav);"; |
| 276 const char* kGoogleURL = "http://www.google.com/"; |
| 277 const char* kGoogleNewsURL = "http://news.google.com/"; |
| 278 |
| 279 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); |
| 280 ExecuteJavaScript(kAddEventListener); |
| 281 // Grab the BrowserPlugin's instance ID from its resize message. |
| 282 const IPC::Message* msg = |
| 283 browser_plugin_manager()->sink().GetFirstMessageMatching( |
| 284 BrowserPluginHostMsg_ResizeGuest::ID); |
| 285 ASSERT_TRUE(msg); |
| 286 PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg); |
| 287 BrowserPluginHostMsg_ResizeGuest::SendParam resize_params; |
| 288 ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params)); |
| 289 int instance_id = resize_params.a; |
| 290 |
| 291 MockBrowserPlugin* browser_plugin = |
| 292 static_cast<MockBrowserPlugin*>( |
| 293 browser_plugin_manager()->GetBrowserPlugin(instance_id)); |
| 294 ASSERT_TRUE(browser_plugin); |
| 295 |
| 296 browser_plugin->DidNavigate(GURL(kGoogleURL)); |
| 297 EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url")); |
| 298 |
| 299 ExecuteJavaScript(kRemoveEventListener); |
| 300 browser_plugin->DidNavigate(GURL(kGoogleNewsURL)); |
| 301 // The URL variable should not change because we've removed the event |
| 302 // listener. |
| 303 EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url")); |
| 304 } |
| 305 |
| 306 } |
| 307 } |
OLD | NEW |