update copyright date
[gnash.git] / plugin / npapi / test.cpp
blob519fb2028f8242d50cc0e33a58c4acc333c3f9c9
1 //
2 // Copyright (C) 2010, 2011 Free Software Foundation, Inc
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <iostream>
20 #include <string>
21 #include <cstdlib>
22 #include <vector>
23 #include <map>
24 #include <cassert>
26 #include "npapi.h"
27 #include "npruntime.h"
28 #include "pluginbase.h"
29 #include "npfunctions.h"
30 #include "dejagnu.h"
31 #include <regex.h>
33 #include "external.h"
35 TestState runtest;
37 std::map<NPIdentifier, NPVariant *> _properties;
38 std::map<NPIdentifier, NPInvokeFunctionPtr> _methods;
40 int
41 main(int argc, char *argv[])
43 using namespace gnash;
45 NPVariant *value = (NPVariant *)NPN_MemAlloc(sizeof(NPVariant));
47 BOOLEAN_TO_NPVARIANT(true, *value);
48 std::string str = plugin::ExternalInterface::convertNPVariant(value);
49 if (str == "<true/>") {
50 runtest.pass("convertNPVariant(true)");
51 } else {
52 runtest.fail("convertNPVariant(true)");
55 BOOLEAN_TO_NPVARIANT(false, *value);
56 str = plugin::ExternalInterface::convertNPVariant(value);
57 if (str == "<false/>") {
58 runtest.pass("convertNPVariant(false)");
59 } else {
60 runtest.fail("convertNPVariant(false)");
63 NULL_TO_NPVARIANT(*value);
64 str = plugin::ExternalInterface::convertNPVariant(value);
65 if (str == "<null/>") {
66 runtest.pass("convertNPVariant(null)");
67 } else {
68 runtest.fail("convertNPVariant(null)");
71 VOID_TO_NPVARIANT(*value);
72 str = plugin::ExternalInterface::convertNPVariant(value);
73 if (str == "<void/>") {
74 runtest.pass("convertNPVariant(void)");
75 } else {
76 runtest.fail("convertNPVariant(void)");
79 DOUBLE_TO_NPVARIANT(123.456, *value);
80 str = plugin::ExternalInterface::convertNPVariant(value);
81 if (str == "<number>123.456</number>") {
82 runtest.pass("convertNPVariant(double)");
83 } else {
84 runtest.fail("convertNPVariant(double)");
87 INT32_TO_NPVARIANT(78, *value);
88 str = plugin::ExternalInterface::convertNPVariant(value);
89 if (str == "<number>78</number>") {
90 runtest.pass("convertNPVariant(int32)");
91 } else {
92 runtest.fail("convertNPVariant(int32)");
95 STRINGZ_TO_NPVARIANT("Hello World!", *value);
96 str = plugin::ExternalInterface::convertNPVariant(value);
97 if (str == "<string>Hello World!</string>") {
98 runtest.pass("convertNPVariant(string)");
99 } else {
100 runtest.fail("convertNPVariant(string)");
103 str = plugin::ExternalInterface::makeProperty("hi", "Hello World!");
104 if (str == "<property id=\"hi\">Hello World!</property>") {
105 runtest.pass("plugin::ExternalInterface::makeProperty()");
106 } else {
107 runtest.fail("plugin::ExternalInterface::makeProperty()");
110 #if 0
111 ARRAY_TO_NPVARIANT(*value);
112 str = plugin::ExternalInterface::convertNPVariant(value);
113 if (str == "<array></array>") {
114 runtest.pass("convertNPVariant(array)");
115 } else {
116 runtest.fail("convertNPVariant(array)");
118 #endif
120 NPObject *obj = (NPObject *)NPN_MemAlloc(sizeof(NPObject));
121 std::string prop1 = plugin::ExternalInterface::makeString("foobar");
122 std::string prop2 = plugin::ExternalInterface::makeNumber(12.34);
123 std::string prop3 = plugin::ExternalInterface::makeNumber(56);
124 std::vector<std::string> aargs;
125 aargs.push_back(prop1);
126 aargs.push_back(prop2);
127 aargs.push_back(prop3);
129 regex_t regex_pat;
130 regcomp (&regex_pat, "<array><property id=\"0\"><string>foobar</string></property><property id=\"1\"><number>12.34</number></property><property id=\"2\"><number>56</number></property></array>", REG_NOSUB|REG_NEWLINE);
131 str = plugin::ExternalInterface::makeArray(aargs);
132 if (regexec (&regex_pat, reinterpret_cast<const char*>(str.c_str()), 0, (regmatch_t *)0, 0)) {
133 runtest.fail("plugin::ExternalInterface::makeArray()");
134 } else {
135 runtest.pass("plugin::ExternalInterface::makeArray()");
138 std::map<std::string, std::string> margs;
139 margs["test1"] = prop1;
140 margs["test2"] = prop2;
141 margs["test3"] = prop3;
143 str = plugin::ExternalInterface::makeObject(margs);
144 std::string xml = "<object><property id=\"test1\"><string>foobar</string></property><property id=\"test2\"><number>12.34</number></property><property id=\"test3\"><number>56</number></property></object>";
146 regcomp (&regex_pat, xml.c_str(), REG_NOSUB|REG_NEWLINE);
148 // std::cout << str << std::endl;
149 if (regexec (&regex_pat, reinterpret_cast<const char*>(str.c_str()), 0, (regmatch_t *)0, 0)) {
150 runtest.fail("plugin::ExternalInterface::makeObject()");
151 } else {
152 runtest.pass("plugin::ExternalInterface::makeObject()");
156 // Parsing tests
158 xml = "<string>Hello World!</string>";
159 GnashNPVariant np = plugin::ExternalInterface::parseXML(xml);
160 std::string data = NPStringToString(NPVARIANT_TO_STRING(np.get()));
161 if (NPVARIANT_IS_STRING(np.get()) &&
162 (data == "Hello World!")) {
163 runtest.pass("plugin::ExternalInterface::parseXML(string)");
164 } else {
165 runtest.fail("plugin::ExternalInterface::parseXML(string)");
168 xml = "<number>123.456</number>";
169 np = plugin::ExternalInterface::parseXML(xml);
170 double num = NPVARIANT_TO_DOUBLE(np.get());
171 if (NPVARIANT_IS_DOUBLE(np.get()) &&
172 (num == 123.456)) {
173 runtest.pass("plugin::ExternalInterface::parseXML(double)");
174 } else {
175 runtest.fail("plugin::ExternalInterface::parseXML(double)");
178 xml = "<number>78</number>";
179 np = plugin::ExternalInterface::parseXML(xml);
180 int inum = NPVARIANT_TO_INT32(np.get());
181 if (NPVARIANT_IS_INT32(np.get()) &&
182 (inum == 78)) {
183 runtest.pass("plugin::ExternalInterface::parseXML(int32)");
184 } else {
185 runtest.fail("plugin::ExternalInterface::parseXML(int32)");
188 xml = "<true/>";
189 np = plugin::ExternalInterface::parseXML(xml);
190 bool flag = NPVARIANT_TO_BOOLEAN(np.get());
191 if (NPVARIANT_IS_BOOLEAN(np.get()) &&
192 (flag == true)) {
193 runtest.pass("plugin::ExternalInterface::parseXML(true)");
194 } else {
195 runtest.fail("plugin::ExternalInterface::parseXML(true)");
198 xml = "<false/>";
199 np = plugin::ExternalInterface::parseXML(xml);
200 flag = NPVARIANT_TO_BOOLEAN(np.get());
201 if (NPVARIANT_IS_BOOLEAN(np.get()) &&
202 (flag == false)) {
203 runtest.pass("plugin::ExternalInterface::parseXML(false)");
204 } else {
205 runtest.fail("plugin::ExternalInterface::parseXML(false)");
208 xml = "<null/>";
209 np = plugin::ExternalInterface::parseXML(xml);
210 if (NPVARIANT_IS_NULL(np.get())) {
211 runtest.pass("plugin::ExternalInterface::parseXML(null)");
212 } else {
213 runtest.fail("plugin::ExternalInterface::parseXML(null)");
216 xml = "<void/>";
217 np = plugin::ExternalInterface::parseXML(xml);
218 if (NPVARIANT_IS_VOID(np.get())) {
219 runtest.pass("plugin::ExternalInterface::parseXML(void)");
220 } else {
221 runtest.fail("plugin::ExternalInterface::parseXML(void)");
224 xml = "<property id=\"0\"><string>foobar</string></property><property id=\"1\"><number>12.34</number></property><property id=\"2\"><number>56</number></property>";
225 std::map<std::string, GnashNPVariant> props = plugin::ExternalInterface::parseProperties(xml);
226 np = props["0"];
227 data = NPStringToString(NPVARIANT_TO_STRING(np.get()));
228 if ((props.size() == 3) && (data == "foobar")) {
229 runtest.pass("plugin::ExternalInterface::parseProperties()");
230 } else {
231 runtest.fail("plugin::ExternalInterface::parseProperties()");
234 xml = "<object><property id=\"test1\"><string>foobar</string></property><property id=\"test2\"><number>12.34</number></property><property id=\"test3\"><number>56</number></property></object>";
235 np = plugin::ExternalInterface::parseXML(xml);
236 if (NPVARIANT_IS_OBJECT(np.get())) {
237 runtest.pass("plugin::ExternalInterface::parseXML(object)");
238 } else {
239 runtest.fail("plugin::ExternalInterface::parseXML(object)");
242 std::vector<std::string> iargs;
243 str = plugin::ExternalInterface::makeString("barfoo");
244 iargs.push_back(str);
245 str = plugin::ExternalInterface::makeNumber(135.78);
246 iargs.push_back(str);
248 str = plugin::ExternalInterface::makeInvoke("barbyfoo", iargs);
249 xml = "<invoke name=\"barbyfoo\" returntype=\"xml\"><arguments><string>barfoo</string><number>135.78</number></arguments></invoke>";
250 // std::cout << str << std::endl;
251 regcomp (&regex_pat, xml.c_str(), REG_NOSUB|REG_NEWLINE);
252 if (regexec (&regex_pat, reinterpret_cast<const char*>(str.c_str()), 0, (regmatch_t *)0, 0) == 0) {
253 runtest.pass("plugin::ExternalInterface::makeInvoke()");
254 } else {
255 runtest.fail("plugin::ExternalInterface::makeInvoke()");
258 xml = "<arguments><string>barfoo</string><number>135.78</number><number>89</number></arguments>";
259 std::vector<GnashNPVariant> arguments = plugin::ExternalInterface::parseArguments(xml);
260 np = arguments[0];
261 str = NPStringToString(NPVARIANT_TO_STRING(np.get()));
262 double dub = NPVARIANT_TO_DOUBLE(arguments[1].get());
263 int val = NPVARIANT_TO_INT32(arguments[2].get());
264 if ((arguments.size() == 3) && (str == "barfoo")
265 && (dub == 135.78) && (val == 89)) {
266 runtest.pass("plugin::ExternalInterface::parseArguments()");
267 } else {
268 runtest.fail("plugin::ExternalInterface::parseArguments()");
271 // Parse an invoke message
272 xml = "<invoke name=\"barbyfoo\" returntype=\"xml\"><arguments><string>barfoo</string><number>135.78</number></arguments></invoke>";
273 plugin::ExternalInterface::invoke_t *invoke = plugin::ExternalInterface::parseInvoke(xml);
274 str = NPStringToString(NPVARIANT_TO_STRING(invoke->args[0].get()));
275 if ((invoke->name == "barbyfoo") && (invoke->type == "xml")
276 && (NPVARIANT_IS_STRING(invoke->args[0].get()))
277 && (str == "barfoo")
278 && (NPVARIANT_IS_DOUBLE(invoke->args[1].get()))
279 && (NPVARIANT_TO_DOUBLE(invoke->args[1].get()) == 135.78)
281 runtest.pass("plugin::ExternalInterface::parseInvoke()");
282 } else {
283 runtest.fail("plugin::ExternalInterface::parseInvoke()");
287 // We have to implement these two memory allocation functions as
288 // they're used in the code we're testing.
289 void *
290 NPN_MemAlloc(uint32_t size)
292 void * rv = NULL;
293 rv = malloc(size);
294 return rv;
297 void
298 NPN_MemFree(void* ptr)
300 assert(ptr);
301 free(ptr);
304 // These are just stubs to get the test case to link standalone.
305 NPIdentifier
306 NPN_GetStringIdentifier(const NPUTF8 *name)
310 nsPluginInstanceBase *
311 NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
313 return NULL;
316 NPError
317 NS_PluginGetValue(NPPVariable aVariable, void *aValue)
319 return NPERR_INVALID_INSTANCE_ERROR;
322 NPError
323 NS_PluginInitialize()
325 return NPERR_INVALID_INSTANCE_ERROR;
328 void
329 NS_PluginShutdown()
333 char*
334 NPP_GetMIMEDescription(void)
336 char *x = 0;
337 return x;
340 void
341 NS_DestroyPluginInstance(nsPluginInstanceBase *aPlugin)
345 // Implement minimal properties handling
346 bool
347 NPN_SetProperty(NPP npp, NPObject* obj, NPIdentifier name,
348 const NPVariant *value)
350 _properties[name] = const_cast<NPVariant *>(value);
351 return true;
354 bool
355 NPN_GetProperty(NPP npp, NPObject* obj, NPIdentifier name,
356 const NPVariant *value)
358 return _properties[name];
361 bool
362 NPN_HasProperty(NPP npp, NPObject* obj, NPIdentifier name,
363 const NPVariant *value)
365 std::map<NPIdentifier, NPVariant *>::iterator it;
366 it = _properties.find(name);
367 if (it != _properties.end()) {
368 return true;
370 return false;
373 void
374 NPN_ReleaseVariantValue(NPVariant *variant)
376 switch(variant->type) {
377 case NPVariantType_String:
379 NPN_MemFree(const_cast<NPUTF8*>(NPVARIANT_TO_STRING(*variant).UTF8Characters));
380 break;
382 case NPVariantType_Object:
384 NPObject* obj = NPVARIANT_TO_OBJECT(*variant);
385 if (obj) {
386 NPN_ReleaseObject(obj);
388 break;
390 default:
394 NULL_TO_NPVARIANT(*variant);
397 NPObject*
398 NPN_RetainObject(NPObject *obj)
399 { assert(obj); ++obj->referenceCount; return obj; }
402 void
403 NPN_ReleaseObject(NPObject *npobj)
405 assert(npobj);
406 --npobj->referenceCount;
407 if (npobj->referenceCount == 0) {
408 NPN_MemFree(npobj);
411 // Local Variables:
412 // mode: C++
413 // indent-tabs-mode: nil
414 // End: