Bug 751744 - Add a Revert to Saved menu item to the scratchpad; r=harth

This commit is contained in:
Girish Sharma 2012-09-06 09:50:05 +05:30
parent 92c24734a3
commit 5708744ab8
6 changed files with 229 additions and 0 deletions

View File

@ -35,6 +35,7 @@ const PREF_RECENT_FILES_MAX = "devtools.scratchpad.recentFilesMax";
const BUTTON_POSITION_SAVE = 0;
const BUTTON_POSITION_CANCEL = 1;
const BUTTON_POSITION_DONT_SAVE = 2;
const BUTTON_POSITION_REVERT=0;
let keysbundle = Services.strings.createBundle("chrome://global-platform/locale/platformKeys.properties");
@ -922,6 +923,72 @@ var Scratchpad = {
}
},
/**
* Restore content from saved version of current file.
*
* @param function aCallback
* Optional function you want to call when file is saved
*/
revertFile: function SP_revertFile(aCallback)
{
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(this.filename);
if (!file.exists()) {
return;
}
this.importFromFile(file, false, function(aStatus, aContent) {
if (aCallback) {
aCallback(aStatus);
}
});
},
/**
* Prompt to revert scratchpad if it has unsaved changes.
*
* @param function aCallback
* Optional function you want to call when file is saved. The callback
* receives three arguments:
* - aRevert (boolean) - tells if the file has been reverted.
* - status (number) - the file revert status result (if the file was
* saved).
*/
promptRevert: function SP_promptRervert(aCallback)
{
if (this.filename) {
let ps = Services.prompt;
let flags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_REVERT +
ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL;
let button = ps.confirmEx(window,
this.strings.GetStringFromName("confirmRevert.title"),
this.strings.GetStringFromName("confirmRevert"),
flags, null, null, null, null, {});
if (button == BUTTON_POSITION_CANCEL) {
if (aCallback) {
aCallback(false);
}
return;
}
if (button == BUTTON_POSITION_REVERT) {
this.revertFile(function(aStatus) {
if(aCallback){
aCallback(true, aStatus);
}
});
return;
}
}
if (aCallback) {
aCallback(false);
}
},
/**
* Open the Error Console.
*/
@ -1107,6 +1174,14 @@ var Scratchpad = {
_onDirtyChanged: function SP__onDirtyChanged(aEvent)
{
Scratchpad._updateTitle();
if (Scratchpad.filename) {
if (Scratchpad.editor.dirty) {
document.getElementById("sp-cmd-revert").removeAttribute("disabled");
}
else {
document.getElementById("sp-cmd-revert").setAttribute("disabled", true);
}
}
},
/**

View File

@ -33,6 +33,7 @@
<command id="sp-cmd-clearRecentFiles" oncommand="Scratchpad.clearRecentFiles();"/>
<command id="sp-cmd-save" oncommand="Scratchpad.saveFile();"/>
<command id="sp-cmd-saveas" oncommand="Scratchpad.saveFileAs();"/>
<command id="sp-cmd-revert" oncommand="Scratchpad.promptRevert();" disabled="true"/>
<!-- TODO: bug 650340 - implement printFile()
<command id="sp-cmd-printFile" oncommand="Scratchpad.printFile();" disabled="true"/>
@ -132,6 +133,10 @@
label="&saveFileAsCmd.label;"
accesskey="&saveFileAsCmd.accesskey;"
command="sp-cmd-saveas"/>
<menuitem id="sp-menu-revert"
label="&revertCmd.label;"
accesskey="&revertCmd.accesskey;"
command="sp-cmd-revert"/>
<menuseparator/>
<!-- TODO: bug 650340 - implement printFile

View File

@ -33,6 +33,7 @@ MOCHITEST_BROWSER_FILES = \
browser_scratchpad_bug_650760_help_key.js \
browser_scratchpad_bug_651942_recent_files.js \
browser_scratchpad_bug756681_display_non_error_exceptions.js \
browser_scratchpad_bug_751744_revert_to_saved.js \
head.js \
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,137 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
let tempScope = {};
Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
let NetUtil = tempScope.NetUtil;
let FileUtils = tempScope.FileUtils;
// Reference to the Scratchpad object.
let gScratchpad;
// Reference to the temporary nsIFiles.
let gFile;
// Temporary file name.
let gFileName = "testFileForBug751744.tmp"
// Content for the temporary file.
let gFileContent = "/* this file is already saved */\n" +
"function foo() { alert('bar') }";
let gLength = gFileContent.length;
// Reference to the menu entry.
let menu;
function startTest()
{
gScratchpad = gScratchpadWindow.Scratchpad;
menu = gScratchpadWindow.document.getElementById("sp-menu-revert");
createAndLoadTemporaryFile();
}
function testAfterSaved() {
// Check if the revert menu is disabled as the file is at saved state.
ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled.");
// chancging the text in the file
gScratchpad.setText("\nfoo();", gLength, gLength);
// Checking the text got changed
is(gScratchpad.getText(), gFileContent + "\nfoo();",
"The text changed the first time.");
// Revert menu now should be enabled.
ok(!menu.hasAttribute("disabled"),
"The revert menu entry is enabled after changing text first time");
// reverting back to last saved state.
gScratchpad.revertFile(testAfterRevert);
}
function testAfterRevert() {
// Check if the file's text got reverted
is(gScratchpad.getText(), gFileContent,
"The text reverted back to original text.");
// The revert menu should be disabled again.
ok(menu.hasAttribute("disabled"),
"The revert menu entry is disabled after reverting.");
// chancging the text in the file again
gScratchpad.setText("\nalert(foo.toSource());", gLength, gLength);
// Saving the file.
gScratchpad.saveFile(testAfterSecondSave);
}
function testAfterSecondSave() {
// revert menu entry should be disabled.
ok(menu.hasAttribute("disabled"),
"The revert menu entry is disabled after saving.");
// changing the text.
gScratchpad.setText("\nfoo();", gLength + 23, gLength + 23);
// revert menu entry should get enabled yet again.
ok(!menu.hasAttribute("disabled"),
"The revert menu entry is enabled after changing text third time");
// reverting back to last saved state.
gScratchpad.revertFile(testAfterSecondRevert);
}
function testAfterSecondRevert() {
// Check if the file's text got reverted
is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());",
"The text reverted back to the changed saved text.");
// The revert menu should be disabled again.
ok(menu.hasAttribute("disabled"),
"Revert menu entry is disabled after reverting to changed saved state.");
gFile.remove(false);
gFile = null;
gScratchpad = null;
}
function createAndLoadTemporaryFile()
{
// Create a temporary file.
gFile = FileUtils.getFile("TmpD", [gFileName]);
gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
// Write the temporary file.
let fout = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
0644, fout.DEFER_OPEN);
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let fileContentStream = converter.convertToInputStream(gFileContent);
NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved);
}
function tempFileSaved(aStatus)
{
ok(Components.isSuccessCode(aStatus),
"the temporary file was saved successfully");
// Import the file into Scratchpad.
gScratchpad.setFilename(gFile.path);
gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
testAfterSaved);
}
function test()
{
gBrowser.selectedTab = gBrowser.addTab();
gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
openScratchpad(startTest);
}, true);
content.location = "data:text/html,<p>test reverting to last saved state of" +
" a file </p>";
}

View File

@ -36,6 +36,9 @@
<!ENTITY openRecentMenu.label "Open Recent">
<!ENTITY openRecentMenu.accesskey "R">
<!ENTITY revertCmd.label "Revert…">
<!ENTITY revertCmd.accesskey "t">
<!ENTITY saveFileCmd.label "Save">
<!ENTITY saveFileCmd.accesskey "S">
<!ENTITY saveFileCmd.commandkey "s">

View File

@ -54,6 +54,14 @@ confirmClose=Do you want to save the changes you made to this scratchpad?
# you try to close a scratchpad with unsaved changes.
confirmClose.title=Unsaved Changes
# LOCALIZATION NOTE (confirmRevert): This is message in the prompt dialog when
# you try to revert unsaved content of scratchpad.
confirmRevert=Do you want to revert the changes you made to this scratchpad?
# LOCALIZATION NOTE (confirmRevert.title): This is title of the prompt dialog when
# you try to revert unsaved content of scratchpad.
confirmRevert.title=Revert Changes
# LOCALIZATION NOTE (scratchpadIntro): This is a multi-line comment explaining
# how to use the Scratchpad. Note that this should be a valid JavaScript
# comment inside /* and */.