1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/plugins/test/mochitest/test_npruntime_npnevaluate.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +<html> 1.5 +<head> 1.6 + <title>NPN_Evaluate Tests</title> 1.7 + <script type="text/javascript" 1.8 + src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <script type="text/javascript" src="utils.js"></script> 1.10 + <link rel="stylesheet" type="text/css" 1.11 + href="/tests/SimpleTest/test.css" /> 1.12 +</head> 1.13 +<body onload="runTests()"> 1.14 + <script class="testbody" type="application/javascript"> 1.15 + 1.16 + SimpleTest.waitForExplicitFinish(); 1.17 + setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED); 1.18 + 1.19 + // global test function 1.20 + function testMe(arg) { 1.21 + var result = arg+arg; 1.22 + for (var i = 1; i < arguments.length; i++) { 1.23 + result += arguments[i] + arguments[i]; 1.24 + } 1.25 + return result; 1.26 + } 1.27 + 1.28 + //// 1.29 + // This test exercises NPN_Evaluate using the test plugin's 1.30 + // npnEvaluateTest method. This method calls NPN_Evaluate on 1.31 + // a string argument passed to it, and returns the eval result. 1.32 + // The array below drives the tests; each array member has two 1.33 + // members: the first is a string to eval, and the second is 1.34 + // the expected result of the eval. 1.35 + // 1.36 + 1.37 + function runTests() { 1.38 + var tests = [ 1.39 + ["3", 3], 1.40 + ["3 + 3", 6], 1.41 + ["'3'", "3"], 1.42 + ["function test() { return 3; } test();", 3], 1.43 + ["testMe(3)", 6], 1.44 + ["testMe(new Object(3))", 6], 1.45 + ["new Object(3)", new Object(3)], 1.46 + ["new Array(1, 2, 3, 4)", [1, 2, 3, 4]], 1.47 + ["document.getElementById('display')", 1.48 + document.getElementById("display")], 1.49 + ["encodeURI('a = b')", "a%20=%20b"], 1.50 + ["document.getElementById('testdiv').innerHTML = 'Hello world!'", 1.51 + "Hello world!"], 1.52 + ["function test2() { var x = {a: '1', b: '2'}; return x; } test2();", 1.53 + {a: '1', b: '2'}], 1.54 + ]; 1.55 + 1.56 + var plugin = document.getElementById("plugin1"); 1.57 + 1.58 + // Test calling NPN_Evaluate from within plugin code. 1.59 + for (var test of tests) { 1.60 + var expected = test[1]; 1.61 + var result = plugin.npnEvaluateTest(test[0]); 1.62 + // serialize the two values for easy comparison 1.63 + var json_expected = JSON.stringify(expected); 1.64 + var json_result = JSON.stringify(result); 1.65 + if (typeof(result) == "function") 1.66 + json_result = result.toString(); 1.67 + if (typeof(expected) == "function") 1.68 + json_expected = expected.toString(); 1.69 + is(json_result, json_expected, 1.70 + "npnEvaluateTest returned an unexpected value"); 1.71 + is(typeof(result), typeof(expected), 1.72 + "npnEvaluateTest return value was of unexpected type"); 1.73 + var success = (json_result == json_expected && 1.74 + typeof(result) == typeof(expected)); 1.75 + $("verbose").appendChild( 1.76 + createEl('span',null, (success ? "pass" : "fail") + ": eval(" + test[0] + ")")); 1.77 + $("verbose").appendChild( 1.78 + createEl('span', null," == " + json_result + "(" + 1.79 + typeof(result) + "), expected " + json_expected + "(" + 1.80 + typeof(expected) + ")")); 1.81 + $("verbose").appendChild( 1.82 + createEl('br') 1.83 + ); 1.84 + } 1.85 + 1.86 + is(document.getElementById('testdiv').innerHTML, "Hello world!", 1.87 + "innerHTML not set correctly via NPN_Evaluate"); 1.88 + 1.89 + SimpleTest.finish(); 1.90 + } 1.91 + </script> 1.92 + 1.93 + <p id="display"></p> 1.94 + 1.95 + <embed id="plugin1" type="application/x-test" width="400" height="100"> 1.96 + </embed> 1.97 + 1.98 + <div id="verbose"> 1.99 + </div> 1.100 + <div id="testdiv"> 1.101 + </div> 1.102 + </body> 1.103 + </html>