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/browser/browser_plugin/test_browser_plugin_guest.h" | |
6 | |
7 #include "base/test/test_timeouts.h" | |
8 #include "content/browser/browser_plugin/browser_plugin_guest.h" | |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | |
10 #include "content/browser/web_contents/web_contents_impl.h" | |
11 #include "content/common/browser_plugin_messages.h" | |
12 #include "content/public/browser/notification_observer.h" | |
13 #include "content/public/browser/notification_types.h" | |
14 #include "content/public/test/test_utils.h" | |
15 #include "ui/gfx/size.h" | |
16 | |
17 namespace content { | |
18 | |
19 class BrowserPluginGuest; | |
20 | |
21 TestBrowserPluginGuest::TestBrowserPluginGuest( | |
22 int instance_id, | |
23 WebContentsImpl* web_contents, | |
24 RenderViewHost* render_view_host) | |
25 : BrowserPluginGuest(instance_id, web_contents, render_view_host), | |
26 update_rect_count_(0), | |
27 crash_observed_(false), | |
28 focus_observed_(false), | |
29 advance_focus_observed_(false), | |
30 was_hidden_observed_(false), | |
31 waiting_for_update_rect_msg_with_size_(false), | |
32 last_update_rect_width_(-1), | |
33 last_update_rect_height_(-1) { | |
34 // Listen to visibility changes so that a test can wait for these changes. | |
35 registrar_.Add(this, | |
36 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
37 Source<WebContents>(web_contents)); | |
38 } | |
39 | |
40 TestBrowserPluginGuest::~TestBrowserPluginGuest() { | |
41 } | |
42 | |
43 void TestBrowserPluginGuest::Observe(int type, | |
44 const NotificationSource& source, | |
45 const NotificationDetails& details) { | |
46 switch (type) { | |
47 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
48 bool visible = *Details<bool>(details).ptr(); | |
49 if (!visible) { | |
50 was_hidden_observed_ = true; | |
51 if (was_hidden_message_loop_runner_) | |
52 was_hidden_message_loop_runner_->Quit(); | |
53 } | |
54 break; | |
55 } | |
56 default: | |
57 NOTREACHED() << "Unexpected notification type: " << type; | |
58 } | |
59 } | |
60 | |
61 void TestBrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) { | |
62 if (msg->type() == BrowserPluginMsg_UpdateRect::ID) { | |
63 PickleIterator iter(*msg); | |
64 | |
65 int instance_id; | |
66 int message_id; | |
67 BrowserPluginMsg_UpdateRect_Params update_rect_params; | |
68 | |
69 if (!IPC::ReadParam(msg, &iter, &instance_id) || | |
70 !IPC::ReadParam(msg, &iter, &message_id) || | |
71 !IPC::ReadParam(msg, &iter, &update_rect_params)) { | |
72 NOTREACHED() << | |
73 "Cannot read BrowserPluginMsg_UpdateRect params from ipc message"; | |
74 } | |
75 last_update_rect_width_ = update_rect_params.view_size.width(); | |
76 last_update_rect_height_ = update_rect_params.view_size.height(); | |
77 update_rect_count_++; | |
78 if (waiting_for_update_rect_msg_with_size_ && | |
79 expected_width_ == last_update_rect_width_ && | |
80 expected_height_ == last_update_rect_height_) { | |
81 waiting_for_update_rect_msg_with_size_ = false; | |
82 if (send_message_loop_runner_) | |
83 send_message_loop_runner_->Quit(); | |
84 } else if (!waiting_for_update_rect_msg_with_size_) { | |
85 if (send_message_loop_runner_) | |
86 send_message_loop_runner_->Quit(); | |
87 } | |
88 } | |
89 BrowserPluginGuest::SendMessageToEmbedder(msg); | |
90 } | |
91 | |
92 void TestBrowserPluginGuest::WaitForUpdateRectMsg() { | |
93 // Check if we already got any UpdateRect message. | |
94 if (update_rect_count_ > 0) | |
95 return; | |
96 send_message_loop_runner_ = new MessageLoopRunner(); | |
97 send_message_loop_runner_->Run(); | |
98 } | |
99 | |
100 void TestBrowserPluginGuest::WaitForUpdateRectMsgWithSize(int width, | |
101 int height) { | |
102 if (update_rect_count_ > 0 && | |
103 last_update_rect_width_ == width && | |
104 last_update_rect_height_ == height) { | |
105 // We already saw this message. | |
106 return; | |
107 } | |
108 waiting_for_update_rect_msg_with_size_ = true; | |
109 expected_width_ = width; | |
110 expected_height_ = height; | |
111 | |
112 send_message_loop_runner_ = new MessageLoopRunner(); | |
113 send_message_loop_runner_->Run(); | |
114 } | |
115 | |
116 void TestBrowserPluginGuest::RenderViewGone(base::TerminationStatus status) { | |
117 crash_observed_ = true; | |
118 LOG(INFO) << "Guest crashed"; | |
119 if (crash_message_loop_runner_) | |
120 crash_message_loop_runner_->Quit(); | |
121 BrowserPluginGuest::RenderViewGone(status); | |
122 } | |
123 | |
124 void TestBrowserPluginGuest::WaitForCrashed() { | |
125 // Check if we already observed a guest crash, return immediately if so. | |
126 if (crash_observed_) | |
127 return; | |
128 | |
129 crash_message_loop_runner_ = new MessageLoopRunner(); | |
130 crash_message_loop_runner_->Run(); | |
131 } | |
132 | |
133 void TestBrowserPluginGuest::WaitForFocus() { | |
134 if (focus_observed_) | |
135 return; | |
136 focus_message_loop_runner_ = new MessageLoopRunner(); | |
137 focus_message_loop_runner_->Run(); | |
138 } | |
139 | |
140 void TestBrowserPluginGuest::WaitForAdvanceFocus() { | |
141 if (advance_focus_observed_) | |
142 return; | |
143 advance_focus_message_loop_runner_ = new MessageLoopRunner(); | |
144 advance_focus_message_loop_runner_->Run(); | |
145 } | |
146 | |
147 void TestBrowserPluginGuest::WaitUntilHidden() { | |
148 if (was_hidden_observed_) { | |
149 was_hidden_observed_ = false; | |
150 return; | |
151 } | |
152 was_hidden_message_loop_runner_ = new MessageLoopRunner(); | |
153 was_hidden_message_loop_runner_->Run(); | |
154 was_hidden_observed_ = false; | |
155 } | |
156 | |
157 void TestBrowserPluginGuest::SetFocus(bool focused) { | |
158 focus_observed_ = true; | |
159 if (focus_message_loop_runner_) | |
160 focus_message_loop_runner_->Quit(); | |
161 BrowserPluginGuest::SetFocus(focused); | |
162 } | |
163 | |
164 bool TestBrowserPluginGuest::ViewTakeFocus(bool reverse) { | |
165 advance_focus_observed_ = true; | |
166 if (advance_focus_message_loop_runner_) | |
167 advance_focus_message_loop_runner_->Quit(); | |
168 return BrowserPluginGuest::ViewTakeFocus(reverse); | |
169 } | |
170 | |
171 } // namespace content | |
OLD | NEW |