dom/plugins/test/mochitest/test_npruntime_npnevaluate.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <html>
michael@0 2 <head>
michael@0 3 <title>NPN_Evaluate Tests</title>
michael@0 4 <script type="text/javascript"
michael@0 5 src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <script type="text/javascript" src="utils.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css"
michael@0 8 href="/tests/SimpleTest/test.css" />
michael@0 9 </head>
michael@0 10 <body onload="runTests()">
michael@0 11 <script class="testbody" type="application/javascript">
michael@0 12
michael@0 13 SimpleTest.waitForExplicitFinish();
michael@0 14 setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
michael@0 15
michael@0 16 // global test function
michael@0 17 function testMe(arg) {
michael@0 18 var result = arg+arg;
michael@0 19 for (var i = 1; i < arguments.length; i++) {
michael@0 20 result += arguments[i] + arguments[i];
michael@0 21 }
michael@0 22 return result;
michael@0 23 }
michael@0 24
michael@0 25 ////
michael@0 26 // This test exercises NPN_Evaluate using the test plugin's
michael@0 27 // npnEvaluateTest method. This method calls NPN_Evaluate on
michael@0 28 // a string argument passed to it, and returns the eval result.
michael@0 29 // The array below drives the tests; each array member has two
michael@0 30 // members: the first is a string to eval, and the second is
michael@0 31 // the expected result of the eval.
michael@0 32 //
michael@0 33
michael@0 34 function runTests() {
michael@0 35 var tests = [
michael@0 36 ["3", 3],
michael@0 37 ["3 + 3", 6],
michael@0 38 ["'3'", "3"],
michael@0 39 ["function test() { return 3; } test();", 3],
michael@0 40 ["testMe(3)", 6],
michael@0 41 ["testMe(new Object(3))", 6],
michael@0 42 ["new Object(3)", new Object(3)],
michael@0 43 ["new Array(1, 2, 3, 4)", [1, 2, 3, 4]],
michael@0 44 ["document.getElementById('display')",
michael@0 45 document.getElementById("display")],
michael@0 46 ["encodeURI('a = b')", "a%20=%20b"],
michael@0 47 ["document.getElementById('testdiv').innerHTML = 'Hello world!'",
michael@0 48 "Hello world!"],
michael@0 49 ["function test2() { var x = {a: '1', b: '2'}; return x; } test2();",
michael@0 50 {a: '1', b: '2'}],
michael@0 51 ];
michael@0 52
michael@0 53 var plugin = document.getElementById("plugin1");
michael@0 54
michael@0 55 // Test calling NPN_Evaluate from within plugin code.
michael@0 56 for (var test of tests) {
michael@0 57 var expected = test[1];
michael@0 58 var result = plugin.npnEvaluateTest(test[0]);
michael@0 59 // serialize the two values for easy comparison
michael@0 60 var json_expected = JSON.stringify(expected);
michael@0 61 var json_result = JSON.stringify(result);
michael@0 62 if (typeof(result) == "function")
michael@0 63 json_result = result.toString();
michael@0 64 if (typeof(expected) == "function")
michael@0 65 json_expected = expected.toString();
michael@0 66 is(json_result, json_expected,
michael@0 67 "npnEvaluateTest returned an unexpected value");
michael@0 68 is(typeof(result), typeof(expected),
michael@0 69 "npnEvaluateTest return value was of unexpected type");
michael@0 70 var success = (json_result == json_expected &&
michael@0 71 typeof(result) == typeof(expected));
michael@0 72 $("verbose").appendChild(
michael@0 73 createEl('span',null, (success ? "pass" : "fail") + ": eval(" + test[0] + ")"));
michael@0 74 $("verbose").appendChild(
michael@0 75 createEl('span', null," == " + json_result + "(" +
michael@0 76 typeof(result) + "), expected " + json_expected + "(" +
michael@0 77 typeof(expected) + ")"));
michael@0 78 $("verbose").appendChild(
michael@0 79 createEl('br')
michael@0 80 );
michael@0 81 }
michael@0 82
michael@0 83 is(document.getElementById('testdiv').innerHTML, "Hello world!",
michael@0 84 "innerHTML not set correctly via NPN_Evaluate");
michael@0 85
michael@0 86 SimpleTest.finish();
michael@0 87 }
michael@0 88 </script>
michael@0 89
michael@0 90 <p id="display"></p>
michael@0 91
michael@0 92 <embed id="plugin1" type="application/x-test" width="400" height="100">
michael@0 93 </embed>
michael@0 94
michael@0 95 <div id="verbose">
michael@0 96 </div>
michael@0 97 <div id="testdiv">
michael@0 98 </div>
michael@0 99 </body>
michael@0 100 </html>

mercurial