PluginTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "PluginTest.h"
  26. #include "PluginObject.h"
  27. #include <assert.h>
  28. #include <string.h>
  29. #if defined(XP_UNIX) || defined(ANDROID)
  30. #include <unistd.h>
  31. #endif
  32. using namespace std;
  33. extern NPNetscapeFuncs *browser;
  34. static void (*shutdownFunction)();
  35. PluginTest* PluginTest::create(NPP npp, const string& identifier)
  36. {
  37. if (identifier.empty())
  38. return new PluginTest(npp, identifier);
  39. CreateTestFunction createTestFunction = createTestFunctions()[identifier];
  40. if (createTestFunction)
  41. return createTestFunction(npp, identifier);
  42. return 0;
  43. }
  44. PluginTest::PluginTest(NPP npp, const string& identifier)
  45. : m_npp(npp)
  46. , m_identifier(identifier)
  47. {
  48. // Reset the shutdown function.
  49. shutdownFunction = 0;
  50. }
  51. PluginTest::~PluginTest()
  52. {
  53. }
  54. void PluginTest::NP_Shutdown()
  55. {
  56. if (shutdownFunction)
  57. shutdownFunction();
  58. }
  59. void PluginTest::registerNPShutdownFunction(void (*func)())
  60. {
  61. assert(!shutdownFunction);
  62. shutdownFunction = func;
  63. }
  64. void PluginTest::indicateTestFailure()
  65. {
  66. // This should really be an assert, but there's no way for the test framework
  67. // to know that the plug-in process crashed, so we'll just sleep for a while
  68. // to ensure that the test times out.
  69. #if defined(XP_WIN)
  70. ::Sleep(100000);
  71. #else
  72. sleep(1000);
  73. #endif
  74. }
  75. NPError PluginTest::NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
  76. {
  77. return NPERR_NO_ERROR;
  78. }
  79. NPError PluginTest::NPP_Destroy(NPSavedData**)
  80. {
  81. return NPERR_NO_ERROR;
  82. }
  83. NPError PluginTest::NPP_SetWindow(NPWindow*)
  84. {
  85. return NPERR_NO_ERROR;
  86. }
  87. NPError PluginTest::NPP_NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
  88. {
  89. return NPERR_NO_ERROR;
  90. }
  91. NPError PluginTest::NPP_DestroyStream(NPStream *stream, NPReason reason)
  92. {
  93. return NPERR_NO_ERROR;
  94. }
  95. int32_t PluginTest::NPP_WriteReady(NPStream*)
  96. {
  97. return 4096;
  98. }
  99. int32_t PluginTest::NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer)
  100. {
  101. return len;
  102. }
  103. int16_t PluginTest::NPP_HandleEvent(void*)
  104. {
  105. return 0;
  106. }
  107. bool PluginTest::NPP_URLNotify(const char* url, NPReason, void* notifyData)
  108. {
  109. // FIXME: Port the code from NPP_URLNotify in main.cpp over to always using
  110. // PluginTest, so we don't have to use a return value to indicate whether the "default" NPP_URLNotify implementation should be invoked.
  111. return false;
  112. }
  113. NPError PluginTest::NPP_GetValue(NPPVariable variable, void *value)
  114. {
  115. // We don't know anything about plug-in values so just return NPERR_GENERIC_ERROR.
  116. return NPERR_GENERIC_ERROR;
  117. }
  118. NPError PluginTest::NPP_SetValue(NPNVariable, void *value)
  119. {
  120. return NPERR_GENERIC_ERROR;
  121. }
  122. // NPN functions.
  123. NPError PluginTest::NPN_GetURL(const char* url, const char* target)
  124. {
  125. return browser->geturl(m_npp, url, target);
  126. }
  127. NPError PluginTest::NPN_GetURLNotify(const char *url, const char *target, void *notifyData)
  128. {
  129. return browser->geturlnotify(m_npp, url, target, notifyData);
  130. }
  131. NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value)
  132. {
  133. return browser->getvalue(m_npp, variable, value);
  134. }
  135. void PluginTest::NPN_InvalidateRect(NPRect* invalidRect)
  136. {
  137. browser->invalidaterect(m_npp, invalidRect);
  138. }
  139. bool PluginTest::NPN_Invoke(NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
  140. {
  141. return browser->invoke(m_npp, npobj, methodName, args, argCount, result);
  142. }
  143. void* PluginTest::NPN_MemAlloc(uint32_t size)
  144. {
  145. return browser->memalloc(size);
  146. }
  147. // NPRuntime NPN functions.
  148. NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8 *name)
  149. {
  150. return browser->getstringidentifier(name);
  151. }
  152. NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid)
  153. {
  154. return browser->getintidentifier(intid);
  155. }
  156. bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier)
  157. {
  158. return browser->identifierisstring(npIdentifier);
  159. }
  160. NPUTF8* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier)
  161. {
  162. return browser->utf8fromidentifier(npIdentifier);
  163. }
  164. int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier)
  165. {
  166. return browser->intfromidentifier(npIdentifier);
  167. }
  168. NPObject* PluginTest::NPN_CreateObject(NPClass* npClass)
  169. {
  170. return browser->createobject(m_npp, npClass);
  171. }
  172. NPObject* PluginTest::NPN_RetainObject(NPObject* npObject)
  173. {
  174. return browser->retainobject(npObject);
  175. }
  176. void PluginTest::NPN_ReleaseObject(NPObject* npObject)
  177. {
  178. browser->releaseobject(npObject);
  179. }
  180. bool PluginTest::NPN_RemoveProperty(NPObject* npObject, NPIdentifier propertyName)
  181. {
  182. return browser->removeproperty(m_npp, npObject, propertyName);
  183. }
  184. void PluginTest::NPN_ReleaseVariantValue(NPVariant* variant)
  185. {
  186. browser->releasevariantvalue(variant);
  187. }
  188. #ifdef XP_MACOSX
  189. bool PluginTest::NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
  190. {
  191. return browser->convertpoint(m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace);
  192. }
  193. #endif
  194. bool PluginTest::executeScript(const NPString* script, NPVariant* result)
  195. {
  196. NPObject* windowScriptObject;
  197. browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject);
  198. return browser->evaluate(m_npp, windowScriptObject, const_cast<NPString*>(script), result);
  199. }
  200. void PluginTest::executeScript(const char* script)
  201. {
  202. NPString npScript;
  203. npScript.UTF8Characters = script;
  204. npScript.UTF8Length = strlen(script);
  205. NPVariant browserResult;
  206. executeScript(&npScript, &browserResult);
  207. browser->releasevariantvalue(&browserResult);
  208. }
  209. void PluginTest::log(const char* format, ...)
  210. {
  211. va_list args;
  212. va_start(args, format);
  213. pluginLogWithArguments(m_npp, format, args);
  214. va_end(args);
  215. }
  216. NPNetscapeFuncs* PluginTest::netscapeFuncs()
  217. {
  218. return browser;
  219. }
  220. void PluginTest::waitUntilDone()
  221. {
  222. executeScript("testRunner.waitUntilDone()");
  223. }
  224. void PluginTest::notifyDone()
  225. {
  226. executeScript("testRunner.notifyDone()");
  227. }
  228. void PluginTest::registerCreateTestFunction(const string& identifier, CreateTestFunction createTestFunction)
  229. {
  230. assert(!createTestFunctions().count(identifier));
  231. createTestFunctions()[identifier] = createTestFunction;
  232. }
  233. std::map<std::string, PluginTest::CreateTestFunction>& PluginTest::createTestFunctions()
  234. {
  235. static std::map<std::string, CreateTestFunction> testFunctions;
  236. return testFunctions;
  237. }