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