summaryrefslogtreecommitdiffstats
path: root/content/renderer/browser_plugin/browser_plugin_browsertest.cc
blob: 7afdd9a8102c0d1f173f0c5d72d52d93b2981c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/browser_plugin/browser_plugin_browsertest.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "content/common/browser_plugin_messages.h"
#include "content/public/common/content_constants.h"
#include "content/renderer/browser_plugin/browser_plugin.h"
#include "content/renderer/browser_plugin/mock_browser_plugin.h"
#include "content/renderer/browser_plugin/mock_browser_plugin_manager.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/renderer_webkitplatformsupport_impl.h"
#include "content/shell/shell_main_delegate.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h"

namespace {
const char kHTMLForBrowserPluginObject[] =
  "<object id='browserplugin' width='640px' height='480px'"
  "  src='foo' type='%s'>";

std::string GetHTMLForBrowserPluginObject() {
  return StringPrintf(kHTMLForBrowserPluginObject,
                      content::kBrowserPluginNewMimeType);
}

}

namespace content {

BrowserPluginTest::BrowserPluginTest() {}

BrowserPluginTest::~BrowserPluginTest() {}

void BrowserPluginTest::SetUp() {
  GetContentClient()->set_renderer_for_testing(&content_renderer_client_);
  content::RenderViewTest::SetUp();
  browser_plugin_manager_.reset(new MockBrowserPluginManager());
  content::ShellMainDelegate::InitializeResourceBundle();
}

void BrowserPluginTest::TearDown() {
  browser_plugin_manager_->Cleanup();
  content::RenderViewTest::TearDown();
}

std::string BrowserPluginTest::ExecuteScriptAndReturnString(
    const std::string& script) {
  v8::Handle<v8::Value>  value = GetMainFrame()->executeScriptAndReturnValue(
      WebKit::WebScriptSource(WebKit::WebString::fromUTF8(script.c_str())));
  if (value.IsEmpty() || !value->IsString())
    return std::string();

  v8::Local<v8::String> v8_str = value->ToString();
  int length = v8_str->Utf8Length() + 1;
  scoped_array<char> str(new char[length]);
  v8_str->WriteUtf8(str.get(), length);
  return str.get();
}

// This test verifies that an initial resize occurs when we instantiate the
// browser plugin. This test also verifies that the browser plugin is waiting
// for a BrowserPluginMsg_UpdateRect in response. We issue an UpdateRect, and
// we observe an UpdateRect_ACK, with the resize_pending_ reset, indiciating
// that the BrowserPlugin is not waiting for any more UpdateRects to
// satisfy its resize request.
TEST_F(BrowserPluginTest, InitialResize) {
  LoadHTML(GetHTMLForBrowserPluginObject().c_str());
  // Verify that the information based on ResizeGuest is correct, and
  // use its TransportDIB::Id to paint.
  const IPC::Message* msg =
      browser_plugin_manager()->sink().GetUniqueMessageMatching(
          BrowserPluginHostMsg_ResizeGuest::ID);
  ASSERT_TRUE(msg);
  PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg);
  BrowserPluginHostMsg_ResizeGuest::SendParam  resize_params;
  ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params));
  int instance_id = resize_params.a;
  BrowserPluginHostMsg_ResizeGuest_Params params(resize_params.b);
  EXPECT_EQ(640, params.width);
  EXPECT_EQ(480, params.height);
  // Verify that the browser plugin wasn't already waiting on a resize when this
  // resize happened.
  EXPECT_FALSE(params.resize_pending);

  MockBrowserPlugin* browser_plugin =
      static_cast<MockBrowserPlugin*>(
          browser_plugin_manager()->GetBrowserPlugin(instance_id));
  ASSERT_TRUE(browser_plugin);
  // Now the browser plugin is expecting a UpdateRect resize.
  EXPECT_TRUE(browser_plugin->resize_pending_);

  // Send the BrowserPlugin an UpdateRect equal to its container size.
  // That should clear the resize_pending_ flag.
  BrowserPluginMsg_UpdateRect_Params update_rect_params;
  update_rect_params.view_size = gfx::Size(640, 480);
  update_rect_params.scale_factor = 1.0f;
  update_rect_params.is_resize_ack = true;
  browser_plugin->UpdateRect(0, update_rect_params);
  EXPECT_FALSE(browser_plugin->resize_pending_);
}

// Verify that the src attribute on the browser plugin works as expected.
TEST_F(BrowserPluginTest, SrcAttribute) {
  LoadHTML(GetHTMLForBrowserPluginObject().c_str());
  // Verify that we're reporting the correct URL to navigate to based on the
  // src attribute.
  {
    const IPC::Message* msg =
    browser_plugin_manager()->sink().GetUniqueMessageMatching(
        BrowserPluginHostMsg_NavigateOrCreateGuest::ID);
    ASSERT_TRUE(msg);

    int instance_id;
    long long frame_id;
    std::string src;
    BrowserPluginHostMsg_NavigateOrCreateGuest::Read(
        msg,
        &instance_id,
        &frame_id,
        &src);
    EXPECT_EQ("foo", src);
  }

  browser_plugin_manager()->sink().ClearMessages();
  // Navigate to bar and observe the associated
  // BrowserPluginHostMsg_NavigateOrCreateGuest message.
  // Verify that the src attribute is updated as well.
  ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'");
  {
    const IPC::Message* msg =
      browser_plugin_manager()->sink().GetUniqueMessageMatching(
          BrowserPluginHostMsg_NavigateOrCreateGuest::ID);
    ASSERT_TRUE(msg);

    int instance_id;
    long long frame_id;
    std::string src;
    BrowserPluginHostMsg_NavigateOrCreateGuest::Read(
        msg,
        &instance_id,
        &frame_id,
        &src);
    EXPECT_EQ("bar", src);
    std::string src_value =
        ExecuteScriptAndReturnString(
            "document.getElementById('browserplugin').src");
    EXPECT_EQ("bar", src_value);
  }
}

TEST_F(BrowserPluginTest, ResizeFlowControl) {
  LoadHTML(GetHTMLForBrowserPluginObject().c_str());
  browser_plugin_manager()->sink().ClearMessages();

  // Resize the browser plugin three times.
  ExecuteJavaScript("document.getElementById('browserplugin').width = '641px'");
  ProcessPendingMessages();
  ExecuteJavaScript("document.getElementById('browserplugin').width = '642px'");
  ProcessPendingMessages();
  ExecuteJavaScript("document.getElementById('browserplugin').width = '643px'");
  ProcessPendingMessages();

  // Expect to see three messsages in the sink.
  EXPECT_EQ(3u, browser_plugin_manager()->sink().message_count());
  const IPC::Message* msg =
      browser_plugin_manager()->sink().GetFirstMessageMatching(
          BrowserPluginHostMsg_ResizeGuest::ID);
  ASSERT_TRUE(msg);
  PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg);
  BrowserPluginHostMsg_ResizeGuest::SendParam  resize_params;
  ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params));
  int instance_id = resize_params.a;
  BrowserPluginHostMsg_ResizeGuest_Params params(resize_params.b);
  EXPECT_EQ(641, params.width);
  EXPECT_EQ(480, params.height);
  // This indicates that the BrowserPlugin has sent out a previous resize
  // request but has not yet received an UpdateRect for that request.
  // We send this resize regardless to update the damage buffer in the
  // browser process, so it's ready when the guest sends the appropriate
  // UpdateRect.
  EXPECT_TRUE(params.resize_pending);

  MockBrowserPlugin* browser_plugin =
      static_cast<MockBrowserPlugin*>(
          browser_plugin_manager()->GetBrowserPlugin(instance_id));
  ASSERT_TRUE(browser_plugin);
  {
    // We send a stale UpdateRect to the BrowserPlugin.
    BrowserPluginMsg_UpdateRect_Params update_rect_params;
    update_rect_params.view_size = gfx::Size(640, 480);
    update_rect_params.scale_factor = 1.0f;
    update_rect_params.is_resize_ack = true;
    browser_plugin->UpdateRect(0, update_rect_params);
    // This tells us that the BrowserPlugin is still expecting another
    // UpdateRect with the most recent size.
    EXPECT_TRUE(browser_plugin->resize_pending_);
  }
  {
    BrowserPluginMsg_UpdateRect_Params update_rect_params;
    update_rect_params.view_size = gfx::Size(643, 480);
    update_rect_params.scale_factor = 1.0f;
    update_rect_params.is_resize_ack = true;
    browser_plugin->UpdateRect(0, update_rect_params);
    // The BrowserPlugin has finally received an UpdateRect that satisifes
    // its current size, and so it is happy.
    EXPECT_FALSE(browser_plugin->resize_pending_);
  }
}

TEST_F(BrowserPluginTest, GuestCrash) {
  LoadHTML(GetHTMLForBrowserPluginObject().c_str());

  // Grab the BrowserPlugin's instance ID from its resize message.
  const IPC::Message* msg =
      browser_plugin_manager()->sink().GetFirstMessageMatching(
          BrowserPluginHostMsg_ResizeGuest::ID);
  ASSERT_TRUE(msg);
  PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg);
  BrowserPluginHostMsg_ResizeGuest::SendParam  resize_params;
  ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params));
  int instance_id = resize_params.a;

  MockBrowserPlugin* browser_plugin =
      static_cast<MockBrowserPlugin*>(
          browser_plugin_manager()->GetBrowserPlugin(instance_id));
  ASSERT_TRUE(browser_plugin);

  WebKit::WebCursorInfo cursor_info;
  // Send an event and verify that the event is deported.
  browser_plugin->handleInputEvent(WebKit::WebMouseEvent(),
                                   cursor_info);
  EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
      BrowserPluginHostMsg_HandleInputEvent::ID));
  browser_plugin_manager()->sink().ClearMessages();

  const char* kAddEventListener =
    "var msg;"
    "function crashListener() {"
    "  msg = 'crashed';"
    "}"
    "document.getElementById('browserplugin')."
    "    addEventListener('crash', crashListener);";

  ExecuteJavaScript(kAddEventListener);

  // Pretend that the guest has crashed
  browser_plugin->GuestCrashed();

  // Verify that our event listener has fired.
  EXPECT_EQ("crashed", ExecuteScriptAndReturnString("msg"));

  // Send an event and verify that events are no longer deported.
  browser_plugin->handleInputEvent(WebKit::WebMouseEvent(),
                                   cursor_info);
  EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
      BrowserPluginHostMsg_HandleInputEvent::ID));

  // Navigate and verify that the guest_crashed_ flag has been reset.
  browser_plugin->SetSrcAttribute("bar");
  EXPECT_FALSE(browser_plugin->guest_crashed_);
}

TEST_F(BrowserPluginTest, RemovePlugin) {
  LoadHTML(GetHTMLForBrowserPluginObject().c_str());
  EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
      BrowserPluginHostMsg_PluginDestroyed::ID));
  ExecuteJavaScript("x = document.getElementById('browserplugin'); "
                    "x.parentNode.removeChild(x);");
  ProcessPendingMessages();
  EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
      BrowserPluginHostMsg_PluginDestroyed::ID));
}

TEST_F(BrowserPluginTest, CustomEvents) {
  const char* kAddEventListener =
    "var url;"
    "function nav(u) {"
    "  url = u;"
    "}"
    "document.getElementById('browserplugin')."
    "    addEventListener('navigation', nav);";
  const char* kRemoveEventListener =
    "document.getElementById('browserplugin')."
    "    removeEventListener('navigation', nav);";
  const char* kGoogleURL = "http://www.google.com/";
  const char* kGoogleNewsURL = "http://news.google.com/";

  LoadHTML(GetHTMLForBrowserPluginObject().c_str());
  ExecuteJavaScript(kAddEventListener);
  // Grab the BrowserPlugin's instance ID from its resize message.
  const IPC::Message* msg =
      browser_plugin_manager()->sink().GetFirstMessageMatching(
          BrowserPluginHostMsg_ResizeGuest::ID);
  ASSERT_TRUE(msg);
  PickleIterator iter = IPC::SyncMessage::GetDataIterator(msg);
  BrowserPluginHostMsg_ResizeGuest::SendParam  resize_params;
  ASSERT_TRUE(IPC::ReadParam(msg, &iter, &resize_params));
  int instance_id = resize_params.a;

  MockBrowserPlugin* browser_plugin =
      static_cast<MockBrowserPlugin*>(
          browser_plugin_manager()->GetBrowserPlugin(instance_id));
  ASSERT_TRUE(browser_plugin);

  browser_plugin->DidNavigate(GURL(kGoogleURL));
  EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url"));

  ExecuteJavaScript(kRemoveEventListener);
  browser_plugin->DidNavigate(GURL(kGoogleNewsURL));
  // The URL variable should not change because we've removed the event
  // listener.
  EXPECT_EQ(kGoogleURL, ExecuteScriptAndReturnString("url"));
}

}  // namespace content