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-75d572ba1dddmaster
parent
4a133d490d
commit
4a514ee231
|
@ -41,7 +41,9 @@ var removedElements = {};
|
||||||
* void apply();
|
* void apply();
|
||||||
* void unapply();
|
* void unapply();
|
||||||
* Element[] elements();
|
* Element[] elements();
|
||||||
* static type();
|
* String getText();
|
||||||
|
*
|
||||||
|
* static String type();
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -53,8 +55,8 @@ var removedElements = {};
|
||||||
// elem - The DOM element that was moved
|
// elem - The DOM element that was moved
|
||||||
// oldNextSibling - The element's next sibling before it was moved
|
// oldNextSibling - The element's next sibling before it was moved
|
||||||
// oldParent - The element's parent 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
|
// text - An optional string visible to user related to this change
|
||||||
svgedit.history.MoveElementCommand = function(elem, oldNextSibling, oldParent, opt_desc) {
|
svgedit.history.MoveElementCommand = function(elem, oldNextSibling, oldParent, text) {
|
||||||
this.elem = elem;
|
this.elem = elem;
|
||||||
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
|
this.text = text ? ("Move " + elem.tagName + " to " + text) : ("Move " + elem.tagName);
|
||||||
this.oldNextSibling = oldNextSibling;
|
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.type = function() { return 'svgedit.history.MoveElementCommand'; }
|
||||||
svgedit.history.MoveElementCommand.prototype.type = svgedit.history.MoveElementCommand.type;
|
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
|
// Function: svgedit.history.MoveElementCommand.apply
|
||||||
// Re-positions the element
|
// Re-positions the element
|
||||||
svgedit.history.MoveElementCommand.prototype.apply = function() {
|
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.type = function() { return 'svgedit.history.InsertElementCommand'; }
|
||||||
svgedit.history.InsertElementCommand.prototype.type = svgedit.history.InsertElementCommand.type;
|
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
|
// Function: svgedit.history.InsertElementCommand.apply
|
||||||
// Re-Inserts the new element
|
// Re-Inserts the new element
|
||||||
svgedit.history.InsertElementCommand.prototype.apply = function() {
|
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.type = function() { return 'svgedit.history.RemoveElementCommand'; }
|
||||||
svgedit.history.RemoveElementCommand.prototype.type = svgedit.history.RemoveElementCommand.type;
|
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
|
// Function: RemoveElementCommand.apply
|
||||||
// Re-removes the new element
|
// Re-removes the new element
|
||||||
svgedit.history.RemoveElementCommand.prototype.apply = function() {
|
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.type = function() { return 'svgedit.history.ChangeElementCommand'; }
|
||||||
svgedit.history.ChangeElementCommand.prototype.type = svgedit.history.ChangeElementCommand.type;
|
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
|
// Function: svgedit.history.ChangeElementCommand.apply
|
||||||
// Performs the stored change action
|
// Performs the stored change action
|
||||||
svgedit.history.ChangeElementCommand.prototype.apply = function() {
|
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.type = function() { return 'svgedit.history.BatchCommand'; }
|
||||||
svgedit.history.BatchCommand.prototype.type = svgedit.history.BatchCommand.type;
|
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
|
// Function: svgedit.history.BatchCommand.apply
|
||||||
// Runs "apply" on all subcommands
|
// Runs "apply" on all subcommands
|
||||||
svgedit.history.BatchCommand.prototype.apply = function() {
|
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
|
// historyEventHandler - an object that conforms to the HistoryEventHandler interface
|
||||||
// (see above)
|
// (see above)
|
||||||
svgedit.history.UndoManager = function(historyEventHandler) {
|
svgedit.history.UndoManager = function(historyEventHandler) {
|
||||||
this.handler_ = historyEventHandler;
|
this.handler_ = historyEventHandler || null;
|
||||||
this.undoStackPointer = 0;
|
this.undoStackPointer = 0;
|
||||||
this.undoStack = [];
|
this.undoStack = [];
|
||||||
|
|
||||||
|
@ -383,14 +410,14 @@ svgedit.history.UndoManager.prototype.getRedoStackSize = function() {
|
||||||
// Returns:
|
// Returns:
|
||||||
// String associated with the next undo command
|
// String associated with the next undo command
|
||||||
svgedit.history.UndoManager.prototype.getNextUndoCommandText = function() {
|
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
|
// Function: svgedit.history.UndoManager.getNextRedoCommandText
|
||||||
// Returns:
|
// Returns:
|
||||||
// String associated with the next redo command
|
// String associated with the next redo command
|
||||||
svgedit.history.UndoManager.prototype.getNextRedoCommandText = function() {
|
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
|
// Function: svgedit.history.UndoManager.undo
|
||||||
|
@ -399,11 +426,15 @@ svgedit.history.UndoManager.prototype.undo = function() {
|
||||||
if (this.undoStackPointer > 0) {
|
if (this.undoStackPointer > 0) {
|
||||||
var cmd = this.undoStack[--this.undoStackPointer];
|
var cmd = this.undoStack[--this.undoStackPointer];
|
||||||
|
|
||||||
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_UNAPPLY, cmd);
|
if (this.handler_ != null) {
|
||||||
|
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_UNAPPLY, cmd);
|
||||||
|
}
|
||||||
|
|
||||||
cmd.unapply();
|
cmd.unapply();
|
||||||
|
|
||||||
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_UNAPPLY, cmd);
|
if (this.handler_ != null) {
|
||||||
|
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_UNAPPLY, cmd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -413,11 +444,15 @@ svgedit.history.UndoManager.prototype.redo = function() {
|
||||||
if (this.undoStackPointer < this.undoStack.length && this.undoStack.length > 0) {
|
if (this.undoStackPointer < this.undoStack.length && this.undoStack.length > 0) {
|
||||||
var cmd = this.undoStack[this.undoStackPointer++];
|
var cmd = this.undoStack[this.undoStackPointer++];
|
||||||
|
|
||||||
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_APPLY, cmd);
|
if (this.handler_ != null) {
|
||||||
|
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.BEFORE_APPLY, cmd);
|
||||||
|
}
|
||||||
|
|
||||||
cmd.apply();
|
cmd.apply();
|
||||||
|
|
||||||
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_APPLY, cmd);
|
if (this.handler_ != null) {
|
||||||
|
this.handler_.handleHistoryEvent(svgedit.history.HistoryEventTypes.AFTER_APPLY, cmd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,36 @@
|
||||||
|
|
||||||
var svgns = 'http://www.w3.org/2000/svg';
|
var svgns = 'http://www.w3.org/2000/svg';
|
||||||
var svg = document.createElementNS(svgns, 'svg');
|
var svg = document.createElementNS(svgns, 'svg');
|
||||||
|
var undoMgr = null;
|
||||||
|
|
||||||
module('svgedit.history Module');
|
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() {
|
test('Test svgedit.history package', function() {
|
||||||
expect(13);
|
expect(13);
|
||||||
|
|
||||||
|
@ -38,6 +65,183 @@
|
||||||
equals(typeof svgedit.history.BatchCommand, typeof function(){});
|
equals(typeof svgedit.history.BatchCommand, typeof function(){});
|
||||||
equals(typeof svgedit.history.UndoManager, 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>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
Loading…
Reference in New Issue