Add some tests for history.js. Update History commands for getText() function

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1865 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2010-11-12 23:10:19 +00:00
parent 4a133d490d
commit 4a514ee231
2 changed files with 249 additions and 10 deletions

View File

@ -41,7 +41,9 @@ var removedElements = {};
* void apply();
* void unapply();
* Element[] elements();
* static type();
* String getText();
*
* static String type();
* }
*/
@ -53,8 +55,8 @@ var removedElements = {};
// elem - The DOM element that was moved
// oldNextSibling - The element's next sibling before it was moved
// oldParent - The element's parent before it was moved
// opt_desc - An optional string visible to user related to this change
svgedit.history.MoveElementCommand = function(elem, oldNextSibling, oldParent, opt_desc) {
// text - An optional string visible to user related to this change
svgedit.history.MoveElementCommand = function(elem, oldNextSibling, oldParent, text) {
this.elem = elem;
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
this.oldNextSibling = oldNextSibling;
@ -65,6 +67,11 @@ svgedit.history.MoveElementCommand = function(elem, oldNextSibling, oldParent, o
svgedit.history.MoveElementCommand.type = function() { return 'svgedit.history.MoveElementCommand'; }
svgedit.history.MoveElementCommand.prototype.type = svgedit.history.MoveElementCommand.type;
// Function: svgedit.history.MoveElementCommand.getText
svgedit.history.MoveElementCommand.prototype.getText = function() {
return this.text;
};
// Function: svgedit.history.MoveElementCommand.apply
// Re-positions the element
svgedit.history.MoveElementCommand.prototype.apply = function() {
@ -99,6 +106,11 @@ svgedit.history.InsertElementCommand = function(elem, text) {
svgedit.history.InsertElementCommand.type = function() { return 'svgedit.history.InsertElementCommand'; }
svgedit.history.InsertElementCommand.prototype.type = svgedit.history.InsertElementCommand.type;
// Function: svgedit.history.InsertElementCommand.getText
svgedit.history.InsertElementCommand.prototype.getText = function() {
return this.text;
};
// Function: svgedit.history.InsertElementCommand.apply
// Re-Inserts the new element
svgedit.history.InsertElementCommand.prototype.apply = function() {
@ -138,6 +150,11 @@ svgedit.history.RemoveElementCommand = function(elem, parent, text) {
svgedit.history.RemoveElementCommand.type = function() { return 'svgedit.history.RemoveElementCommand'; }
svgedit.history.RemoveElementCommand.prototype.type = svgedit.history.RemoveElementCommand.type;
// Function: svgedit.history.RemoveElementCommand.getText
svgedit.history.RemoveElementCommand.prototype.getText = function() {
return this.text;
};
// Function: RemoveElementCommand.apply
// Re-removes the new element
svgedit.history.RemoveElementCommand.prototype.apply = function() {
@ -185,6 +202,11 @@ svgedit.history.ChangeElementCommand = function(elem, attrs, text) {
svgedit.history.ChangeElementCommand.type = function() { return 'svgedit.history.ChangeElementCommand'; }
svgedit.history.ChangeElementCommand.prototype.type = svgedit.history.ChangeElementCommand.type;
// Function: svgedit.history.ChangeElementCommand.getText
svgedit.history.ChangeElementCommand.prototype.getText = function() {
return this.text;
};
// Function: svgedit.history.ChangeElementCommand.apply
// Performs the stored change action
svgedit.history.ChangeElementCommand.prototype.apply = function() {
@ -283,6 +305,11 @@ svgedit.history.BatchCommand = function(text) {
svgedit.history.BatchCommand.type = function() { return 'svgedit.history.BatchCommand'; }
svgedit.history.BatchCommand.prototype.type = svgedit.history.BatchCommand.type;
// Function: svgedit.history.BatchCommand.getText
svgedit.history.BatchCommand.prototype.getText = function() {
return this.text;
};
// Function: svgedit.history.BatchCommand.apply
// Runs "apply" on all subcommands
svgedit.history.BatchCommand.prototype.apply = function() {
@ -348,7 +375,7 @@ svgedit.history.BatchCommand.prototype.isEmpty = function() {
// historyEventHandler - an object that conforms to the HistoryEventHandler interface
// (see above)
svgedit.history.UndoManager = function(historyEventHandler) {
this.handler_ = historyEventHandler;
this.handler_ = historyEventHandler || null;
this.undoStackPointer = 0;
this.undoStack = [];
@ -383,14 +410,14 @@ svgedit.history.UndoManager.prototype.getRedoStackSize = function() {
// Returns:
// String associated with the next undo command
svgedit.history.UndoManager.prototype.getNextUndoCommandText = function() {
return this.undoStackPointer > 0 ? this.undoStack[this.undoStackPointer-1].text : "";
return this.undoStackPointer > 0 ? this.undoStack[this.undoStackPointer-1].getText() : "";
};
// Function: svgedit.history.UndoManager.getNextRedoCommandText
// Returns:
// String associated with the next redo command
svgedit.history.UndoManager.prototype.getNextRedoCommandText = function() {
return this.undoStackPointer < this.undoStack.length ? this.undoStack[this.undoStackPointer].text : "";
return this.undoStackPointer < this.undoStack.length ? this.undoStack[this.undoStackPointer].getText() : "";
};
// Function: svgedit.history.UndoManager.undo
@ -399,12 +426,16 @@ svgedit.history.UndoManager.prototype.undo = function() {
if (this.undoStackPointer > 0) {
var cmd = this.undoStack[--this.undoStackPointer];
if (this.handler_ != null) {
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_UNAPPLY, cmd);
}
cmd.unapply();
if (this.handler_ != null) {
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_UNAPPLY, cmd);
}
}
};
// Function: svgedit.history.UndoManager.redo
@ -413,12 +444,16 @@ svgedit.history.UndoManager.prototype.redo = function() {
if (this.undoStackPointer < this.undoStack.length && this.undoStack.length > 0) {
var cmd = this.undoStack[this.undoStackPointer++];
if (this.handler_ != null) {
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_APPLY, cmd);
}
cmd.apply();
if (this.handler_ != null) {
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_APPLY, cmd);
}
}
};
// Function: svgedit.history.UndoManager.addCommandToHistory

View File

@ -18,9 +18,36 @@
var svgns = 'http://www.w3.org/2000/svg';
var svg = document.createElementNS(svgns, 'svg');
var undoMgr = null;
module('svgedit.history Module');
var MockCommand = function(opt_text) {
this.text_ = opt_text;
};
MockCommand.prototype.apply = function() {
};
MockCommand.prototype.unapply = function() {
};
MockCommand.prototype.getText = function() {
return this.text_;
};
MockCommand.prototype.elements = function() {
return [];
};
var MockHistoryEventHandler = function() {
};
MockHistoryEventHandler.prototype.handleHistoryEvent = function(eventType, command) {
};
function setUp() {
undoMgr = new svgedit.history.UndoManager();
}
function tearDown() {
undoMgr = null;
}
test('Test svgedit.history package', function() {
expect(13);
@ -38,6 +65,183 @@
equals(typeof svgedit.history.BatchCommand, typeof function(){});
equals(typeof svgedit.history.UndoManager, typeof function(){});
});
test('Test UndoManager methods', function() {
expect(14);
setUp();
ok(undoMgr);
ok(undoMgr.addCommandToHistory);
ok(undoMgr.getUndoStackSize);
ok(undoMgr.getRedoStackSize);
ok(undoMgr.resetUndoStack);
ok(undoMgr.getNextUndoCommandText);
ok(undoMgr.getNextRedoCommandText);
equals(typeof undoMgr, typeof {});
equals(typeof undoMgr.addCommandToHistory, typeof function(){});
equals(typeof undoMgr.getUndoStackSize, typeof function(){});
equals(typeof undoMgr.getRedoStackSize, typeof function(){});
equals(typeof undoMgr.resetUndoStack, typeof function(){});
equals(typeof undoMgr.getNextUndoCommandText, typeof function(){});
equals(typeof undoMgr.getNextRedoCommandText, typeof function(){});
tearDown();
});
test('Test UndoManager.addCommandToHistory() function', function() {
expect(3);
setUp();
equals(undoMgr.getUndoStackSize(), 0);
undoMgr.addCommandToHistory(new MockCommand());
equals(undoMgr.getUndoStackSize(), 1);
undoMgr.addCommandToHistory(new MockCommand());
equals(undoMgr.getUndoStackSize(), 2);
tearDown();
});
test('Test UndoManager.getUndoStackSize() and getRedoStackSize() functions', function() {
expect(18);
setUp();
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
equals(undoMgr.getUndoStackSize(), 3);
equals(undoMgr.getRedoStackSize(), 0);
undoMgr.undo();
equals(undoMgr.getUndoStackSize(), 2);
equals(undoMgr.getRedoStackSize(), 1);
undoMgr.undo();
equals(undoMgr.getUndoStackSize(), 1);
equals(undoMgr.getRedoStackSize(), 2);
undoMgr.undo();
equals(undoMgr.getUndoStackSize(), 0);
equals(undoMgr.getRedoStackSize(), 3);
undoMgr.undo();
equals(undoMgr.getUndoStackSize(), 0);
equals(undoMgr.getRedoStackSize(), 3);
undoMgr.redo();
equals(undoMgr.getUndoStackSize(), 1);
equals(undoMgr.getRedoStackSize(), 2);
undoMgr.redo();
equals(undoMgr.getUndoStackSize(), 2);
equals(undoMgr.getRedoStackSize(), 1);
undoMgr.redo();
equals(undoMgr.getUndoStackSize(), 3);
equals(undoMgr.getRedoStackSize(), 0);
undoMgr.redo();
equals(undoMgr.getUndoStackSize(), 3);
equals(undoMgr.getRedoStackSize(), 0);
tearDown();
});
test('Test UndoManager.resetUndoStackSize() function', function() {
expect(4);
setUp();
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.addCommandToHistory(new MockCommand());
undoMgr.undo();
equals(undoMgr.getUndoStackSize(), 2);
equals(undoMgr.getRedoStackSize(), 1);
undoMgr.resetUndoStack();
equals(undoMgr.getUndoStackSize(), 0);
equals(undoMgr.getRedoStackSize(), 0);
tearDown();
});
test('Test UndoManager.getNextUndoCommandText() function', function() {
expect(9);
setUp();
equals(undoMgr.getNextUndoCommandText(), '');
undoMgr.addCommandToHistory(new MockCommand('First'));
undoMgr.addCommandToHistory(new MockCommand('Second'));
undoMgr.addCommandToHistory(new MockCommand('Third'));
equals(undoMgr.getNextUndoCommandText(), 'Third');
undoMgr.undo();
equals(undoMgr.getNextUndoCommandText(), 'Second');
undoMgr.undo();
equals(undoMgr.getNextUndoCommandText(), 'First');
undoMgr.undo();
equals(undoMgr.getNextUndoCommandText(), '');
undoMgr.redo();
equals(undoMgr.getNextUndoCommandText(), 'First');
undoMgr.redo();
equals(undoMgr.getNextUndoCommandText(), 'Second');
undoMgr.redo();
equals(undoMgr.getNextUndoCommandText(), 'Third');
undoMgr.redo();
equals(undoMgr.getNextUndoCommandText(), 'Third');
tearDown();
});
test('Test UndoManager.getNextRedoCommandText() function', function() {
expect(8);
setUp();
equals(undoMgr.getNextRedoCommandText(), '');
undoMgr.addCommandToHistory(new MockCommand('First'));
undoMgr.addCommandToHistory(new MockCommand('Second'));
undoMgr.addCommandToHistory(new MockCommand('Third'));
equals(undoMgr.getNextRedoCommandText(), '');
undoMgr.undo();
equals(undoMgr.getNextRedoCommandText(), 'Third');
undoMgr.undo();
equals(undoMgr.getNextRedoCommandText(), 'Second');
undoMgr.undo();
equals(undoMgr.getNextRedoCommandText(), 'First');
undoMgr.redo();
equals(undoMgr.getNextRedoCommandText(), 'Second');
undoMgr.redo();
equals(undoMgr.getNextRedoCommandText(), 'Third');
undoMgr.redo();
equals(undoMgr.getNextRedoCommandText(), '');
tearDown();
});
});
</script>
</head>