From 675a2b215b0c9fb58a6cfa435632aabb2c720b51 Mon Sep 17 00:00:00 2001 From: Jonathan Griffin Date: Wed, 30 Sep 2009 15:11:57 -0700 Subject: [PATCH] Bug 518940. Adding tests for NPN_Invoke. r=bsmedberg --- modules/plugin/test/mochitest/Makefile.in | 1 + .../mochitest/test_npruntime_npninvoke.html | 161 +++++++++++++++++ modules/plugin/test/testplugin/README | 12 +- modules/plugin/test/testplugin/nptest.cpp | 163 ++++++++++++++++++ 4 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 modules/plugin/test/mochitest/test_npruntime_npninvoke.html diff --git a/modules/plugin/test/mochitest/Makefile.in b/modules/plugin/test/mochitest/Makefile.in index 8cfc6b0de85e..ddd3993c50d4 100644 --- a/modules/plugin/test/mochitest/Makefile.in +++ b/modules/plugin/test/mochitest/Makefile.in @@ -46,6 +46,7 @@ include $(topsrcdir)/config/rules.mk _MOCHITEST_FILES = \ test_npobject_getters.html \ + test_npruntime_npninvoke.html \ loremipsum.txt \ loremipsum_file.txt \ post.sjs \ diff --git a/modules/plugin/test/mochitest/test_npruntime_npninvoke.html b/modules/plugin/test/mochitest/test_npruntime_npninvoke.html new file mode 100644 index 000000000000..cb3089e73e23 --- /dev/null +++ b/modules/plugin/test/mochitest/test_npruntime_npninvoke.html @@ -0,0 +1,161 @@ + + + NPN_Invoke Tests + + + + + +

+ + + + + + +
+
+ + diff --git a/modules/plugin/test/testplugin/README b/modules/plugin/test/testplugin/README index bbfe765417c8..56c1bdad9f15 100644 --- a/modules/plugin/test/testplugin/README +++ b/modules/plugin/test/testplugin/README @@ -27,12 +27,22 @@ returns true if it succeeds and false if it doesn't. It should never succeed. == NPRuntime testing == -The test plugin object supports the following scriptable method: +The test plugin object supports the following scriptable methods: * identifierToStringTest(ident) Converts a string, int32 or double parameter 'ident' to an NPIdentifier and then to a string, which is returned. +* 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 +call is compared against 'expected', and if they match, npnInvokeTest will +return true. Otherwise, it will return false. + +* getError() +If an error has occurred during the last stream or npruntime function, +this will return a string error message, otherwise it returns "pass". + == Private browsing == The test plugin object supports the following scriptable methods: diff --git a/modules/plugin/test/testplugin/nptest.cpp b/modules/plugin/test/testplugin/nptest.cpp index 8ebff591c6d2..05904385cae9 100644 --- a/modules/plugin/test/testplugin/nptest.cpp +++ b/modules/plugin/test/testplugin/nptest.cpp @@ -63,6 +63,7 @@ static NPClass sNPClass; 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 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); @@ -81,6 +82,7 @@ static bool getLastMouseY(NPObject* npobj, const NPVariant* args, uint32_t argCo static bool getError(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static const NPUTF8* sPluginMethodIdentifierNames[] = { + "npnInvokeTest", "setUndefinedValueTest", "identifierToStringTest", "timerTest", @@ -100,6 +102,7 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = { }; static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)]; static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = { + npnInvokeTest, setUndefinedValueTest, identifierToStringTest, timerTest, @@ -1004,6 +1007,25 @@ NPN_Write(NPP instance, return sBrowserFuncs->write(instance, stream, len, buf); } +bool +NPN_Enumerate(NPP instance, + NPObject *npobj, + NPIdentifier **identifiers, + uint32_t *identifierCount) +{ + return sBrowserFuncs->enumerate(instance, npobj, identifiers, + identifierCount); +} + +bool +NPN_GetProperty(NPP instance, + NPObject *npobj, + NPIdentifier propertyName, + NPVariant *result) +{ + return sBrowserFuncs->getproperty(instance, npobj, propertyName, result); +} + // // npruntime object functions // @@ -1095,6 +1117,147 @@ scriptableConstruct(NPObject* npobj, const NPVariant* args, uint32_t argCount, N // test functions // +static bool +compareVariants(NPP instance, const NPVariant* var1, const NPVariant* var2, ostringstream* err) +{ + bool success = true; + if (var1->type != var2->type) { + *err << "Variant types don't match; got " << var1->type << + " expected " << var2->type; + return false; + } + + switch (var1->type) { + case NPVariantType_Int32: { + int32_t result = NPVARIANT_TO_INT32(*var1); + int32_t expected = NPVARIANT_TO_INT32(*var2); + if (result != expected) { + *err << "Variant values don't match; got " << result << + " expected " << expected; + success = false; + } + break; + } + case NPVariantType_Double: { + double result = NPVARIANT_TO_DOUBLE(*var1); + double expected = NPVARIANT_TO_DOUBLE(*var2); + if (result != expected) { + *err << "Variant values don't match; got " << result << + " expected " << expected; + success = false; + } + break; + } + case NPVariantType_Void: { + // void values are always equivalent + break; + } + case NPVariantType_Null: { + // null values are always equivalent + break; + } + case NPVariantType_Bool: { + bool result = NPVARIANT_TO_BOOLEAN(*var1); + bool expected = NPVARIANT_TO_BOOLEAN(*var2); + if (result != expected) { + *err << "Variant values don't match; got " << result << + " expected " << expected; + success = false; + } + break; + } + case NPVariantType_String: { + const NPString* result = &NPVARIANT_TO_STRING(*var1); + const NPString* expected = &NPVARIANT_TO_STRING(*var2); + if (strcmp(result->UTF8Characters, expected->UTF8Characters) || + strlen(result->UTF8Characters) != strlen(expected->UTF8Characters)) { + *err << "Variant values don't match; got " << + result->UTF8Characters << " expected " << + expected->UTF8Characters; + success = false; + } + break; + } + case NPVariantType_Object: { + uint32_t i, identifierCount = 0; + NPIdentifier* identifiers; + NPObject* result = NPVARIANT_TO_OBJECT(*var1); + NPObject* expected = NPVARIANT_TO_OBJECT(*var2); + bool enumerate_result = NPN_Enumerate(instance, expected, + &identifiers, &identifierCount); + if (!enumerate_result) { + *err << "NPN_Enumerate failed"; + success = false; + } + for (i = 0; i < identifierCount; i++) { + NPUTF8* utf8String = NPN_UTF8FromIdentifier(identifiers[i]); + NPVariant resultVariant, expectedVariant; + if (!NPN_GetProperty(instance, expected, identifiers[i], + &expectedVariant)) { + *err << "NPN_GetProperty returned false"; + success = false; + } + else { + if (!NPN_HasProperty(instance, result, identifiers[i])) { + *err << "NPN_HasProperty returned false"; + success = false; + } + else { + if (!NPN_GetProperty(instance, result, identifiers[i], + &resultVariant)) { + *err << "NPN_GetProperty 2 returned false"; + success = false; + } + else { + success = compareVariants(instance, &resultVariant, + &expectedVariant, err); + NPN_ReleaseVariantValue(&expectedVariant); + } + } + NPN_ReleaseVariantValue(&resultVariant); + } + } + break; + } + default: + *err << "Unknown variant type"; + success = false; + } + + return success; +} + +static bool +npnInvokeTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + NPP npp = static_cast(npobj)->npp; + InstanceData* id = static_cast(npp->pdata); + id->err.str(""); + if (argCount < 2) + return false; + + NPIdentifier function = variantToIdentifier(args[0]); + if (!function) + return false; + + NPObject* windowObject; + NPN_GetValue(npp, NPNVWindowNPObject, &windowObject); + if (!windowObject) + return false; + + NPVariant invokeResult; + bool invokeReturn = NPN_Invoke(npp, windowObject, function, + argCount > 2 ? &args[2] : NULL, argCount - 2, &invokeResult); + + bool compareResult = compareVariants(npp, &invokeResult, &args[1], + &id->err); + + NPN_ReleaseObject(windowObject); + NPN_ReleaseVariantValue(&invokeResult); + BOOLEAN_TO_NPVARIANT(invokeReturn && compareResult, *result); + return true; +} + static bool setUndefinedValueTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) {