Search in sources :

Example 1 with UndoManager

use of com.sldeditor.common.undo.UndoManager in project sldeditor by robward-scisys.

the class UndoManagerTest method testUndoMechanism.

/**
 * Test method for adding undo events and calling undo and redo.
 */
@Test
public void testUndoMechanism() {
    UndoManager.destroyInstance();
    DummyUndo listener = new DummyUndo();
    DummyUndoParent parentListener = new DummyUndoParent();
    UndoManager instance = UndoManager.getInstance();
    instance.addListener(listener);
    // CHECKSTYLE:OFF
    UndoEvent event1 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
    UndoEvent event2 = new UndoEvent(parentListener, FieldIdEnum.ANCHOR_POINT_H, Integer.valueOf(2), Integer.valueOf(3));
    UndoEvent event3 = new UndoEvent(parentListener, FieldIdEnum.DISPLACEMENT_Y, Integer.valueOf(20), Integer.valueOf(900));
    UndoEvent event4 = new UndoEvent(parentListener, FieldIdEnum.DEFAULT_STYLE, Boolean.TRUE, Boolean.FALSE);
    UndoEvent event5 = new UndoEvent(parentListener, FieldIdEnum.HALO_RADIUS, Double.valueOf(44.3), Double.valueOf(67.5));
    // CHECKSTYLE:ON
    instance.addUndoEvent(event1);
    assertTrue(listener.undoAllowed);
    assertFalse(listener.redoAllowed);
    instance.addUndoEvent(event2);
    assertTrue(listener.undoAllowed);
    assertFalse(listener.redoAllowed);
    instance.addUndoEvent(event3);
    assertTrue(listener.undoAllowed);
    assertFalse(listener.redoAllowed);
    instance.addUndoEvent(event4);
    assertTrue(listener.undoAllowed);
    assertFalse(listener.redoAllowed);
    instance.addUndoEvent(event5);
    assertTrue(listener.undoAllowed);
    assertFalse(listener.redoAllowed);
    // Test going beyond end of last item
    assertNull(parentListener.redoAction);
    assertNull(parentListener.undoAction);
    instance.redo();
    assertNull(parentListener.redoAction);
    assertNull(parentListener.undoAction);
    instance.undo();
    assertNull(parentListener.redoAction);
    assertEquals(event5, parentListener.undoAction);
    assertTrue(listener.undoAllowed);
    assertTrue(listener.redoAllowed);
    instance.redo();
    assertNull(parentListener.undoAction);
    assertEquals(event5, parentListener.redoAction);
    instance.undo();
    instance.undo();
    instance.undo();
    instance.undo();
    assertNull(parentListener.redoAction);
    assertEquals(event2, parentListener.undoAction);
    instance.redo();
    assertNull(parentListener.undoAction);
    assertEquals(event2, parentListener.redoAction);
    // Test going beyond start of list
    instance.undo();
    assertEquals(true, listener.redoAllowed);
    assertEquals(true, listener.undoAllowed);
    instance.undo();
    assertEquals(true, listener.redoAllowed);
    assertEquals(false, listener.undoAllowed);
    instance.undo();
    assertEquals(true, listener.redoAllowed);
    assertEquals(false, listener.undoAllowed);
    assertEquals(event1, parentListener.undoAction);
    assertNull(parentListener.redoAction);
    instance.redo();
    assertNull(parentListener.undoAction);
    assertEquals(event1, parentListener.redoAction);
    // Add new event when the undo pointer is not at the end
    UndoEvent event10 = new UndoEvent(parentListener, FieldIdEnum.ALIGN, Integer.valueOf(19), Integer.valueOf(20));
    // Expecting the undo events from the pointer onwards to be deleted and replaced with new
    // event
    parentListener.undoAction = null;
    parentListener.redoAction = null;
    instance.addUndoEvent(event10);
    instance.redo();
    // Should be at the end
    assertNull(parentListener.undoAction);
    assertNull(parentListener.redoAction);
    instance.undo();
    assertNull(parentListener.redoAction);
    assertEquals(event10, parentListener.undoAction);
}
Also used : UndoEvent(com.sldeditor.common.undo.UndoEvent) UndoManager(com.sldeditor.common.undo.UndoManager) Test(org.junit.Test)

Aggregations

UndoEvent (com.sldeditor.common.undo.UndoEvent)1 UndoManager (com.sldeditor.common.undo.UndoManager)1 Test (org.junit.Test)1