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.
5 #include "content/browser/browser_plugin/test_browser_plugin_guest.h"
7 #include "base/test/test_timeouts.h"
8 #include "content/browser/renderer_host/render_view_host_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/browser_plugin/browser_plugin_messages.h"
14 class BrowserPluginGuest
;
16 TestBrowserPluginGuest::TestBrowserPluginGuest(
18 WebContentsImpl
* web_contents
)
19 : BrowserPluginGuest(instance_id
, false, web_contents
, NULL
),
20 update_rect_count_(0),
21 damage_buffer_call_count_(0),
22 exit_observed_(false),
23 focus_observed_(false),
24 blur_observed_(false),
25 advance_focus_observed_(false),
26 was_hidden_observed_(false),
27 set_damage_buffer_observed_(false),
28 input_observed_(false),
29 load_stop_observed_(false),
30 ime_cancel_observed_(false),
31 waiting_for_damage_buffer_with_size_(false),
32 last_damage_buffer_size_(gfx::Size()) {
35 TestBrowserPluginGuest::~TestBrowserPluginGuest() {
38 WebContentsImpl
* TestBrowserPluginGuest::web_contents() const {
39 return static_cast<WebContentsImpl
*>(BrowserPluginGuest::web_contents());
42 void TestBrowserPluginGuest::SendMessageToEmbedder(IPC::Message
* msg
) {
43 if (msg
->type() == BrowserPluginMsg_UpdateRect::ID
) {
46 BrowserPluginMsg_UpdateRect_Params params
;
47 BrowserPluginMsg_UpdateRect::Read(msg
, &instance_id
, ¶ms
);
48 last_view_size_observed_
= params
.view_size
;
49 if (!expected_auto_view_size_
.IsEmpty() &&
50 expected_auto_view_size_
== params
.view_size
) {
51 if (auto_view_size_message_loop_runner_
.get())
52 auto_view_size_message_loop_runner_
->Quit();
54 if (send_message_loop_runner_
.get())
55 send_message_loop_runner_
->Quit();
57 BrowserPluginGuest::SendMessageToEmbedder(msg
);
60 void TestBrowserPluginGuest::WaitForUpdateRectMsg() {
61 // Check if we already got any UpdateRect message.
62 if (update_rect_count_
> 0)
64 send_message_loop_runner_
= new MessageLoopRunner();
65 send_message_loop_runner_
->Run();
68 void TestBrowserPluginGuest::ResetUpdateRectCount() {
69 update_rect_count_
= 0;
72 void TestBrowserPluginGuest::WaitForDamageBufferWithSize(
73 const gfx::Size
& size
) {
74 if (damage_buffer_call_count_
> 0 && last_damage_buffer_size_
== size
)
77 expected_damage_buffer_size_
= size
;
78 waiting_for_damage_buffer_with_size_
= true;
79 damage_buffer_message_loop_runner_
= new MessageLoopRunner();
80 damage_buffer_message_loop_runner_
->Run();
83 void TestBrowserPluginGuest::RenderProcessGone(base::TerminationStatus status
) {
84 exit_observed_
= true;
85 if (status
!= base::TERMINATION_STATUS_NORMAL_TERMINATION
&&
86 status
!= base::TERMINATION_STATUS_STILL_RUNNING
)
87 VLOG(0) << "Guest crashed status: " << status
;
88 if (crash_message_loop_runner_
.get())
89 crash_message_loop_runner_
->Quit();
90 BrowserPluginGuest::RenderProcessGone(status
);
93 void TestBrowserPluginGuest::OnHandleInputEvent(
95 const gfx::Rect
& guest_window_rect
,
96 const blink::WebInputEvent
* event
) {
97 BrowserPluginGuest::OnHandleInputEvent(instance_id
,
100 input_observed_
= true;
101 if (input_message_loop_runner_
.get())
102 input_message_loop_runner_
->Quit();
105 void TestBrowserPluginGuest::WaitForExit() {
106 // Check if we already observed a guest crash, return immediately if so.
110 crash_message_loop_runner_
= new MessageLoopRunner();
111 crash_message_loop_runner_
->Run();
114 void TestBrowserPluginGuest::WaitForFocus() {
115 if (focus_observed_
) {
116 focus_observed_
= false;
119 focus_message_loop_runner_
= new MessageLoopRunner();
120 focus_message_loop_runner_
->Run();
121 focus_observed_
= false;
124 void TestBrowserPluginGuest::WaitForBlur() {
125 if (blur_observed_
) {
126 blur_observed_
= false;
129 blur_message_loop_runner_
= new MessageLoopRunner();
130 blur_message_loop_runner_
->Run();
131 blur_observed_
= false;
134 void TestBrowserPluginGuest::WaitForAdvanceFocus() {
135 if (advance_focus_observed_
)
137 advance_focus_message_loop_runner_
= new MessageLoopRunner();
138 advance_focus_message_loop_runner_
->Run();
141 void TestBrowserPluginGuest::WaitUntilHidden() {
142 if (was_hidden_observed_
) {
143 was_hidden_observed_
= false;
146 was_hidden_message_loop_runner_
= new MessageLoopRunner();
147 was_hidden_message_loop_runner_
->Run();
148 was_hidden_observed_
= false;
151 void TestBrowserPluginGuest::WaitForInput() {
152 if (input_observed_
) {
153 input_observed_
= false;
157 input_message_loop_runner_
= new MessageLoopRunner();
158 input_message_loop_runner_
->Run();
159 input_observed_
= false;
162 void TestBrowserPluginGuest::WaitForLoadStop() {
163 if (load_stop_observed_
) {
164 load_stop_observed_
= false;
168 load_stop_message_loop_runner_
= new MessageLoopRunner();
169 load_stop_message_loop_runner_
->Run();
170 load_stop_observed_
= false;
173 void TestBrowserPluginGuest::WaitForViewSize(const gfx::Size
& view_size
) {
174 if (last_view_size_observed_
== view_size
) {
175 last_view_size_observed_
= gfx::Size();
179 expected_auto_view_size_
= view_size
;
180 auto_view_size_message_loop_runner_
= new MessageLoopRunner();
181 auto_view_size_message_loop_runner_
->Run();
182 last_view_size_observed_
= gfx::Size();
185 void TestBrowserPluginGuest::WaitForImeCancel() {
186 if (ime_cancel_observed_
) {
187 ime_cancel_observed_
= false;
191 ime_cancel_message_loop_runner_
= new MessageLoopRunner();
192 ime_cancel_message_loop_runner_
->Run();
193 ime_cancel_observed_
= false;
196 void TestBrowserPluginGuest::OnSetFocus(int instance_id
, bool focused
) {
198 focus_observed_
= true;
199 if (focus_message_loop_runner_
.get())
200 focus_message_loop_runner_
->Quit();
202 blur_observed_
= true;
203 if (blur_message_loop_runner_
.get())
204 blur_message_loop_runner_
->Quit();
206 BrowserPluginGuest::OnSetFocus(instance_id
, focused
);
209 void TestBrowserPluginGuest::OnTakeFocus(bool reverse
) {
210 advance_focus_observed_
= true;
211 if (advance_focus_message_loop_runner_
.get())
212 advance_focus_message_loop_runner_
->Quit();
213 BrowserPluginGuest::OnTakeFocus(reverse
);
216 void TestBrowserPluginGuest::SetDamageBuffer(
217 const BrowserPluginHostMsg_ResizeGuest_Params
& params
) {
218 ++damage_buffer_call_count_
;
219 last_damage_buffer_size_
= params
.view_rect
.size();
221 if (waiting_for_damage_buffer_with_size_
&&
222 expected_damage_buffer_size_
== params
.view_rect
.size() &&
223 damage_buffer_message_loop_runner_
.get()) {
224 damage_buffer_message_loop_runner_
->Quit();
225 waiting_for_damage_buffer_with_size_
= false;
228 BrowserPluginGuest::SetDamageBuffer(params
);
231 void TestBrowserPluginGuest::DidStopLoading(
232 RenderViewHost
* render_view_host
) {
233 BrowserPluginGuest::DidStopLoading(render_view_host
);
234 load_stop_observed_
= true;
235 if (load_stop_message_loop_runner_
.get())
236 load_stop_message_loop_runner_
->Quit();
239 void TestBrowserPluginGuest::OnImeCancelComposition() {
240 if (!ime_cancel_observed_
) {
241 ime_cancel_observed_
= true;
242 if (ime_cancel_message_loop_runner_
.get())
243 ime_cancel_message_loop_runner_
->Quit();
245 BrowserPluginGuest::OnImeCancelComposition();
248 void TestBrowserPluginGuest::WasHidden() {
249 was_hidden_observed_
= true;
250 if (was_hidden_message_loop_runner_
.get())
251 was_hidden_message_loop_runner_
->Quit();
254 } // namespace content