1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_revert_to_saved.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* vim: set ts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 +/* Bug 751744 */ 1.8 + 1.9 +let tempScope = {}; 1.10 +Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); 1.11 +Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); 1.12 +let NetUtil = tempScope.NetUtil; 1.13 +let FileUtils = tempScope.FileUtils; 1.14 + 1.15 +// Reference to the Scratchpad object. 1.16 +let gScratchpad; 1.17 + 1.18 +// Reference to the temporary nsIFiles. 1.19 +let gFile; 1.20 + 1.21 +// Temporary file name. 1.22 +let gFileName = "testFileForBug751744.tmp" 1.23 + 1.24 + 1.25 +// Content for the temporary file. 1.26 +let gFileContent = "/* this file is already saved */\n" + 1.27 + "function foo() { alert('bar') }"; 1.28 +let gLength = gFileContent.length; 1.29 + 1.30 +// Reference to the menu entry. 1.31 +let menu; 1.32 + 1.33 +function startTest() 1.34 +{ 1.35 + gScratchpad = gScratchpadWindow.Scratchpad; 1.36 + menu = gScratchpadWindow.document.getElementById("sp-cmd-revert"); 1.37 + createAndLoadTemporaryFile(); 1.38 +} 1.39 + 1.40 +function testAfterSaved() { 1.41 + // Check if the revert menu is disabled as the file is at saved state. 1.42 + ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled."); 1.43 + 1.44 + // chancging the text in the file 1.45 + gScratchpad.setText(gScratchpad.getText() + "\nfoo();"); 1.46 + // Checking the text got changed 1.47 + is(gScratchpad.getText(), gFileContent + "\nfoo();", 1.48 + "The text changed the first time."); 1.49 + 1.50 + // Revert menu now should be enabled. 1.51 + ok(!menu.hasAttribute("disabled"), 1.52 + "The revert menu entry is enabled after changing text first time"); 1.53 + 1.54 + // reverting back to last saved state. 1.55 + gScratchpad.revertFile(testAfterRevert); 1.56 +} 1.57 + 1.58 +function testAfterRevert() { 1.59 + // Check if the file's text got reverted 1.60 + is(gScratchpad.getText(), gFileContent, 1.61 + "The text reverted back to original text."); 1.62 + // The revert menu should be disabled again. 1.63 + ok(menu.hasAttribute("disabled"), 1.64 + "The revert menu entry is disabled after reverting."); 1.65 + 1.66 + // chancging the text in the file again 1.67 + gScratchpad.setText(gScratchpad.getText() + "\nalert(foo.toSource());"); 1.68 + // Saving the file. 1.69 + gScratchpad.saveFile(testAfterSecondSave); 1.70 +} 1.71 + 1.72 +function testAfterSecondSave() { 1.73 + // revert menu entry should be disabled. 1.74 + ok(menu.hasAttribute("disabled"), 1.75 + "The revert menu entry is disabled after saving."); 1.76 + 1.77 + // changing the text. 1.78 + gScratchpad.setText(gScratchpad.getText() + "\nfoo();"); 1.79 + 1.80 + // revert menu entry should get enabled yet again. 1.81 + ok(!menu.hasAttribute("disabled"), 1.82 + "The revert menu entry is enabled after changing text third time"); 1.83 + 1.84 + // reverting back to last saved state. 1.85 + gScratchpad.revertFile(testAfterSecondRevert); 1.86 +} 1.87 + 1.88 +function testAfterSecondRevert() { 1.89 + // Check if the file's text got reverted 1.90 + is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());", 1.91 + "The text reverted back to the changed saved text."); 1.92 + // The revert menu should be disabled again. 1.93 + ok(menu.hasAttribute("disabled"), 1.94 + "Revert menu entry is disabled after reverting to changed saved state."); 1.95 + gFile.remove(false); 1.96 + gFile = gScratchpad = menu = null; 1.97 + finish(); 1.98 +} 1.99 + 1.100 +function createAndLoadTemporaryFile() 1.101 +{ 1.102 + // Create a temporary file. 1.103 + gFile = FileUtils.getFile("TmpD", [gFileName]); 1.104 + gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); 1.105 + 1.106 + // Write the temporary file. 1.107 + let fout = Cc["@mozilla.org/network/file-output-stream;1"]. 1.108 + createInstance(Ci.nsIFileOutputStream); 1.109 + fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, 1.110 + 0o644, fout.DEFER_OPEN); 1.111 + 1.112 + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. 1.113 + createInstance(Ci.nsIScriptableUnicodeConverter); 1.114 + converter.charset = "UTF-8"; 1.115 + let fileContentStream = converter.convertToInputStream(gFileContent); 1.116 + 1.117 + NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved); 1.118 +} 1.119 + 1.120 +function tempFileSaved(aStatus) 1.121 +{ 1.122 + ok(Components.isSuccessCode(aStatus), 1.123 + "the temporary file was saved successfully"); 1.124 + 1.125 + // Import the file into Scratchpad. 1.126 + gScratchpad.setFilename(gFile.path); 1.127 + gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, 1.128 + testAfterSaved); 1.129 +} 1.130 + 1.131 +function test() 1.132 +{ 1.133 + waitForExplicitFinish(); 1.134 + 1.135 + gBrowser.selectedTab = gBrowser.addTab(); 1.136 + gBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.137 + gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.138 + openScratchpad(startTest); 1.139 + }, true); 1.140 + 1.141 + content.location = "data:text/html,<p>test reverting to last saved state of" + 1.142 + " a file </p>"; 1.143 +}