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.
6 * Copyright (C) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
30 #include "PluginTest.h"
32 #include "PluginObject.h"
35 #if defined(XP_UNIX) || defined(ANDROID)
40 extern NPNetscapeFuncs
*browser
;
42 static void (*shutdownFunction
)();
44 PluginTest
* PluginTest::create(NPP npp
, const string
& identifier
) {
45 if (identifier
.empty())
46 return new PluginTest(npp
, identifier
);
48 CreateTestFunction createTestFunction
= createTestFunctions()[identifier
];
49 if (createTestFunction
)
50 return createTestFunction(npp
, identifier
);
55 PluginTest::PluginTest(NPP npp
, const string
& identifier
)
56 : m_npp(npp
), m_identifier(identifier
) {
57 // Reset the shutdown function.
61 PluginTest::~PluginTest() {}
63 void PluginTest::NP_Shutdown() {
68 void PluginTest::registerNPShutdownFunction(void (*func
)()) {
69 assert(!shutdownFunction
);
70 shutdownFunction
= func
;
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.
84 NPError
PluginTest::NPP_New(NPMIMEType pluginType
,
90 return NPERR_NO_ERROR
;
93 NPError
PluginTest::NPP_Destroy(NPSavedData
**) { return NPERR_NO_ERROR
; }
95 NPError
PluginTest::NPP_SetWindow(NPWindow
*) { return NPERR_NO_ERROR
; }
97 NPError
PluginTest::NPP_NewStream(NPMIMEType type
,
101 return NPERR_NO_ERROR
;
104 NPError
PluginTest::NPP_DestroyStream(NPStream
* stream
, NPReason reason
) {
105 return NPERR_NO_ERROR
;
108 int32_t PluginTest::NPP_WriteReady(NPStream
*) { return 4096; }
110 int32_t PluginTest::NPP_Write(NPStream
*,
117 int16_t PluginTest::NPP_HandleEvent(void*) { return 0; }
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.
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
;
132 NPError
PluginTest::NPP_SetValue(NPNVariable
, void* value
) {
133 return NPERR_GENERIC_ERROR
;
138 NPError
PluginTest::NPN_GetURL(const char* url
, const char* target
) {
139 return browser
->geturl(m_npp
, url
, target
);
142 NPError
PluginTest::NPN_GetURLNotify(const char* url
,
145 return browser
->geturlnotify(m_npp
, url
, target
, notifyData
);
148 NPError
PluginTest::NPN_GetValue(NPNVariable variable
, void* value
) {
149 return browser
->getvalue(m_npp
, variable
, value
);
152 void PluginTest::NPN_InvalidateRect(NPRect
* invalidRect
) {
153 browser
->invalidaterect(m_npp
, invalidRect
);
156 bool PluginTest::NPN_Invoke(NPObject
* npobj
,
157 NPIdentifier methodName
,
158 const NPVariant
* args
,
161 return browser
->invoke(m_npp
, npobj
, methodName
, args
, argCount
, result
);
164 void* PluginTest::NPN_MemAlloc(uint32_t size
) {
165 return browser
->memalloc(size
);
168 // NPRuntime NPN functions.
170 NPIdentifier
PluginTest::NPN_GetStringIdentifier(const NPUTF8
* name
) {
171 return browser
->getstringidentifier(name
);
174 NPIdentifier
PluginTest::NPN_GetIntIdentifier(int32_t intid
) {
175 return browser
->getintidentifier(intid
);
178 bool PluginTest::NPN_IdentifierIsString(NPIdentifier npIdentifier
) {
179 return browser
->identifierisstring(npIdentifier
);
182 NPUTF8
* PluginTest::NPN_UTF8FromIdentifier(NPIdentifier npIdentifier
) {
183 return browser
->utf8fromidentifier(npIdentifier
);
186 int32_t PluginTest::NPN_IntFromIdentifier(NPIdentifier npIdentifier
) {
187 return browser
->intfromidentifier(npIdentifier
);
190 NPObject
* PluginTest::NPN_CreateObject(NPClass
* npClass
) {
191 return browser
->createobject(m_npp
, npClass
);
194 NPObject
* PluginTest::NPN_RetainObject(NPObject
* npObject
) {
195 return browser
->retainobject(npObject
);
198 void PluginTest::NPN_ReleaseObject(NPObject
* npObject
) {
199 browser
->releaseobject(npObject
);
202 bool PluginTest::NPN_GetProperty(NPObject
* npObject
,
203 NPIdentifier propertyName
,
205 return browser
->getproperty(m_npp
, npObject
, propertyName
, value
);
208 bool PluginTest::NPN_RemoveProperty(NPObject
* npObject
,
209 NPIdentifier propertyName
) {
210 return browser
->removeproperty(m_npp
, npObject
, propertyName
);
213 void PluginTest::NPN_ReleaseVariantValue(NPVariant
* variant
) {
214 browser
->releasevariantvalue(variant
);
218 bool PluginTest::NPN_ConvertPoint(double sourceX
,
220 NPCoordinateSpace sourceSpace
,
223 NPCoordinateSpace destSpace
) {
224 return browser
->convertpoint(
225 m_npp
, sourceX
, sourceY
, sourceSpace
, destX
, destY
, destSpace
);
229 bool PluginTest::executeScript(const NPString
* script
, NPVariant
* result
) {
230 NPObject
* windowScriptObject
;
231 browser
->getvalue(m_npp
, NPNVWindowNPObject
, &windowScriptObject
);
233 return browser
->evaluate(
234 m_npp
, windowScriptObject
, const_cast<NPString
*>(script
), result
);
237 void PluginTest::executeScript(const char* script
) {
239 npScript
.UTF8Characters
= script
;
240 npScript
.UTF8Length
= strlen(script
);
242 NPVariant browserResult
;
243 executeScript(&npScript
, &browserResult
);
244 browser
->releasevariantvalue(&browserResult
);
247 void PluginTest::log(const char* format
, ...) {
249 va_start(args
, format
);
250 pluginLogWithArguments(m_npp
, format
, args
);
254 NPNetscapeFuncs
* PluginTest::netscapeFuncs() { return browser
; }
256 void PluginTest::waitUntilDone() {
257 executeScript("testRunner.waitUntilDone()");
260 void PluginTest::notifyDone() { executeScript("testRunner.notifyDone()"); }
262 void PluginTest::registerCreateTestFunction(
263 const string
& identifier
,
264 CreateTestFunction createTestFunction
) {
265 assert(!createTestFunctions().count(identifier
));
267 createTestFunctions()[identifier
] = createTestFunction
;
270 std::map
<std::string
, PluginTest::CreateTestFunction
>&
271 PluginTest::createTestFunctions() {
272 static std::map
<std::string
, CreateTestFunction
> testFunctions
;
274 return testFunctions
;