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