# HG changeset patch # Parent 05d0b012b15377ce95519c88ef324bb2ca5b2b32 diff --git a/openide.text/test/unit/src/org/openide/text/UndoRedoAfterSaveTest.java b/openide.text/test/unit/src/org/openide/text/UndoRedoAfterSaveTest.java new file mode 100644 --- /dev/null +++ b/openide.text/test/unit/src/org/openide/text/UndoRedoAfterSaveTest.java @@ -0,0 +1,352 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ + + +package org.openide.text; + + + +import java.beans.PropertyChangeListener; +import java.io.IOException; +import java.util.ArrayList; +import javax.swing.text.*; +import org.netbeans.junit.*; +import org.openide.awt.UndoRedo; +import org.openide.util.Exceptions; + +/** + * Bug 21237 - Redo should be possible after save. + * + * @author Ernie Rael + */ +public class UndoRedoAfterSaveTest extends NbTestCase implements CloneableEditorSupport.Env { + static { + System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); + } + /** the support to work with */ + private CES support; + // Env variables + private String content = "Hello"; + private boolean valid = true; + private boolean modified = false; + /** if not null contains message why this document cannot be modified */ + private String cannotBeModified; + private java.util.Date date = new java.util.Date (); + private java.util.List propL = new ArrayList(); + private java.beans.VetoableChangeListener vetoL; + + /** Creates new TextTest */ + public UndoRedoAfterSaveTest (String s) { + super(s); + } + + protected javax.swing.text.EditorKit createEditorKit() { + return new NbLikeEditorKit(); + } + + UndoRedo.Manager ur() { + return support.getUndoRedo(); + } + + @Override + protected void setUp () { + support = new CES (this, org.openide.util.Lookup.EMPTY); + } + + public void testRedoAfterSave() throws Exception { + content = ""; + StyledDocument d = support.openDocument(); + d.insertString(d.getLength(), "a", null); + + d.insertString(d.getLength(), "b", null); + + assertEquals("insert: data", "ab", d.getText(0, d.getLength())); + assertTrue("insert: modified", support.isModified()); + assertTrue("insert: can undo", ur().canUndo()); + assertFalse("insert: no redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo: data", "a", d.getText(0, d.getLength())); + assertTrue("undo: modified", support.isModified()); + assertTrue("undo: can undo", ur().canUndo()); + assertTrue("undo: can redo", ur().canRedo()); + + support.saveDocument (); + assertFalse("save: not modified", support.isModified()); + assertTrue("save: can undo", ur().canUndo()); + assertTrue("save: can redo", ur().canRedo()); + + ur().redo(); + assertEquals("redo: data", "ab", d.getText(0, d.getLength())); + assertTrue("redo: modified", support.isModified()); + assertTrue("redo: can undo", ur().canUndo()); + assertFalse("redo: no redo", ur().canRedo()); + } + + public void testUndoAfterSaveSimple() throws Exception { + content = ""; + StyledDocument d = support.openDocument(); + d.insertString(d.getLength(), "a", null); + + support.saveDocument (); + assertFalse("save: not modified", support.isModified()); + assertTrue("save: can undo", ur().canUndo()); + assertFalse("save: no redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo1, before savePoint: data", "", d.getText(0, d.getLength())); + + assertTrue("undo1, before savePoint: modified", support.isModified()); + assertFalse("undo1, before savePoint: can undo", ur().canUndo()); + assertTrue("undo1, before savePoint: can redo", ur().canRedo()); + } + + public void testUndoAfterSaveExplore() throws Exception { + content = ""; + StyledDocument d = support.openDocument(); + d.insertString(d.getLength(), "a", null); + + support.saveDocument (); + assertFalse("save: not modified", support.isModified()); + assertTrue("save: can undo", ur().canUndo()); + assertFalse("save: no redo", ur().canRedo()); + + d.insertString(d.getLength(), "b", null); + assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); + assertTrue("insert, after savePoint: modified", support.isModified()); + assertTrue("insert, after savePoint: can undo", ur().canUndo()); + assertFalse("insert, after savePoint: no redo", ur().canRedo()); + + support.saveDocument (); + assertFalse("save2: not modified", support.isModified()); + assertTrue("save2: can undo", ur().canUndo()); + assertFalse("save2: no redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo1, before savePoint2: data", "a", d.getText(0, d.getLength())); + assertTrue("undo1, before savePoint2: modified", support.isModified()); + assertTrue("undo1, before savePoint2: can undo", ur().canUndo()); + assertTrue("undo1, before savePoint2: can redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo2, before savePoint2: data", "", d.getText(0, d.getLength())); + assertTrue("undo2, before savePoint2: modified", support.isModified()); + assertFalse("undo2, before savePoint2: can undo", ur().canUndo()); + assertTrue("undo2, before savePoint2: can redo", ur().canRedo()); + + ur().redo(); + assertEquals("redo1, before savePoint2: data", "a", d.getText(0, d.getLength())); + assertTrue("redo1, before savePoint2: modified", support.isModified()); + assertTrue("redo1, before savePoint2: can undo", ur().canUndo()); + assertTrue("redo1, before savePoint2: can redo", ur().canRedo()); + } + + public void testUndoAfterSave() throws Exception { + content = ""; + StyledDocument d = support.openDocument(); + d.insertString(d.getLength(), "a", null); + + support.saveDocument (); + assertFalse("save: not modified", support.isModified()); + assertTrue("save: can undo", ur().canUndo()); + assertFalse("save: no redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo1, before savePoint: data", "", d.getText(0, d.getLength())); + assertTrue("undo1, before savePoint: modified", support.isModified()); + assertFalse("undo1, before savePoint: can undo", ur().canUndo()); + assertTrue("undo1, before savePoint: can redo", ur().canRedo()); + + ur().redo(); // back to the save point + + d.insertString(d.getLength(), "b", null); + + assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); + assertTrue("insert, after savePoint: modified", support.isModified()); + assertTrue("insert, after savePoint: can undo", ur().canUndo()); + assertFalse("insert, after savePoint: no redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo, at savePoint: data", "a", d.getText(0, d.getLength())); + assertFalse("undo, at savePoint: not modified", support.isModified()); + assertTrue("undo, at savePoint: can undo", ur().canUndo()); + assertTrue("undo, at savePoint: can redo", ur().canRedo()); + + ur().undo(); + assertEquals("undo2, before savePoint: data", "", d.getText(0, d.getLength())); + assertTrue("undo2, before savePoint: modified", support.isModified()); + assertFalse("undo2, before savePoint: can undo", ur().canUndo()); + assertTrue("undo2, before savePoint: can redo", ur().canRedo()); + + ur().redo(); + assertEquals("redo, at savePoint: data", "a", d.getText(0, d.getLength())); + assertFalse("redo, at savePoint: not modified", support.isModified()); + assertTrue("redo, at savePoint: can undo", ur().canUndo()); + assertTrue("redo, at savePoint: can redo", ur().canRedo()); + } + + // + // Implementation of the CloneableEditorSupport.Env + // + + @Override + public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { + propL.add (l); + } + @Override + public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { + propL.remove (l); + } + + @Override + public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertNull ("This is the first veto listener", vetoL); + vetoL = l; + } + @Override + public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { + assertEquals ("Removing the right veto one", vetoL, l); + vetoL = null; + } + + @Override + public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { + return support; + } + + @Override + public String getMimeType() { + return "text/plain"; + } + + @Override + public java.util.Date getTime() { + return date; + } + + @Override + public java.io.InputStream inputStream() throws java.io.IOException { + return new java.io.ByteArrayInputStream (content.getBytes ()); + } + @Override + public java.io.OutputStream outputStream() throws java.io.IOException { + class ContentStream extends java.io.ByteArrayOutputStream { + @Override + public void close () throws java.io.IOException { + super.close (); + content = new String (toByteArray ()); + } + } + + return new ContentStream (); + } + + @Override + public boolean isValid() { + return valid; + } + + @Override + public boolean isModified() { + return modified; + } + + @Override + public void markModified() throws java.io.IOException { + if (cannotBeModified != null) { + IOException e = new IOException (); + Exceptions.attachLocalizedMessage(e, cannotBeModified); + throw e; + } + + modified = true; + } + + @Override + public void unmarkModified() { + modified = false; + } + + /** Implementation of the CES */ + private final class CES extends CloneableEditorSupport { + public boolean plain; + + + public CES (Env env, org.openide.util.Lookup l) { + super (env, l); + } + + @Override + protected String messageName() { + return "Name"; + } + + @Override + protected String messageOpened() { + return "Opened"; + } + + @Override + protected String messageOpening() { + return "Opening"; + } + + @Override + protected String messageSave() { + return "Save"; + } + + @Override + protected String messageToolTip() { + return "ToolTip"; + } + + @Override + protected javax.swing.text.EditorKit createEditorKit() { + if (plain) { + return super.createEditorKit (); + } else { + return UndoRedoAfterSaveTest.this.createEditorKit (); + } + } + } // end of CES + +} diff --git a/openide.text/test/unit/src/org/openide/text/UndoRedoWrappingCooperationTest.java b/openide.text/test/unit/src/org/openide/text/UndoRedoWrappingCooperationTest.java --- a/openide.text/test/unit/src/org/openide/text/UndoRedoWrappingCooperationTest.java +++ b/openide.text/test/unit/src/org/openide/text/UndoRedoWrappingCooperationTest.java @@ -61,10 +61,6 @@ /** * Testing CES's UndoGroupManager; BEGIN_COMMIT_GROUP, END_COMMIT_GROUP * - * Also included are tests testSaveDocumentErrorCase and testRedoAfterSave. - * They fail for some base CES functionality. They could be moved - * to UndoRedoCooperationTest. - * * @author Ernie Rael */ public class UndoRedoWrappingCooperationTest extends NbTestCase implements CloneableEditorSupport.Env { @@ -313,7 +309,7 @@ assertTrue("undo after endChunk: can redo", ur().canRedo()); } - public void testSaveDocumentWhileActiveChunkCommon(boolean doFailCase) throws Exception { + public void testSaveDocumentWhileActiveChunk() throws Exception { content = ""; StyledDocument d = support.openDocument(); CompoundEdit ce = beginChunk(d); @@ -342,14 +338,7 @@ ur().undo(); assertEquals("undo, before save: data", "", d.getText(0, d.getLength())); - - if(doFailCase) { - // **************************************************************** - // CES BUG??? - assertTrue("undo, before save: modified", support.isModified()); - // **************************************************************** - } - + assertTrue("undo, before save: modified", support.isModified()); assertFalse("undo, before save: can undo", ur().canUndo()); assertTrue("undo, before save: can redo", ur().canRedo()); @@ -360,16 +349,6 @@ assertTrue("redo, at save: can redo", ur().canRedo()); } - public void testSaveDocumentWhileActiveChunk() throws Exception { - testSaveDocumentWhileActiveChunkCommon(false); - } - - // This fails, below is "testSaveDocumentErrorCase" without chunking, - // it also fails. - // public void testSaveDocumentWhileActiveChunkErroCase() throws Exception { - // testSaveDocumentWhileActiveChunkCommon(true); - // } - public void testNestedChunks() throws Exception { content = ""; StyledDocument d = support.openDocument();