summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/chrome/test_paris_weakmap_keys.xul
blob: ac7ff041dfb0698e4e005baeaae41581e34e66d4 (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=777385
-->
<window title="Mozilla Bug 777385"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <div></div>
  <div id="mydivname"></div>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id="
     target="_blank">Mozilla Bug 777385</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  /** Test for Bug 777385 **/

  let Cu = Components.utils;

  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();

  // RGBColor is a non-nsISupports refCounted class using WebIDL bindings.

  // non-nsISupports cycle-collected classes should fail as weak map keys.
  let context = window.getComputedStyle(document.documentElement, null).getPropertyCSSValue("color").getRGBColorValue();
  let contextFail = false;
  try {
    live_map.set(context, 2);
  } catch (e) {
    contextFail = true;
  }

  ok(contextFail, "Cycle collected non-nsISupports classes aren't allowed as weak map keys.");

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

  SimpleTest.waitForExplicitFinish();

  Cu.schedulePreciseGC(function () {
    SpecialPowers.DOMWindowUtils.cycleCollect();
    SpecialPowers.DOMWindowUtils.garbageCollect();
    SpecialPowers.DOMWindowUtils.garbageCollect();

    is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(live_map).length, 1,
       "Live nsISupports new DOM bindings wrappercached native weak map key should not be removed.");

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

    SimpleTest.finish();
  });

  ]]>
  </script>
</window>