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