| 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/renderer/browser_plugin/browser_plugin_browsertest.h" | |
| 6 | |
| 7 #include "base/debug/leak_annotations.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/singleton.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/pickle.h" | |
| 12 #include "content/public/common/content_constants.h" | |
| 13 #include "content/public/renderer/content_renderer_client.h" | |
| 14 #include "content/renderer/browser_plugin/browser_plugin.h" | |
| 15 #include "content/renderer/browser_plugin/browser_plugin_manager_factory.h" | |
| 16 #include "content/renderer/browser_plugin/mock_browser_plugin.h" | |
| 17 #include "content/renderer/browser_plugin/mock_browser_plugin_manager.h" | |
| 18 #include "content/renderer/render_thread_impl.h" | |
| 19 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | |
| 20 #include "skia/ext/platform_canvas.h" | |
| 21 #include "third_party/WebKit/public/platform/WebCursorInfo.h" | |
| 22 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
| 23 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 24 #include "third_party/WebKit/public/web/WebScriptSource.h" | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 namespace { | |
| 29 const char kHTMLForBrowserPluginObject[] = | |
| 30 "<object id='browserplugin' width='640px' height='480px'" | |
| 31 " src='foo' type='%s'></object>" | |
| 32 "<script>document.querySelector('object').nonExistentAttribute;</script>"; | |
| 33 | |
| 34 std::string GetHTMLForBrowserPluginObject() { | |
| 35 return base::StringPrintf(kHTMLForBrowserPluginObject, | |
| 36 kBrowserPluginMimeType); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 // Test factory for creating test instances of BrowserPluginManager. | |
| 42 class TestBrowserPluginManagerFactory : public BrowserPluginManagerFactory { | |
| 43 public: | |
| 44 virtual MockBrowserPluginManager* CreateBrowserPluginManager( | |
| 45 RenderViewImpl* render_view) OVERRIDE { | |
| 46 return new MockBrowserPluginManager(render_view); | |
| 47 } | |
| 48 | |
| 49 // Singleton getter. | |
| 50 static TestBrowserPluginManagerFactory* GetInstance() { | |
| 51 return Singleton<TestBrowserPluginManagerFactory>::get(); | |
| 52 } | |
| 53 | |
| 54 protected: | |
| 55 TestBrowserPluginManagerFactory() {} | |
| 56 virtual ~TestBrowserPluginManagerFactory() {} | |
| 57 | |
| 58 private: | |
| 59 // For Singleton. | |
| 60 friend struct DefaultSingletonTraits<TestBrowserPluginManagerFactory>; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginManagerFactory); | |
| 63 }; | |
| 64 | |
| 65 BrowserPluginTest::BrowserPluginTest() {} | |
| 66 | |
| 67 BrowserPluginTest::~BrowserPluginTest() {} | |
| 68 | |
| 69 void BrowserPluginTest::SetUp() { | |
| 70 BrowserPluginManager::set_factory_for_testing( | |
| 71 TestBrowserPluginManagerFactory::GetInstance()); | |
| 72 content::RenderViewTest::SetUp(); | |
| 73 } | |
| 74 | |
| 75 void BrowserPluginTest::TearDown() { | |
| 76 BrowserPluginManager::set_factory_for_testing( | |
| 77 TestBrowserPluginManagerFactory::GetInstance()); | |
| 78 #if defined(LEAK_SANITIZER) | |
| 79 // Do this before shutting down V8 in RenderViewTest::TearDown(). | |
| 80 // http://crbug.com/328552 | |
| 81 __lsan_do_leak_check(); | |
| 82 #endif | |
| 83 RenderViewTest::TearDown(); | |
| 84 } | |
| 85 | |
| 86 std::string BrowserPluginTest::ExecuteScriptAndReturnString( | |
| 87 const std::string& script) { | |
| 88 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | |
| 89 v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue( | |
| 90 blink::WebScriptSource(blink::WebString::fromUTF8(script.c_str()))); | |
| 91 if (value.IsEmpty() || !value->IsString()) | |
| 92 return std::string(); | |
| 93 | |
| 94 v8::Local<v8::String> v8_str = value->ToString(); | |
| 95 int length = v8_str->Utf8Length() + 1; | |
| 96 scoped_ptr<char[]> str(new char[length]); | |
| 97 v8_str->WriteUtf8(str.get(), length); | |
| 98 return str.get(); | |
| 99 } | |
| 100 | |
| 101 int BrowserPluginTest::ExecuteScriptAndReturnInt( | |
| 102 const std::string& script) { | |
| 103 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | |
| 104 v8::Handle<v8::Value> value = GetMainFrame()->executeScriptAndReturnValue( | |
| 105 blink::WebScriptSource(blink::WebString::fromUTF8(script.c_str()))); | |
| 106 if (value.IsEmpty() || !value->IsInt32()) | |
| 107 return 0; | |
| 108 | |
| 109 return value->Int32Value(); | |
| 110 } | |
| 111 | |
| 112 // A return value of false means that a value was not present. The return value | |
| 113 // of the script is stored in |result| | |
| 114 bool BrowserPluginTest::ExecuteScriptAndReturnBool( | |
| 115 const std::string& script, bool* result) { | |
| 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->IsBoolean()) | |
| 120 return false; | |
| 121 | |
| 122 *result = value->BooleanValue(); | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 MockBrowserPlugin* BrowserPluginTest::GetCurrentPlugin() { | |
| 127 BrowserPluginHostMsg_Attach_Params params; | |
| 128 return GetCurrentPluginWithAttachParams(¶ms); | |
| 129 } | |
| 130 | |
| 131 MockBrowserPlugin* BrowserPluginTest::GetCurrentPluginWithAttachParams( | |
| 132 BrowserPluginHostMsg_Attach_Params* params) { | |
| 133 MockBrowserPlugin* browser_plugin = static_cast<MockBrowserPluginManager*>( | |
| 134 browser_plugin_manager())->last_plugin(); | |
| 135 if (!browser_plugin) | |
| 136 return NULL; | |
| 137 | |
| 138 browser_plugin->Attach(); | |
| 139 | |
| 140 int instance_id = 0; | |
| 141 const IPC::Message* msg = | |
| 142 browser_plugin_manager()->sink().GetUniqueMessageMatching( | |
| 143 BrowserPluginHostMsg_Attach::ID); | |
| 144 if (!msg) | |
| 145 return NULL; | |
| 146 | |
| 147 PickleIterator iter(*msg); | |
| 148 if (!iter.ReadInt(&instance_id)) | |
| 149 return NULL; | |
| 150 | |
| 151 if (!IPC::ParamTraits<BrowserPluginHostMsg_Attach_Params>::Read( | |
| 152 msg, &iter, params)) { | |
| 153 return NULL; | |
| 154 } | |
| 155 | |
| 156 browser_plugin->OnAttachACK(instance_id); | |
| 157 return browser_plugin; | |
| 158 } | |
| 159 | |
| 160 // This test verifies that an initial resize occurs when we instantiate the | |
| 161 // browser plugin. | |
| 162 TEST_F(BrowserPluginTest, InitialResize) { | |
| 163 LoadHTML(GetHTMLForBrowserPluginObject().c_str()); | |
| 164 // Verify that the information in Attach is correct. | |
| 165 BrowserPluginHostMsg_Attach_Params params; | |
| 166 MockBrowserPlugin* browser_plugin = GetCurrentPluginWithAttachParams(¶ms); | |
| 167 | |
| 168 EXPECT_EQ(640, params.resize_guest_params.view_size.width()); | |
| 169 EXPECT_EQ(480, params.resize_guest_params.view_size.height()); | |
| 170 ASSERT_TRUE(browser_plugin); | |
| 171 } | |
| 172 | |
| 173 } // namespace content | |
| OLD | NEW |