<?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"?>
<window title="about:memory"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <!-- This file uses fake memory reporters to test the presentation of memory
       reports in about:memory.  test_memoryReporters.xul uses the real
       memory reporters to test whether the memory reporters are producing
       sensible results. -->

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml"></body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  "use strict";

  SimpleTest.expectAssertions(27);

  const Cc = Components.classes;
  const Ci = Components.interfaces;
  const Cr = Components.results;
  let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].
            getService(Ci.nsIMemoryReporterManager);

  // Hide all the real reporters;  we'll restore them at the end.
  mgr.blockRegistrationAndHideExistingReporters();

  // Setup various fake-but-deterministic reporters.
  const KB = 1024;
  const MB = KB * KB;
  const NONHEAP = Ci.nsIMemoryReporter.KIND_NONHEAP;
  const HEAP    = Ci.nsIMemoryReporter.KIND_HEAP;
  const OTHER   = Ci.nsIMemoryReporter.KIND_OTHER;

  const BYTES = Ci.nsIMemoryReporter.UNITS_BYTES;
  const COUNT = Ci.nsIMemoryReporter.UNITS_COUNT;
  const COUNT_CUMULATIVE = Ci.nsIMemoryReporter.UNITS_COUNT_CUMULATIVE;
  const PERCENTAGE = Ci.nsIMemoryReporter.UNITS_PERCENTAGE;

  let fakeReporters = [
    { collectReports: function(aCbObj, aClosure, aAnonymize) {
        function f(aP, aK, aU, aA) {
          aCbObj.callback("", aP, aK, aU, aA, "Desc.", aClosure);
        }
        f("heap-allocated",     OTHER,   BYTES, 500 * MB);
        f("heap-unallocated",   OTHER,   BYTES, 100 * MB);
        f("explicit/a",         HEAP,    BYTES, 222 * MB);
        f("explicit/b/a",       HEAP,    BYTES,  85 * MB);
        f("explicit/b/b",       HEAP,    BYTES,  75 * MB);
        f("explicit/b/c/a",     HEAP,    BYTES,  70 * MB);
        f("explicit/b/c/b",     HEAP,    BYTES,   2 * MB); // omitted
        f("explicit/g/a",       HEAP,    BYTES,   6 * MB);
        f("explicit/g/b",       HEAP,    BYTES,   5 * MB);
        f("explicit/g/other",   HEAP,    BYTES,   4 * MB);
        // A degenerate tree with the same name as a non-degenerate tree should
        // work ok.
        f("explicit",           OTHER,   BYTES, 888 * MB);
        f("other1/a/b",         OTHER,   BYTES, 111 * MB);
        f("other1/c/d",         OTHER,   BYTES,  22 * MB);
        f("other1/c/e",         OTHER,   BYTES,  33 * MB);
        f("other4",             OTHER,   COUNT_CUMULATIVE, 777);
        f("other4",             OTHER,   COUNT_CUMULATIVE, 111);
        f("other3/a/b/c/d/e",   OTHER,   PERCENTAGE, 2000);
        f("other3/a/b/c/d/f",   OTHER,   PERCENTAGE, 10);
        f("other3/a/b/c/d/g",   OTHER,   PERCENTAGE, 5);
        f("other3/a/b/c/d/g",   OTHER,   PERCENTAGE, 5);
        // Check that a rounded-up-to-100.00% value is shown as "100.0%" (i.e. one
        // decimal point).
        f("other6/big",         OTHER,   COUNT, 99999);
        f("other6/small",       OTHER,   COUNT, 1);
        // Check that a 0 / 0 is handled correctly.
        f("other7/zero",        OTHER,   BYTES, 0);
        // These compartments ones shouldn't be displayed.
        f("compartments/user/foo",   OTHER, COUNT, 1);
        f("compartments/system/foo", OTHER, COUNT, 1);
      }
    },
    { collectReports: function(aCbObj, aClosure, aAnonymize) {
        function f(aP, aK, aU, aA) {
          aCbObj.callback("", aP, aK, aU, aA, "Desc.", aClosure);
        }
        f("explicit/c/d",     NONHEAP, BYTES,  13 * MB);
        f("explicit/c/d",     NONHEAP, BYTES,  10 * MB); // dup
        f("explicit/c/other", NONHEAP, BYTES,  77 * MB);
        f("explicit/cc",      NONHEAP, BYTES,  13 * MB);
        f("explicit/cc",      NONHEAP, BYTES,  10 * MB); // dup
        f("explicit/d",       NONHEAP, BYTES, 499 * KB); // omitted
        f("explicit/e",       NONHEAP, BYTES, 100 * KB); // omitted
        f("explicit/f/g/h/i", HEAP,    BYTES,  10 * MB);
        f("explicit/f/g/h/j", HEAP,    BYTES,  10 * MB);
      }
    },
    { collectReports: function(aCbObj, aClosure, aAnonymize) {
        function f(aP, aK, aU, aA) {
          aCbObj.callback("", aP, aK, aU, aA, "Desc.", aClosure);
        }
        f("other3",           OTHER,   COUNT, 777);
        f("other2",           OTHER,   BYTES, 222 * MB);
        f("perc2",            OTHER,   PERCENTAGE, 10000);
        f("perc1",            OTHER,   PERCENTAGE, 4567);
        f("compartments/user/https:\\\\very-long-url.com\\very-long\\oh-so-long\\really-quite-long.html?a=2&b=3&c=4&d=5&e=abcdefghijklmnopqrstuvwxyz&f=123456789123456789123456789", OTHER, COUNT, 1);
      }
    },
    { collectReports: function(aCbObj, aClosure, aAnonymize) {
        function f(aP) {
          aCbObj.callback("", aP, OTHER, COUNT, 1, "Desc.", aClosure);
        }
        f("compartments/user/bar");
        f("compartments/system/bar");
      }
    }
  ];
  for (let i = 0; i < fakeReporters.length; i++) {
    mgr.registerStrongReporterEvenIfBlocked(fakeReporters[i]);
  }

  // The main process always comes first when we display about:memory.  The
  // remaining processes are sorted by their |resident| values (starting with
  // the largest).  Processes without a |resident| memory reporter are saved
  // for the end.
  let fakeReporters2 = [
    { collectReports: function(aCbObj, aClosure, aAnonymize) {
        function f(aP1, aP2, aK, aU, aA) {
          aCbObj.callback(aP1, aP2, aK, aU, aA, "Desc.", aClosure);
        }
        f("2nd", "heap-allocated",  OTHER,   BYTES,1000* MB);
        f("2nd", "heap-unallocated",OTHER,   BYTES,100 * MB);
        f("2nd", "explicit/a/b/c",  HEAP,    BYTES,497 * MB);
        f("2nd", "explicit/a/b/c",  HEAP,    BYTES,  1 * MB); // dup: merge
        f("2nd", "explicit/a/b/c",  HEAP,    BYTES,  1 * MB); // dup: merge
        f("2nd", "explicit/flip\\the\\backslashes",
                                    HEAP,    BYTES,200 * MB);
        f("2nd", "explicit/compartment(compartment-url)",
                                    HEAP,    BYTES,200 * MB);
        f("2nd", "other0",          OTHER,   BYTES,666 * MB);
        f("2nd", "other1",          OTHER,   BYTES,111 * MB);

        // Check that we can handle "heap-allocated" not being present.
        f("3rd", "explicit/a/b",    HEAP,    BYTES,333 * MB);
        f("3rd", "explicit/a/c",    HEAP,    BYTES,444 * MB);
        f("3rd", "other1",          OTHER,   BYTES,  1 * MB);
        f("3rd", "resident",        OTHER,   BYTES,100 * MB);

        // Invalid values (negative, too-big) should be identified.
        f("4th", "heap-allocated",   OTHER,   BYTES,100 * MB);
        f("4th", "resident",         OTHER,   BYTES,200 * MB);
        f("4th", "explicit/js/compartment(http:\\\\too-big.com\\)/stuff",
                                     HEAP,    BYTES,150 * MB);
        f("4th", "explicit/ok",      HEAP,    BYTES,  5 * MB);
        f("4th", "explicit/neg1",    NONHEAP, BYTES, -2 * MB);
        // -111 becomes "-0.00MB" in non-verbose mode, and getting the negative
        // sign in there correctly is non-trivial.
        f("4th",  "other1",          OTHER,   BYTES,-111);
        f("4th",  "other2",          OTHER,   BYTES,-222 * MB);
        f("4th",  "other3",          OTHER,   COUNT, -333);
        f("4th",  "other4",          OTHER,   COUNT_CUMULATIVE, -444);
        f("4th",  "other5",          OTHER,   PERCENTAGE, -555);
        f("4th",  "other6",          OTHER,   PERCENTAGE, 66666);

        // If a negative value is within a collapsed sub-tree in non-verbose mode,
        // we should get the warning at the top and the relevant sub-trees should
        // be expanded, even in non-verbose mode.
        f("5th", "heap-allocated",   OTHER,   BYTES,100 * MB);
        f("5th", "explicit/big",     HEAP,    BYTES, 99 * MB);
        f("5th", "explicit/a/pos",   HEAP,    BYTES, 40 * KB);
        f("5th", "explicit/a/neg1",  NONHEAP, BYTES,-20 * KB);
        f("5th", "explicit/a/neg2",  NONHEAP, BYTES,-10 * KB);
        f("5th", "explicit/b/c/d/e", NONHEAP, BYTES, 20 * KB);
        f("5th", "explicit/b/c/d/f", NONHEAP, BYTES,-60 * KB);
        f("5th", "explicit/b/c/g/h", NONHEAP, BYTES, 10 * KB);
        f("5th", "explicit/b/c/i/j", NONHEAP, BYTES,  5 * KB);
      }
    }
  ];
  for (let i = 0; i < fakeReporters2.length; i++) {
    mgr.registerStrongReporterEvenIfBlocked(fakeReporters2[i]);
  }
  fakeReporters = fakeReporters.concat(fakeReporters2);
  ]]>
  </script>

  <iframe id="amFrame"  height="300" src="about:memory"></iframe>
  <!-- vary the capitalization to make sure that works -->
  <iframe id="amvFrame" height="300" src="About:Memory"></iframe>

  <script type="application/javascript">
  <![CDATA[
  let amExpectedText =
"\
Main Process\n\
Explicit Allocations\n\
\n\
623.58 MB (100.0%) -- explicit\n\
├──232.00 MB (37.20%) -- b\n\
│  ├───85.00 MB (13.63%) ── a\n\
│  ├───75.00 MB (12.03%) ── b\n\
│  └───72.00 MB (11.55%) -- c\n\
│      ├──70.00 MB (11.23%) ── a\n\
│      └───2.00 MB (00.32%) ── b\n\
├──222.00 MB (35.60%) ── a\n\
├──100.00 MB (16.04%) -- c\n\
│  ├───77.00 MB (12.35%) ── other\n\
│  └───23.00 MB (03.69%) ── d [2]\n\
├───23.00 MB (03.69%) ── cc [2]\n\
├───20.00 MB (03.21%) -- f/g/h\n\
│   ├──10.00 MB (01.60%) ── i\n\
│   └──10.00 MB (01.60%) ── j\n\
├───15.00 MB (02.41%) ++ g\n\
├───11.00 MB (01.76%) ── heap-unclassified\n\
└────0.58 MB (00.09%) ++ (2 tiny)\n\
\n\
Other Measurements\n\
\n\
5 (100.0%) -- compartments\n\
├──3 (60.00%) -- user\n\
│  ├──1 (20.00%) ── bar\n\
│  ├──1 (20.00%) ── foo\n\
│  └──1 (20.00%) ── https://very-long-url.com/very-long/oh-so-long/really-quite-long.html?a=2&b=3&c=4&d=5&e=abcdefghijklmnopqrstuvwxyz&f=123456789123456789123456789\n\
└──2 (40.00%) -- system\n\
   ├──1 (20.00%) ── bar\n\
   └──1 (20.00%) ── foo\n\
\n\
166.00 MB (100.0%) -- other1\n\
├──111.00 MB (66.87%) ── a/b\n\
└───55.00 MB (33.13%) -- c\n\
    ├──33.00 MB (19.88%) ── e\n\
    └──22.00 MB (13.25%) ── d\n\
\n\
20.20% (100.0%) -- other3\n\
└──20.20% (100.0%) -- a/b/c/d\n\
   ├──20.00% (99.01%) ── e\n\
   └───0.20% (00.99%) ++ (2 tiny)\n\
\n\
100,000 (100.0%) -- other6\n\
├───99,999 (100.0%) ── big\n\
└────────1 (00.00%) ── small\n\
\n\
0.00 MB (100.0%) -- other7\n\
└──0.00 MB (100.0%) ── zero\n\
\n\
888.00 MB ── explicit\n\
500.00 MB ── heap-allocated\n\
100.00 MB ── heap-unallocated\n\
222.00 MB ── other2\n\
      777 ── other3\n\
      888 ── other4 [2]\n\
   45.67% ── perc1\n\
  100.00% ── perc2\n\
\n\
End of Main Process\n\
4th\n\
\n\
WARNING: the following values are negative or unreasonably large.\n\
\n\
    explicit/js/compartment(http://too-big.com/)/stuff\n\
    explicit/(2 tiny)\n\
    explicit/(2 tiny)/neg1\n\
    explicit/(2 tiny)/heap-unclassified\n\
    other1\n\
    other2\n\
    other3\n\
    other4\n\
    other5 \n\
\n\
This indicates a defect in one or more memory reporters. The invalid values are highlighted.\n\
Explicit Allocations\n\
\n\
98.00 MB (100.0%) -- explicit\n\
├──150.00 MB (153.06%) ── js/compartment(http://too-big.com/)/stuff [?!]\n\
├───5.00 MB (05.10%) ── ok\n\
└──-57.00 MB (-58.16%) -- (2 tiny) [?!]\n\
   ├───-2.00 MB (-2.04%) ── neg1 [?!]\n\
   └──-55.00 MB (-56.12%) ── heap-unclassified [?!]\n\
\n\
Other Measurements\n\
\n\
 100.00 MB ── heap-allocated\n\
  -0.00 MB ── other1 [?!]\n\
-222.00 MB ── other2 [?!]\n\
      -333 ── other3 [?!]\n\
      -444 ── other4 [?!]\n\
    -5.55% ── other5 [?!]\n\
   666.66% ── other6\n\
 200.00 MB ── resident\n\
\n\
End of 4th\n\
3rd\n\
\n\
WARNING: the 'heap-allocated' memory reporter does not work for this platform and/or configuration. This means that 'heap-unclassified' is not shown and the 'explicit' tree shows less memory than it should.\n\
Explicit Allocations\n\
\n\
777.00 MB (100.0%) -- explicit\n\
└──777.00 MB (100.0%) -- a\n\
   ├──444.00 MB (57.14%) ── c\n\
   └──333.00 MB (42.86%) ── b\n\
\n\
Other Measurements\n\
\n\
  1.00 MB ── other1\n\
100.00 MB ── resident\n\
\n\
End of 3rd\n\
2nd\n\
Explicit Allocations\n\
\n\
1,000.00 MB (100.0%) -- explicit\n\
├────499.00 MB (49.90%) ── a/b/c [3]\n\
├────200.00 MB (20.00%) ── compartment(compartment-url)\n\
├────200.00 MB (20.00%) ── flip/the/backslashes\n\
└────101.00 MB (10.10%) ── heap-unclassified\n\
\n\
Other Measurements\n\
\n\
1,000.00 MB ── heap-allocated\n\
  100.00 MB ── heap-unallocated\n\
  666.00 MB ── other0\n\
  111.00 MB ── other1\n\
\n\
End of 2nd\n\
5th\n\
\n\
WARNING: the following values are negative or unreasonably large.\n\
\n\
    explicit/(3 tiny)/a/neg2\n\
    explicit/(3 tiny)/a/neg1\n\
    explicit/(3 tiny)/b/c\n\
    explicit/(3 tiny)/b/c/d\n\
    explicit/(3 tiny)/b/c/d/f \n\
\n\
This indicates a defect in one or more memory reporters. The invalid values are highlighted.\n\
Explicit Allocations\n\
\n\
99.95 MB (100.0%) -- explicit\n\
├──99.00 MB (99.05%) ── big\n\
└───0.95 MB (00.95%) -- (3 tiny)\n\
    ├──0.96 MB (00.96%) ── heap-unclassified\n\
    ├──0.01 MB (00.01%) -- a\n\
    │  ├──0.04 MB (00.04%) ── pos\n\
    │  ├──-0.01 MB (-0.01%) ── neg2 [?!]\n\
    │  └──-0.02 MB (-0.02%) ── neg1 [?!]\n\
    └──-0.02 MB (-0.02%) -- b/c [?!]\n\
       ├───0.01 MB (00.01%) ── g/h\n\
       ├───0.00 MB (00.00%) ── i/j\n\
       └──-0.04 MB (-0.04%) -- d [?!]\n\
          ├───0.02 MB (00.02%) ── e\n\
          └──-0.06 MB (-0.06%) ── f [?!]\n\
\n\
Other Measurements\n\
\n\
100.00 MB ── heap-allocated\n\
\n\
End of 5th\n\
";

  let amvExpectedText =
"\
Main Process\n\
Explicit Allocations\n\
\n\
653,876,224 B (100.0%) -- explicit\n\
├──243,269,632 B (37.20%) -- b\n\
│  ├───89,128,960 B (13.63%) ── a\n\
│  ├───78,643,200 B (12.03%) ── b\n\
│  └───75,497,472 B (11.55%) -- c\n\
│      ├──73,400,320 B (11.23%) ── a\n\
│      └───2,097,152 B (00.32%) ── b\n\
├──232,783,872 B (35.60%) ── a\n\
├──104,857,600 B (16.04%) -- c\n\
│  ├───80,740,352 B (12.35%) ── other\n\
│  └───24,117,248 B (03.69%) ── d [2]\n\
├───24,117,248 B (03.69%) ── cc [2]\n\
├───20,971,520 B (03.21%) -- f/g/h\n\
│   ├──10,485,760 B (01.60%) ── i\n\
│   └──10,485,760 B (01.60%) ── j\n\
├───15,728,640 B (02.41%) -- g\n\
│   ├───6,291,456 B (00.96%) ── a\n\
│   ├───5,242,880 B (00.80%) ── b\n\
│   └───4,194,304 B (00.64%) ── other\n\
├───11,534,336 B (01.76%) ── heap-unclassified\n\
├──────510,976 B (00.08%) ── d\n\
└──────102,400 B (00.02%) ── e\n\
\n\
Other Measurements\n\
\n\
5 (100.0%) -- compartments\n\
├──3 (60.00%) -- user\n\
│  ├──1 (20.00%) ── bar\n\
│  ├──1 (20.00%) ── foo\n\
│  └──1 (20.00%) ── https://very-long-url.com/very-long/oh-so-long/really-quite-long.html?a=2&b=3&c=4&d=5&e=abcdefghijklmnopqrstuvwxyz&f=123456789123456789123456789\n\
└──2 (40.00%) -- system\n\
   ├──1 (20.00%) ── bar\n\
   └──1 (20.00%) ── foo\n\
\n\
174,063,616 B (100.0%) -- other1\n\
├──116,391,936 B (66.87%) ── a/b\n\
└───57,671,680 B (33.13%) -- c\n\
    ├──34,603,008 B (19.88%) ── e\n\
    └──23,068,672 B (13.25%) ── d\n\
\n\
20.20% (100.0%) -- other3\n\
└──20.20% (100.0%) -- a/b/c/d\n\
   ├──20.00% (99.01%) ── e\n\
   ├───0.10% (00.50%) ── f\n\
   └───0.10% (00.50%) ── g [2]\n\
\n\
100,000 (100.0%) -- other6\n\
├───99,999 (100.0%) ── big\n\
└────────1 (00.00%) ── small\n\
\n\
0 B (100.0%) -- other7\n\
└──0 B (100.0%) ── zero\n\
\n\
931,135,488 B ── explicit\n\
524,288,000 B ── heap-allocated\n\
104,857,600 B ── heap-unallocated\n\
232,783,872 B ── other2\n\
          777 ── other3\n\
          888 ── other4 [2]\n\
       45.67% ── perc1\n\
      100.00% ── perc2\n\
\n\
End of Main Process\n\
4th\n\
\n\
WARNING: the following values are negative or unreasonably large.\n\
\n\
    explicit/js/compartment(http://too-big.com/)/stuff\n\
    explicit/neg1\n\
    explicit/heap-unclassified\n\
    other1\n\
    other2\n\
    other3\n\
    other4\n\
    other5 \n\
\n\
This indicates a defect in one or more memory reporters. The invalid values are highlighted.\n\
Explicit Allocations\n\
\n\
102,760,448 B (100.0%) -- explicit\n\
├──157,286,400 B (153.06%) ── js/compartment(http://too-big.com/)/stuff [?!]\n\
├────5,242,880 B (05.10%) ── ok\n\
├───-2,097,152 B (-2.04%) ── neg1 [?!]\n\
└──-57,671,680 B (-56.12%) ── heap-unclassified [?!]\n\
\n\
Other Measurements\n\
\n\
 104,857,600 B ── heap-allocated\n\
        -111 B ── other1 [?!]\n\
-232,783,872 B ── other2 [?!]\n\
          -333 ── other3 [?!]\n\
          -444 ── other4 [?!]\n\
        -5.55% ── other5 [?!]\n\
       666.66% ── other6\n\
 209,715,200 B ── resident\n\
\n\
End of 4th\n\
3rd\n\
\n\
WARNING: the 'heap-allocated' memory reporter does not work for this platform and/or configuration. This means that 'heap-unclassified' is not shown and the 'explicit' tree shows less memory than it should.\n\
Explicit Allocations\n\
\n\
814,743,552 B (100.0%) -- explicit\n\
└──814,743,552 B (100.0%) -- a\n\
   ├──465,567,744 B (57.14%) ── c\n\
   └──349,175,808 B (42.86%) ── b\n\
\n\
Other Measurements\n\
\n\
  1,048,576 B ── other1\n\
104,857,600 B ── resident\n\
\n\
End of 3rd\n\
2nd\n\
Explicit Allocations\n\
\n\
1,048,576,000 B (100.0%) -- explicit\n\
├────523,239,424 B (49.90%) ── a/b/c [3]\n\
├────209,715,200 B (20.00%) ── compartment(compartment-url)\n\
├────209,715,200 B (20.00%) ── flip/the/backslashes\n\
└────105,906,176 B (10.10%) ── heap-unclassified\n\
\n\
Other Measurements\n\
\n\
1,048,576,000 B ── heap-allocated\n\
  104,857,600 B ── heap-unallocated\n\
  698,351,616 B ── other0\n\
  116,391,936 B ── other1\n\
\n\
End of 2nd\n\
5th\n\
\n\
WARNING: the following values are negative or unreasonably large.\n\
\n\
    explicit/a/neg2\n\
    explicit/a/neg1\n\
    explicit/b/c\n\
    explicit/b/c/d\n\
    explicit/b/c/d/f \n\
\n\
This indicates a defect in one or more memory reporters. The invalid values are highlighted.\n\
Explicit Allocations\n\
\n\
104,801,280 B (100.0%) -- explicit\n\
├──103,809,024 B (99.05%) ── big\n\
├────1,007,616 B (00.96%) ── heap-unclassified\n\
├───────10,240 B (00.01%) -- a\n\
│       ├──40,960 B (00.04%) ── pos\n\
│       ├──-10,240 B (-0.01%) ── neg2 [?!]\n\
│       └──-20,480 B (-0.02%) ── neg1 [?!]\n\
└──────-25,600 B (-0.02%) -- b/c [?!]\n\
       ├───10,240 B (00.01%) ── g/h\n\
       ├────5,120 B (00.00%) ── i/j\n\
       └──-40,960 B (-0.04%) -- d [?!]\n\
          ├───20,480 B (00.02%) ── e\n\
          └──-61,440 B (-0.06%) ── f [?!]\n\
\n\
Other Measurements\n\
\n\
104,857,600 B ── heap-allocated\n\
\n\
End of 5th\n\
";

  function finish()
  {
    mgr.unblockRegistrationAndRestoreOriginalReporters();
    SimpleTest.finish();
  }

  // Cut+paste the entire page and check that the cut text matches what we
  // expect.  This tests the output in general and also that the cutting and
  // pasting works as expected.
  function test(aFrameId, aVerbose, aExpected, aNext) {
    SimpleTest.executeSoon(function() {
      ok(document.title === "about:memory", "document.title is correct");
      let mostRecentActual;
      let frame = document.getElementById(aFrameId);
      frame.focus();

      // Set the verbose checkbox value and click the go button.
      let doc = frame.contentWindow.document;
      let measureButton = doc.getElementById("measureButton");
      let verbose = doc.getElementById("verbose");
      verbose.checked = aVerbose;
      measureButton.click();

      SimpleTest.waitForClipboard(
        function(aActual) {
          mostRecentActual = aActual;
          let rslt = aActual.trim() === aExpected.trim();
          if (!rslt) {
            // Try copying again.
            synthesizeKey("A", {accelKey: true});
            synthesizeKey("C", {accelKey: true});
          }

          return rslt;
        },
        function() {
          synthesizeKey("A", {accelKey: true});
          synthesizeKey("C", {accelKey: true});
        },
        aNext,
        function() {
          ok(false, "pasted text doesn't match for " + aFrameId);
          dump("******EXPECTED******\n");
          dump("<<<" + aExpected + ">>>\n");
          dump("*******ACTUAL*******\n");
          dump("<<<" + mostRecentActual + ">>>\n");
          dump("********************\n");
          finish();
        }
      );
    });
  }

  SimpleTest.waitForFocus(function() {
    test(
      "amFrame",
      /* verbose = */ false,
      amExpectedText,
      function() {
        test(
          "amvFrame",
          /* verbose = */ true,
          amvExpectedText,
          function() {
            finish()
          }
        )
      }
    );
  });
  SimpleTest.waitForExplicitFinish();
  ]]>
  </script>
</window>