Mypal/dom/plugins/test/mochitest/test_npruntime_npnevaluate....

102 lines
3.4 KiB
HTML

<html>
<head>
<title>NPN_Evaluate Tests</title>
<script type="text/javascript"
src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="plugin-utils.js"></script>
<link rel="stylesheet" type="text/css"
href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests()">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
// 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.
//
function runTests() {
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() { var ret; try { win = window.open(); win.document.writeln('wibble'); ret = 'no error' } catch(e) { ret = e.name; } win.close(); return ret; })()", "no error"],
];
var plugin = document.getElementById("plugin1");
// Test calling NPN_Evaluate from within plugin code.
for (var test of 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));
$("verbose").appendChild(
createEl('span',null, (success ? "pass" : "fail") + ": eval(" + test[0] + ")"));
$("verbose").appendChild(
createEl('span', null," == " + json_result + "(" +
typeof(result) + "), expected " + json_expected + "(" +
typeof(expected) + ")"));
$("verbose").appendChild(
createEl('br')
);
}
is(document.getElementById('testdiv').innerHTML, "Hello world!",
"innerHTML not set correctly via NPN_Evaluate");
SimpleTest.finish();
}
</script>
<p id="display"></p>
<embed id="plugin1" type="application/x-test" width="400" height="100">
</embed>
<div id="verbose">
</div>
<div id="testdiv">
</div>
</body>
</html>