1 /* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "PluginTest.h" 27 28 #include "PluginObject.h" 29 #include <assert.h> 30 #include <string.h> 31 32 #if defined(XP_UNIX) || defined(ANDROID) 33 #include <unistd.h> 34 #endif 35 36 using namespace std; 37 extern NPNetscapeFuncs *browser; 38 39 static void (*shutdownFunction)(); 40 41 PluginTest* PluginTest::create(NPP npp, const string& identifier) 42 { 43 if (identifier.empty()) 44 return new PluginTest(npp, identifier); 45 46 CreateTestFunction createTestFunction = createTestFunctions()[identifier]; 47 if (createTestFunction) 48 return createTestFunction(npp, identifier); 49 50 return 0; 51 } 52 53 PluginTest::PluginTest(NPP npp, const string& identifier) 54 : m_npp(npp) 55 , m_identifier(identifier) 56 { 57 // Reset the shutdown function. 58 shutdownFunction = 0; 59 } 60 61 PluginTest::~PluginTest() 62 { 63 } 64 65 void PluginTest::NP_Shutdown() 66 { 67 if (shutdownFunction) 68 shutdownFunction(); 69 } 70 71 void PluginTest::registerNPShutdownFunction(void (*func)()) 72 { 73 assert(!shutdownFunction); 74 shutdownFunction = func; 75 } 76 77 void PluginTest::indicateTestFailure() 78 { 79 // This should really be an assert, but there's no way for the test framework 80 // to know that the plug-in process crashed, so we'll just sleep for a while 81 // to ensure that the test times out. 82 #if defined(XP_WIN) 83 ::Sleep(100000); 84 #else 85 sleep(1000); 86 #endif 87 } 88 89 NPError PluginTest::NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved) 90 { 91 return NPERR_NO_ERROR; 92 } 93 94 NPError PluginTest::NPP_Destroy(NPSavedData**) 95 { 96 return NPERR_NO_ERROR; 97 } 98 99 NPError PluginTest::NPP_SetWindow(NPWindow*) 100 { 101 return NPERR_NO_ERROR; 102 } 103 104 NPError PluginTest::NPP_NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype) 105 { 106 return NPERR_NO_ERROR; 107 } 108 109 NPError PluginTest::NPP_DestroyStream(NPStream *stream, NPReason reason) 110 { 111 return NPERR_NO_ERROR; 112 } 113 114 int32_t PluginTest::NPP_WriteReady(NPStream*) 115 { 116 return 4096; 117 } 118 119 int32_t PluginTest::NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer) 120 { 121 return len; 122 } 123 124 int16_t PluginTest::NPP_HandleEvent(void*) 125 { 126 return 0; 127 } 128 129 bool PluginTest::NPP_URLNotify(const char* url, NPReason, void* notifyData) 130 { 131 // FIXME: Port the code from NPP_URLNotify in main.cpp over to always using 132 // PluginTest, so we don't have to use a return value to indicate whether the "default" NPP_URLNotify implementation should be invoked. 133 return false; 134 } 135 136 NPError PluginTest::NPP_GetValue(NPPVariable variable, void *value) 137 { 138 // We don't know anything about plug-in values so just return NPERR_GENERIC_ERROR. 139 return NPERR_GENERIC_ERROR; 140 } 141 142 NPError PluginTest::NPP_SetValue(NPNVariable, void *value) 143 { 144 return NPERR_GENERIC_ERROR; 145 } 146 147 // NPN functions. 148 149 NPError PluginTest::NPN_GetURL(const char* url, const char* target) 150 { 151 return browser->geturl(m_npp, url, target); 152 } 153 154 NPError PluginTest::NPN_GetURLNotify(const char *url, const char *target, void *notifyData) 155 { 156 return browser->geturlnotify(m_npp, url, target, notifyData); 157 } 158 159 NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value) 160 { 161 return browser->getvalue(m_npp, variable, value); 162 } 163 164 void PluginTest::NPN_InvalidateRect(NPRect* invalidRect) 165 { 166 browser->invalidaterect(m_npp, invalidRect); 167 } 168 169 bool PluginTest::NPN_Invoke(NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) 170 { 171 return browser->invoke(m_npp, npobj, methodName, args, argCount, result); 172 } 173 174 void* PluginTest::NPN_MemAlloc(uint32_t size) 175 { 176 return browser->memalloc(size); 177 } 178 179 // NPRuntime NPN functions. 180 181 NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8 *name) 182 { 183 return browser->getstringidentifier(name); 184 } 185 186 NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid) 187 { 188 return browser->getintidentifier(intid); 189 } 190 191 bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier) 192 { 193 return browser->identifierisstring(npIdentifier); 194 } 195 196 NPUTF8* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier) 197 { 198 return browser->utf8fromidentifier(npIdentifier); 199 } 200 201 int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier) 202 { 203 return browser->intfromidentifier(npIdentifier); 204 } 205 206 NPObject* PluginTest::NPN_CreateObject(NPClass* npClass) 207 { 208 return browser->createobject(m_npp, npClass); 209 } 210 211 NPObject* PluginTest::NPN_RetainObject(NPObject* npObject) 212 { 213 return browser->retainobject(npObject); 214 } 215 216 void PluginTest::NPN_ReleaseObject(NPObject* npObject) 217 { 218 browser->releaseobject(npObject); 219 } 220 221 bool PluginTest::NPN_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* value) 222 { 223 return browser->getproperty(m_npp, npObject, propertyName, value); 224 } 225 226 bool PluginTest::NPN_RemoveProperty(NPObject* npObject, NPIdentifier propertyName) 227 { 228 return browser->removeproperty(m_npp, npObject, propertyName); 229 } 230 231 void PluginTest::NPN_ReleaseVariantValue(NPVariant* variant) 232 { 233 browser->releasevariantvalue(variant); 234 } 235 236 #ifdef XP_MACOSX 237 bool PluginTest::NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace) 238 { 239 return browser->convertpoint(m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace); 240 } 241 #endif 242 243 bool PluginTest::executeScript(const NPString* script, NPVariant* result) 244 { 245 NPObject* windowScriptObject; 246 browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject); 247 248 return browser->evaluate(m_npp, windowScriptObject, const_cast<NPString*>(script), result); 249 } 250 251 void PluginTest::executeScript(const char* script) 252 { 253 NPString npScript; 254 npScript.UTF8Characters = script; 255 npScript.UTF8Length = strlen(script); 256 257 NPVariant browserResult; 258 executeScript(&npScript, &browserResult); 259 browser->releasevariantvalue(&browserResult); 260 } 261 262 void PluginTest::log(const char* format, ...) 263 { 264 va_list args; 265 va_start(args, format); 266 pluginLogWithArguments(m_npp, format, args); 267 va_end(args); 268 } 269 270 NPNetscapeFuncs* PluginTest::netscapeFuncs() 271 { 272 return browser; 273 } 274 275 void PluginTest::waitUntilDone() 276 { 277 executeScript("testRunner.waitUntilDone()"); 278 } 279 280 void PluginTest::notifyDone() 281 { 282 executeScript("testRunner.notifyDone()"); 283 } 284 285 void PluginTest::registerCreateTestFunction(const string& identifier, CreateTestFunction createTestFunction) 286 { 287 assert(!createTestFunctions().count(identifier)); 288 289 createTestFunctions()[identifier] = createTestFunction; 290 } 291 292 std::map<std::string, PluginTest::CreateTestFunction>& PluginTest::createTestFunctions() 293 { 294 static std::map<std::string, CreateTestFunction> testFunctions; 295 296 return testFunctions; 297 } 298