OLD | NEW |
| (Empty) |
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 | |
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 | |
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 | |
61 PluginTest::~PluginTest() {} | |
62 | |
63 void PluginTest::NP_Shutdown() { | |
64 if (shutdownFunction) | |
65 shutdownFunction(); | |
66 } | |
67 | |
68 void PluginTest::registerNPShutdownFunction(void (*func)()) { | |
69 assert(!shutdownFunction); | |
70 shutdownFunction = func; | |
71 } | |
72 | |
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 plugin 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 | |
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 | |
93 NPError PluginTest::NPP_Destroy(NPSavedData**) { return NPERR_NO_ERROR; } | |
94 | |
95 NPError PluginTest::NPP_SetWindow(NPWindow*) { return NPERR_NO_ERROR; } | |
96 | |
97 NPError PluginTest::NPP_NewStream(NPMIMEType type, | |
98 NPStream* stream, | |
99 NPBool seekable, | |
100 uint16_t* stype) { | |
101 return NPERR_NO_ERROR; | |
102 } | |
103 | |
104 NPError PluginTest::NPP_DestroyStream(NPStream* stream, NPReason reason) { | |
105 return NPERR_NO_ERROR; | |
106 } | |
107 | |
108 int32_t PluginTest::NPP_WriteReady(NPStream*) { return 4096; } | |
109 | |
110 int32_t PluginTest::NPP_Write(NPStream*, | |
111 int32_t offset, | |
112 int32_t len, | |
113 void* buffer) { | |
114 return len; | |
115 } | |
116 | |
117 int16_t PluginTest::NPP_HandleEvent(void*) { return 0; } | |
118 | |
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 | |
126 NPError PluginTest::NPP_GetValue(NPPVariable variable, void* value) { | |
127 // We don't know anything about plugin values so just return | |
128 // NPERR_GENERIC_ERROR. | |
129 return NPERR_GENERIC_ERROR; | |
130 } | |
131 | |
132 NPError PluginTest::NPP_SetValue(NPNVariable, void* value) { | |
133 return NPERR_GENERIC_ERROR; | |
134 } | |
135 | |
136 // NPN functions. | |
137 | |
138 NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value) { | |
139 return browser->getvalue(m_npp, variable, value); | |
140 } | |
141 | |
142 void PluginTest::NPN_InvalidateRect(NPRect* invalidRect) { | |
143 browser->invalidaterect(m_npp, invalidRect); | |
144 } | |
145 | |
146 bool PluginTest::NPN_Invoke(NPObject* npobj, | |
147 NPIdentifier methodName, | |
148 const NPVariant* args, | |
149 uint32_t argCount, | |
150 NPVariant* result) { | |
151 return browser->invoke(m_npp, npobj, methodName, args, argCount, result); | |
152 } | |
153 | |
154 void* PluginTest::NPN_MemAlloc(uint32_t size) { | |
155 return browser->memalloc(size); | |
156 } | |
157 | |
158 // NPRuntime NPN functions. | |
159 | |
160 NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8* name) { | |
161 return browser->getstringidentifier(name); | |
162 } | |
163 | |
164 NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid) { | |
165 return browser->getintidentifier(intid); | |
166 } | |
167 | |
168 bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier) { | |
169 return browser->identifierisstring(npIdentifier); | |
170 } | |
171 | |
172 NPUTF8* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier) { | |
173 return browser->utf8fromidentifier(npIdentifier); | |
174 } | |
175 | |
176 int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier) { | |
177 return browser->intfromidentifier(npIdentifier); | |
178 } | |
179 | |
180 NPObject* PluginTest::NPN_CreateObject(NPClass* npClass) { | |
181 return browser->createobject(m_npp, npClass); | |
182 } | |
183 | |
184 NPObject* PluginTest::NPN_RetainObject(NPObject* npObject) { | |
185 return browser->retainobject(npObject); | |
186 } | |
187 | |
188 void PluginTest::NPN_ReleaseObject(NPObject* npObject) { | |
189 browser->releaseobject(npObject); | |
190 } | |
191 | |
192 bool PluginTest::NPN_GetProperty(NPObject* npObject, | |
193 NPIdentifier propertyName, | |
194 NPVariant* value) { | |
195 return browser->getproperty(m_npp, npObject, propertyName, value); | |
196 } | |
197 | |
198 bool PluginTest::NPN_RemoveProperty(NPObject* npObject, | |
199 NPIdentifier propertyName) { | |
200 return browser->removeproperty(m_npp, npObject, propertyName); | |
201 } | |
202 | |
203 void PluginTest::NPN_ReleaseVariantValue(NPVariant* variant) { | |
204 browser->releasevariantvalue(variant); | |
205 } | |
206 | |
207 #ifdef XP_MACOSX | |
208 bool PluginTest::NPN_ConvertPoint(double sourceX, | |
209 double sourceY, | |
210 NPCoordinateSpace sourceSpace, | |
211 double* destX, | |
212 double* destY, | |
213 NPCoordinateSpace destSpace) { | |
214 return browser->convertpoint( | |
215 m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace); | |
216 } | |
217 #endif | |
218 | |
219 bool PluginTest::executeScript(const NPString* script, NPVariant* result) { | |
220 NPObject* windowScriptObject; | |
221 browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject); | |
222 | |
223 return browser->evaluate( | |
224 m_npp, windowScriptObject, const_cast<NPString*>(script), result); | |
225 } | |
226 | |
227 void PluginTest::executeScript(const char* script) { | |
228 NPString npScript; | |
229 npScript.UTF8Characters = script; | |
230 npScript.UTF8Length = strlen(script); | |
231 | |
232 NPVariant browserResult; | |
233 executeScript(&npScript, &browserResult); | |
234 browser->releasevariantvalue(&browserResult); | |
235 } | |
236 | |
237 void PluginTest::log(const char* format, ...) { | |
238 va_list args; | |
239 va_start(args, format); | |
240 pluginLogWithArguments(m_npp, format, args); | |
241 va_end(args); | |
242 } | |
243 | |
244 NPNetscapeFuncs* PluginTest::netscapeFuncs() { return browser; } | |
245 | |
246 void PluginTest::waitUntilDone() { | |
247 executeScript("testRunner.waitUntilDone()"); | |
248 } | |
249 | |
250 void PluginTest::notifyDone() { executeScript("testRunner.notifyDone()"); } | |
251 | |
252 void PluginTest::registerCreateTestFunction( | |
253 const string& identifier, | |
254 CreateTestFunction createTestFunction) { | |
255 assert(!createTestFunctions().count(identifier)); | |
256 | |
257 createTestFunctions()[identifier] = createTestFunction; | |
258 } | |
259 | |
260 std::map<std::string, PluginTest::CreateTestFunction>& | |
261 PluginTest::createTestFunctions() { | |
262 static std::map<std::string, CreateTestFunction> testFunctions; | |
263 | |
264 return testFunctions; | |
265 } | |
OLD | NEW |