Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / testing / plugin / PluginTest.cpp
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 <string.h>
30
31 #if defined(XP_UNIX) || defined(ANDROID)
32 #include <unistd.h>
33 #endif
34
35 using namespace std;
36 extern NPNetscapeFuncs *browser;
37
38 static void (*shutdownFunction)();
39
40 PluginTest* PluginTest::create(NPP npp, const string& identifier)
41 {
42     if (identifier.empty())
43         return new PluginTest(npp, identifier);
44
45     CreateTestFunction createTestFunction = createTestFunctions()[identifier];
46     if (createTestFunction)
47         return createTestFunction(npp, identifier);
48
49     return 0;
50 }
51
52 PluginTest::PluginTest(NPP npp, const string& identifier)
53     : m_npp(npp)
54     , m_identifier(identifier)
55 {
56     // Reset the shutdown function.
57     shutdownFunction = 0;
58 }
59
60 PluginTest::~PluginTest()
61 {
62 }
63
64 void PluginTest::NP_Shutdown()
65 {
66     if (shutdownFunction)
67         shutdownFunction();
68 }
69
70 void PluginTest::registerNPShutdownFunction(void (*func)())
71 {
72     assert(!shutdownFunction);
73     shutdownFunction = func;
74 }
75
76 void PluginTest::indicateTestFailure()
77 {
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.
81 #if defined(XP_WIN)
82     ::Sleep(100000);
83 #else
84     sleep(1000);
85 #endif
86 }
87
88 NPError PluginTest::NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved)
89 {
90     return NPERR_NO_ERROR;
91 }
92
93 NPError PluginTest::NPP_Destroy(NPSavedData**)
94 {
95     return NPERR_NO_ERROR;
96 }
97
98 NPError PluginTest::NPP_SetWindow(NPWindow*)
99 {
100     return NPERR_NO_ERROR;
101 }
102
103 NPError PluginTest::NPP_NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
104 {
105     return NPERR_NO_ERROR;
106 }
107
108 NPError PluginTest::NPP_DestroyStream(NPStream *stream, NPReason reason)
109 {
110     return NPERR_NO_ERROR;
111 }
112
113 int32_t PluginTest::NPP_WriteReady(NPStream*)
114 {
115     return 4096;
116 }
117
118 int32_t PluginTest::NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer)
119 {
120     return len;
121 }
122
123 int16_t PluginTest::NPP_HandleEvent(void*)
124 {
125     return 0;
126 }
127
128 bool PluginTest::NPP_URLNotify(const char* url, NPReason, void* notifyData)
129 {
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.
132     return false;
133 }
134
135 NPError PluginTest::NPP_GetValue(NPPVariable variable, void *value)
136 {
137     // We don't know anything about plug-in values so just return NPERR_GENERIC_ERROR.
138     return NPERR_GENERIC_ERROR;
139 }
140
141 NPError PluginTest::NPP_SetValue(NPNVariable, void *value)
142 {
143     return NPERR_GENERIC_ERROR;
144 }
145
146 // NPN functions.
147
148 NPError PluginTest::NPN_GetURL(const char* url, const char* target)
149 {
150     return browser->geturl(m_npp, url, target);
151 }
152
153 NPError PluginTest::NPN_GetURLNotify(const char *url, const char *target, void *notifyData)
154 {
155     return browser->geturlnotify(m_npp, url, target, notifyData);
156 }
157
158 NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value)
159 {
160     return browser->getvalue(m_npp, variable, value);
161 }
162
163 void PluginTest::NPN_InvalidateRect(NPRect* invalidRect)
164 {
165     browser->invalidaterect(m_npp, invalidRect);
166 }
167
168 bool PluginTest::NPN_Invoke(NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
169 {
170     return browser->invoke(m_npp, npobj, methodName, args, argCount, result);
171 }
172
173 void* PluginTest::NPN_MemAlloc(uint32_t size)
174 {
175     return browser->memalloc(size);
176 }
177
178 // NPRuntime NPN functions.
179
180 NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8 *name)
181 {
182     return browser->getstringidentifier(name);
183 }
184
185 NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid)
186 {
187     return browser->getintidentifier(intid);
188 }
189
190 bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier)
191 {
192     return browser->identifierisstring(npIdentifier);
193 }
194
195 NPUTF8* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier)
196 {
197     return browser->utf8fromidentifier(npIdentifier);
198 }
199
200 int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier)
201 {
202     return browser->intfromidentifier(npIdentifier);
203 }
204
205 NPObject* PluginTest::NPN_CreateObject(NPClass* npClass)
206 {
207     return browser->createobject(m_npp, npClass);
208 }
209
210 NPObject* PluginTest::NPN_RetainObject(NPObject* npObject)
211 {
212     return browser->retainobject(npObject);
213 }
214
215 void PluginTest::NPN_ReleaseObject(NPObject* npObject)
216 {
217     browser->releaseobject(npObject);
218 }
219
220 bool PluginTest::NPN_GetProperty(NPObject* npObject, NPIdentifier propertyName, NPVariant* value)
221 {
222     return browser->getproperty(m_npp, npObject, propertyName, value);
223 }
224
225 bool PluginTest::NPN_RemoveProperty(NPObject* npObject, NPIdentifier propertyName)
226 {
227     return browser->removeproperty(m_npp, npObject, propertyName);
228 }
229
230 void PluginTest::NPN_ReleaseVariantValue(NPVariant* variant)
231 {
232     browser->releasevariantvalue(variant);
233 }
234
235 #ifdef XP_MACOSX
236 bool PluginTest::NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
237 {
238     return browser->convertpoint(m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace);
239 }
240 #endif
241
242 bool PluginTest::executeScript(const NPString* script, NPVariant* result)
243 {
244     NPObject* windowScriptObject;
245     browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject);
246
247     return browser->evaluate(m_npp, windowScriptObject, const_cast<NPString*>(script), result);
248 }
249
250 void PluginTest::executeScript(const char* script)
251 {
252     NPString npScript;
253     npScript.UTF8Characters = script;
254     npScript.UTF8Length = strlen(script);
255
256     NPVariant browserResult;
257     executeScript(&npScript, &browserResult);
258     browser->releasevariantvalue(&browserResult);
259 }
260
261 void PluginTest::log(const char* format, ...)
262 {
263     va_list args;
264     va_start(args, format);
265     pluginLogWithArguments(m_npp, format, args);
266     va_end(args);
267 }
268
269 NPNetscapeFuncs* PluginTest::netscapeFuncs()
270 {
271     return browser;
272 }
273
274 void PluginTest::waitUntilDone()
275 {
276     executeScript("testRunner.waitUntilDone()");
277 }
278
279 void PluginTest::notifyDone()
280 {
281     executeScript("testRunner.notifyDone()");
282 }
283
284 void PluginTest::registerCreateTestFunction(const string& identifier, CreateTestFunction createTestFunction)
285 {
286     assert(!createTestFunctions().count(identifier));
287
288     createTestFunctions()[identifier] = createTestFunction;
289 }
290
291 std::map<std::string, PluginTest::CreateTestFunction>& PluginTest::createTestFunctions()
292 {
293     static std::map<std::string, CreateTestFunction> testFunctions;
294
295     return testFunctions;
296 }