browser_jsonview_save_json.js (5245B)
1 /* eslint-disable no-unused-vars, no-undef */ 2 /* Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ */ 4 5 "use strict"; 6 7 const saveButton = "button.save"; 8 const prettifyButton = "button.prettyprint"; 9 10 const { MockFilePicker } = SpecialPowers; 11 MockFilePicker.init(window.browsingContext); 12 MockFilePicker.returnValue = MockFilePicker.returnOK; 13 14 Services.scriptloader.loadSubScript( 15 "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", 16 this 17 ); 18 19 function awaitSavedFileContents(name, ext) { 20 return new Promise((resolve, reject) => { 21 MockFilePicker.showCallback = fp => { 22 try { 23 ok(true, "File picker was opened"); 24 const fileName = fp.defaultString; 25 is( 26 fileName, 27 name, 28 "File picker should provide the correct default filename." 29 ); 30 is( 31 fp.defaultExtension, 32 ext, 33 "File picker should provide the correct default file extension." 34 ); 35 const destFile = destDir.clone(); 36 destFile.append(fileName); 37 MockFilePicker.setFiles([destFile]); 38 MockFilePicker.showCallback = null; 39 mockTransferCallback = async function (downloadSuccess) { 40 try { 41 ok( 42 downloadSuccess, 43 "JSON should have been downloaded successfully" 44 ); 45 ok(destFile.exists(), "The downloaded file should exist."); 46 const { path } = destFile; 47 await BrowserTestUtils.waitForCondition(() => IOUtils.exists(path)); 48 await BrowserTestUtils.waitForCondition(async () => { 49 const { size } = await IOUtils.stat(path); 50 return size > 0; 51 }); 52 const buffer = await IOUtils.read(path); 53 resolve(new TextDecoder().decode(buffer)); 54 } catch (error) { 55 reject(error); 56 } 57 }; 58 } catch (error) { 59 reject(error); 60 } 61 }; 62 }); 63 } 64 65 function createTemporarySaveDirectory() { 66 const saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile); 67 saveDir.append("jsonview-testsavedir"); 68 if (!saveDir.exists()) { 69 info("Creating temporary save directory."); 70 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); 71 } 72 info("Temporary save directory: " + saveDir.path); 73 return saveDir; 74 } 75 76 const destDir = createTemporarySaveDirectory(); 77 mockTransferRegisterer.register(); 78 MockFilePicker.displayDirectory = destDir; 79 registerCleanupFunction(function () { 80 mockTransferRegisterer.unregister(); 81 MockFilePicker.cleanup(); 82 destDir.remove(true); 83 ok(!destDir.exists(), "Destination dir should be removed"); 84 }); 85 86 add_task(async function () { 87 info("Test 1 save JSON started"); 88 89 const JSON_FILE = "simple_json.json"; 90 const TEST_JSON_URL = URL_ROOT + JSON_FILE; 91 const tab = await addJsonViewTab(TEST_JSON_URL); 92 93 const response = await fetch(new Request(TEST_JSON_URL)); 94 95 info("Fetched JSON contents."); 96 const rawJSON = await response.text(); 97 const prettyJSON = JSON.stringify(JSON.parse(rawJSON), null, " "); 98 isnot( 99 rawJSON, 100 prettyJSON, 101 "Original and prettified JSON should be different." 102 ); 103 104 // Attempt to save original JSON via saveBrowser (ctrl/cmd+s or "Save Page As" command). 105 let data = awaitSavedFileContents(JSON_FILE, "json"); 106 saveBrowser(tab.linkedBrowser); 107 is(await data, rawJSON, "Original JSON contents should have been saved."); 108 109 // Attempt to save original JSON via "Save" button 110 data = awaitSavedFileContents(JSON_FILE, "json"); 111 await clickJsonNode(saveButton); 112 info("Clicked Save button."); 113 is(await data, rawJSON, "Original JSON contents should have been saved."); 114 115 // Attempt to save prettified JSON via "Save" button 116 await selectJsonViewContentTab("rawdata"); 117 info("Switched to Raw Data tab."); 118 await clickJsonNode(prettifyButton); 119 info("Clicked Pretty Print button."); 120 data = awaitSavedFileContents(JSON_FILE, "json"); 121 await clickJsonNode(saveButton); 122 info("Clicked Save button."); 123 is( 124 await data, 125 prettyJSON, 126 "Prettified JSON contents should have been saved." 127 ); 128 129 // saveBrowser should still save original contents. 130 data = awaitSavedFileContents(JSON_FILE, "json"); 131 saveBrowser(tab.linkedBrowser); 132 is(await data, rawJSON, "Original JSON contents should have been saved."); 133 }); 134 135 add_task(async function () { 136 info("Test 2 save JSON started"); 137 138 const TEST_JSON_URL = "data:application/json,2"; 139 await addJsonViewTab(TEST_JSON_URL); 140 141 info("Checking that application/json adds .json extension by default."); 142 const data = awaitSavedFileContents("Untitled.json", "json"); 143 await clickJsonNode(saveButton); 144 info("Clicked Save button."); 145 is(await data, "2", "JSON contents should have been saved."); 146 }); 147 148 add_task(async function () { 149 info("Test 3 save JSON started"); 150 151 const TEST_JSON_URL = "data:application/manifest+json,3"; 152 await addJsonViewTab(TEST_JSON_URL); 153 154 info("Checking that application/manifest+json does not add .json extension."); 155 const data = awaitSavedFileContents("Untitled", null); 156 await clickJsonNode(saveButton); 157 info("Clicked Save button."); 158 is(await data, "3", "JSON contents should have been saved."); 159 });