Bug 684546 - The Scratchpad editor's undo/redo state is preserved across file loads. r=msucan
authorKenny Heaton <kennyheaton@gmail.com>
Tue, 20 Dec 2011 20:03:16 +0200
changeset 85582 a98e349f29f7f8f2ac0ec6d6b36b89eb9b7905a2
parent 85581 fe77d3b0aeceac03a4241791e4f2a2c6edaf1ce5
child 85583 cd921d073b226cfa86ce6cb7c91307929ebf45c0
push idunknown
push userunknown
push dateunknown
reviewersmsucan
bugs684546
milestone11.0a1
Bug 684546 - The Scratchpad editor's undo/redo state is preserved across file loads. r=msucan
browser/devtools/scratchpad/scratchpad.js
browser/devtools/scratchpad/test/Makefile.in
browser/devtools/scratchpad/test/browser_scratchpad_bug684546_reset_undo.js
browser/devtools/sourceeditor/source-editor-orion.jsm
browser/devtools/sourceeditor/source-editor-textarea.jsm
browser/devtools/sourceeditor/test/Makefile.in
browser/devtools/sourceeditor/test/browser_bug684546_reset_undo.js
--- a/browser/devtools/scratchpad/scratchpad.js
+++ b/browser/devtools/scratchpad/scratchpad.js
@@ -19,16 +19,17 @@
  * Portions created by the Initial Developer are Copyright (C) 2011
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Rob Campbell <robcee@mozilla.com> (original author)
  *   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
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -578,16 +579,17 @@ var Scratchpad = {
     let self = this;
     NetUtil.asyncFetch(channel, function(aInputStream, aStatus) {
       let content = null;
 
       if (Components.isSuccessCode(aStatus)) {
         content = NetUtil.readInputStreamToString(aInputStream,
                                                   aInputStream.available());
         self.setText(content);
+        self.editor.resetUndo();
       }
       else if (!aSilentError) {
         window.alert(self.strings.GetStringFromName("openFile.failed"));
       }
 
       if (aCallback) {
         aCallback.call(self, aStatus, content);
       }
--- a/browser/devtools/scratchpad/test/Makefile.in
+++ b/browser/devtools/scratchpad/test/Makefile.in
@@ -55,11 +55,12 @@ include $(topsrcdir)/config/rules.mk
 		browser_scratchpad_bug_660560_tab.js \
 		browser_scratchpad_open.js \
 		browser_scratchpad_restore.js \
 		browser_scratchpad_bug_679467_falsy.js \
 		browser_scratchpad_bug_699130_edit_ui_updates.js \
 		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)
new file mode 100644
--- /dev/null
+++ b/browser/devtools/scratchpad/test/browser_scratchpad_bug684546_reset_undo.js
@@ -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;
+});
--- a/browser/devtools/sourceeditor/source-editor-orion.jsm
+++ b/browser/devtools/sourceeditor/source-editor-orion.jsm
@@ -16,16 +16,17 @@
  *
  * The Initial Developer of the Original Code is
  * The Mozilla Foundation.
  * Portions created by the Initial Developer are Copyright (C) 2011
  * the Initial Developer. All Rights Reserved.
  *
  * 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
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -549,16 +550,24 @@ SourceEditor.prototype = {
    *         True if there are changes that can be repeated, false otherwise.
    */
   canRedo: function SE_canRedo()
   {
     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
    * endCompoundChange().
    */
   startCompoundChange: function SE_startCompoundChange()
   {
     this._undoStack.startCompoundChange();
   },
--- a/browser/devtools/sourceeditor/source-editor-textarea.jsm
+++ b/browser/devtools/sourceeditor/source-editor-textarea.jsm
@@ -16,16 +16,17 @@
  *
  * The Initial Developer of the Original Code is
  * The Mozilla Foundation.
  * Portions created by the Initial Developer are Copyright (C) 2011
  * the Initial Developer. All Rights Reserved.
  *
  * 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
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -143,18 +144,17 @@ SourceEditor.prototype = {
 
     // Mimic the mode change.
     this.setMode(aConfig.mode || SourceEditor.DEFAULTS.MODE);
 
     this._editor.transactionManager.maxTransactionCount =
       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.
     this._editActionListener = new EditActionListener(this);
     this._editor.addEditActionListener(this._editActionListener);
 
     this._lineDelimiter = win.navigator.platform.indexOf("Win") > -1 ?
                           "\r\n" : "\n";
@@ -402,16 +402,25 @@ SourceEditor.prototype = {
   {
     let isEnabled = {};
     let canRedo = {};
     this._editor.canRedo(isEnabled, canRedo);
     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
    * endCompoundChange().
    */
   startCompoundChange: function SE_startCompoundChange()
   {
     this._editor.beginTransaction();
   },
--- a/browser/devtools/sourceeditor/test/Makefile.in
+++ b/browser/devtools/sourceeditor/test/Makefile.in
@@ -45,14 +45,15 @@ include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _BROWSER_TEST_FILES = \
 		browser_sourceeditor_initialization.js \
 		browser_bug684862_paste_html.js \
 		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 \
 
 libs:: $(_BROWSER_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/browser/devtools/sourceeditor/test/browser_bug684546_reset_undo.js
@@ -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;
+});