| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 <map> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "build/build_config.h" | |
| 11 #include "chrome/common/render_messages.h" | |
| 12 #include "chrome/renderer/pepper_devices.h" | |
| 13 #include "chrome/renderer/webplugin_delegate_pepper.h" | |
| 14 #include "chrome/test/render_view_test.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/npapi/bindings/npapi.h" | |
| 17 #include "third_party/npapi/bindings/npruntime.h" | |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPlugin.h" | |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginParams.h" | |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | |
| 21 #include "webkit/plugins/npapi/plugin_instance.h" | |
| 22 #include "webkit/plugins/npapi/plugin_list.h" | |
| 23 #include "webkit/plugins/npapi/webplugin_impl.h" | |
| 24 | |
| 25 class PepperDeviceTest; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const FilePath::CharType kTestPluginFileName[] = | |
| 30 FILE_PATH_LITERAL("pepper-device-tester"); | |
| 31 const char kTestPluginMimeType[] = "chrome-test/pepper-device-test"; | |
| 32 | |
| 33 // This maps the NPP instances to the test object so our C callbacks can easily | |
| 34 // get back to the object. There will normally be only one item in this map. | |
| 35 static std::map<NPP, PepperDeviceTest*> active_tests; | |
| 36 | |
| 37 NPError NPP_New(NPMIMEType plugin_type, NPP instance, | |
| 38 uint16 mode, int16 argc, char* argn[], | |
| 39 char* argv[], NPSavedData* saved) { | |
| 40 // Watch out: active_tests won't contain the NPP pointer until after this | |
| 41 // call is complete, so don't use it. | |
| 42 return NPERR_NO_ERROR; | |
| 43 } | |
| 44 | |
| 45 NPError NPP_Destroy(NPP instance, NPSavedData** saved) { | |
| 46 if (!instance) | |
| 47 return NPERR_INVALID_INSTANCE_ERROR; | |
| 48 return NPERR_NO_ERROR; | |
| 49 } | |
| 50 | |
| 51 NPError NPP_SetWindow(NPP instance, NPWindow* window) { | |
| 52 return NPERR_NO_ERROR; | |
| 53 } | |
| 54 | |
| 55 int16 NPP_HandleEvent(NPP instance, void* event) { | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 NPError NPP_GetValue(NPP instance, NPPVariable variable, void* value) { | |
| 60 if (!instance) | |
| 61 return NPERR_INVALID_INSTANCE_ERROR; | |
| 62 switch (variable) { | |
| 63 case NPPVpluginNeedsXEmbed: | |
| 64 *static_cast<NPBool*>(value) = 1; | |
| 65 return NPERR_NO_ERROR; | |
| 66 default: | |
| 67 return NPERR_INVALID_PARAM; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 NPError NPP_SetValue(NPP instance, NPNVariable variable, void* value) { | |
| 72 return NPERR_NO_ERROR; | |
| 73 } | |
| 74 | |
| 75 NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* plugin_funcs) { | |
| 76 plugin_funcs->newp = NPP_New; | |
| 77 plugin_funcs->destroy = NPP_Destroy; | |
| 78 plugin_funcs->setwindow = NPP_SetWindow; | |
| 79 plugin_funcs->event = NPP_HandleEvent; | |
| 80 plugin_funcs->getvalue = NPP_GetValue; | |
| 81 plugin_funcs->setvalue = NPP_SetValue; | |
| 82 return NPERR_NO_ERROR; | |
| 83 } | |
| 84 | |
| 85 #if defined(OS_MACOSX) || defined(OS_WIN) | |
| 86 NPError API_CALL NP_Initialize(NPNetscapeFuncs* browser_funcs) { | |
| 87 return NPERR_NO_ERROR; | |
| 88 } | |
| 89 #else | |
| 90 NPError API_CALL NP_Initialize(NPNetscapeFuncs* browser_funcs, | |
| 91 NPPluginFuncs* plugin_funcs) { | |
| 92 NP_GetEntryPoints(plugin_funcs); | |
| 93 return NPERR_NO_ERROR; | |
| 94 } | |
| 95 #endif | |
| 96 | |
| 97 NPError API_CALL NP_Shutdown() { | |
| 98 return NPERR_NO_ERROR; | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 // PepperDeviceTest ------------------------------------------------------------ | |
| 104 | |
| 105 class PepperDeviceTest : public RenderViewTest { | |
| 106 public: | |
| 107 WebPluginDelegatePepper* pepper_plugin() const { return pepper_plugin_; } | |
| 108 | |
| 109 NPP npp() const { return pepper_plugin_->instance()->npp(); } | |
| 110 | |
| 111 protected: | |
| 112 // Logs that the given flush command was called in flush_calls. | |
| 113 static void FlushCalled(NPP instance, | |
| 114 NPDeviceContext* context, | |
| 115 NPError err, | |
| 116 NPUserData* user_data); | |
| 117 | |
| 118 // Audio callback, currently empty. | |
| 119 static void AudioCallback(NPDeviceContextAudio* context); | |
| 120 | |
| 121 // A log of flush commands we can use to check the async callbacks. | |
| 122 struct FlushData { | |
| 123 NPP instance; | |
| 124 NPDeviceContext* context; | |
| 125 NPError err; | |
| 126 NPUserData* user_data; | |
| 127 }; | |
| 128 std::vector<FlushData> flush_calls_; | |
| 129 | |
| 130 private: | |
| 131 // testing::Test implementation. | |
| 132 virtual void SetUp(); | |
| 133 virtual void TearDown(); | |
| 134 | |
| 135 scoped_ptr<webkit::npapi::WebPluginImpl> plugin_; | |
| 136 WebPluginDelegatePepper* pepper_plugin_; // FIXME(brettw): check lifetime. | |
| 137 }; | |
| 138 | |
| 139 void PepperDeviceTest::SetUp() { | |
| 140 RenderViewTest::SetUp(); | |
| 141 | |
| 142 webkit::npapi::PluginEntryPoints entry_points = { | |
| 143 #if !defined(OS_POSIX) || defined(OS_MACOSX) | |
| 144 NP_GetEntryPoints, | |
| 145 #endif | |
| 146 NP_Initialize, | |
| 147 NP_Shutdown | |
| 148 }; | |
| 149 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin( | |
| 150 FilePath(kTestPluginFileName), | |
| 151 "Pepper device test plugin", | |
| 152 "Pepper device test plugin", | |
| 153 kTestPluginMimeType, | |
| 154 entry_points); | |
| 155 | |
| 156 // Create the WebKit plugin with no delegates (this seems to work | |
| 157 // sufficiently for the test). | |
| 158 WebKit::WebPluginParams params; | |
| 159 plugin_.reset(new webkit::npapi::WebPluginImpl( | |
| 160 NULL, params, FilePath(), std::string(), | |
| 161 base::WeakPtr<webkit::npapi::WebPluginPageDelegate>())); | |
| 162 | |
| 163 // Create a pepper plugin for the RenderView. | |
| 164 pepper_plugin_ = WebPluginDelegatePepper::Create( | |
| 165 FilePath(kTestPluginFileName), kTestPluginMimeType, view_->AsWeakPtr()); | |
| 166 ASSERT_TRUE(pepper_plugin_); | |
| 167 ASSERT_TRUE(pepper_plugin_->Initialize(GURL(), std::vector<std::string>(), | |
| 168 std::vector<std::string>(), | |
| 169 plugin_.get(), false)); | |
| 170 | |
| 171 // Normally the RenderView creates the pepper plugin and registers it with | |
| 172 // its internal list. Since we're creating it manually, we have to reach in | |
| 173 // and register it to prevent tear-down from asserting. | |
| 174 view_->current_oldstyle_pepper_plugins_.insert(pepper_plugin_); | |
| 175 | |
| 176 active_tests[npp()] = this; | |
| 177 | |
| 178 // Need to specify a window size or graphics calls will fail on the 0x0 | |
| 179 // bitmap. | |
| 180 gfx::Rect rect(0, 0, 100, 100); | |
| 181 view_->OnResize(rect.size(), gfx::Rect()); | |
| 182 pepper_plugin_->UpdateGeometry(rect, rect); | |
| 183 } | |
| 184 | |
| 185 void PepperDeviceTest::TearDown() { | |
| 186 active_tests.erase(active_tests.find(npp())); | |
| 187 | |
| 188 plugin_.reset(); | |
| 189 if (pepper_plugin_) | |
| 190 pepper_plugin_->PluginDestroyed(); | |
| 191 | |
| 192 webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin( | |
| 193 FilePath(kTestPluginFileName)); | |
| 194 | |
| 195 RenderViewTest::TearDown(); | |
| 196 } | |
| 197 | |
| 198 // static | |
| 199 void PepperDeviceTest::FlushCalled(NPP instance, | |
| 200 NPDeviceContext* context, | |
| 201 NPError err, | |
| 202 NPUserData* user_data) { | |
| 203 if (active_tests.find(instance) == active_tests.end()) | |
| 204 return; | |
| 205 PepperDeviceTest* that = active_tests[instance]; | |
| 206 | |
| 207 FlushData flush_data; | |
| 208 flush_data.instance = instance; | |
| 209 flush_data.context = context; | |
| 210 flush_data.err = err; | |
| 211 flush_data.user_data = user_data; | |
| 212 that->flush_calls_.push_back(flush_data); | |
| 213 } | |
| 214 | |
| 215 void PepperDeviceTest::AudioCallback(NPDeviceContextAudio* context) { | |
| 216 } | |
| 217 | |
| 218 | |
| 219 // ----------------------------------------------------------------------------- | |
| 220 | |
| 221 // TODO(brettw) this crashes on Mac. Figure out why and enable. | |
| 222 #if !defined(OS_MACOSX) | |
| 223 | |
| 224 TEST_F(PepperDeviceTest, Flush) { | |
| 225 // Create a 2D device. | |
| 226 NPDeviceContext2DConfig config; | |
| 227 NPDeviceContext2D context; | |
| 228 EXPECT_EQ(NPERR_NO_ERROR, | |
| 229 pepper_plugin()->Device2DInitializeContext(&config, &context)); | |
| 230 | |
| 231 // Flush the bitmap. Here we fake the invalidate call to the RenderView since | |
| 232 // there isn't an actual visible web page that would otherwise get painted. | |
| 233 // The callback should not get called synchronously. | |
| 234 pepper_plugin()->Device2DFlushContext(npp(), &context, &FlushCalled, NULL); | |
| 235 view_->didInvalidateRect(WebKit::WebRect(0, 0, 100, 100)); | |
| 236 EXPECT_TRUE(flush_calls_.empty()); | |
| 237 | |
| 238 // Run the message loop which should process the pending paints, there should | |
| 239 // still be no callbacks since the stuff hasn't been copied to the screen, | |
| 240 // but there should be a paint message sent to the browser. | |
| 241 MessageLoop::current()->RunAllPending(); | |
| 242 EXPECT_TRUE(flush_calls_.empty()); | |
| 243 EXPECT_TRUE(render_thread_.sink().GetFirstMessageMatching( | |
| 244 ViewHostMsg_UpdateRect::ID)); | |
| 245 | |
| 246 // Send a paint ACK, this should trigger the callback. | |
| 247 view_->OnMessageReceived(ViewMsg_UpdateRect_ACK(view_->routing_id())); | |
| 248 EXPECT_EQ(1u, flush_calls_.size()); | |
| 249 } | |
| 250 #endif | |
| 251 | |
| 252 TEST_F(PepperDeviceTest, AudioInit) { | |
| 253 NPDeviceContextAudioConfig config; | |
| 254 config.sampleRate = NPAudioSampleRate44100Hz; | |
| 255 config.sampleType = NPAudioSampleTypeInt16; | |
| 256 config.outputChannelMap = NPAudioChannelStereo; | |
| 257 config.callback = &AudioCallback; | |
| 258 config.userData = this; | |
| 259 NPDeviceContextAudio context; | |
| 260 EXPECT_EQ(NPERR_NO_ERROR, | |
| 261 pepper_plugin()->DeviceAudioInitializeContext(&config, &context)); | |
| 262 EXPECT_TRUE(render_thread_.sink().GetFirstMessageMatching( | |
| 263 ViewHostMsg_CreateAudioStream::ID)); | |
| 264 EXPECT_EQ(NPERR_NO_ERROR, | |
| 265 pepper_plugin()->DeviceAudioDestroyContext(&context)); | |
| 266 EXPECT_TRUE(render_thread_.sink().GetFirstMessageMatching( | |
| 267 ViewHostMsg_CloseAudioStream::ID)); | |
| 268 } | |
| 269 | |
| OLD | NEW |