summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_paris_weakmap_keys.html
blob: 1b786138d44562f31022e30e9e937f3d23750947 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=777385
-->
<head>
  <meta charset="utf-8">
  <title>Tests for WebIDL objects as weak map keys</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 777385 **/

  SimpleTest.waitForExplicitFinish();

  // We wait to run this until the load event because it needs to access an element.
  function go() {

  let live_map = new WeakMap;

  let get_div_style = function () {
    return document.getElementById("mydivname").style;
  }

  let make_live_map = function () {
    let my_div_style = get_div_style();
    let div_fail = false;
    try {
      live_map.set(my_div_style, 12345);
    } catch (e) {
      div_fail = true;
    }
    ok(!div_fail, "Using elem.style as a weak map key should not produce an exception.");

    is(live_map.get(get_div_style()), 12345, "Live map should have live style with right value before GC.");

  }

  make_live_map();

  let tf = new TestFunctions;

  let add_non_isupports2 = function () {
    let testKey = tf.wrapperCachedNonISupportsObject;

    let testFail = false;
    try {
      live_map.set(testKey, 23456);
    } catch (e) {
      testFail = true;
    }

    ok(!testFail, "Using a wrapper cached non-nsISupports class as a weak map key should not produce an exception.");

    is(live_map.get(testKey), 23456, "Live map should have wrapper cached non-nsISupports class right value before GC.");
  }

  add_non_isupports2();


  /* Set up for running precise GC/CC then check the results. */

  SpecialPowers.exactGC(function () {
    SpecialPowers.forceCC();
    SpecialPowers.forceGC();
    SpecialPowers.forceGC();

    is(SpecialPowers.nondeterministicGetWeakMapKeys(live_map).length, 2,
       "Live WebIDL bindings keys should not be removed from a weak map.");

    is(live_map.get(get_div_style()), 12345, "Live weak map should have live style with right value after GC.");
    is(live_map.get(tf.wrapperCachedNonISupportsObject), 23456,
       "Live weak map should have live wrapper cached non-nsISupports class with right value after GC.");

    SimpleTest.finish();
  });

  }

  SimpleTest.waitForExplicitFinish();

  addLoadEvent(function() {
    SpecialPowers.pushPrefEnv({set: [['dom.expose_test_interfaces', true]]},
                              go);
  });
  </script>
</head>
<div></div>
<div id="mydivname"></div>
<body>
<p id="display"></p>
</body>
</html>