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
32using namespace std;
33extern NPNetscapeFuncs *browser;
34
35static void (*shutdownFunction)();
36
37PluginTest* 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
49PluginTest::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
57PluginTest::~PluginTest()
58{
59}
60
61void PluginTest::NP_Shutdown()
62{
63    if (shutdownFunction)
64        shutdownFunction();
65}
66
67void PluginTest::registerNPShutdownFunction(void (*func)())
68{
69    assert(!shutdownFunction);
70    shutdownFunction = func;
71}
72
73void 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
85NPError 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
90NPError PluginTest::NPP_Destroy(NPSavedData**)
91{
92    return NPERR_NO_ERROR;
93}
94
95NPError PluginTest::NPP_DestroyStream(NPStream *stream, NPReason reason)
96{
97    return NPERR_NO_ERROR;
98}
99
100NPError PluginTest::NPP_GetValue(NPPVariable variable, void *value)
101{
102    // We don't know anything about plug-in values so just return NPERR_GENERIC_ERROR.
103    return NPERR_GENERIC_ERROR;
104}
105
106NPError PluginTest::NPP_SetWindow(NPP, NPWindow*)
107{
108    return NPERR_NO_ERROR;
109}
110
111int16_t PluginTest::NPP_HandleEvent(void*)
112{
113    return 0;
114}
115
116void PluginTest::NPN_InvalidateRect(NPRect* invalidRect)
117{
118    browser->invalidaterect(m_npp, invalidRect);
119}
120
121NPError PluginTest::NPN_GetURL(const char* url, const char* target)
122{
123    return browser->geturl(m_npp, url, target);
124}
125
126NPIdentifier PluginTest::NPN_GetStringIdentifier(const NPUTF8 *name)
127{
128    return browser->getstringidentifier(name);
129}
130
131NPIdentifier PluginTest::NPN_GetIntIdentifier(int32_t intid)
132{
133    return browser->getintidentifier(intid);
134}
135
136NPError PluginTest::NPN_GetValue(NPNVariable variable, void* value)
137{
138    return browser->getvalue(m_npp, variable, value);
139}
140
141NPObject* PluginTest::NPN_CreateObject(NPClass* npClass)
142{
143    return browser->createobject(m_npp, npClass);
144}
145
146bool PluginTest::NPN_RemoveProperty(NPObject* npObject, NPIdentifier propertyName)
147{
148    return browser->removeproperty(m_npp, npObject, propertyName);
149}
150
151#ifdef XP_MACOSX
152bool PluginTest::NPN_ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
153{
154    return browser->convertpoint(m_npp, sourceX, sourceY, sourceSpace, destX, destY, destSpace);
155}
156#endif
157
158void PluginTest::executeScript(const char* script)
159{
160    NPObject* windowScriptObject;
161    browser->getvalue(m_npp, NPNVWindowNPObject, &windowScriptObject);
162
163    NPString npScript;
164    npScript.UTF8Characters = script;
165    npScript.UTF8Length = strlen(script);
166
167    NPVariant browserResult;
168    browser->evaluate(m_npp, windowScriptObject, &npScript, &browserResult);
169    browser->releasevariantvalue(&browserResult);
170}
171
172void PluginTest::log(const char* format, ...)
173{
174    va_list args;
175    va_start(args, format);
176    pluginLogWithArguments(m_npp, format, args);
177    va_end(args);
178}
179
180void PluginTest::waitUntilDone()
181{
182    executeScript("layoutTestController.waitUntilDone()");
183}
184
185void PluginTest::notifyDone()
186{
187    executeScript("layoutTestController.notifyDone()");
188}
189
190void PluginTest::registerCreateTestFunction(const string& identifier, CreateTestFunction createTestFunction)
191{
192    assert(!createTestFunctions().count(identifier));
193
194    createTestFunctions()[identifier] = createTestFunction;
195}
196
197std::map<std::string, PluginTest::CreateTestFunction>& PluginTest::createTestFunctions()
198{
199    static std::map<std::string, CreateTestFunction> testFunctions;
200
201    return testFunctions;
202}
203