Bug 518940. Add NPN_Evaluate tests.

This commit is contained in:
Jonathan Griffin 2009-10-23 13:41:52 -07:00
parent fbb1d45b16
commit 672d88205f
4 changed files with 135 additions and 0 deletions

View File

@ -46,6 +46,7 @@ include $(topsrcdir)/config/rules.mk
_MOCHITEST_FILES = \
test_npobject_getters.html \
test_npruntime_npnevaluate.html \
test_npruntime_npninvoke.html \
test_npruntime_npninvokedefault.html \
test_npruntime_npnsetexception.html \

View File

@ -0,0 +1,98 @@
<html>
<head>
<title>NPN_Evaluate Tests</title>
<script type="text/javascript"
src="/MochiKit/packed.js"></script>
<script type="text/javascript"
src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css"
href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests()">
<p id="display"></p>
<embed id="plugin1" type="application/x-test" width="400" height="100">
</embed>
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
// global test function
function testMe(arg) {
var result = arg+arg;
for (var i = 1; i < arguments.length; i++) {
result += arguments[i] + arguments[i];
}
return result;
}
////
// This test exercises NPN_Evaluate using the test plugin's
// npnEvaluateTest method. This method calls NPN_Evaluate on
// a string argument passed to it, and returns the eval result.
// The array below drives the tests; each array member has two
// members: the first is a string to eval, and the second is
// the expected result of the eval.
//
var tests = [
["3", 3],
["3 + 3", 6],
["'3'", "3"],
["function test() { return 3; } test();", 3],
["testMe(3)", 6],
["testMe(new Object(3))", 6],
["new Object(3)", new Object(3)],
["new Array(1, 2, 3, 4)", [1, 2, 3, 4]],
["document.getElementById('display')",
document.getElementById("display")],
["encodeURI('a = b')", "a%20=%20b"],
["document.getElementById('testdiv').innerHTML = 'Hello world!'",
"Hello world!"],
["function test2() { var x = {a: '1', b: '2'}; return x; } test2();",
{a: '1', b: '2'}],
];
function runTests() {
var plugin = document.getElementById("plugin1");
// Test calling NPN_Evaluate from within plugin code.
for each (var test in tests) {
var expected = test[1];
var result = plugin.npnEvaluateTest(test[0]);
// serialize the two values for easy comparison
var json_expected = JSON.stringify(expected);
var json_result = JSON.stringify(result);
if (typeof(result) == "function")
json_result = result.toString();
if (typeof(expected) == "function")
json_expected = expected.toString();
is(json_result, json_expected,
"npnEvaluateTest returned an unexpected value");
is(typeof(result), typeof(expected),
"npnEvaluateTest return value was of unexpected type");
var success = (json_result == json_expected &&
typeof(result) == typeof(expected));
appendChildNodes($("verbose"),
SPAN((success ? "pass" : "fail") + ": eval(" + test[0] + ")"));
appendChildNodes($("verbose"),
SPAN(" == " + json_result + "(" +
typeof(result) + "), expected " + json_expected + "(" +
typeof(expected) + ")"),
BR()
);
}
is(document.getElementById('testdiv').innerHTML, "Hello world!",
"innerHTML not set correctly via NPN_Evaluate");
SimpleTest.finish();
}
</script>
<div id="verbose">
</div>
<div id="testdiv">
</div>
</body>
</html>

View File

@ -33,6 +33,10 @@ The test plugin object supports the following scriptable methods:
Converts a string, int32 or double parameter 'ident' to an NPIdentifier and
then to a string, which is returned.
* npnEvaluateTest(script)
Calls NPN_Evaluate on the 'script' argument, which is a string containing
some script to be executed. Returns the result of the evaluation.
* npnInvokeTest(method, expected, args...)
Causes the plugin to call the specified script method using NPN_Invoke,
passing it 1 or more arguments specified in args. The return value of this

View File

@ -65,6 +65,7 @@ static NPClass sNPClass;
typedef bool (* ScriptableFunction)
(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static bool npnEvaluateTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static bool npnInvokeTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static bool npnInvokeDefaultTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static bool setUndefinedValueTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
@ -88,6 +89,7 @@ static bool setColor(NPObject* npobj, const NPVariant* args, uint32_t argCount,
static bool throwExceptionNextInvoke(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static const NPUTF8* sPluginMethodIdentifierNames[] = {
"npnEvaluateTest",
"npnInvokeTest",
"npnInvokeDefaultTest",
"setUndefinedValueTest",
@ -112,6 +114,7 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = {
};
static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)];
static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = {
npnEvaluateTest,
npnInvokeTest,
npnInvokeDefaultTest,
setUndefinedValueTest,
@ -1148,6 +1151,12 @@ NPN_GetProperty(NPP instance,
return sBrowserFuncs->getproperty(instance, npobj, propertyName, result);
}
bool
NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
{
return sBrowserFuncs->evaluate(instance, npobj, script, result);
}
void
NPN_SetException(NPObject *npobj, const NPUTF8 *message)
{
@ -1484,6 +1493,29 @@ npnInvokeTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVaria
return true;
}
static bool
npnEvaluateTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
bool success = false;
NPP npp = static_cast<TestNPObject*>(npobj)->npp;
if (argCount != 1)
return false;
if (!NPVARIANT_IS_STRING(args[0]))
return false;
NPObject* windowObject;
NPN_GetValue(npp, NPNVWindowNPObject, &windowObject);
if (!windowObject)
return false;
success = NPN_Evaluate(npp, windowObject, (NPString*)&NPVARIANT_TO_STRING(args[0]), result);
NPN_ReleaseObject(windowObject);
return success;
}
static bool
setUndefinedValueTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{