file_bug1069772.xul 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
  3. <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
  4. type="text/css"?>
  5. <!--
  6. https://bugzilla.mozilla.org/show_bug.cgi?id=1069772
  7. -->
  8. <window title="Mozilla Bug 1069772"
  9. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" onload="run();">
  10. <!-- test code goes here -->
  11. <script type="application/javascript">
  12. <![CDATA[
  13. const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
  14. Cu.import("resource://testing-common/ContentTask.jsm");
  15. Cu.import("resource://testing-common/BrowserTestUtils.jsm");
  16. Cu.import("resource://gre/modules/Services.jsm");
  17. ContentTask.setTestScope(window.opener.wrappedJSObject);
  18. let imports = ['SimpleTest', 'ok', 'is'];
  19. for (let name of imports) {
  20. window[name] = window.opener.wrappedJSObject[name];
  21. }
  22. /** Test for Bug 1069772 **/
  23. function run() {
  24. // test the transition:
  25. // prerender => visible => hidden => visible
  26. // on a non-remote browser
  27. createPrerenderedBrowser(false)
  28. .then(browser => verifyBrowser(browser, true, false))
  29. .then(browser => verifyVisibility(browser, 'prerender'))
  30. .then(browser => makePrerenderedBrowserActive(browser))
  31. .then(browser => verifyBrowser(browser, false, false))
  32. .then(browser => verifyVisibility(browser, 'visible'))
  33. .then(browser => hideBrowser(browser))
  34. .then(browser => verifyBrowser(browser, false, false))
  35. .then(browser => verifyVisibility(browser, 'hidden'))
  36. .then(browser => showBrowser(browser))
  37. .then(browser => verifyBrowser(browser, false, false))
  38. .then(browser => verifyVisibility(browser, 'visible'))
  39. // test the transition:
  40. // prerender => visible => hidden => visible
  41. // on a remote browser
  42. .then(() => createPrerenderedBrowser(true))
  43. .then(browser => verifyBrowser(browser, true, true))
  44. .then(browser => verifyVisibility(browser, 'prerender'))
  45. .then(browser => makePrerenderedBrowserActive(browser))
  46. .then(browser => verifyBrowser(browser, false, true))
  47. .then(browser => verifyVisibility(browser, 'visible'))
  48. .then(browser => hideBrowser(browser))
  49. .then(browser => verifyBrowser(browser, false, true))
  50. .then(browser => verifyVisibility(browser, 'hidden'))
  51. .then(browser => showBrowser(browser))
  52. .then(browser => verifyBrowser(browser, false, true))
  53. .then(browser => verifyVisibility(browser, 'visible'))
  54. // finish test
  55. .then(() => {
  56. window.close();
  57. SimpleTest.finish();
  58. });
  59. }
  60. function createPrerenderedBrowser(remote) {
  61. let browser = document.createElement('browser');
  62. browser.setAttribute('type', 'content');
  63. browser.setAttribute('prerendered', true);
  64. browser.setAttribute('remote', remote);
  65. browser.setAttribute('src', 'data:text/html;charset=UTF-8,<html><body>' +
  66. '<iframe id="iframe" src="data:text/html;charset=UTF-8,Hello Frame!">' +
  67. '</iframe></body></html>');
  68. // wait for 'load' and 'pageshow'
  69. let promises = [];
  70. promises.push(BrowserTestUtils.browserLoaded(browser));
  71. promises.push(new Promise(resolve =>
  72. Services.mm.addMessageListener('test:pageshow', resolve)));
  73. Services.mm.loadFrameScript('data:,' +
  74. 'addEventListener("pageshow", ' +
  75. '() => sendAsyncMessage("test:pageshow", null), false);',
  76. true);
  77. document.getElementById('stack').appendChild(browser);
  78. return Promise.all(promises).then(() => browser);
  79. }
  80. function verifyBrowser(browser, prerendered, remote) {
  81. let docShellOrTabParent = remote ?
  82. browser.frameLoader.tabParent : browser.frameLoader.docShell;
  83. ok(docShellOrTabParent, 'docShellOrTabParent should not be null');
  84. is(docShellOrTabParent.isPrerendered, prerendered,
  85. 'isPrerendered should be ' + prerendered);
  86. return browser;
  87. }
  88. function verifyVisibility(browser, visibility) {
  89. return ContentTask.spawn(browser, visibility, (v) => {
  90. let iframe = content.document.getElementById('iframe');
  91. is(content.document.visibilityState, v, 'check doc.visibilityState');
  92. is(content.document.hidden, v != 'visible', 'check doc.hidden');
  93. is(iframe.contentDocument.visibilityState, v, 'check iframe doc.visibilityState');
  94. is(iframe.contentDocument.hidden, v != 'visible', 'check iframe doc.hidden');
  95. }).then(() => browser);
  96. }
  97. function makePrerenderedBrowserActive(browser) {
  98. let promise = waitForVisibilityChange(browser);
  99. browser.setAttribute('prerendered', false);
  100. browser.makePrerenderedBrowserActive();
  101. return promise.then(() => browser);
  102. }
  103. function hideBrowser(browser) {
  104. let promise = waitForVisibilityChange(browser);
  105. browser.docShellIsActive = false;
  106. return promise.then(() => browser);
  107. }
  108. function showBrowser(browser) {
  109. let promise = waitForVisibilityChange(browser);
  110. browser.docShellIsActive = true;
  111. return promise.then(() => browser);
  112. }
  113. function waitForVisibilityChange(browser) {
  114. return ContentTask.spawn(browser, null, () => {
  115. return new Promise(resolve => {
  116. let iframe = content.document.getElementById('iframe');
  117. iframe.contentDocument.addEventListener('visibilitychange', function listener() {
  118. iframe.contentDocument.removeEventListener('visibilitychange', listener);
  119. resolve();
  120. });
  121. });
  122. });
  123. }
  124. ]]>
  125. </script>
  126. <stack id="stack" flex="1" />
  127. </window>