browser_bug1108547.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. requestLongerTimeout(2);
  4. function test() {
  5. waitForExplicitFinish();
  6. runPass("file_bug1108547-2.html", function() {
  7. runPass("file_bug1108547-3.html", function() {
  8. finish();
  9. });
  10. });
  11. }
  12. function runPass(getterFile, finishedCallback) {
  13. var rootDir = "http://mochi.test:8888/browser/dom/html/test/";
  14. var testBrowser;
  15. var privateWin;
  16. function whenDelayedStartupFinished(win, callback) {
  17. let topic = "browser-delayed-startup-finished";
  18. Services.obs.addObserver(function onStartup(aSubject) {
  19. if (win != aSubject)
  20. return;
  21. Services.obs.removeObserver(onStartup, topic);
  22. executeSoon(callback);
  23. }, topic, false);
  24. }
  25. // First, set the cookie in a normal window.
  26. gBrowser.selectedTab = gBrowser.addTab(rootDir + "file_bug1108547-1.html");
  27. BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then(afterOpenCookieSetter);
  28. function afterOpenCookieSetter() {
  29. gBrowser.removeCurrentTab();
  30. // Now, open a private window.
  31. privateWin = OpenBrowserWindow({private: true});
  32. whenDelayedStartupFinished(privateWin, afterPrivateWindowOpened);
  33. }
  34. function afterPrivateWindowOpened() {
  35. // In the private window, open the getter file, and wait for a new tab to be opened.
  36. privateWin.gBrowser.selectedTab = privateWin.gBrowser.addTab(rootDir + getterFile);
  37. testBrowser = privateWin.gBrowser.selectedBrowser;
  38. privateWin.gBrowser.tabContainer.addEventListener("TabOpen", onNewTabOpened, true);
  39. }
  40. function fetchResult() {
  41. return ContentTask.spawn(testBrowser, null, function() {
  42. return content.document.getElementById("result").textContent;
  43. });
  44. }
  45. function onNewTabOpened() {
  46. // When the new tab is opened, wait for it to load.
  47. privateWin.gBrowser.tabContainer.removeEventListener("TabOpen", onNewTabOpened, true);
  48. BrowserTestUtils.browserLoaded(privateWin.gBrowser.tabs[privateWin.gBrowser.tabs.length - 1].linkedBrowser).then(fetchResult).then(onNewTabLoaded);
  49. }
  50. function onNewTabLoaded(result) {
  51. // Now, ensure that the private tab doesn't have access to the cookie set in normal mode.
  52. is(result, "", "Shouldn't have access to the cookies");
  53. // We're done with the private window, close it.
  54. privateWin.close();
  55. // Clear all cookies.
  56. Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager).removeAll();
  57. // Open a new private window, this time to set a cookie inside it.
  58. privateWin = OpenBrowserWindow({private: true});
  59. whenDelayedStartupFinished(privateWin, afterPrivateWindowOpened2);
  60. }
  61. function afterPrivateWindowOpened2() {
  62. // In the private window, open the setter file, and wait for it to load.
  63. privateWin.gBrowser.selectedTab = privateWin.gBrowser.addTab(rootDir + "file_bug1108547-1.html");
  64. BrowserTestUtils.browserLoaded(privateWin.gBrowser.selectedBrowser).then(afterOpenCookieSetter2);
  65. }
  66. function afterOpenCookieSetter2() {
  67. // We're done with the private window now, close it.
  68. privateWin.close();
  69. // Now try to read the cookie in a normal window, and wait for a new tab to be opened.
  70. gBrowser.selectedTab = gBrowser.addTab(rootDir + getterFile);
  71. testBrowser = gBrowser.selectedBrowser;
  72. gBrowser.tabContainer.addEventListener("TabOpen", onNewTabOpened2, true);
  73. }
  74. function onNewTabOpened2() {
  75. // When the new tab is opened, wait for it to load.
  76. gBrowser.tabContainer.removeEventListener("TabOpen", onNewTabOpened2, true);
  77. BrowserTestUtils.browserLoaded(gBrowser.tabs[gBrowser.tabs.length - 1].linkedBrowser).then(fetchResult).then(onNewTabLoaded2);
  78. }
  79. function onNewTabLoaded2(result) {
  80. // Now, ensure that the normal tab doesn't have access to the cookie set in private mode.
  81. is(result, "", "Shouldn't have access to the cookies");
  82. // Remove both of the tabs opened here.
  83. gBrowser.removeCurrentTab();
  84. gBrowser.removeCurrentTab();
  85. privateWin = null;
  86. testBrowser = null;
  87. finishedCallback();
  88. }
  89. }