test_getweakmapkeys.xul 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
  3. <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
  4. <!--
  5. https://bugzilla.mozilla.org/show_bug.cgi?id=688277
  6. -->
  7. <window title="Mozilla Bug "
  8. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  9. <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  10. <!-- test results are displayed in the html:body -->
  11. <body xmlns="http://www.w3.org/1999/xhtml">
  12. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id="
  13. target="_blank">Mozilla Bug 688277</a>
  14. </body>
  15. <!-- test code goes here -->
  16. <script type="application/javascript">
  17. <![CDATA[
  18. /** Test for Bug 688277 **/
  19. let Cu = Components.utils;
  20. /* Fail gracefully if junk is passed in. */
  21. is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(11), undefined,
  22. "nondeterministicGetWeakMapKeys should return undefined for non-objects");
  23. is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys({}), undefined,
  24. "nondeterministicGetWeakMapKeys should return undefined for non-weakmap objects");
  25. is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(null), undefined,
  26. "nondeterministicGetWeakMapKeys should return undefined for null");
  27. /* return an empty array for an empty WeakMap */
  28. let mempty = new WeakMap();
  29. is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(mempty).length, 0,
  30. "nondeterministicGetWeakMapKeys should return empty array for empty weakmap");
  31. /* Test freeing/nonfreeing. */
  32. let m = new WeakMap();
  33. let liveKeys = new Array();
  34. let add_elements = function () {
  35. let k1 = {};
  36. m.set(k1, "live1");
  37. liveKeys.push(k1);
  38. let k2 = {};
  39. m.set(k2, "dead1");
  40. let k = {};
  41. m.set(k, k); /* simple cycle */
  42. };
  43. add_elements();
  44. Cu.schedulePreciseGC(function () {
  45. let keys = ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(m);
  46. is(liveKeys.length, 1, "Wrong number of live keys.");
  47. is(keys.length, 1, "Should have one weak map key.");
  48. is(m.get(keys[0]), "live1", "live1 should be live");
  49. SimpleTest.finish();
  50. });
  51. SimpleTest.waitForExplicitFinish();
  52. ]]>
  53. </script>
  54. </window>