browser_object_attachment.js (4653B)
1 ChromeUtils.defineESModuleGetters(this, { 2 Downloads: "resource://gre/modules/Downloads.sys.mjs", 3 }); 4 5 const httpsTestRoot = getRootDirectory(gTestPath).replace( 6 "chrome://mochitests/content", 7 "https://example.com" 8 ); 9 10 async function loadAndCheck(file, displayInline, downloadFile = null) { 11 // Get the downloads list and add a view to listen for a download to be added. 12 // We do this even if we aren't going to download anything, so we notice if a 13 // download is started. 14 let download; 15 let downloadList = await Downloads.getList(Downloads.ALL); 16 let downloadView = { 17 async onDownloadAdded(aDownload) { 18 info("download added"); 19 ok(downloadFile, "Should be expecting a download"); 20 download = aDownload; 21 22 // Clean up the download from the list 23 downloadList.remove(aDownload); 24 await aDownload.finalize(true); 25 }, 26 }; 27 downloadList.addView(downloadView); 28 29 // Open the new URL and perform the load. 30 await BrowserTestUtils.withNewTab( 31 `${httpsTestRoot}/${file}`, 32 async browser => { 33 is( 34 browser.browsingContext.children.length, 35 displayInline ? 1 : 0, 36 `Should ${displayInline ? "not " : ""}have a child frame` 37 ); 38 39 await SpecialPowers.spawn( 40 browser, 41 [displayInline], 42 async displayInline => { 43 let obj = content.document.querySelector("object"); 44 is( 45 obj.displayedType, 46 displayInline 47 ? Ci.nsIObjectLoadingContent.TYPE_DOCUMENT 48 : Ci.nsIObjectLoadingContent.TYPE_FALLBACK, 49 `should be displaying TYPE_${displayInline ? "DOCUMENT" : "FALLBACK"}` 50 ); 51 } 52 ); 53 } 54 ); 55 56 // Clean up our download observer. 57 downloadList.removeView(downloadView); 58 if (downloadFile) { 59 is( 60 download.source.url, 61 `${httpsTestRoot}/${downloadFile}`, 62 "Download has the correct source" 63 ); 64 } else { 65 is(download, undefined, "Should not have seen a download"); 66 } 67 } 68 69 add_task(async function test_pdf_object_attachment() { 70 await SpecialPowers.pushPrefEnv({ 71 set: [ 72 ["dom.navigation.object_embed.allow_retargeting", false], 73 ["browser.download.open_pdf_attachments_inline", false], 74 ], 75 }); 76 77 // PDF attachment should display inline. 78 await loadAndCheck("file_pdf_object_attachment.html", true); 79 }); 80 81 add_task(async function test_img_object_attachment() { 82 await SpecialPowers.pushPrefEnv({ 83 set: [["dom.navigation.object_embed.allow_retargeting", false]], 84 }); 85 86 // Image attachment should display inline. 87 await loadAndCheck("file_img_object_attachment.html", true); 88 }); 89 90 add_task(async function test_svg_object_attachment() { 91 await SpecialPowers.pushPrefEnv({ 92 set: [["dom.navigation.object_embed.allow_retargeting", false]], 93 }); 94 95 // SVG attachment should fail to load. 96 await loadAndCheck("file_svg_object_attachment.html", false); 97 }); 98 99 add_task(async function test_html_object_attachment() { 100 await SpecialPowers.pushPrefEnv({ 101 set: [["dom.navigation.object_embed.allow_retargeting", false]], 102 }); 103 104 // HTML attachment should fail to load. 105 await loadAndCheck("file_html_object_attachment.html", false); 106 }); 107 108 add_task(async function test_pdf_object_attachment_allow_retargeting() { 109 await SpecialPowers.pushPrefEnv({ 110 set: [ 111 ["dom.navigation.object_embed.allow_retargeting", true], 112 ["browser.download.open_pdf_attachments_inline", false], 113 ], 114 }); 115 116 // Even if `allow_retargeting` is enabled, we always display PDFs inline. 117 await loadAndCheck("file_pdf_object_attachment.html", true); 118 }); 119 120 add_task(async function test_img_object_attachment_allow_retargeting() { 121 await SpecialPowers.pushPrefEnv({ 122 set: [["dom.navigation.object_embed.allow_retargeting", true]], 123 }); 124 125 // Even if `allow_retargeting` is enabled, we always display images inline. 126 await loadAndCheck("file_img_object_attachment.html", true); 127 }); 128 129 add_task(async function test_svg_object_attachment_allow_retargeting() { 130 await SpecialPowers.pushPrefEnv({ 131 set: [["dom.navigation.object_embed.allow_retargeting", true]], 132 }); 133 134 // SVG attachments are downloaded if allow_retargeting is set. 135 await loadAndCheck( 136 "file_svg_object_attachment.html", 137 false, 138 "file_svg_attachment.svg" 139 ); 140 }); 141 142 add_task(async function test_html_object_attachment_allow_retargeting() { 143 await SpecialPowers.pushPrefEnv({ 144 set: [["dom.navigation.object_embed.allow_retargeting", true]], 145 }); 146 147 // HTML attachments are downloaded if allow_retargeting is set. 148 await loadAndCheck( 149 "file_html_object_attachment.html", 150 false, 151 "file_html_attachment.html" 152 ); 153 });