Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(995)

Side by Side Diff: content/browser/browser_plugin/browser_plugin_host_browsertest.cc

Issue 10868012: Browser Plugin: New Implementation (Browser Side) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master-trial-obrowser
Patch Set: Clean up BrowserPluginHostTest, remove unused var, use constants for str. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 = "data:text/html,hello world";
awong 2012/09/09 18:08:09 Isn't this technically malformed HTML?
lazyboy 2012/09/10 16:30:17 Added <html>... is this what you mean?
awong 2012/09/10 19:21:07 Yep! Thanks!
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 private:
107 DISALLOW_COPY_AND_ASSIGN(BrowserPluginHostTest);
108 };
109
110 // This test loads a guest that has infinite loop, therefore it hangs the guest
111 // and eventually gets killed.
112 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateGuest) {
113 ASSERT_TRUE(test_server()->Start());
114 GURL test_url(test_server()->GetURL("files/browser_plugin_embedder.html"));
115 NavigateToURL(shell(), test_url);
116
117 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>(
118 shell()->web_contents());
119 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
120 embedder_web_contents->GetRenderViewHost());
121
122 test_url = test_server()->GetURL(
123 "files/browser_plugin_infinite_loop_child.html");
124 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16(
125 StringPrintf("SetSrc('%s');", test_url.spec().c_str())));
126
127 // Wait to make sure embedder is created/attached to WebContents.
128 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation();
129
130 TestBrowserPluginEmbedder* test_embedder =
131 static_cast<TestBrowserPluginEmbedder*>(
132 embedder_web_contents->GetBrowserPluginEmbedder());
133 ASSERT_TRUE(test_embedder);
134 test_embedder->WaitForGuestAdded();
135
136 // Verify that we have exactly one guest.
137 const ContainerInstanceMap& instance_map = test_embedder->
138 guests_for_testing();
139 EXPECT_EQ(1u, instance_map.size());
140
141 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>(
142 instance_map.begin()->second);
143
144 // Wait for the guest to send an UpdateRectMsg, meaning it is ready.
145 test_guest->WaitForUpdateRectMsg();
146
147 WebKit::WebMouseEvent mouse_event;
148 mouse_event.type = WebInputEvent::MouseDown;
149 mouse_event.button = WebMouseEvent::ButtonMiddle;
150 mouse_event.x = 35;
151 mouse_event.y = 35;
152 mouse_event.globalX = 35;
153 mouse_event.globalY = 35;
154
155 IPC::Message* input_message = new ViewMsg_HandleInputEvent(
156 embedder_web_contents->GetRenderViewHost()->GetRoutingID());
157 input_message->WriteData(reinterpret_cast<const char*>(&mouse_event),
158 sizeof(WebKit::WebMouseEvent));
159 embedder_web_contents->GetRenderViewHost()->Send(input_message);
160
161 // Expect the guest to crash.
162 test_guest->WaitForCrashed();
163 }
164
165 // This test ensures that if guest isn't there and we resize the guest (from
166 // js), it remembers the size correctly.
167 //
168 // Initially we load an embedder with a guest without a src attribute (which has
169 // dimension 640x480), then resize it to 100x200, then we set the source to a
170 // sample guest. In the end we verify that the correct size has been set.
awong 2012/09/09 18:08:09 Thanks for adding this!
lazyboy 2012/09/10 16:30:17 Great. Done.
171 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateAfterResize) {
172 ASSERT_TRUE(test_server()->Start());
173 GURL test_url(test_server()->GetURL(
174 "files/browser_plugin_embedder.html"));
175 NavigateToURL(shell(), test_url);
176
177 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>(
178 shell()->web_contents());
179 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
180 embedder_web_contents->GetRenderViewHost());
181
182 int nxt_width = 100;
183 int nxt_height = 200;
184 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16(
185 StringPrintf("SetSize(%d, %d);", nxt_width, nxt_height)));
186
187 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16(
188 StringPrintf("SetSrc('%s');", kHTMLForGuest)));
189
190 // Wait to make sure embedder is created/attached to WebContents.
191 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation();
192
193 TestBrowserPluginEmbedder* test_embedder =
194 static_cast<TestBrowserPluginEmbedder*>(
195 embedder_web_contents->GetBrowserPluginEmbedder());
196 ASSERT_TRUE(test_embedder);
197 test_embedder->WaitForGuestAdded();
198
199 // Verify that we have exactly one guest.
200 const ContainerInstanceMap& instance_map = test_embedder->
201 guests_for_testing();
202 EXPECT_EQ(1u, instance_map.size());
203
204 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>(
205 instance_map.begin()->second);
206
207 // Wait for the guest to send an UpdateRectMsg, the dimensions should be
208 // 100 x 200.
209 test_guest->WaitForUpdateRectMsgWithSize(nxt_width, nxt_height);
210 }
211
212 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698