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/browser_plugin/test_timeout_tracker.h" |
| 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 14 #include "content/browser/web_contents/web_contents_impl.h" |
| 15 #include "content/common/view_messages.h" |
| 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/notification_types.h" |
| 18 #include "content/public/test/test_utils.h" |
| 19 #include "content/shell/shell.h" |
| 20 #include "content/test/content_browser_test_utils.h" |
| 21 #include "content/test/content_browser_test.h" |
| 22 #include "ipc/ipc_message.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 24 |
| 25 using WebKit::WebInputEvent; |
| 26 using WebKit::WebMouseEvent; |
| 27 using content::BrowserPluginEmbedder; |
| 28 using content::BrowserPluginGuest; |
| 29 using content::BrowserPluginHostFactory; |
| 30 |
| 31 namespace content { |
| 32 |
| 33 const char* kHTMLForGuest = |
| 34 "data:text/html,<html><body>hello world</body></html>"; |
| 35 |
| 36 // Test factory for creating test instances of BrowserPluginEmbedder and |
| 37 // BrowserPluginGuest. |
| 38 class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { |
| 39 public: |
| 40 virtual BrowserPluginGuest* CreateBrowserPluginGuest( |
| 41 int instance_id, |
| 42 WebContentsImpl* web_contents, |
| 43 RenderViewHost* render_view_host) OVERRIDE { |
| 44 return new TestBrowserPluginGuest(instance_id, |
| 45 web_contents, |
| 46 render_view_host); |
| 47 } |
| 48 |
| 49 // Also keeps track of number of instances created. |
| 50 virtual BrowserPluginEmbedder* CreateBrowserPluginEmbedder( |
| 51 WebContentsImpl* web_contents, |
| 52 RenderViewHost* render_view_host) OVERRIDE { |
| 53 embedder_instance_count_++; |
| 54 if (message_loop_runner_) |
| 55 message_loop_runner_->Quit(); |
| 56 |
| 57 return new TestBrowserPluginEmbedder(web_contents, render_view_host); |
| 58 } |
| 59 |
| 60 // Singleton getter. |
| 61 static TestBrowserPluginHostFactory* GetInstance() { |
| 62 return Singleton<TestBrowserPluginHostFactory>::get(); |
| 63 } |
| 64 |
| 65 // Waits for at least one embedder to be created in the test. Returns true if |
| 66 // we have a guest, false if waiting times out. |
| 67 bool WaitForEmbedderCreation() { |
| 68 // Check if already have created instance. |
| 69 if (embedder_instance_count_ > 0) |
| 70 return true; |
| 71 // Wait otherwise. |
| 72 message_loop_runner_ = new MessageLoopRunner(); |
| 73 return TestTimeoutTracker::RunInActionTimeout( |
| 74 message_loop_runner_.get()); |
| 75 } |
| 76 |
| 77 private: |
| 78 // For Singleton. |
| 79 friend struct DefaultSingletonTraits<TestBrowserPluginHostFactory>; |
| 80 |
| 81 TestBrowserPluginHostFactory() : embedder_instance_count_(0) {} |
| 82 virtual ~TestBrowserPluginHostFactory() {} |
| 83 |
| 84 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 85 int embedder_instance_count_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginHostFactory); |
| 88 }; |
| 89 |
| 90 class BrowserPluginHostTest : public ContentBrowserTest { |
| 91 public: |
| 92 BrowserPluginHostTest() {} |
| 93 |
| 94 virtual void SetUp() OVERRIDE { |
| 95 // Override factory to create tests instances of BrowserPlugin*. |
| 96 content::BrowserPluginEmbedder::set_factory_for_testing( |
| 97 TestBrowserPluginHostFactory::GetInstance()); |
| 98 content::BrowserPluginGuest::set_factory_for_testing( |
| 99 TestBrowserPluginHostFactory::GetInstance()); |
| 100 |
| 101 ContentBrowserTest::SetUp(); |
| 102 } |
| 103 virtual void TearDown() OVERRIDE { |
| 104 content::BrowserPluginEmbedder::set_factory_for_testing(NULL); |
| 105 content::BrowserPluginGuest::set_factory_for_testing(NULL); |
| 106 |
| 107 ContentBrowserTest::TearDown(); |
| 108 } |
| 109 |
| 110 private: |
| 111 DISALLOW_COPY_AND_ASSIGN(BrowserPluginHostTest); |
| 112 }; |
| 113 |
| 114 // This test loads a guest that has infinite loop, therefore it hangs the guest |
| 115 // and eventually gets killed. |
| 116 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateGuest) { |
| 117 ASSERT_TRUE(test_server()->Start()); |
| 118 GURL test_url(test_server()->GetURL("files/browser_plugin_embedder.html")); |
| 119 NavigateToURL(shell(), test_url); |
| 120 |
| 121 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( |
| 122 shell()->web_contents()); |
| 123 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 124 embedder_web_contents->GetRenderViewHost()); |
| 125 |
| 126 test_url = test_server()->GetURL( |
| 127 "files/browser_plugin_infinite_loop_child.html"); |
| 128 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 129 StringPrintf("SetSrc('%s');", test_url.spec().c_str()))); |
| 130 |
| 131 // Wait to make sure embedder is created/attached to WebContents. |
| 132 ASSERT_TRUE( |
| 133 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation()); |
| 134 |
| 135 TestBrowserPluginEmbedder* test_embedder = |
| 136 static_cast<TestBrowserPluginEmbedder*>( |
| 137 embedder_web_contents->GetBrowserPluginEmbedder()); |
| 138 ASSERT_TRUE(test_embedder); |
| 139 ASSERT_TRUE(test_embedder->WaitForGuestAdded()); |
| 140 |
| 141 // Verify that we have exactly one guest. |
| 142 const BrowserPluginEmbedder::ContainerInstanceMap& instance_map = |
| 143 test_embedder->guest_web_contents_for_testing(); |
| 144 EXPECT_EQ(1u, instance_map.size()); |
| 145 |
| 146 WebContentsImpl* test_guest_web_contents = static_cast<WebContentsImpl*>( |
| 147 instance_map.begin()->second); |
| 148 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( |
| 149 test_guest_web_contents->GetBrowserPluginGuest()); |
| 150 |
| 151 // Wait for the guest to send an UpdateRectMsg, meaning it is ready. |
| 152 ASSERT_TRUE(test_guest->WaitForUpdateRectMsg()); |
| 153 |
| 154 WebKit::WebMouseEvent mouse_event; |
| 155 mouse_event.type = WebInputEvent::MouseDown; |
| 156 mouse_event.button = WebMouseEvent::ButtonMiddle; |
| 157 mouse_event.x = 35; |
| 158 mouse_event.y = 35; |
| 159 mouse_event.globalX = 35; |
| 160 mouse_event.globalY = 35; |
| 161 |
| 162 IPC::Message* input_message = new ViewMsg_HandleInputEvent( |
| 163 embedder_web_contents->GetRenderViewHost()->GetRoutingID()); |
| 164 input_message->WriteData(reinterpret_cast<const char*>(&mouse_event), |
| 165 sizeof(WebKit::WebMouseEvent)); |
| 166 embedder_web_contents->GetRenderViewHost()->Send(input_message); |
| 167 |
| 168 // Expect the guest to crash. |
| 169 ASSERT_TRUE(test_guest->WaitForCrashed()); |
| 170 } |
| 171 |
| 172 // This test ensures that if guest isn't there and we resize the guest (from |
| 173 // js), it remembers the size correctly. |
| 174 // |
| 175 // Initially we load an embedder with a guest without a src attribute (which has |
| 176 // dimension 640x480), then resize it to 100x200, then we set the source to a |
| 177 // sample guest. In the end we verify that the correct size has been set. |
| 178 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateAfterResize) { |
| 179 ASSERT_TRUE(test_server()->Start()); |
| 180 GURL test_url(test_server()->GetURL( |
| 181 "files/browser_plugin_embedder.html")); |
| 182 NavigateToURL(shell(), test_url); |
| 183 |
| 184 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( |
| 185 shell()->web_contents()); |
| 186 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 187 embedder_web_contents->GetRenderViewHost()); |
| 188 |
| 189 int nxt_width = 100; |
| 190 int nxt_height = 200; |
| 191 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 192 StringPrintf("SetSize(%d, %d);", nxt_width, nxt_height))); |
| 193 |
| 194 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 195 StringPrintf("SetSrc('%s');", kHTMLForGuest))); |
| 196 |
| 197 // Wait to make sure embedder is created/attached to WebContents. |
| 198 ASSERT_TRUE( |
| 199 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation()); |
| 200 |
| 201 TestBrowserPluginEmbedder* test_embedder = |
| 202 static_cast<TestBrowserPluginEmbedder*>( |
| 203 embedder_web_contents->GetBrowserPluginEmbedder()); |
| 204 ASSERT_TRUE(test_embedder); |
| 205 ASSERT_TRUE(test_embedder->WaitForGuestAdded()); |
| 206 |
| 207 // Verify that we have exactly one guest. |
| 208 const BrowserPluginEmbedder::ContainerInstanceMap& instance_map = |
| 209 test_embedder->guest_web_contents_for_testing(); |
| 210 EXPECT_EQ(1u, instance_map.size()); |
| 211 |
| 212 WebContentsImpl* test_guest_web_contents = static_cast<WebContentsImpl*>( |
| 213 instance_map.begin()->second); |
| 214 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( |
| 215 test_guest_web_contents->GetBrowserPluginGuest()); |
| 216 |
| 217 |
| 218 // Wait for the guest to send an UpdateRectMsg, the dimensions should be |
| 219 // 100 x 200. |
| 220 ASSERT_TRUE(test_guest->WaitForUpdateRectMsgWithSize(nxt_width, nxt_height)); |
| 221 } |
| 222 |
| 223 } // namespace content |
OLD | NEW |