browser_persist_mixed_content_image.js (3727B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_PATH = getRootDirectory(gTestPath).replace( 7 "chrome://mochitests/content", 8 "https://example.org" 9 ); 10 11 var MockFilePicker = SpecialPowers.MockFilePicker; 12 MockFilePicker.init(window.browsingContext); 13 14 registerCleanupFunction(async function () { 15 info("Running the cleanup code"); 16 MockFilePicker.cleanup(); 17 if (gTestDir && gTestDir.exists()) { 18 // On Windows, sometimes nsIFile.remove() throws, probably because we're 19 // still writing to the directory we're trying to remove, despite 20 // waiting for the download to complete. Just retry a bit later... 21 let succeeded = false; 22 while (!succeeded) { 23 try { 24 gTestDir.remove(true); 25 succeeded = true; 26 } catch (ex) { 27 await new Promise(requestAnimationFrame); 28 } 29 } 30 } 31 }); 32 33 let gTestDir = null; 34 35 function createTemporarySaveDirectory() { 36 var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile); 37 saveDir.append("testsavedir"); 38 if (!saveDir.exists()) { 39 info("create testsavedir!"); 40 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); 41 } 42 info("return from createTempSaveDir: " + saveDir.path); 43 return saveDir; 44 } 45 46 add_task(async function test_image_download() { 47 await BrowserTestUtils.withNewTab( 48 TEST_PATH + "test_mixed_content_image.html", 49 async browser => { 50 // Add the image, and wait for it to load. 51 await SpecialPowers.spawn(browser, [], async function () { 52 let loc = content.document.location.href; 53 let httpRoot = loc.replace("https", "http"); 54 let imgloc = new content.URL("dummy.png", httpRoot); 55 let img = content.document.createElement("img"); 56 img.src = imgloc; 57 await new Promise(resolve => { 58 img.onload = resolve; 59 content.document.body.appendChild(img); 60 }); 61 }); 62 gTestDir = createTemporarySaveDirectory(); 63 64 let destFile = gTestDir.clone(); 65 66 MockFilePicker.displayDirectory = gTestDir; 67 let fileName; 68 MockFilePicker.showCallback = function (fp) { 69 info("showCallback"); 70 fileName = fp.defaultString; 71 info("fileName: " + fileName); 72 destFile.append(fileName); 73 info("path: " + destFile.path); 74 MockFilePicker.setFiles([destFile]); 75 MockFilePicker.filterIndex = 0; // just save the file 76 info("done showCallback"); 77 }; 78 let publicDownloads = await Downloads.getList(Downloads.PUBLIC); 79 let downloadFinishedPromise = new Promise(resolve => { 80 publicDownloads.addView({ 81 onDownloadChanged(download) { 82 info("Download changed!"); 83 if (download.succeeded || download.error) { 84 info("Download succeeded or errored"); 85 publicDownloads.removeView(this); 86 publicDownloads.removeFinished(); 87 resolve(download); 88 } 89 }, 90 }); 91 }); 92 // open the context menu. 93 let popup = document.getElementById("contentAreaContextMenu"); 94 let popupShown = BrowserTestUtils.waitForEvent(popup, "popupshown"); 95 BrowserTestUtils.synthesizeMouseAtCenter( 96 "img", 97 { type: "contextmenu", button: 2 }, 98 browser 99 ); 100 await popupShown; 101 let popupHidden = BrowserTestUtils.waitForEvent(popup, "popuphidden"); 102 popup.activateItem(popup.querySelector("#context-saveimage")); 103 await popupHidden; 104 info("Context menu hidden, waiting for download to finish"); 105 let imageDownload = await downloadFinishedPromise; 106 ok(imageDownload.succeeded, "Image should have downloaded successfully"); 107 } 108 ); 109 });