1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/plugins/test/mochitest/test_npruntime_npninvokedefault.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +<html> 1.5 +<head> 1.6 + <title>NPN_Invoke_Default 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_InvokeDefault using the test plugin's 1.30 + // npnInvokeDefaultTest method. This method invokes an object 1.31 + // with a single parameter, and returns the result of the invocation. 1.32 + // The test list below is used to drive the tests. Each member of the 1.33 + // array contains three members: the object to invoke, an argument to 1.34 + // invoke it with, and the expected result of the invocation. 1.35 + // 1.36 + var tests = [ 1.37 + // Number object 1.38 + ["Number", 3, 3], 1.39 + ["Number", "3", 3], 1.40 + ["Number", "0x20", 32], 1.41 + ["Number", "three", Number.NaN], 1.42 + ["Number", "1e+3", 1000], 1.43 + ["Number", 5.6612, 5.6612], 1.44 + // Array object 1.45 + ["Array", 3, Array(3)], 1.46 + // Boolean object 1.47 + ["Boolean", 0, false], 1.48 + ["Boolean", null, false], 1.49 + ["Boolean", "", false], 1.50 + ["Boolean", false, false], 1.51 + ["Boolean", true, true], 1.52 + ["Boolean", "true", true], 1.53 + ["Boolean", "false", true], 1.54 + ["Boolean", new Boolean(false), true], 1.55 + ["Boolean", { "value": false }, true], 1.56 + // Function object 1.57 + ["Function", "return 3", Function("return 3")], 1.58 + ["Function", "window.alert('test')", Function("window.alert('test')")], 1.59 + // Object object 1.60 + ["Object", undefined, Object()], 1.61 + ["Object", null, Object()], 1.62 + ["Object", true, new Boolean(true)], 1.63 + ["Object", Boolean(), new Boolean(false)], 1.64 + ["Object", "a string", new String("a string")], 1.65 + ["Object", 3.14, new Number(3.14)], 1.66 + ["Object", { "key1": "test", "key2": 15 }, { "key1": "test", "key2": 15 }], 1.67 + ["Object", [1, 3, 5, 7, 9, 11, 13, 17], [1, 3, 5, 7, 9, 11, 13, 17]], 1.68 + // RegExp object 1.69 + ["RegExp", "...", RegExp("...")], 1.70 + // String object 1.71 + ["String", "testing", "testing"], 1.72 + ["String", "test\u0020me", "test me"], 1.73 + ["String", "314", "314"], 1.74 + ["String", "true", "true"], 1.75 + ["String", "null", "null"], 1.76 + ["String", "2 + 2", String("2 + 2")], 1.77 + ["String", ""], 1.78 + // global functions 1.79 + ["testMe", 3, 6], 1.80 + ["testMe", "string", [1,2], "stringstring1,21,2"], 1.81 + ["testMe", "me", "meme"], 1.82 + ["testMe", undefined, Number.NaN], 1.83 + ["testMe", [1, 2], "1,21,2"], 1.84 + ["testMe", 3, 4, 14], 1.85 + ["isNaN", "junk", true], 1.86 + ["parseInt", "156", 156], 1.87 + ["encodeURI", "a = b", "a%20=%20b"], 1.88 + ]; 1.89 + 1.90 + function runTests() { 1.91 + var plugin = document.getElementById("plugin1"); 1.92 + 1.93 + // Test calling NPN_InvokeDefault from within plugin code. 1.94 + for (var test of tests) { 1.95 + var result; 1.96 + var expected = test[test.length - 1]; 1.97 + switch (test.length) { 1.98 + case 2: 1.99 + result = plugin.npnInvokeDefaultTest(test[0]); 1.100 + break; 1.101 + case 3: 1.102 + result = plugin.npnInvokeDefaultTest(test[0], test[1]); 1.103 + break; 1.104 + case 4: 1.105 + result = plugin.npnInvokeDefaultTest(test[0], test[1], test[2]); 1.106 + break; 1.107 + } 1.108 + // serialize the two values for easy 1.109 + var json_expected = JSON.stringify(expected); 1.110 + var json_result = JSON.stringify(result); 1.111 + if (typeof(result) == "function") 1.112 + json_result = result.toString(); 1.113 + if (typeof(test[2]) == "function") 1.114 + json_expected = expected.toString(); 1.115 + is(json_result, json_expected, 1.116 + "npnInvokeDefault returned an unexpected value"); 1.117 + is(typeof(result), typeof(expected), 1.118 + "npnInvokeDefaultTest return value was of unexpected type"); 1.119 + var success = (json_result == json_expected && 1.120 + typeof(result) == typeof(expected)); 1.121 + $("verbose").appendChild( 1.122 + createEl('span', null, ((success ? "pass" : "fail") + ": " + test[0] + "("))); 1.123 + for (var i = 1; i < test.length - 1; i++) { 1.124 + $("verbose").appendChild( 1.125 + createEl('span', null, (JSON.stringify(test[i]) + (i < test.length - 2 ? "," : "")))); 1.126 + } 1.127 + $("verbose").appendChild( 1.128 + createEl('span', null, (") == " + json_result + "(" + 1.129 + typeof(result) + "), expected " + json_expected + "(" + 1.130 + typeof(expected) + ")"))); 1.131 + $("verbose").appendChild(createEl('br')); 1.132 + } 1.133 + 1.134 + // Test calling the invokedefault method of plugin-defined object 1.135 + is(plugin(), "Test Plug-in", 1.136 + "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); 1.137 + is(plugin(1), "Test Plug-in;1", 1.138 + "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); 1.139 + is(plugin("test"), "Test Plug-in;test", 1.140 + "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); 1.141 + is(plugin(undefined, -1, null), "Test Plug-in;undefined;-1;null", 1.142 + "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); 1.143 + 1.144 + SimpleTest.finish(); 1.145 + } 1.146 + </script> 1.147 + 1.148 + <p id="display"></p> 1.149 + 1.150 + <embed id="plugin1" type="application/x-test" width="400" height="100"> 1.151 + </embed> 1.152 + 1.153 + <div id="verbose"> 1.154 + </div> 1.155 + </body> 1.156 + </html>