Bug 684546 - The Scratchpad editor's undo/redo state is preserved across file loads. r=msucan

This commit is contained in:
Kenny Heaton 2011-12-20 20:03:16 +02:00
parent a4f4fcb886
commit cef977575f
7 changed files with 249 additions and 2 deletions

View File

@ -24,6 +24,7 @@
* Erik Vold <erikvvold@gmail.com>
* David Dahl <ddahl@mozilla.com>
* Mihai Sucan <mihai.sucan@gmail.com>
* Kenny Heaton <kennyheaton@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -583,6 +584,7 @@ var Scratchpad = {
content = NetUtil.readInputStreamToString(aInputStream,
aInputStream.available());
self.setText(content);
self.editor.resetUndo();
}
else if (!aSilentError) {
window.alert(self.strings.GetStringFromName("openFile.failed"));

View File

@ -60,6 +60,7 @@ _BROWSER_TEST_FILES = \
browser_scratchpad_bug_669612_unsaved.js \
head.js \
browser_scratchpad_bug_653427_confirm_close.js \
browser_scratchpad_bug684546_reset_undo.js \
libs:: $(_BROWSER_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)

View File

@ -0,0 +1,155 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
// Reference to the Scratchpad chrome window object.
let gScratchpadWindow;
// Reference to the Scratchpad object.
let gScratchpad;
// Reference to the temporary nsIFile we will work with.
let gFileA;
let gFileB;
// The temporary file content.
let gFileAContent = "// File A ** Hello World!";
let gFileBContent = "// File B ** Goodbye All";
// Help track if one or both files are saved
let gFirstFileSaved = false;
function test()
{
waitForExplicitFinish();
gBrowser.selectedTab = gBrowser.addTab();
gBrowser.selectedBrowser.addEventListener("load", function browserLoad() {
gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true);
openScratchpad(runTests);
}, true);
content.location = "data:text/html,<p>test that undo get's reset after file load in Scratchpad";
}
function runTests()
{
gScratchpad = gScratchpadWindow.Scratchpad;
// Create a temporary file.
gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]);
gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]);
gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
// Write the temporary file.
let foutA = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
foutA.init(gFileA.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
0644, foutA.DEFER_OPEN);
let foutB = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
foutB.init(gFileB.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
0644, foutB.DEFER_OPEN);
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let fileContentStreamA = converter.convertToInputStream(gFileAContent);
let fileContentStreamB = converter.convertToInputStream(gFileBContent);
NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved);
NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved);
}
function tempFileSaved(aStatus)
{
let success = Components.isSuccessCode(aStatus);
ok(success, "a temporary file was saved successfully");
if (!success)
{
finish();
return;
}
if (gFirstFileSaved && success)
{
ok((gFirstFileSaved && success), "Both files loaded");
// Import the file A into Scratchpad.
gScratchpad.importFromFile(gFileA.QueryInterface(Ci.nsILocalFile), true,
fileAImported);
}
gFirstFileSaved = success;
}
function fileAImported(aStatus, aFileContent)
{
ok(Components.isSuccessCode(aStatus),
"the temporary file A was imported successfully with Scratchpad");
is(aFileContent, gFileAContent, "received data is correct");
is(gScratchpad.getText(), gFileAContent, "the editor content is correct");
gScratchpad.setText("new text", gScratchpad.getText().length);
is(gScratchpad.getText(), gFileAContent + "new text", "text updated correctly");
gScratchpad.undo();
is(gScratchpad.getText(), gFileAContent, "undo works");
gScratchpad.redo();
is(gScratchpad.getText(), gFileAContent + "new text", "redo works");
// Import the file B into Scratchpad.
gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true,
fileBImported);
}
function fileBImported(aStatus, aFileContent)
{
ok(Components.isSuccessCode(aStatus),
"the temporary file B was imported successfully with Scratchpad");
is(aFileContent, gFileBContent, "received data is correct");
is(gScratchpad.getText(), gFileBContent, "the editor content is correct");
ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load");
gScratchpad.undo();
is(gScratchpad.getText(), gFileBContent,
"the editor content is still correct after undo");
gScratchpad.setText("new text", gScratchpad.getText().length);
is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly");
gScratchpad.undo();
is(gScratchpad.getText(), gFileBContent, "undo works");
ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)");
gScratchpad.redo();
is(gScratchpad.getText(), gFileBContent + "new text", "redo works");
// Done!
finish();
}
registerCleanupFunction(function() {
if (gFileA && gFileA.exists())
{
gFileA.remove(false);
gFileA = null;
}
if (gFileB && gFileB.exists())
{
gFileB.remove(false);
gFileB = null;
}
gScratchpad = null;
});

View File

@ -21,6 +21,7 @@
*
* Contributor(s):
* Mihai Sucan <mihai.sucan@gmail.com> (original author)
* Kenny Heaton <kennyheaton@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -553,6 +554,14 @@ SourceEditor.prototype = {
return this._undoStack.canRedo();
},
/**
* Reset the Undo stack
*/
resetUndo: function SE_resetUndo()
{
this._undoStack.reset();
},
/**
* Start a compound change in the editor. Compound changes are grouped into
* only one change that you can undo later, after you invoke

View File

@ -21,6 +21,7 @@
*
* Contributor(s):
* Mihai Sucan <mihai.sucan@gmail.com> (original author)
* Kenny Heaton <kennyheaton@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -148,8 +149,7 @@ SourceEditor.prototype = {
aConfig.undoLimit || SourceEditor.DEFAULTS.UNDO_LIMIT;
// Make sure that the transactions stack is clean.
this._editor.transactionManager.clear();
this._editor.resetModificationCount();
this.resetUndo();
// Add the edit action listener so we can fire the SourceEditor TextChanged
// events.
@ -406,6 +406,15 @@ SourceEditor.prototype = {
return canRedo.value;
},
/**
* Reset the Undo stack
*/
resetUndo: function SE_resetUndo()
{
this._editor.transactionManager.clear();
this._editor.resetModificationCount();
},
/**
* Start a compound change in the editor. Compound changes are grouped into
* only one change that you can undo later, after you invoke

View File

@ -50,6 +50,7 @@ _BROWSER_TEST_FILES = \
browser_bug687573_vscroll.js \
browser_bug687568_pagescroll.js \
browser_bug687580_drag_and_drop.js \
browser_bug684546_reset_undo.js \
browser_bug695035_middle_click_paste.js \
browser_bug687160_line_api.js \
head.js \

View File

@ -0,0 +1,70 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.import("resource:///modules/source-editor.jsm");
let testWin;
let editor;
function test()
{
waitForExplicitFinish();
const windowUrl = "data:application/vnd.mozilla.xul+xml,<?xml version='1.0'?>" +
"<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'" +
" title='test for bug 684546 - reset undo' width='300' height='500'>" +
"<box flex='1'/></window>";
const windowFeatures = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
testWin = Services.ww.openWindow(null, windowUrl, "_blank", windowFeatures, null);
testWin.addEventListener("load", function onWindowLoad() {
testWin.removeEventListener("load", onWindowLoad, false);
waitForFocus(initEditor, testWin);
}, false);
}
function initEditor()
{
let box = testWin.document.querySelector("box");
editor = new SourceEditor();
editor.init(box, {}, editorLoaded);
}
function editorLoaded()
{
editor.setText("First");
editor.setText("Second", 5);
is(editor.getText(), "FirstSecond", "text set correctly.");
editor.undo();
is(editor.getText(), "First", "undo works.");
editor.redo();
is(editor.getText(), "FirstSecond", "redo works.");
editor.resetUndo();
ok(!editor.canUndo(), "canUndo() is correct");
ok(!editor.canRedo(), "canRedo() is correct");
editor.undo();
is(editor.getText(), "FirstSecond", "reset undo works correctly");
editor.setText("Third", 11);
is(editor.getText(), "FirstSecondThird", "text set correctly");
editor.undo();
is(editor.getText(), "FirstSecond", "undo works after reset");
editor.redo();
is(editor.getText(), "FirstSecondThird", "redo works after reset");
editor.resetUndo();
ok(!editor.canUndo(), "canUndo() is correct (again)");
ok(!editor.canRedo(), "canRedo() is correct (again)");
editor.undo();
is(editor.getText(), "FirstSecondThird", "reset undo still works correctly");
finish();
}
registerCleanupFunction(function() {
editor.destroy();
testWin.close();
testWin = editor = null;
});