Bug 522791. Add basic tests for NPN_SetException.

--HG--
extra : rebase_source : 5d4f69cb5c54136775f0752713d055ad9f407c7a
This commit is contained in:
Jonathan Griffin 2009-10-19 14:00:59 -07:00
Родитель 078f217e46
Коммит 70187bd76e
5 изменённых файлов: 113 добавлений и 0 удалений

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

@ -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 \

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

@ -0,0 +1,49 @@
<html>
<head>
<title>NPN_SetException 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();
function runTests() {
// test a single exception thrown in scriptable invoke
var plugin = document.getElementById("plugin1");
plugin.throwExceptionNextInvoke();
try {
plugin.npnInvokeTest("badFunction");
ok(false, "exception not thrown");
}
catch (e) {
is(e, "badFunction", "wrong exception thrown");
}
// test multiple exceptions thrown in scriptable invokedefault
plugin.throwExceptionNextInvoke();
try {
plugin("first exception", "second exception");
ok(false, "exception not thrown");
}
catch (e) {
is(e, "second exception", "wrong exception thrown");
}
SimpleTest.finish();
}
</script>
<div id="verbose">
</div>
</body>
</html>

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

@ -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.

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

@ -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<TestNPObject*>(npobj)->npp;
InstanceData* id = static_cast<InstanceData*>(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<TestNPObject*>(npobj)->npp;
InstanceData* id = static_cast<InstanceData*>(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<TestNPObject*>(npobj)->npp;
InstanceData* id = static_cast<InstanceData*>(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)
{

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

@ -91,6 +91,7 @@ typedef struct InstanceData {
bool lastReportedPrivateModeState;
bool hasWidget;
bool npnNewStream;
bool throwOnNextInvoke;
uint32_t timerID1;
uint32_t timerID2;
int32_t lastMouseX;