layout/forms/test/test_bug536567_perwindowpb.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=536567
     5 -->
     6 <head>
     7   <title>Test for Bug 536567</title>
     8   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     9   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    10   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=536567">Mozilla Bug 536567</a>
    14 <p id="display"></p>
    15 <pre id="test">
    16 <script type="application/javascript">
    17 /** Test for Bug 536567 **/
    19 const Cc = Components.classes;
    20 const Ci = Components.interfaces;
    21 const Cu = Components.utils;
    22 const Cm = Components.manager;
    24 Cu.import("resource://gre/modules/Services.jsm");
    26 var MockFilePicker = SpecialPowers.MockFilePicker;
    27 MockFilePicker.init(window);
    29 var tmpDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile);
    30 var homeDir = Services.dirsvc.get("Desk", Ci.nsILocalFile);
    32 function newDir() {
    33   var dir = tmpDir.clone();
    34   dir.append("testdir" + Math.floor(Math.random() * 10000));
    35   dir.QueryInterface(Ci.nsILocalFile);
    36   dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0700);
    37   return dir;
    38 }
    40 var dirs = [];
    41 for(var i = 0; i < 6; i++) {
    42   dirs.push(newDir());
    43 }
    44 dirs.push(homeDir);
    45 var domains = ['http://mochi.test:8888', 'http://example.org:80', 'http://example.com:80'];
    46 /*
    47  * These tests take 3 args each:
    48  * - which domain to load
    49  * - the filePicker displayDirectory we expect to be set
    50  * - the file to pick (in most cases this will show up in the next test,
    51  *   as indicated by the comments)
    52  */
    53 var tests = [
    54   "clear history",
    55   [0, 6, 0], // 0 -> 3
    56   [1, 6, 1], // 1 -> 4
    57   [2, 6, 2], // 2 -> 5
    58   [0, 0, 3], // 3 -> 6
    59   [1, 1, 1], // 4 -> 8
    60   [2, 2, 2], // 5 -> 9
    61   [0, 3, 1], // 6 -> 7
    62   [0, 1, 0], // 7 -> x
    63   [1, 1, 1], // 8 -> x
    64   [2, 2, 2], // 9 -> x
    65   "clear history",
    66   [0, 6, 0], // 11 -> 15
    67   [1, 6, 1], // 12 -> 16
    68   [2, 6, 2], // 13 -> 17
    69   "pb on",
    70   [0, 0, 3], // 15 -> 18
    71   [1, 1, 4], // 16 -> 19
    72   [2, 2, 5], // 17 -> 20
    73   [0, 3, 3], // 18 -> x
    74   [1, 4, 4], // 19 -> x
    75   [2, 5, 5], // 20 -> x
    76   "pb off",
    77   [0, 0, 5], // 22 -> 26
    78   [1, 1, 4], // 23 -> 27
    79   [2, 2, 3], // 24 -> 28
    80   "pb on",
    81   [0, 3, 5], // 26 -> x
    82   [1, 4, 4], // 27 -> x
    83   [2, 5, 3], // 28 -> x
    84   "clear history",
    85   // Not checking after clear history because browser.download.lastDir content
    86   // pref is not being clear properly in private windows.
    87   //[0, 6, 0], // 30 -> x
    88   //[1, 6, 1], // 31 -> x
    89   //[2, 6, 2], // 32 -> x
    90   "pb off"
    91 ];
    93 var testIndex = 0;
    94 var content;
    95 var normalWindow;
    96 var privateWindow;
    97 var normalWindowIframe;
    98 var privateWindowIframe;
   100 function runTest() {
   101   var test = tests[testIndex];
   102   if (test == undefined) {
   103     endTest();
   104   } else if (test == "pb on") {
   105     content = privateWindowIframe;
   106     testIndex++;
   107     runTest();
   108   } else if (test == "pb off") {
   109     content = normalWindowIframe;
   110     testIndex++;
   111     runTest();
   112   } else if (test == "clear history") {
   113     Services.obs.notifyObservers(null, "browser:purge-session-history", "");
   114     testIndex++;
   115     runTest();
   116   } else {
   117     var file = dirs[test[2]].clone();
   118     file.append("file.file");
   119     MockFilePicker.returnFiles = [file];
   120     content.setAttribute('src', domains[test[0]] + '/chrome/layout/forms/test/bug536567_subframe.html');
   121   }
   122 }
   124 function endTest() {
   125   for(var i = 0; i < dirs.length - 1; i++) {
   126     dirs[i].remove(true);
   127   }
   129   normalWindow.close();
   130   privateWindow.close();
   131   MockFilePicker.cleanup();
   132   SimpleTest.finish();
   133 }
   135 var mainWindow =
   136   window.QueryInterface(Ci.nsIInterfaceRequestor).
   137     getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).
   138     rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).
   139     getInterface(Ci.nsIDOMWindow);
   140 var contentPage = "http://mochi.test:8888/chrome/layout/forms/test/bug536567_iframe.html";
   142 function whenDelayedStartupFinished(aWindow, aCallback) {
   143   Services.obs.addObserver(function observer(aSubject, aTopic) {
   144     if (aWindow == aSubject) {
   145       Services.obs.removeObserver(observer, aTopic);
   146       setTimeout(aCallback, 0);
   147     }
   148   }, "browser-delayed-startup-finished", false);
   149 }
   151 function testOnWindow(aIsPrivate, aCallback) {
   152   var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
   153   whenDelayedStartupFinished(win, function() {
   154     win.addEventListener("DOMContentLoaded", function onInnerLoad() {
   155       if (win.content.location.href != contentPage) {
   156         win.gBrowser.loadURI(contentPage);
   157         return;
   158       }
   159       win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
   160       win.gBrowser.selectedBrowser.focus();
   161       SimpleTest.info("DOMContentLoaded's window: " + win.location + " vs. " + window.location);
   162       win.setTimeout(function() { aCallback(win); }, 0);
   163     }, true);
   164     SimpleTest.info("load's window: " + win.location + " vs. " + window.location);
   165     win.setTimeout(function() { win.gBrowser.loadURI(contentPage); }, 0);
   166   });
   167 }
   169 MockFilePicker.showCallback = function(filepicker) {
   170   var test = tests[testIndex];
   171   var returned = -1;
   172   for (var i = 0; i < dirs.length; i++) {
   173     if (dirs[i].path == MockFilePicker.displayDirectory.path) {
   174       returned = i;
   175       break;
   176     }
   177   }
   178   if (test[1] == -1) {
   179     ok(false, "We should never get an unknown directory back");
   180   } else {
   181     is(returned, test[1], 'test ' + testIndex);
   182   }
   184   filepicker.window.setTimeout(function() {
   185     testIndex++;
   186     runTest();
   187   }, 0);
   188 };
   190 window.onload = function() {
   191   SimpleTest.waitForExplicitFinish();
   192   testOnWindow(false, function(aWin) {
   193     var selectedBrowser = aWin.gBrowser.selectedBrowser;
   195     normalWindow = aWin;
   196     normalWindowIframe =
   197       selectedBrowser.contentDocument.getElementById("content");
   199     testOnWindow(true, function(aPrivateWin) {
   200       selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
   202       privateWindow = aPrivateWin;
   203       privateWindowIframe =
   204         selectedBrowser.contentDocument.getElementById("content");
   206       content = normalWindowIframe;
   207       selectedBrowser.contentWindow.setTimeout(runTest, 0);
   208     });
   209   });
   210 };
   212 </script>
   213 </pre>
   214 </body>
   215 </html>

mercurial