Bug 518940. Add tests for NPN_InvokeDefault. r=bsmedberg

This commit is contained in:
Jonathan Griffin 2009-10-12 10:38:11 -07:00
Родитель 6199c61a06
Коммит 1f2d72eb7a
4 изменённых файлов: 172 добавлений и 0 удалений

Просмотреть файл

@ -47,6 +47,7 @@ include $(topsrcdir)/config/rules.mk
_MOCHITEST_FILES = \
test_npobject_getters.html \
test_npruntime_npninvoke.html \
test_npruntime_npninvokedefault.html \
loremipsum.txt \
loremipsum_file.txt \
post.sjs \

Просмотреть файл

@ -0,0 +1,122 @@
<html>
<head>
<title>NPN_Invoke_Default 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) {
return arg + arg;
}
////
// This test exercises NPN_InvokeDefault using the test plugin's
// npnInvokeDefaultTest method. This method invokes an object
// with a single parameter, and returns the result of the invocation.
// The test list below is used to drive the tests. Each member of the
// array contains three members: the object to invoke, an argument to
// invoke it with, and the expected result of the invocation.
//
var tests = [
// Number object
["Number", 3, 3],
["Number", "3", 3],
["Number", "0x20", 32],
["Number", "three", Number.NaN],
["Number", "1e+3", 1000],
["Number", 5.6612, 5.6612],
// Array object
["Array", 3, Array(3)],
// Boolean object
["Boolean", 0, false],
["Boolean", null, false],
["Boolean", "", false],
["Boolean", false, false],
["Boolean", true, true],
["Boolean", "true", true],
["Boolean", "false", true],
["Boolean", new Boolean(false), true],
["Boolean", { "value": false }, true],
// Date object
["Date", 50000, Date(50000)],
["Date", "December 17, 1995 03:24:00", Date("December 17, 1995 03:24:00")],
// Function object
["Function", "return 3", Function("return 3")],
["Function", "window.alert('test')", Function("window.alert('test')")],
// Object object
["Object", undefined, Object()],
["Object", null, Object()],
["Object", true, new Boolean(true)],
["Object", Boolean(), new Boolean(false)],
["Object", "a string", new String("a string")],
["Object", 3.14, new Number(3.14)],
["Object", { "key1": "test", "key2": 15 }, { "key1": "test", "key2": 15 }],
["Object", [1, 3, 5, 7, 9, 11, 13, 17], [1, 3, 5, 7, 9, 11, 13, 17]],
// RegExp object
["RegExp", "...", RegExp("...")],
// String object
["String", "testing", "testing"],
["String", "test\u0020me", "test me"],
["String", "314", "314"],
["String", "true", "true"],
["String", "null", "null"],
["String", "2 + 2", String("2 + 2")],
// global functions
["testMe", 3, 6],
["testMe", "me", "meme"],
["testMe", undefined, Number.NaN],
["testMe", [1, 2], "1,21,2"],
["isNaN", "junk", true],
["parseInt", "156", 156],
["encodeURI", "a = b", "a%20=%20b"],
];
function runTests() {
var plugin = document.getElementById("plugin1");
for each (var test in tests) {
var result = plugin.npnInvokeDefaultTest(test[0], test[1]);
// serialize the two values for easy
var json_expected = JSON.stringify(test[2]);
var json_result = JSON.stringify(result);
if (typeof(result) == "function")
json_result = result.toString();
if (typeof(test[2]) == "function")
json_expected = test[2].toString();
is(json_result, json_expected,
"npnInvokeDefault returned an unexpected value");
is(typeof(result), typeof(test[2]),
"npnInvokeDefaultTest return value was of unexpected type");
var success = (json_result == json_expected &&
typeof(result) == typeof(test[2]));
appendChildNodes($("verbose"),
SPAN((success ? "pass" : "fail") + ": " + test[0] + "(" +
JSON.stringify(test[1]) + ") == " + json_result + "(" +
typeof(result) + "), expected " + json_expected + "(" +
typeof(test[2]) + ")"),
BR()
);
}
SimpleTest.finish();
}
</script>
<div id="verbose">
</div>
</body>
</html>

Просмотреть файл

@ -39,6 +39,10 @@ passing it 1 or more arguments specified in args. The return value of this
call is compared against 'expected', and if they match, npnInvokeTest will
return true. Otherwise, it will return false.
* npnInvokeDefaultTest(object, argument)
Causes the plugin to call NPN_InvokeDefault on the specified object,
with the specified argument. Returns the result of the invocation.
* getError()
If an error has occurred during the last stream or npruntime function,
this will return a string error message, otherwise it returns "pass".

Просмотреть файл

@ -66,6 +66,7 @@ typedef bool (* ScriptableFunction)
(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);
static bool identifierToStringTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
static bool timerTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result);
@ -86,6 +87,7 @@ static bool doInternalConsistencyCheck(NPObject* npobj, const NPVariant* args, u
static const NPUTF8* sPluginMethodIdentifierNames[] = {
"npnInvokeTest",
"npnInvokeDefaultTest",
"setUndefinedValueTest",
"identifierToStringTest",
"timerTest",
@ -107,6 +109,7 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = {
static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)];
static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = {
npnInvokeTest,
npnInvokeDefaultTest,
setUndefinedValueTest,
identifierToStringTest,
timerTest,
@ -1001,6 +1004,12 @@ NPN_Invoke(NPP npp, NPObject* obj, NPIdentifier methodName, const NPVariant *arg
return sBrowserFuncs->invoke(npp, obj, methodName, args, argCount, result);
}
bool
NPN_InvokeDefault(NPP npp, NPObject* obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
return sBrowserFuncs->invokeDefault(npp, obj, args, argCount, result);
}
const char*
NPN_UserAgent(NPP instance)
{
@ -1325,6 +1334,42 @@ compareVariants(NPP instance, const NPVariant* var1, const NPVariant* var2)
return success;
}
static bool
npnInvokeDefaultTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{
bool success = false;
NPP npp = static_cast<TestNPObject*>(npobj)->npp;
NPObject* windowObject;
NPN_GetValue(npp, NPNVWindowNPObject, &windowObject);
if (!windowObject)
return false;
NPIdentifier objectIdentifier = variantToIdentifier(args[0]);
if (!objectIdentifier)
return false;
NPVariant objectVariant;
if (NPN_GetProperty(npp, windowObject, objectIdentifier,
&objectVariant)) {
if (NPVARIANT_IS_OBJECT(objectVariant)) {
NPObject* selfObject = NPVARIANT_TO_OBJECT(objectVariant);
if (selfObject != NULL) {
NPVariant resultVariant;
if (NPN_InvokeDefault(npp, selfObject, &args[1], argCount - 1,
&resultVariant)) {
*result = resultVariant;
success = true;
}
}
}
NPN_ReleaseVariantValue(&objectVariant);
}
NPN_ReleaseObject(windowObject);
return success;
}
static bool
npnInvokeTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result)
{