1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
<html>
<head>
<title>NPN_Invoke 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">
////
// This test exercises NPN_Invoke by calling the plugin's npnInvokeTest
// method, which in turn invokes a script method with 1 or more parameters,
// and then compares the return vale with an expected value. This is good
// for verifying various NPVariant values and types moving between
// the browser and the plugin.
//
SimpleTest.waitForExplicitFinish();
setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
// This function returns all the arguments passed to it, either as a
// single variable (in the caes of 1 argument), or as an array.
function returnArgs() {
if (arguments.length == 1)
return arguments[0];
var arr = new Array();
for (i = 0; i < arguments.length; i++) {
arr.push(arguments[i]);
}
return arr;
}
// Same as above but explicitly expects two arguments.
function returnTwoArgs(arg1, arg2) {
return [arg1, arg2];
}
// some objects and arrays used in the tests...
var obj = {"key1": "string", "key2": 0, "key3": true, "key4": undefined,
"key5": null, "key6": -551235.12552, "key7": false};
var arr = ["string", 0, true, false, undefined, null, -1, 55512.1252];
var obj2 = {"key1": "string", "key2": 0, "key3": true, "key4": undefined,
"key5": null, "key6": -551235.12552, "key7": false, "array": arr};
var arr2 = ["string", false, undefined, null, -1, 55512.1252,
{"a": "apple", "b": true, "c": undefined}];
////
// A list of tests to run. Each member of the main array contains
// two members: the first contains the arguments passed to npnInvokeTest,
// and the second is the expected result.
//
var tests = [
// numeric values
[["returnArgs", 0, 0], true],
[["returnArgs", 1, 1], true],
[["returnArgs", 32768, 32768], true],
[["returnArgs", -32768, -32768], true],
[["returnArgs", 2147483648, 2147483648], true],
[["returnArgs", -2147483648, -2147483648], true],
[["returnArgs", 1.0, 1.0], true],
[["returnArgs", 1.592786, 1.592786], true],
[["returnArgs", 1.592786837, 1.592786837], true],
[["returnArgs", -1.592786, -1.592786], true],
[["returnArgs", -1.592786837, -1.592786837], true],
[["returnArgs", 15235.592786, 15235.592786], true],
// null, void, bool
[["returnArgs", null, null], true],
[["returnArgs", undefined, undefined], true],
[["returnArgs", undefined, null], false],
[["returnArgs", null, undefined], false],
[["returnArgs", 0, undefined], false],
[["returnArgs", 0, null], false],
[["returnArgs", 0, false], false],
[["returnArgs", 1, true], false],
[["returnArgs", true, true], true],
[["returnArgs", false, false], true],
// strings
[["returnArgs", "", ""], true],
[["returnArgs", "test", "test"], true],
[["returnArgs", "test", "testing"], false],
[["returnArgs", "test\n", "test\n"], true],
[["returnArgs", "test\nline2", "test\nline2"], true],
[["returnArgs", "test\nline2", "testline2"], false],
[["returnArgs", "test\u000aline2", "test\u000aline2"], true],
[["returnArgs", "test\u0020line2", "test line2"], true],
[["returnArgs", "test line2", "test\u0020line2"], true],
// objects and arrays
[["returnArgs", obj, obj], true],
[["returnArgs", arr, arr], true],
[["returnArgs", obj2, obj2], true],
[["returnArgs", arr2, arr2], true],
// multiple arguments
[["returnArgs", [0, 1, 2], 0, 1, 2], true],
[["returnArgs", [5, "a", true, false, null],
5, "a", true, false, null], true],
[["returnArgs", [-1500.583, "test string\n",
[5, 10, 15, 20], {"a": 1, "b": 2}],
-1500.583, "test string\n", [5, 10, 15, 20], {"a": 1, "b": 2}], true],
[["returnArgs", [undefined, 0, null, "yes"],
undefined, 0, null, "yes"], true],
[["returnArgs", [0, undefined, null, "yes"],
0, undefined, null, "yes"], true],
// too many/too few args
[["returnTwoArgs", ["a", "b"], "a", "b", "c"], true],
[["returnTwoArgs", ["a", undefined], "a"], true],
[["returnTwoArgs", [undefined, undefined]], true],
];
function runTests() {
var plugin = document.getElementById("plugin1");
var result;
for (var test of tests) {
switch (test[0].length) {
case 2:
result = plugin.npnInvokeTest(test[0][0], test[0][1]);
break;
case 3:
result = plugin.npnInvokeTest(test[0][0], test[0][1], test[0][2]);
break;
case 4:
result = plugin.npnInvokeTest(test[0][0], test[0][1], test[0][2],
test[0][3]);
break;
case 5:
result = plugin.npnInvokeTest(test[0][0], test[0][1], test[0][2],
test[0][3], test[0][4]);
case 6:
result = plugin.npnInvokeTest(test[0][0], test[0][1], test[0][2],
test[0][3], test[0][4], test[0][5]);
break;
case 7:
result = plugin.npnInvokeTest(test[0][0], test[0][1], test[0][2],
test[0][3], test[0][4], test[0][5], test[0][6]);
break;
default:
is(false, "bad number of test arguments");
}
is(result, test[1], "npnInvokeTestFailed: " + plugin.getError());
$("verbose").appendChild(
createEl('span', null, ((result == test[1] ? "pass" : "fail") + ": " + test[0])));
if (result != test[1])
$("verbose").appendChild(createEl("span", null, (" " + plugin.getError())));
$("verbose").appendChild(createEl('br'));
}
SimpleTest.finish();
}
</script>
<p id="display"></p>
<embed id="plugin1" type="application/x-test" width="400" height="100">
</embed>
<div id="verbose">
</div>
</body>
</html>
|