dom/plugins/test/mochitest/test_npruntime_npninvokedefault.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

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

mercurial