2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "PluginTest.h"
28 #include "PluginObject.h"
31 #if defined(XP_UNIX) || defined(ANDROID)
36 extern NPNetscapeFuncs *browser;
38 static void (*shutdownFunction)();
40 PluginTest* PluginTest::create(NPP npp, const string& identifier)
42 if (identifier.empty())
43 return new PluginTest(npp, identifier);
45 CreateTestFunction createTestFunction = createTestFunctions()[identifier];
46 if (createTestFunction)
47 return createTestFunction(npp, identifier);
52 PluginTest::PluginTest(NPP npp, const string& identifier)
54 , m_identifier(identifier)
56 // Reset the shutdown function.
60 PluginTest::~PluginTest()
64 void PluginTest::NP_Shutdown()
70 void PluginTest::registerNPShutdownFunction(void (*func)())
72 assert(!shutdownFunction);
73 shutdownFunction = func;
76 void PluginTest::indicateTestFailure()
78 // This should really be an assert, but there's no way for the test framework
79 // to know that the plug-in process crashed, so we'll just sleep for a while
80 // to ensure that the test times out.
88 NPError PluginTest::NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
90 return NPERR_NO_ERROR;
93 NPError PluginTest::NPP_Destroy(NPSavedData**)
95 return NPERR_NO_ERROR;
98 NPError PluginTest::NPP_SetWindow(NPWindow*)
100 return NPERR_NO_ERROR;
103 NPError PluginTest::NPP_NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
105 return NPERR_NO_ERROR;
108 NPError PluginTest::NPP_DestroyStream(NPStream *stream, NPReason reason)
110 return NPERR_NO_ERROR;
113 int32_t PluginTest::NPP_WriteReady(NPStream*)
118 int32_t PluginTest::NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer)
123 int16_t PluginTest::NPP_HandleEvent(void*)
128 bool PluginTest::NPP_URLNotify(const char* url, NPReason, void* notifyData)
130 // FIXME: Port the code from NPP_URLNotify in main.cpp over to always using
131 // PluginTest, so we don't have to use a return value to indicate whether the "default" NPP_URLNotify implementation should be invoked.
135 NPError PluginTest::NPP_GetValue(NPPVariable variable, void *value)
137 // We don't know anything about plug-in values so just return NPERR_GENERIC_ERROR.
138 return NPERR_GENERIC_ERROR;
141 NPError PluginTest::NPP_SetValue(NPNVariable, void *value)
143 return NPERR_GENERIC_ERROR;
148 NPError PluginTest::NPN_GetURL(const char* url, const char* target)
150 return browser->geturl(m_npp, url, target);
153 NPError PluginTest::NPN_GetURLNotify(const char *url, const char *target, void *notifyData)
155 return browser->geturlnotify(m_npp, url, target, notifyData);
158 NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value)
160 return browser->getvalue(m_npp, variable, value);
163 void PluginTest::NPN_InvalidateRect(NPRect* invalidRect)
165 browser->invalidaterect(m_npp, invalidRect);
168 bool PluginTest::NPN_Invoke(NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
170 return browser->invoke(m_npp, npobj, methodName, args, argCount, result);
173 void* PluginTest::NPN_MemAlloc(uint32_t size)
175 return browser->memalloc(size);
178 // NPRuntime NPN functions.
180 NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8 *name)
182 return browser->getstringidentifier(name);
185 NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid)
187 return browser->getintidentifier(intid);
190 bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier)
192 return browser->identifierisstring(npIdentifier);
195 NPUTF8* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier)
197 return browser->utf8fromidentifier(npIdentifier);
200 int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier)
202 return browser->intfromidentifier(npIdentifier);
205 NPObject* PluginTest::NPN_CreateObject(NPClass* npClass)
207 return browser->createobject(m_npp, npClass);
210 NPObject* PluginTest::NPN_RetainObject(NPObject* npObject)
212 return browser->retainobject(npObject);
215 void PluginTest::NPN_ReleaseObject(NPObject* npObject)
217 browser->releaseobject(npObject);
220 bool PluginTest::NPN_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* value)
222 return browser->getproperty(m_npp, npObject, propertyName, value);
225 bool PluginTest::NPN_RemoveProperty(NPObject* npObject, NPIdentifier propertyName)
227 return browser->removeproperty(m_npp, npObject, propertyName);
230 void PluginTest::NPN_ReleaseVariantValue(NPVariant* variant)
232 browser->releasevariantvalue(variant);
236 bool PluginTest::NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
238 return browser->convertpoint(m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace);
242 bool PluginTest::executeScript(const NPString* script, NPVariant* result)
244 NPObject* windowScriptObject;
245 browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject);
247 return browser->evaluate(m_npp, windowScriptObject, const_cast<NPString*>(script), result);
250 void PluginTest::executeScript(const char* script)
253 npScript.UTF8Characters = script;
254 npScript.UTF8Length = strlen(script);
256 NPVariant browserResult;
257 executeScript(&npScript, &browserResult);
258 browser->releasevariantvalue(&browserResult);
261 void PluginTest::log(const char* format, ...)
264 va_start(args, format);
265 pluginLogWithArguments(m_npp, format, args);
269 NPNetscapeFuncs* PluginTest::netscapeFuncs()
274 void PluginTest::waitUntilDone()
276 executeScript("testRunner.waitUntilDone()");
279 void PluginTest::notifyDone()
281 executeScript("testRunner.notifyDone()");
284 void PluginTest::registerCreateTestFunction(const string& identifier, CreateTestFunction createTestFunction)
286 assert(!createTestFunctions().count(identifier));
288 createTestFunctions()[identifier] = createTestFunction;
291 std::map<std::string, PluginTest::CreateTestFunction>& PluginTest::createTestFunctions()
293 static std::map<std::string, CreateTestFunction> testFunctions;
295 return testFunctions;