summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/test_npruntime_npnevaluate.html
blob: 4a1c22978baec3bc95376f40af6e3de8ccaa9f19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<html>
<head>
  <title>NPN_Evaluate Tests</title>
  <script type="text/javascript" 
          src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="plugin-utils.js"></script>
  <link rel="stylesheet" type="text/css" 
        href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests()">
  <script class="testbody" type="application/javascript">
  
  SimpleTest.waitForExplicitFinish();
  setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED);
  
  // global test function
  function testMe(arg) {
    var result = arg+arg;
    for (var i = 1; i < arguments.length; i++) {
      result += arguments[i] + arguments[i];
    }
    return result;
  }

  ////
  // This test exercises NPN_Evaluate using the test plugin's
  // npnEvaluateTest method.  This method calls NPN_Evaluate on 
  // a string argument passed to it, and returns the eval result.
  // The array below drives the tests; each array member has two
  // members:  the first is a string to eval, and the second is 
  // the expected result of the eval.
  //

  function runTests() {
    var tests = [
      ["3", 3],
      ["3 + 3", 6],
      ["'3'", "3"],
      ["function test() { return 3; } test();", 3],
      ["testMe(3)", 6],
      ["testMe(new Object(3))", 6],
      ["new Object(3)", new Object(3)],
      ["new Array(1, 2, 3, 4)", [1, 2, 3, 4]],
      ["document.getElementById('display')", 
          document.getElementById("display")],
      ["encodeURI('a = b')", "a%20=%20b"],
      ["document.getElementById('testdiv').innerHTML = 'Hello world!'", 
          "Hello world!"],
      ["function test2() { var x = {a: '1', b: '2'}; return x; } test2();", 
          {a: '1', b: '2'}],
      ["(function() { var ret; try { win = window.open(); win.document.writeln('wibble'); ret = 'no error' } catch(e) { ret = e.name; } win.close(); return ret; })()", "no error"],
    ];

    var plugin = document.getElementById("plugin1");

    // Test calling NPN_Evaluate from within plugin code.
    for (var test of tests) {
      var expected = test[1];
      var result = plugin.npnEvaluateTest(test[0]);
      // serialize the two values for easy comparison
      var json_expected = JSON.stringify(expected);
      var json_result = JSON.stringify(result);
      if (typeof(result) == "function")
          json_result = result.toString();
      if (typeof(expected) == "function")
          json_expected = expected.toString();
      is(json_result, json_expected, 
          "npnEvaluateTest returned an unexpected value");
      is(typeof(result), typeof(expected), 
          "npnEvaluateTest return value was of unexpected type");
      var success = (json_result == json_expected && 
          typeof(result) == typeof(expected));
      $("verbose").appendChild(
          createEl('span',null, (success ? "pass" : "fail") + ": eval(" + test[0] + ")"));
      $("verbose").appendChild(
          createEl('span', null," == " + json_result + "(" + 
          typeof(result) + "), expected " + json_expected + "(" + 
          typeof(expected) + ")"));
      $("verbose").appendChild(
          createEl('br')
          );
    }
    
    is(document.getElementById('testdiv').innerHTML, "Hello world!",
      "innerHTML not set correctly via NPN_Evaluate");
    
    SimpleTest.finish();
  }
  </script>

  <p id="display"></p>

  <embed id="plugin1" type="application/x-test" width="400" height="100">
  </embed>

  <div id="verbose">
  </div>
  <div id="testdiv">
  </div>
 </body>
 </html>