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

     1 <html>
     2 <head>
     3   <title>NPN_Evaluate 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_Evaluate using the test plugin's
    27   // npnEvaluateTest method.  This method calls NPN_Evaluate on 
    28   // a string argument passed to it, and returns the eval result.
    29   // The array below drives the tests; each array member has two
    30   // members:  the first is a string to eval, and the second is 
    31   // the expected result of the eval.
    32   //
    34   function runTests() {
    35     var tests = [
    36       ["3", 3],
    37       ["3 + 3", 6],
    38       ["'3'", "3"],
    39       ["function test() { return 3; } test();", 3],
    40       ["testMe(3)", 6],
    41       ["testMe(new Object(3))", 6],
    42       ["new Object(3)", new Object(3)],
    43       ["new Array(1, 2, 3, 4)", [1, 2, 3, 4]],
    44       ["document.getElementById('display')", 
    45           document.getElementById("display")],
    46       ["encodeURI('a = b')", "a%20=%20b"],
    47       ["document.getElementById('testdiv').innerHTML = 'Hello world!'", 
    48           "Hello world!"],
    49       ["function test2() { var x = {a: '1', b: '2'}; return x; } test2();", 
    50           {a: '1', b: '2'}],
    51     ];
    53     var plugin = document.getElementById("plugin1");
    55     // Test calling NPN_Evaluate from within plugin code.
    56     for (var test of tests) {
    57       var expected = test[1];
    58       var result = plugin.npnEvaluateTest(test[0]);
    59       // serialize the two values for easy comparison
    60       var json_expected = JSON.stringify(expected);
    61       var json_result = JSON.stringify(result);
    62       if (typeof(result) == "function")
    63           json_result = result.toString();
    64       if (typeof(expected) == "function")
    65           json_expected = expected.toString();
    66       is(json_result, json_expected, 
    67           "npnEvaluateTest returned an unexpected value");
    68       is(typeof(result), typeof(expected), 
    69           "npnEvaluateTest return value was of unexpected type");
    70       var success = (json_result == json_expected && 
    71           typeof(result) == typeof(expected));
    72       $("verbose").appendChild(
    73           createEl('span',null, (success ? "pass" : "fail") + ": eval(" + test[0] + ")"));
    74       $("verbose").appendChild(
    75           createEl('span', null," == " + json_result + "(" + 
    76           typeof(result) + "), expected " + json_expected + "(" + 
    77           typeof(expected) + ")"));
    78       $("verbose").appendChild(
    79           createEl('br')
    80           );
    81     }
    83     is(document.getElementById('testdiv').innerHTML, "Hello world!",
    84       "innerHTML not set correctly via NPN_Evaluate");
    86     SimpleTest.finish();
    87   }
    88   </script>
    90   <p id="display"></p>
    92   <embed id="plugin1" type="application/x-test" width="400" height="100">
    93   </embed>
    95   <div id="verbose">
    96   </div>
    97   <div id="testdiv">
    98   </div>
    99  </body>
   100  </html>

mercurial