Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / browser_plugin / browser_plugin_browsertest.cc
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/renderer/browser_plugin/browser_plugin_browsertest.h"
6
7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h"
9 #include "base/path_service.h"
10 #include "base/pickle.h"
11 #include "content/public/common/content_constants.h"
12 #include "content/renderer/browser_plugin/browser_plugin.h"
13 #include "content/renderer/browser_plugin/browser_plugin_manager_factory.h"
14 #include "content/renderer/browser_plugin/mock_browser_plugin.h"
15 #include "content/renderer/browser_plugin/mock_browser_plugin_manager.h"
16 #include "content/renderer/render_thread_impl.h"
17 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
18 #include "skia/ext/platform_canvas.h"
19 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
21 #include "third_party/WebKit/public/web/WebScriptSource.h"
22
23 namespace {
24 const char kHTMLForBrowserPluginObject[] =
25     "<object id='browserplugin' width='640px' height='480px'"
26     " src='foo' type='%s'></object>"
27     "<script>document.querySelector('object').nonExistentAttribute;</script>";
28
29 const char kHTMLForBrowserPluginWithAllAttributes[] =
30     "<object id='browserplugin' width='640' height='480' type='%s'"
31     " autosize maxheight='600' maxwidth='800' minheight='240'"
32     " minwidth='320' name='Jim' partition='someid' src='foo'>";
33
34 const char kHTMLForSourcelessPluginObject[] =
35     "<object id='browserplugin' width='640px' height='480px' type='%s'>";
36
37 const char kHTMLForPartitionedPluginObject[] =
38     "<object id='browserplugin' width='640px' height='480px'"
39     "  src='foo' type='%s' partition='someid'>";
40
41 const char kHTMLForInvalidPartitionedPluginObject[] =
42     "<object id='browserplugin' width='640px' height='480px'"
43     "  type='%s' partition='persist:'>";
44
45 const char kHTMLForPartitionedPersistedPluginObject[] =
46     "<object id='browserplugin' width='640px' height='480px'"
47     "  src='foo' type='%s' partition='persist:someid'>";
48
49 std::string GetHTMLForBrowserPluginObject() {
50   return base::StringPrintf(kHTMLForBrowserPluginObject,
51                             content::kBrowserPluginMimeType);
52 }
53
54 }  // namespace
55
56 namespace content {
57
58 class TestContentRendererClient : public ContentRendererClient {
59  public:
60   TestContentRendererClient() : ContentRendererClient() {
61   }
62   virtual ~TestContentRendererClient() {
63   }
64   virtual bool AllowBrowserPlugin(
65       blink::WebPluginContainer* container) OVERRIDE {
66     // Allow BrowserPlugin for tests.
67     return true;
68   }
69 };
70
71 // Test factory for creating test instances of BrowserPluginManager.
72 class TestBrowserPluginManagerFactory : public BrowserPluginManagerFactory {
73  public:
74   virtual MockBrowserPluginManager* CreateBrowserPluginManager(
75       RenderViewImpl* render_view) OVERRIDE {
76     return new MockBrowserPluginManager(render_view);
77   }
78
79   // Singleton getter.
80   static TestBrowserPluginManagerFactory* GetInstance() {
81     return Singleton<TestBrowserPluginManagerFactory>::get();
82   }
83
84  protected:
85   TestBrowserPluginManagerFactory() {}
86   virtual ~TestBrowserPluginManagerFactory() {}
87
88  private:
89   // For Singleton.
90   friend struct DefaultSingletonTraits<TestBrowserPluginManagerFactory>;
91
92   DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginManagerFactory);
93 };
94
95 BrowserPluginTest::BrowserPluginTest() {}
96
97 BrowserPluginTest::~BrowserPluginTest() {}
98
99 void BrowserPluginTest::SetUp() {
100   test_content_renderer_client_.reset(new TestContentRendererClient);
101   SetRendererClientForTesting(test_content_renderer_client_.get());
102   BrowserPluginManager::set_factory_for_testing(
103       TestBrowserPluginManagerFactory::GetInstance());
104   content::RenderViewTest::SetUp();
105 }
106
107 void BrowserPluginTest::TearDown() {
108   BrowserPluginManager::set_factory_for_testing(
109       TestBrowserPluginManagerFactory::GetInstance());
110   content::RenderViewTest::TearDown();
111   test_content_renderer_client_.reset();
112 }
113
114 std::string BrowserPluginTest::ExecuteScriptAndReturnString(
115     const std::string& script) {
116   v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
117   v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue(
118       blink::WebScriptSource(blink::WebString::fromUTF8(script.c_str())));
119   if (value.IsEmpty() || !value->IsString())
120     return std::string();
121
122   v8::Local<v8::String> v8_str = value->ToString();
123   int length = v8_str->Utf8Length() + 1;
124   scoped_ptr<char[]> str(new char[length]);
125   v8_str->WriteUtf8(str.get(), length);
126   return str.get();
127 }
128
129 int BrowserPluginTest::ExecuteScriptAndReturnInt(
130     const std::string& script) {
131   v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
132   v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue(
133       blink::WebScriptSource(blink::WebString::fromUTF8(script.c_str())));
134   if (value.IsEmpty() || !value->IsInt32())
135     return 0;
136
137   return value->Int32Value();
138 }
139
140 // A return value of false means that a value was not present. The return value
141 // of the script is stored in |result|
142 bool BrowserPluginTest::ExecuteScriptAndReturnBool(
143     const std::string& script, bool* result) {
144   v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
145   v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue(
146       blink::WebScriptSource(blink::WebString::fromUTF8(script.c_str())));
147   if (value.IsEmpty() || !value->IsBoolean())
148     return false;
149
150   *result = value->BooleanValue();
151   return true;
152 }
153
154 MockBrowserPlugin* BrowserPluginTest::GetCurrentPlugin() {
155   BrowserPluginHostMsg_Attach_Params params;
156   return GetCurrentPluginWithAttachParams(&params);
157 }
158
159 MockBrowserPlugin* BrowserPluginTest::GetCurrentPluginWithAttachParams(
160     BrowserPluginHostMsg_Attach_Params* params) {
161   int instance_id = 0;
162   const IPC::Message* msg =
163       browser_plugin_manager()->sink().GetUniqueMessageMatching(
164           BrowserPluginHostMsg_Attach::ID);
165   if (!msg)
166     return NULL;
167
168   PickleIterator iter(*msg);
169   if (!iter.ReadInt(&instance_id))
170     return NULL;
171
172   if (!IPC::ParamTraits<BrowserPluginHostMsg_Attach_Params>::Read(
173       msg, &iter, params))
174     return NULL;
175
176   MockBrowserPlugin* browser_plugin = static_cast<MockBrowserPlugin*>(
177       browser_plugin_manager()->GetBrowserPlugin(instance_id));
178
179   BrowserPluginMsg_Attach_ACK_Params attach_ack_params;
180   browser_plugin->OnAttachACK(instance_id, attach_ack_params);
181
182   return browser_plugin;
183 }
184
185 // This test verifies that an initial resize occurs when we instantiate the
186 // browser plugin. This test also verifies that the browser plugin is waiting
187 // for a BrowserPluginMsg_UpdateRect in response. We issue an UpdateRect, and
188 // we observe an UpdateRect_ACK, with the |pending_damage_buffer_| reset,
189 // indiciating that the BrowserPlugin is not waiting for any more UpdateRects to
190 // satisfy its resize request.
191 TEST_F(BrowserPluginTest, InitialResize) {
192   LoadHTML(GetHTMLForBrowserPluginObject().c_str());
193   // Verify that the information in Attach is correct.
194   BrowserPluginHostMsg_Attach_Params params;
195   MockBrowserPlugin* browser_plugin = GetCurrentPluginWithAttachParams(&params);
196
197   EXPECT_EQ(640, params.resize_guest_params.view_rect.width());
198   EXPECT_EQ(480, params.resize_guest_params.view_rect.height());
199   ASSERT_TRUE(browser_plugin);
200   // Now the browser plugin is expecting a UpdateRect resize.
201   int instance_id = browser_plugin->guest_instance_id();
202   EXPECT_TRUE(browser_plugin->pending_damage_buffer_.get());
203
204   // Send the BrowserPlugin an UpdateRect equal to its container size with
205   // the same damage buffer. That should clear |pending_damage_buffer_|.
206   BrowserPluginMsg_UpdateRect_Params update_rect_params;
207   update_rect_params.damage_buffer_sequence_id =
208       browser_plugin->damage_buffer_sequence_id_;
209   update_rect_params.view_size = gfx::Size(640, 480);
210   update_rect_params.scale_factor = 1.0f;
211   update_rect_params.is_resize_ack = true;
212   update_rect_params.needs_ack = true;
213   BrowserPluginMsg_UpdateRect msg(instance_id, update_rect_params);
214   browser_plugin->OnMessageReceived(msg);
215   EXPECT_FALSE(browser_plugin->pending_damage_buffer_.get());
216 }
217
218 // This test verifies that all attributes (present at the time of writing) are
219 // parsed on initialization. However, this test does minimal checking of
220 // correct behavior.
221 TEST_F(BrowserPluginTest, ParseAllAttributes) {
222   std::string html = base::StringPrintf(kHTMLForBrowserPluginWithAllAttributes,
223                                         content::kBrowserPluginMimeType);
224   LoadHTML(html.c_str());
225   bool result;
226   bool has_value = ExecuteScriptAndReturnBool(
227       "document.getElementById('browserplugin').autosize", &result);
228   EXPECT_TRUE(has_value);
229   EXPECT_TRUE(result);
230   int maxHeight = ExecuteScriptAndReturnInt(
231       "document.getElementById('browserplugin').maxheight");
232   EXPECT_EQ(600, maxHeight);
233   int maxWidth = ExecuteScriptAndReturnInt(
234       "document.getElementById('browserplugin').maxwidth");
235   EXPECT_EQ(800, maxWidth);
236   int minHeight = ExecuteScriptAndReturnInt(
237       "document.getElementById('browserplugin').minheight");
238   EXPECT_EQ(240, minHeight);
239   int minWidth = ExecuteScriptAndReturnInt(
240       "document.getElementById('browserplugin').minwidth");
241   EXPECT_EQ(320, minWidth);
242   std::string name = ExecuteScriptAndReturnString(
243       "document.getElementById('browserplugin').name");
244   EXPECT_STREQ("Jim", name.c_str());
245   std::string partition = ExecuteScriptAndReturnString(
246       "document.getElementById('browserplugin').partition");
247   EXPECT_STREQ("someid", partition.c_str());
248   std::string src = ExecuteScriptAndReturnString(
249       "document.getElementById('browserplugin').src");
250   EXPECT_STREQ("foo", src.c_str());
251 }
252
253 // Verify that the src attribute on the browser plugin works as expected.
254 TEST_F(BrowserPluginTest, SrcAttribute) {
255   LoadHTML(GetHTMLForBrowserPluginObject().c_str());
256   // Verify that we're reporting the correct URL to navigate to based on the
257   // src attribute.
258   {
259     BrowserPluginHostMsg_Attach_Params params;
260     MockBrowserPlugin* browser_plugin =
261         GetCurrentPluginWithAttachParams(&params);
262     ASSERT_TRUE(browser_plugin);
263     EXPECT_EQ("foo", params.src);
264   }
265
266   browser_plugin_manager()->sink().ClearMessages();
267   // Navigate to bar and observe the associated
268   // BrowserPluginHostMsg_NavigateGuest message.
269   // Verify that the src attribute is updated as well.
270   ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'");
271   {
272     // Verify that we do not get a Attach on subsequent navigations.
273     const IPC::Message* create_msg =
274         browser_plugin_manager()->sink().GetUniqueMessageMatching(
275             BrowserPluginHostMsg_Attach::ID);
276     ASSERT_FALSE(create_msg);
277
278     const IPC::Message* msg =
279         browser_plugin_manager()->sink().GetUniqueMessageMatching(
280             BrowserPluginHostMsg_NavigateGuest::ID);
281     ASSERT_TRUE(msg);
282
283     int instance_id = 0;
284     std::string src;
285     BrowserPluginHostMsg_NavigateGuest::Read(msg, &instance_id, &src);
286     EXPECT_EQ("bar", src);
287     std::string src_value =
288         ExecuteScriptAndReturnString(
289             "document.getElementById('browserplugin').src");
290     EXPECT_EQ("bar", src_value);
291   }
292 }
293
294 TEST_F(BrowserPluginTest, ResizeFlowControl) {
295   LoadHTML(GetHTMLForBrowserPluginObject().c_str());
296   MockBrowserPlugin* browser_plugin = GetCurrentPlugin();
297   ASSERT_TRUE(browser_plugin);
298   int instance_id = browser_plugin->guest_instance_id();
299   EXPECT_TRUE(browser_plugin->pending_damage_buffer_.get());
300   // Send an UpdateRect to the BrowserPlugin to make it use the pending damage
301   // buffer.
302   {
303     // We send a stale UpdateRect to the BrowserPlugin.
304     BrowserPluginMsg_UpdateRect_Params update_rect_params;
305     update_rect_params.view_size = gfx::Size(640, 480);
306     update_rect_params.scale_factor = 1.0f;
307     update_rect_params.is_resize_ack = true;
308     update_rect_params.needs_ack = true;
309     // By sending |damage_buffer_sequence_id| back to BrowserPlugin on
310     // UpdateRect, then the BrowserPlugin knows that the browser process has
311     // received and has begun to use the |pending_damage_buffer_|.
312     update_rect_params.damage_buffer_sequence_id =
313         browser_plugin->damage_buffer_sequence_id_;
314     BrowserPluginMsg_UpdateRect msg(instance_id, update_rect_params);
315     browser_plugin->OnMessageReceived(msg);
316     EXPECT_EQ(NULL, browser_plugin->pending_damage_buffer_.get());
317   }
318
319   browser_plugin_manager()->sink().ClearMessages();
320
321   // Resize the browser plugin three times.
322
323   ExecuteJavaScript("document.getElementById('browserplugin').width = '641px'");
324   GetMainFrame()->view()->layout();
325   ProcessPendingMessages();
326
327   ExecuteJavaScript("document.getElementById('browserplugin').width = '642px'");
328   GetMainFrame()->view()->layout();
329   ProcessPendingMessages();
330
331   ExecuteJavaScript("document.getElementById('browserplugin').width = '643px'");
332   GetMainFrame()->view()->layout();
333   ProcessPendingMessages();
334
335   // Expect to see one resize messsage in the sink. BrowserPlugin will not issue
336   // subsequent resize requests until the first request is satisfied by the
337   // guest. The rest of the messages could be
338   // BrowserPluginHostMsg_UpdateGeometry msgs.
339   EXPECT_LE(1u, browser_plugin_manager()->sink().message_count());
340   for (size_t i = 0; i < browser_plugin_manager()->sink().message_count();
341        ++i) {
342     const IPC::Message* msg = browser_plugin_manager()->sink().GetMessageAt(i);
343     if (msg->type() != BrowserPluginHostMsg_ResizeGuest::ID)
344       EXPECT_EQ(msg->type(), BrowserPluginHostMsg_UpdateGeometry::ID);
345   }
346   const IPC::Message* msg =
347       browser_plugin_manager()->sink().GetUniqueMessageMatching(
348           BrowserPluginHostMsg_ResizeGuest::ID);
349   ASSERT_TRUE(msg);
350   BrowserPluginHostMsg_ResizeGuest_Params params;
351   BrowserPluginHostMsg_ResizeGuest::Read(msg, &instance_id, &params);
352   EXPECT_EQ(641, params.view_rect.width());
353   EXPECT_EQ(480, params.view_rect.height());
354   // This indicates that the BrowserPlugin has sent out a previous resize
355   // request but has not yet received an UpdateRect for that request.
356   EXPECT_TRUE(browser_plugin->pending_damage_buffer_.get());
357
358   {
359     // We send a stale UpdateRect to the BrowserPlugin.
360     BrowserPluginMsg_UpdateRect_Params update_rect_params;
361     update_rect_params.view_size = gfx::Size(641, 480);
362     update_rect_params.scale_factor = 1.0f;
363     update_rect_params.is_resize_ack = true;
364     update_rect_params.needs_ack = true;
365     update_rect_params.damage_buffer_sequence_id =
366         browser_plugin->damage_buffer_sequence_id_;
367     BrowserPluginMsg_UpdateRect msg(instance_id, update_rect_params);
368     browser_plugin->OnMessageReceived(msg);
369     // This tells us that the BrowserPlugin is still expecting another
370     // UpdateRect with the most recent size.
371     EXPECT_TRUE(browser_plugin->pending_damage_buffer_.get());
372   }
373   // Send the BrowserPlugin another UpdateRect, but this time with a size
374   // that matches the size of the container.
375   {
376     BrowserPluginMsg_UpdateRect_Params update_rect_params;
377     update_rect_params.view_size = gfx::Size(643, 480);
378     update_rect_params.scale_factor = 1.0f;
379     update_rect_params.is_resize_ack = true;
380     update_rect_params.needs_ack = true;
381     update_rect_params.damage_buffer_sequence_id =
382         browser_plugin->damage_buffer_sequence_id_;
383     BrowserPluginMsg_UpdateRect msg(instance_id, update_rect_params);
384     browser_plugin->OnMessageReceived(msg);
385     // The BrowserPlugin has finally received an UpdateRect that satisifes
386     // its current size, and so it is happy.
387     EXPECT_FALSE(browser_plugin->pending_damage_buffer_.get());
388   }
389 }
390
391 TEST_F(BrowserPluginTest, RemovePlugin) {
392   LoadHTML(GetHTMLForBrowserPluginObject().c_str());
393   EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
394       BrowserPluginHostMsg_PluginDestroyed::ID));
395   ExecuteJavaScript("x = document.getElementById('browserplugin'); "
396                     "x.parentNode.removeChild(x);");
397   ProcessPendingMessages();
398   EXPECT_TRUE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
399       BrowserPluginHostMsg_PluginDestroyed::ID));
400 }
401
402 // This test verifies that PluginDestroyed messages do not get sent from a
403 // BrowserPlugin that has never navigated.
404 TEST_F(BrowserPluginTest, RemovePluginBeforeNavigation) {
405   std::string html = base::StringPrintf(kHTMLForSourcelessPluginObject,
406                                         content::kBrowserPluginMimeType);
407   LoadHTML(html.c_str());
408   EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
409       BrowserPluginHostMsg_PluginDestroyed::ID));
410   ExecuteJavaScript("x = document.getElementById('browserplugin'); "
411                     "x.parentNode.removeChild(x);");
412   ProcessPendingMessages();
413   EXPECT_FALSE(browser_plugin_manager()->sink().GetUniqueMessageMatching(
414       BrowserPluginHostMsg_PluginDestroyed::ID));
415 }
416
417 // Verify that the 'partition' attribute on the browser plugin is parsed
418 // correctly.
419 TEST_F(BrowserPluginTest, PartitionAttribute) {
420   std::string html = base::StringPrintf(kHTMLForPartitionedPluginObject,
421                                         content::kBrowserPluginMimeType);
422   LoadHTML(html.c_str());
423   std::string partition_value = ExecuteScriptAndReturnString(
424       "document.getElementById('browserplugin').partition");
425   EXPECT_STREQ("someid", partition_value.c_str());
426
427   html = base::StringPrintf(kHTMLForPartitionedPersistedPluginObject,
428                             content::kBrowserPluginMimeType);
429   LoadHTML(html.c_str());
430   partition_value = ExecuteScriptAndReturnString(
431       "document.getElementById('browserplugin').partition");
432   EXPECT_STREQ("persist:someid", partition_value.c_str());
433
434   // Verify that once HTML has defined a source and partition, we cannot change
435   // the partition anymore.
436   ExecuteJavaScript(
437       "try {"
438       "  document.getElementById('browserplugin').partition = 'foo';"
439       "  document.title = 'success';"
440       "} catch (e) { document.title = e.message; }");
441   std::string title = ExecuteScriptAndReturnString("document.title");
442   EXPECT_STREQ(
443       "The object has already navigated, so its partition cannot be changed.",
444       title.c_str());
445
446   // Load a browser tag without 'src' defined.
447   html = base::StringPrintf(kHTMLForSourcelessPluginObject,
448                             content::kBrowserPluginMimeType);
449   LoadHTML(html.c_str());
450
451   // Ensure we don't parse just "persist:" string and return exception.
452   ExecuteJavaScript(
453       "try {"
454       "  document.getElementById('browserplugin').partition = 'persist:';"
455       "  document.title = 'success';"
456       "} catch (e) { document.title = e.message; }");
457   title = ExecuteScriptAndReturnString("document.title");
458   EXPECT_STREQ("Invalid partition attribute.", title.c_str());
459 }
460
461 // This test verifies that BrowserPlugin enters an error state when the
462 // partition attribute is invalid.
463 TEST_F(BrowserPluginTest, InvalidPartition) {
464   std::string html = base::StringPrintf(kHTMLForInvalidPartitionedPluginObject,
465                                         content::kBrowserPluginMimeType);
466   LoadHTML(html.c_str());
467   // Attempt to navigate with an invalid partition.
468   {
469     ExecuteJavaScript(
470         "try {"
471         "  document.getElementById('browserplugin').src = 'bar';"
472         "  document.title = 'success';"
473         "} catch (e) { document.title = e.message; }");
474     std::string title = ExecuteScriptAndReturnString("document.title");
475     EXPECT_STREQ("Invalid partition attribute.", title.c_str());
476     // Verify that the 'src' attribute has not been updated.
477     EXPECT_EQ("", ExecuteScriptAndReturnString(
478         "document.getElementById('browserplugin').src"));
479   }
480
481   // Verify that the BrowserPlugin accepts changes to its src attribue after
482   // setting the partition to a valid value.
483   ExecuteJavaScript(
484       "document.getElementById('browserplugin').partition = 'persist:foo'");
485   ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'");
486   EXPECT_EQ("bar", ExecuteScriptAndReturnString(
487       "document.getElementById('browserplugin').src"));
488   ProcessPendingMessages();
489   // Verify that the BrowserPlugin does not 'deadlock': it can recover from
490   // the partition ID error state.
491   {
492     ExecuteJavaScript(
493         "try {"
494         "  document.getElementById('browserplugin').partition = 'persist:1337';"
495         "  document.title = 'success';"
496         "} catch (e) { document.title = e.message; }");
497     std::string title = ExecuteScriptAndReturnString("document.title");
498     EXPECT_STREQ(
499         "The object has already navigated, so its partition cannot be changed.",
500         title.c_str());
501     ExecuteJavaScript("document.getElementById('browserplugin').src = '42'");
502     EXPECT_EQ("42", ExecuteScriptAndReturnString(
503         "document.getElementById('browserplugin').src"));
504   }
505 }
506
507 // Test to verify that after the first navigation, the partition attribute
508 // cannot be modified.
509 TEST_F(BrowserPluginTest, ImmutableAttributesAfterNavigation) {
510   std::string html = base::StringPrintf(kHTMLForSourcelessPluginObject,
511                                         content::kBrowserPluginMimeType);
512   LoadHTML(html.c_str());
513
514   ExecuteJavaScript(
515       "document.getElementById('browserplugin').partition = 'storage'");
516   std::string partition_value = ExecuteScriptAndReturnString(
517       "document.getElementById('browserplugin').partition");
518   EXPECT_STREQ("storage", partition_value.c_str());
519
520   std::string src_value = ExecuteScriptAndReturnString(
521       "document.getElementById('browserplugin').src");
522   EXPECT_STREQ("", src_value.c_str());
523
524   ExecuteJavaScript("document.getElementById('browserplugin').src = 'bar'");
525   ProcessPendingMessages();
526   {
527     BrowserPluginHostMsg_Attach_Params params;
528     MockBrowserPlugin* browser_plugin =
529         GetCurrentPluginWithAttachParams(&params);
530     ASSERT_TRUE(browser_plugin);
531
532     EXPECT_STREQ("storage", params.storage_partition_id.c_str());
533     EXPECT_FALSE(params.persist_storage);
534     EXPECT_STREQ("bar", params.src.c_str());
535   }
536
537   // Setting the partition should throw an exception and the value should not
538   // change.
539   ExecuteJavaScript(
540       "try {"
541       "  document.getElementById('browserplugin').partition = 'someid';"
542       "  document.title = 'success';"
543       "} catch (e) { document.title = e.message; }");
544
545   std::string title = ExecuteScriptAndReturnString("document.title");
546   EXPECT_STREQ(
547       "The object has already navigated, so its partition cannot be changed.",
548       title.c_str());
549
550   partition_value = ExecuteScriptAndReturnString(
551       "document.getElementById('browserplugin').partition");
552   EXPECT_STREQ("storage", partition_value.c_str());
553 }
554
555 TEST_F(BrowserPluginTest, AutoSizeAttributes) {
556   std::string html = base::StringPrintf(kHTMLForSourcelessPluginObject,
557                                         content::kBrowserPluginMimeType);
558   LoadHTML(html.c_str());
559   const char* kSetAutoSizeParametersAndNavigate =
560     "var browserplugin = document.getElementById('browserplugin');"
561     "browserplugin.autosize = true;"
562     "browserplugin.minwidth = 42;"
563     "browserplugin.minheight = 43;"
564     "browserplugin.maxwidth = 1337;"
565     "browserplugin.maxheight = 1338;"
566     "browserplugin.src = 'foobar';";
567   const char* kDisableAutoSize =
568     "document.getElementById('browserplugin').removeAttribute('autosize');";
569
570   int instance_id = 0;
571   // Set some autosize parameters before navigating then navigate.
572   // Verify that the BrowserPluginHostMsg_Attach message contains
573   // the correct autosize parameters.
574   ExecuteJavaScript(kSetAutoSizeParametersAndNavigate);
575   ProcessPendingMessages();
576
577   BrowserPluginHostMsg_Attach_Params params;
578   MockBrowserPlugin* browser_plugin =
579       GetCurrentPluginWithAttachParams(&params);
580   ASSERT_TRUE(browser_plugin);
581
582   EXPECT_TRUE(params.auto_size_params.enable);
583   EXPECT_EQ(42, params.auto_size_params.min_size.width());
584   EXPECT_EQ(43, params.auto_size_params.min_size.height());
585   EXPECT_EQ(1337, params.auto_size_params.max_size.width());
586   EXPECT_EQ(1338, params.auto_size_params.max_size.height());
587
588   // Verify that we are waiting for the browser process to grab the new
589   // damage buffer.
590   EXPECT_TRUE(browser_plugin->pending_damage_buffer_.get());
591   // Disable autosize. AutoSize state will not be sent to the guest until
592   // the guest has responded to the last resize request.
593   ExecuteJavaScript(kDisableAutoSize);
594   ProcessPendingMessages();
595
596   const IPC::Message* auto_size_msg =
597   browser_plugin_manager()->sink().GetUniqueMessageMatching(
598       BrowserPluginHostMsg_SetAutoSize::ID);
599   EXPECT_FALSE(auto_size_msg);
600
601   // Send the BrowserPlugin an UpdateRect equal to its |max_size| with
602   // the same damage buffer.
603   BrowserPluginMsg_UpdateRect_Params update_rect_params;
604   update_rect_params.damage_buffer_sequence_id =
605       browser_plugin->damage_buffer_sequence_id_;
606   update_rect_params.view_size = gfx::Size(1337, 1338);
607   update_rect_params.scale_factor = 1.0f;
608   update_rect_params.is_resize_ack = true;
609   update_rect_params.needs_ack = true;
610   BrowserPluginMsg_UpdateRect msg(instance_id, update_rect_params);
611   browser_plugin->OnMessageReceived(msg);
612
613   // Verify that the autosize state has been updated.
614   {
615     const IPC::Message* auto_size_msg =
616     browser_plugin_manager()->sink().GetUniqueMessageMatching(
617         BrowserPluginHostMsg_UpdateRect_ACK::ID);
618     ASSERT_TRUE(auto_size_msg);
619
620     int instance_id = 0;
621     bool needs_ack = false;
622     BrowserPluginHostMsg_AutoSize_Params auto_size_params;
623     BrowserPluginHostMsg_ResizeGuest_Params resize_params;
624     BrowserPluginHostMsg_UpdateRect_ACK::Read(auto_size_msg,
625                                               &instance_id,
626                                               &needs_ack,
627                                               &auto_size_params,
628                                               &resize_params);
629     EXPECT_FALSE(auto_size_params.enable);
630     // These value are not populated (as an optimization) if autosize is
631     // disabled.
632     EXPECT_EQ(0, auto_size_params.min_size.width());
633     EXPECT_EQ(0, auto_size_params.min_size.height());
634     EXPECT_EQ(0, auto_size_params.max_size.width());
635     EXPECT_EQ(0, auto_size_params.max_size.height());
636   }
637 }
638
639 }  // namespace content