Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <html> |
michael@0 | 2 | <head> |
michael@0 | 3 | <title>NPN_Invoke_Default 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_InvokeDefault using the test plugin's |
michael@0 | 27 | // npnInvokeDefaultTest method. This method invokes an object |
michael@0 | 28 | // with a single parameter, and returns the result of the invocation. |
michael@0 | 29 | // The test list below is used to drive the tests. Each member of the |
michael@0 | 30 | // array contains three members: the object to invoke, an argument to |
michael@0 | 31 | // invoke it with, and the expected result of the invocation. |
michael@0 | 32 | // |
michael@0 | 33 | var tests = [ |
michael@0 | 34 | // Number object |
michael@0 | 35 | ["Number", 3, 3], |
michael@0 | 36 | ["Number", "3", 3], |
michael@0 | 37 | ["Number", "0x20", 32], |
michael@0 | 38 | ["Number", "three", Number.NaN], |
michael@0 | 39 | ["Number", "1e+3", 1000], |
michael@0 | 40 | ["Number", 5.6612, 5.6612], |
michael@0 | 41 | // Array object |
michael@0 | 42 | ["Array", 3, Array(3)], |
michael@0 | 43 | // Boolean object |
michael@0 | 44 | ["Boolean", 0, false], |
michael@0 | 45 | ["Boolean", null, false], |
michael@0 | 46 | ["Boolean", "", false], |
michael@0 | 47 | ["Boolean", false, false], |
michael@0 | 48 | ["Boolean", true, true], |
michael@0 | 49 | ["Boolean", "true", true], |
michael@0 | 50 | ["Boolean", "false", true], |
michael@0 | 51 | ["Boolean", new Boolean(false), true], |
michael@0 | 52 | ["Boolean", { "value": false }, true], |
michael@0 | 53 | // Function object |
michael@0 | 54 | ["Function", "return 3", Function("return 3")], |
michael@0 | 55 | ["Function", "window.alert('test')", Function("window.alert('test')")], |
michael@0 | 56 | // Object object |
michael@0 | 57 | ["Object", undefined, Object()], |
michael@0 | 58 | ["Object", null, Object()], |
michael@0 | 59 | ["Object", true, new Boolean(true)], |
michael@0 | 60 | ["Object", Boolean(), new Boolean(false)], |
michael@0 | 61 | ["Object", "a string", new String("a string")], |
michael@0 | 62 | ["Object", 3.14, new Number(3.14)], |
michael@0 | 63 | ["Object", { "key1": "test", "key2": 15 }, { "key1": "test", "key2": 15 }], |
michael@0 | 64 | ["Object", [1, 3, 5, 7, 9, 11, 13, 17], [1, 3, 5, 7, 9, 11, 13, 17]], |
michael@0 | 65 | // RegExp object |
michael@0 | 66 | ["RegExp", "...", RegExp("...")], |
michael@0 | 67 | // String object |
michael@0 | 68 | ["String", "testing", "testing"], |
michael@0 | 69 | ["String", "test\u0020me", "test me"], |
michael@0 | 70 | ["String", "314", "314"], |
michael@0 | 71 | ["String", "true", "true"], |
michael@0 | 72 | ["String", "null", "null"], |
michael@0 | 73 | ["String", "2 + 2", String("2 + 2")], |
michael@0 | 74 | ["String", ""], |
michael@0 | 75 | // global functions |
michael@0 | 76 | ["testMe", 3, 6], |
michael@0 | 77 | ["testMe", "string", [1,2], "stringstring1,21,2"], |
michael@0 | 78 | ["testMe", "me", "meme"], |
michael@0 | 79 | ["testMe", undefined, Number.NaN], |
michael@0 | 80 | ["testMe", [1, 2], "1,21,2"], |
michael@0 | 81 | ["testMe", 3, 4, 14], |
michael@0 | 82 | ["isNaN", "junk", true], |
michael@0 | 83 | ["parseInt", "156", 156], |
michael@0 | 84 | ["encodeURI", "a = b", "a%20=%20b"], |
michael@0 | 85 | ]; |
michael@0 | 86 | |
michael@0 | 87 | function runTests() { |
michael@0 | 88 | var plugin = document.getElementById("plugin1"); |
michael@0 | 89 | |
michael@0 | 90 | // Test calling NPN_InvokeDefault from within plugin code. |
michael@0 | 91 | for (var test of tests) { |
michael@0 | 92 | var result; |
michael@0 | 93 | var expected = test[test.length - 1]; |
michael@0 | 94 | switch (test.length) { |
michael@0 | 95 | case 2: |
michael@0 | 96 | result = plugin.npnInvokeDefaultTest(test[0]); |
michael@0 | 97 | break; |
michael@0 | 98 | case 3: |
michael@0 | 99 | result = plugin.npnInvokeDefaultTest(test[0], test[1]); |
michael@0 | 100 | break; |
michael@0 | 101 | case 4: |
michael@0 | 102 | result = plugin.npnInvokeDefaultTest(test[0], test[1], test[2]); |
michael@0 | 103 | break; |
michael@0 | 104 | } |
michael@0 | 105 | // serialize the two values for easy |
michael@0 | 106 | var json_expected = JSON.stringify(expected); |
michael@0 | 107 | var json_result = JSON.stringify(result); |
michael@0 | 108 | if (typeof(result) == "function") |
michael@0 | 109 | json_result = result.toString(); |
michael@0 | 110 | if (typeof(test[2]) == "function") |
michael@0 | 111 | json_expected = expected.toString(); |
michael@0 | 112 | is(json_result, json_expected, |
michael@0 | 113 | "npnInvokeDefault returned an unexpected value"); |
michael@0 | 114 | is(typeof(result), typeof(expected), |
michael@0 | 115 | "npnInvokeDefaultTest return value was of unexpected type"); |
michael@0 | 116 | var success = (json_result == json_expected && |
michael@0 | 117 | typeof(result) == typeof(expected)); |
michael@0 | 118 | $("verbose").appendChild( |
michael@0 | 119 | createEl('span', null, ((success ? "pass" : "fail") + ": " + test[0] + "("))); |
michael@0 | 120 | for (var i = 1; i < test.length - 1; i++) { |
michael@0 | 121 | $("verbose").appendChild( |
michael@0 | 122 | createEl('span', null, (JSON.stringify(test[i]) + (i < test.length - 2 ? "," : "")))); |
michael@0 | 123 | } |
michael@0 | 124 | $("verbose").appendChild( |
michael@0 | 125 | createEl('span', null, (") == " + json_result + "(" + |
michael@0 | 126 | typeof(result) + "), expected " + json_expected + "(" + |
michael@0 | 127 | typeof(expected) + ")"))); |
michael@0 | 128 | $("verbose").appendChild(createEl('br')); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // Test calling the invokedefault method of plugin-defined object |
michael@0 | 132 | is(plugin(), "Test Plug-in", |
michael@0 | 133 | "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); |
michael@0 | 134 | is(plugin(1), "Test Plug-in;1", |
michael@0 | 135 | "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); |
michael@0 | 136 | is(plugin("test"), "Test Plug-in;test", |
michael@0 | 137 | "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); |
michael@0 | 138 | is(plugin(undefined, -1, null), "Test Plug-in;undefined;-1;null", |
michael@0 | 139 | "calling NPN_InvokeDefault on plugin-defined Object doesn't work"); |
michael@0 | 140 | |
michael@0 | 141 | SimpleTest.finish(); |
michael@0 | 142 | } |
michael@0 | 143 | </script> |
michael@0 | 144 | |
michael@0 | 145 | <p id="display"></p> |
michael@0 | 146 | |
michael@0 | 147 | <embed id="plugin1" type="application/x-test" width="400" height="100"> |
michael@0 | 148 | </embed> |
michael@0 | 149 | |
michael@0 | 150 | <div id="verbose"> |
michael@0 | 151 | </div> |
michael@0 | 152 | </body> |
michael@0 | 153 | </html> |