From 70187bd76e3e46faa42ddb1a1ef3550bafdd4241 Mon Sep 17 00:00:00 2001 From: Jonathan Griffin Date: Mon, 19 Oct 2009 14:00:59 -0700 Subject: [PATCH] Bug 522791. Add basic tests for NPN_SetException. --HG-- extra : rebase_source : 5d4f69cb5c54136775f0752713d055ad9f407c7a --- modules/plugin/test/mochitest/Makefile.in | 1 + .../test_npruntime_npnsetexception.html | 49 +++++++++++++++++ modules/plugin/test/testplugin/README | 10 ++++ modules/plugin/test/testplugin/nptest.cpp | 52 +++++++++++++++++++ modules/plugin/test/testplugin/nptest.h | 1 + 5 files changed, 113 insertions(+) create mode 100644 modules/plugin/test/mochitest/test_npruntime_npnsetexception.html diff --git a/modules/plugin/test/mochitest/Makefile.in b/modules/plugin/test/mochitest/Makefile.in index fc3fac7c305..95ecc6fc4f2 100644 --- a/modules/plugin/test/mochitest/Makefile.in +++ b/modules/plugin/test/mochitest/Makefile.in @@ -48,6 +48,7 @@ _MOCHITEST_FILES = \ test_npobject_getters.html \ test_npruntime_npninvoke.html \ test_npruntime_npninvokedefault.html \ + test_npruntime_npnsetexception.html \ loremipsum.txt \ loremipsum_file.txt \ post.sjs \ diff --git a/modules/plugin/test/mochitest/test_npruntime_npnsetexception.html b/modules/plugin/test/mochitest/test_npruntime_npnsetexception.html new file mode 100644 index 00000000000..f34d0d6d12f --- /dev/null +++ b/modules/plugin/test/mochitest/test_npruntime_npnsetexception.html @@ -0,0 +1,49 @@ + + + NPN_SetException Tests + + + + + +

+ + + + + + +
+
+ + diff --git a/modules/plugin/test/testplugin/README b/modules/plugin/test/testplugin/README index 513842a8ef8..75f841c1ad6 100644 --- a/modules/plugin/test/testplugin/README +++ b/modules/plugin/test/testplugin/README @@ -47,6 +47,16 @@ with the specified argument. Returns the result of the invocation. If an error has occurred during the last stream or npruntime function, this will return a string error message, otherwise it returns "pass". +* throwExceptionNextInvoke() +Sets a flag which causes the next call to a scriptable method to throw +one or more exceptions. If no parameters are passed to the next +scriptable method call, it will cause a generic exception to be thrown. +Otherwise there will be one exception thrown per argument, with the argument +used as the exception message. Example: + + plugin.throwExceptionNextInvoke(); + plugin.npnInvokeTest("first exception message", "second exception message"); + * () - default method Returns a string consisting of the plugin name, concatenated with any arguments passed to the method. diff --git a/modules/plugin/test/testplugin/nptest.cpp b/modules/plugin/test/testplugin/nptest.cpp index 42b2bd3d9ce..6919c6f6bc1 100644 --- a/modules/plugin/test/testplugin/nptest.cpp +++ b/modules/plugin/test/testplugin/nptest.cpp @@ -85,6 +85,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 bool doInternalConsistencyCheck(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static bool setColor(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); +static bool throwExceptionNextInvoke(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result); static const NPUTF8* sPluginMethodIdentifierNames[] = { "npnInvokeTest", @@ -107,6 +108,7 @@ static const NPUTF8* sPluginMethodIdentifierNames[] = { "getError", "doInternalConsistencyCheck", "setColor", + "throwExceptionNextInvoke", }; static NPIdentifier sPluginMethodIdentifiers[ARRAY_LENGTH(sPluginMethodIdentifierNames)]; static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMethodIdentifierNames)] = { @@ -130,6 +132,7 @@ static const ScriptableFunction sPluginMethodFunctions[ARRAY_LENGTH(sPluginMetho getError, doInternalConsistencyCheck, setColor, + throwExceptionNextInvoke, }; static const char* NPN_GetURLNotifyCookie = "NPN_GetURLNotify_Cookie"; @@ -448,6 +451,7 @@ NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* instanceData->streamBufSize = 0; instanceData->fileBuf = NULL; instanceData->fileBufSize = 0; + instanceData->throwOnNextInvoke = false; instanceData->testrange = NULL; instanceData->hasWidget = false; instanceData->npnNewStream = false; @@ -1144,6 +1148,12 @@ NPN_GetProperty(NPP instance, return sBrowserFuncs->getproperty(instance, npobj, propertyName, result); } +void +NPN_SetException(NPObject *npobj, const NPUTF8 *message) +{ + return sBrowserFuncs->setexception(npobj, message); +} + // // npruntime object functions // @@ -1182,6 +1192,22 @@ scriptableHasMethod(NPObject* npobj, NPIdentifier name) bool scriptableInvoke(NPObject* npobj, NPIdentifier name, const NPVariant* args, uint32_t argCount, NPVariant* result) { + NPP npp = static_cast(npobj)->npp; + InstanceData* id = static_cast(npp->pdata); + if (id->throwOnNextInvoke) { + id->throwOnNextInvoke = false; + if (argCount == 0) { + NPN_SetException(npobj, NULL); + } + else { + for (uint32_t i = 0; i < argCount; i++) { + const NPString* argstr = &NPVARIANT_TO_STRING(args[i]); + NPN_SetException(npobj, argstr->UTF8Characters); + } + } + return false; + } + for (int i = 0; i < int(ARRAY_LENGTH(sPluginMethodIdentifiers)); i++) { if (name == sPluginMethodIdentifiers[i]) return sPluginMethodFunctions[i](npobj, args, argCount, result); @@ -1192,6 +1218,22 @@ scriptableInvoke(NPObject* npobj, NPIdentifier name, const NPVariant* args, uint bool scriptableInvokeDefault(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) { + NPP npp = static_cast(npobj)->npp; + InstanceData* id = static_cast(npp->pdata); + if (id->throwOnNextInvoke) { + id->throwOnNextInvoke = false; + if (argCount == 0) { + NPN_SetException(npobj, NULL); + } + else { + for (uint32_t i = 0; i < argCount; i++) { + const NPString* argstr = &NPVARIANT_TO_STRING(args[i]); + NPN_SetException(npobj, argstr->UTF8Characters); + } + } + return false; + } + ostringstream value; value << PLUGIN_NAME; for (uint32_t i = 0; i < argCount; i++) { @@ -1366,6 +1408,16 @@ compareVariants(NPP instance, const NPVariant* var1, const NPVariant* var2) return success; } +static bool +throwExceptionNextInvoke(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + NPP npp = static_cast(npobj)->npp; + InstanceData* id = static_cast(npp->pdata); + id->throwOnNextInvoke = true; + BOOLEAN_TO_NPVARIANT(true, *result); + return true; +} + static bool npnInvokeDefaultTest(NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) { diff --git a/modules/plugin/test/testplugin/nptest.h b/modules/plugin/test/testplugin/nptest.h index 5055333df1e..d68bdc17382 100644 --- a/modules/plugin/test/testplugin/nptest.h +++ b/modules/plugin/test/testplugin/nptest.h @@ -91,6 +91,7 @@ typedef struct InstanceData { bool lastReportedPrivateModeState; bool hasWidget; bool npnNewStream; + bool throwOnNextInvoke; uint32_t timerID1; uint32_t timerID2; int32_t lastMouseX;