2 // Copyright (C) 2010 Free Software Foundation, Inc
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.
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
28 #include "npruntime.h"
29 #include "pluginbase.h"
30 #include "npfunctions.h"
32 #include "../../testsuite/check.h"
37 TestState
& runtest
= _runtest
;
39 std::map
<NPIdentifier
, NPVariant
*> _properties
;
40 std::map
<NPIdentifier
, NPInvokeFunctionPtr
> _methods
;
45 using namespace gnash
;
47 NPVariant
*value
= (NPVariant
*)NPN_MemAlloc(sizeof(NPVariant
));
49 BOOLEAN_TO_NPVARIANT(true, *value
);
50 std::string str
= plugin::ExternalInterface::convertNPVariant(value
);
51 if (str
== "<true/>") {
52 runtest
.pass("convertNPVariant(true)");
54 runtest
.fail("convertNPVariant(true)");
57 BOOLEAN_TO_NPVARIANT(false, *value
);
58 str
= plugin::ExternalInterface::convertNPVariant(value
);
59 if (str
== "<false/>") {
60 runtest
.pass("convertNPVariant(false)");
62 runtest
.fail("convertNPVariant(false)");
65 NULL_TO_NPVARIANT(*value
);
66 str
= plugin::ExternalInterface::convertNPVariant(value
);
67 if (str
== "<null/>") {
68 runtest
.pass("convertNPVariant(null)");
70 runtest
.fail("convertNPVariant(null)");
73 VOID_TO_NPVARIANT(*value
);
74 str
= plugin::ExternalInterface::convertNPVariant(value
);
75 if (str
== "<void/>") {
76 runtest
.pass("convertNPVariant(void)");
78 runtest
.fail("convertNPVariant(void)");
81 DOUBLE_TO_NPVARIANT(123.456, *value
);
82 str
= plugin::ExternalInterface::convertNPVariant(value
);
83 if (str
== "<number>123.456</number>") {
84 runtest
.pass("convertNPVariant(double)");
86 runtest
.fail("convertNPVariant(double)");
89 INT32_TO_NPVARIANT(78, *value
);
90 str
= plugin::ExternalInterface::convertNPVariant(value
);
91 if (str
== "<number>78</number>") {
92 runtest
.pass("convertNPVariant(int32)");
94 runtest
.fail("convertNPVariant(int32)");
97 STRINGZ_TO_NPVARIANT("Hello World!", *value
);
98 str
= plugin::ExternalInterface::convertNPVariant(value
);
99 if (str
== "<string>Hello World!</string>") {
100 runtest
.pass("convertNPVariant(string)");
102 runtest
.fail("convertNPVariant(string)");
105 str
= plugin::ExternalInterface::makeProperty("hi", "Hello World!");
106 if (str
== "<property id=\"hi\">Hello World!</property>") {
107 runtest
.pass("plugin::ExternalInterface::makeProperty()");
109 runtest
.fail("plugin::ExternalInterface::makeProperty()");
113 ARRAY_TO_NPVARIANT(*value
);
114 str
= plugin::ExternalInterface::convertNPVariant(value
);
115 if (str
== "<array></array>") {
116 runtest
.pass("convertNPVariant(array)");
118 runtest
.fail("convertNPVariant(array)");
122 std::string prop1
= plugin::ExternalInterface::makeString("foobar");
123 std::string prop2
= plugin::ExternalInterface::makeNumber(12.34);
124 std::string prop3
= plugin::ExternalInterface::makeNumber(56);
125 std::vector
<std::string
> aargs
;
126 aargs
.push_back(prop1
);
127 aargs
.push_back(prop2
);
128 aargs
.push_back(prop3
);
131 regcomp (®ex_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
);
132 str
= plugin::ExternalInterface::makeArray(aargs
);
133 if (regexec (®ex_pat
, reinterpret_cast<const char*>(str
.c_str()), 0, (regmatch_t
*)0, 0)) {
134 runtest
.fail("plugin::ExternalInterface::makeArray()");
136 runtest
.pass("plugin::ExternalInterface::makeArray()");
139 std::map
<std::string
, std::string
> margs
;
140 margs
["test1"] = prop1
;
141 margs
["test2"] = prop2
;
142 margs
["test3"] = prop3
;
144 str
= plugin::ExternalInterface::makeObject(margs
);
145 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>";
147 regfree (®ex_pat
);
148 regcomp (®ex_pat
, xml
.c_str(), REG_NOSUB
|REG_NEWLINE
);
150 // std::cout << str << std::endl;
151 if (regexec (®ex_pat
, reinterpret_cast<const char*>(str
.c_str()), 0, (regmatch_t
*)0, 0)) {
152 runtest
.fail("plugin::ExternalInterface::makeObject()");
154 runtest
.pass("plugin::ExternalInterface::makeObject()");
160 xml
= "<string>Hello World!</string>";
161 GnashNPVariant np
= plugin::ExternalInterface::parseXML(xml
);
162 std::string data
= NPStringToString(NPVARIANT_TO_STRING(np
.get()));
163 if (NPVARIANT_IS_STRING(np
.get()) &&
164 (data
== "Hello World!")) {
165 runtest
.pass("plugin::ExternalInterface::parseXML(string)");
167 runtest
.fail("plugin::ExternalInterface::parseXML(string)");
170 xml
= "<number>123.456</number>";
171 np
= plugin::ExternalInterface::parseXML(xml
);
172 double num
= NPVARIANT_TO_DOUBLE(np
.get());
173 if (NPVARIANT_IS_DOUBLE(np
.get()) &&
175 runtest
.pass("plugin::ExternalInterface::parseXML(double)");
177 runtest
.fail("plugin::ExternalInterface::parseXML(double)");
180 xml
= "<number>78</number>";
181 np
= plugin::ExternalInterface::parseXML(xml
);
182 int inum
= NPVARIANT_TO_INT32(np
.get());
183 if (NPVARIANT_IS_INT32(np
.get()) &&
185 runtest
.pass("plugin::ExternalInterface::parseXML(int32)");
187 runtest
.fail("plugin::ExternalInterface::parseXML(int32)");
191 np
= plugin::ExternalInterface::parseXML(xml
);
192 bool flag
= NPVARIANT_TO_BOOLEAN(np
.get());
193 if (NPVARIANT_IS_BOOLEAN(np
.get()) &&
195 runtest
.pass("plugin::ExternalInterface::parseXML(true)");
197 runtest
.fail("plugin::ExternalInterface::parseXML(true)");
201 np
= plugin::ExternalInterface::parseXML(xml
);
202 flag
= NPVARIANT_TO_BOOLEAN(np
.get());
203 if (NPVARIANT_IS_BOOLEAN(np
.get()) &&
205 runtest
.pass("plugin::ExternalInterface::parseXML(false)");
207 runtest
.fail("plugin::ExternalInterface::parseXML(false)");
211 np
= plugin::ExternalInterface::parseXML(xml
);
212 if (NPVARIANT_IS_NULL(np
.get())) {
213 runtest
.pass("plugin::ExternalInterface::parseXML(null)");
215 runtest
.fail("plugin::ExternalInterface::parseXML(null)");
219 np
= plugin::ExternalInterface::parseXML(xml
);
220 if (NPVARIANT_IS_VOID(np
.get())) {
221 runtest
.pass("plugin::ExternalInterface::parseXML(void)");
223 runtest
.fail("plugin::ExternalInterface::parseXML(void)");
226 xml
= "<property id=\"0\"><string>foobar</string></property><property id=\"1\"><number>12.34</number></property><property id=\"2\"><number>56</number></property>";
227 std::map
<std::string
, GnashNPVariant
> props
= plugin::ExternalInterface::parseProperties(xml
);
229 data
= NPStringToString(NPVARIANT_TO_STRING(np
.get()));
230 if ((props
.size() == 3) && (data
== "foobar")) {
231 runtest
.pass("plugin::ExternalInterface::parseProperties()");
233 runtest
.fail("plugin::ExternalInterface::parseProperties()");
236 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>";
237 np
= plugin::ExternalInterface::parseXML(xml
);
238 if (NPVARIANT_IS_OBJECT(np
.get())) {
239 runtest
.pass("plugin::ExternalInterface::parseXML(object)");
241 runtest
.fail("plugin::ExternalInterface::parseXML(object)");
244 std::vector
<std::string
> iargs
;
245 str
= plugin::ExternalInterface::makeString("barfoo");
246 iargs
.push_back(str
);
247 str
= plugin::ExternalInterface::makeNumber(135.78);
248 iargs
.push_back(str
);
250 str
= plugin::ExternalInterface::makeInvoke("barbyfoo", iargs
);
251 xml
= "<invoke name=\"barbyfoo\" returntype=\"xml\"><arguments><string>barfoo</string><number>135.78</number></arguments></invoke>";
252 // std::cout << str << std::endl;
253 regfree (®ex_pat
);
254 regcomp (®ex_pat
, xml
.c_str(), REG_NOSUB
|REG_NEWLINE
);
255 if (regexec (®ex_pat
, reinterpret_cast<const char*>(str
.c_str()), 0, (regmatch_t
*)0, 0) == 0) {
256 runtest
.pass("plugin::ExternalInterface::makeInvoke()");
258 runtest
.fail("plugin::ExternalInterface::makeInvoke()");
261 xml
= "<arguments><string>barfoo</string><number>135.78</number><number>89</number></arguments>";
262 std::vector
<GnashNPVariant
> arguments
= plugin::ExternalInterface::parseArguments(xml
);
264 str
= NPStringToString(NPVARIANT_TO_STRING(np
.get()));
265 double dub
= NPVARIANT_TO_DOUBLE(arguments
[1].get());
266 int val
= NPVARIANT_TO_INT32(arguments
[2].get());
267 if ((arguments
.size() == 3) && (str
== "barfoo")
268 && (dub
== 135.78) && (val
== 89)) {
269 runtest
.pass("plugin::ExternalInterface::parseArguments()");
271 runtest
.fail("plugin::ExternalInterface::parseArguments()");
274 // Parse an invoke message
275 xml
= "<invoke name=\"barbyfoo\" returntype=\"xml\"><arguments><string>barfoo</string><number>135.78</number></arguments></invoke>";
276 std::auto_ptr
<plugin::ExternalInterface::invoke_t
> invoke ( plugin::ExternalInterface::parseInvoke(xml
) );
277 str
= NPStringToString(NPVARIANT_TO_STRING(invoke
->args
[0].get()));
278 if ((invoke
->name
== "barbyfoo") && (invoke
->type
== "xml")
279 && (NPVARIANT_IS_STRING(invoke
->args
[0].get()))
281 && (NPVARIANT_IS_DOUBLE(invoke
->args
[1].get()))
282 && (NPVARIANT_TO_DOUBLE(invoke
->args
[1].get()) == 135.78)
284 runtest
.pass("plugin::ExternalInterface::parseInvoke()");
286 runtest
.fail("plugin::ExternalInterface::parseInvoke()");
289 // Test for bug #31766
290 xml
= "<invoke name=\"reportFlashTiming\" returntype=\"xml\"><arguments><string>reportFlashTiming</string><object><property id=\"5\"><number>1297286708921</number></property><property id=\"4\"><string>vr</string></p";
291 invoke
.reset ( plugin::ExternalInterface::parseInvoke(xml
) );
292 if ((invoke
->name
== "reportFlashTiming") && (invoke
->type
== "xml")
293 && invoke
->args
.empty())
295 runtest
.pass("plugin::ExternalInterface::parseInvoke() with missing closing invoke tag");
297 runtest
.fail("plugin::ExternalInterface::parseInvoke() with missing closing invoke tag");
301 xml
= "<invoke name=\"reportFlashTiming\" returntype=\"xml\"><arguments><string>reportFlashTiming</string><object><property id=\"5\"><number>1297326407594</number></property><property id=\"4\"><string>vr</string></property><property id=\"3\"><number>1297326407147</number></property><property id=\"2\"><string>gv</string></property><property id=\"1\"><number>1297326406281</number></property><property id=\"0\"><string>fs</string></property></object><string>34</string><number>2</number><string>AASb6VeOkQtvnu_8</string><string>0</string><string>LNX%2010%2C1%2C999%2C0</string><string>Gnash%20GNU%2FLinux</string></arguments></invoke>";
302 invoke
.reset ( plugin::ExternalInterface::parseInvoke(xml
) );
303 check_equals (invoke
->name
, "reportFlashTiming");
304 check_equals (invoke
->type
, "xml");
305 xcheck_equals (invoke
->args
.size(), 8);
307 check(NPVARIANT_IS_STRING(invoke
->args
[0].get()));
308 str
= NPStringToString(NPVARIANT_TO_STRING(invoke
->args
[0].get()));
309 check_equals(str
, "reportFlashTiming");
311 xcheck(NPVARIANT_IS_OBJECT(invoke
->args
[1].get()));
312 // TODO: check object contents
314 xcheck(NPVARIANT_IS_STRING(invoke
->args
[2].get()));
315 // str = NPStringToString(NPVARIANT_TO_STRING(invoke->args[2].get()));
316 // check_equals(str, "34");
318 xcheck(NPVARIANT_IS_DOUBLE(invoke
->args
[3].get()));
319 // check_equals(NPVARIANT_TO_DOUBLE(invoke->args[3].get()), 2);
321 check(NPVARIANT_IS_STRING(invoke
->args
[4].get()));
322 str
= NPStringToString(NPVARIANT_TO_STRING(invoke
->args
[4].get()));
323 xcheck_equals(str
, "AASb6VeOkQtvnu_8");
325 xcheck(NPVARIANT_IS_STRING(invoke
->args
[5].get()));
326 // str = NPStringToString(NPVARIANT_TO_STRING(invoke->args[5].get()));
327 // check_equals(str, "0");
329 xcheck(NPVARIANT_IS_STRING(invoke
->args
[6].get()));
330 // str = NPStringToString(NPVARIANT_TO_STRING(invoke->args[6].get()));
331 // check_equals(str, "LNX%2010%2C1%2C999%2C0");
333 xcheck(NPVARIANT_IS_STRING(invoke
->args
[7].get()));
334 // str = NPStringToString(NPVARIANT_TO_STRING(invoke->args[7].get()));
335 // check_equals(str, "Gnash%20GNU%2FLinux");
339 xml
= "<object><property id=\"5\">";
340 GnashNPVariant v
= plugin::ExternalInterface::parseXML(xml
);
341 check(NPVARIANT_IS_NULL(v
.get()));
346 NULL_TO_NPVARIANT(val
);
347 check(NPVARIANT_IS_NULL(val
));
348 GnashNPVariant v
= val
;
349 check(NPVARIANT_IS_NULL(v
.get()));
352 regfree (®ex_pat
);
356 // We have to implement these two memory allocation functions as
357 // they're used in the code we're testing.
359 NPN_MemAlloc(uint32_t size
)
367 NPN_MemFree(void* ptr
)
373 // These are just stubs to get the test case to link standalone.
375 NPN_GetStringIdentifier(const NPUTF8
*)
380 nsPluginInstanceBase
*
381 NS_NewPluginInstance(nsPluginCreateData
*)
387 NS_PluginGetValue(NPPVariable
, void *)
389 return NPERR_INVALID_INSTANCE_ERROR
;
393 NS_PluginInitialize()
395 return NPERR_INVALID_INSTANCE_ERROR
;
404 NPP_GetMIMEDescription(void)
411 NS_DestroyPluginInstance(nsPluginInstanceBase
*)
415 // Implement minimal properties handling
417 NPN_SetProperty(NPP
, NPObject
*, NPIdentifier name
,
418 const NPVariant
*value
)
420 _properties
[name
] = const_cast<NPVariant
*>(value
);
425 NPN_GetProperty(NPP
, NPObject
* , NPIdentifier name
,
426 const NPVariant
*value
)
428 std::map
<NPIdentifier
, NPVariant
*>::iterator it
;
429 it
= _properties
.find(name
);
430 if (it
== _properties
.end()) return false;
436 NPN_HasProperty(NPP
, NPObject
* , NPIdentifier name
)
438 std::map
<NPIdentifier
, NPVariant
*>::iterator it
;
439 it
= _properties
.find(name
);
440 if (it
!= _properties
.end()) {
447 NPN_ReleaseVariantValue(NPVariant
*variant
)
449 switch(variant
->type
) {
450 case NPVariantType_String
:
452 NPN_MemFree(const_cast<NPUTF8
*>(NPVARIANT_TO_STRING(*variant
).UTF8Characters
));
455 case NPVariantType_Object
:
457 NPObject
* obj
= NPVARIANT_TO_OBJECT(*variant
);
459 NPN_ReleaseObject(obj
);
467 NULL_TO_NPVARIANT(*variant
);
471 NPN_RetainObject(NPObject
*obj
)
472 { assert(obj
); ++obj
->referenceCount
; return obj
; }
476 NPN_ReleaseObject(NPObject
*npobj
)
479 --npobj
->referenceCount
;
480 if (npobj
->referenceCount
== 0) {
486 // indent-tabs-mode: nil