browser_test_data_text_csv.js (3289B)
1 "use strict"; 2 3 const kTestPath = getRootDirectory(gTestPath).replace( 4 "chrome://mochitests/content", 5 "http://example.com" 6 ); 7 const kTestURI = kTestPath + "file_data_text_csv.html"; 8 9 function addWindowListener(aURL) { 10 return new Promise(resolve => { 11 Services.wm.addListener({ 12 onOpenWindow(aXULWindow) { 13 info("window opened, waiting for focus"); 14 Services.wm.removeListener(this); 15 var domwindow = aXULWindow.docShell.domWindow; 16 waitForFocus(function () { 17 is( 18 domwindow.document.location.href, 19 aURL, 20 "should have seen the right window open" 21 ); 22 resolve(domwindow); 23 }, domwindow); 24 }, 25 onCloseWindow() {}, 26 }); 27 }); 28 } 29 30 function promisePanelOpened() { 31 if (DownloadsPanel.panel && DownloadsPanel.panel.state == "open") { 32 return Promise.resolve(); 33 } 34 return BrowserTestUtils.waitForEvent(DownloadsPanel.panel, "popupshown"); 35 } 36 37 add_task(async function test_with_pref_enabled() { 38 await SpecialPowers.pushPrefEnv({ 39 set: [ 40 ["security.data_uri.block_toplevel_data_uri_navigations", true], 41 ["browser.download.always_ask_before_handling_new_types", true], 42 ], 43 }); 44 45 let windowPromise = addWindowListener( 46 "chrome://mozapps/content/downloads/unknownContentType.xhtml" 47 ); 48 BrowserTestUtils.startLoadingURIString(gBrowser, kTestURI); 49 let win = await windowPromise; 50 51 let expectedValue = "Untitled.csv"; 52 is( 53 win.document.getElementById("location").value, 54 expectedValue, 55 "file name of download should match" 56 ); 57 let mainWindowActivated = BrowserTestUtils.waitForEvent(window, "activate"); 58 await BrowserTestUtils.closeWindow(win); 59 await mainWindowActivated; 60 }); 61 62 add_task(async function test_with_pref_disabled() { 63 await SpecialPowers.pushPrefEnv({ 64 set: [ 65 ["security.data_uri.block_toplevel_data_uri_navigations", true], 66 ["browser.download.always_ask_before_handling_new_types", false], 67 ], 68 }); 69 let downloadsPanelPromise = promisePanelOpened(); 70 let downloadsPromise = Downloads.getList(Downloads.PUBLIC); 71 let sourceURLBit = "text/csv;foo,bar,foobar"; 72 73 info("Loading URI for pref enabled"); 74 BrowserTestUtils.startLoadingURIString(gBrowser, kTestURI); 75 info("Waiting for downloads panel to open"); 76 await downloadsPanelPromise; 77 info("Getting downloads info after opening downloads panel"); 78 let downloadList = await downloadsPromise; 79 80 is(DownloadsPanel.isPanelShowing, true, "DownloadsPanel should be open."); 81 is( 82 downloadList._downloads.length, 83 1, 84 "File should be successfully downloaded." 85 ); 86 87 let [download] = downloadList._downloads; 88 is(download.contentType, "text/csv", "File contentType should be correct."); 89 is( 90 download.source.url, 91 `data:${sourceURLBit}`, 92 "File name should be correct." 93 ); 94 95 info("Cleaning up downloads"); 96 try { 97 if (Services.appinfo.OS === "WINNT") { 98 // We need to make the file writable to delete it on Windows. 99 await IOUtils.setPermissions(download.target.path, 0o600); 100 } 101 await IOUtils.remove(download.target.path); 102 } catch (ex) { 103 info("The file " + download.target.path + " is not removed, " + ex); 104 } 105 106 await downloadList.remove(download); 107 await download.finalize(); 108 });