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