Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / shell / tools / plugin / PluginTest.cpp
blobd0a56c711e7a673711f3313be4a09a67ac6ce7b8
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.
5 /*
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
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.
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"
33 #include <string.h>
35 #if defined(XP_UNIX) || defined(ANDROID)
36 #include <unistd.h>
37 #endif
39 using namespace std;
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);
52 return 0;
55 PluginTest::PluginTest(NPP npp, const string& identifier)
56 : m_npp(npp), m_identifier(identifier) {
57 // Reset the shutdown function.
58 shutdownFunction = 0;
61 PluginTest::~PluginTest() {}
63 void PluginTest::NP_Shutdown() {
64 if (shutdownFunction)
65 shutdownFunction();
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.
77 #if defined(XP_WIN)
78 ::Sleep(100000);
79 #else
80 sleep(1000);
81 #endif
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;
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,
98 NPStream* stream,
99 NPBool seekable,
100 uint16_t* stype) {
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*,
111 int32_t offset,
112 int32_t len,
113 void* buffer) {
114 return len;
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.
123 return false;
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;
136 // NPN functions.
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,
143 const char* target,
144 void* notifyData) {
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,
159 uint32_t argCount,
160 NPVariant* result) {
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,
204 NPVariant* value) {
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);
217 #ifdef XP_MACOSX
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);
227 #endif
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) {
238 NPString npScript;
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, ...) {
248 va_list args;
249 va_start(args, format);
250 pluginLogWithArguments(m_npp, format, args);
251 va_end(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;