Add unit tests for svgtransformlist.js and all_tests page
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1853 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
a681fc2579
commit
3aabcb428b
|
@ -1534,10 +1534,11 @@ var getStrokedBBox = this.getStrokedBBox = function(elems) {
|
|||
// fine in FF, but not in other browsers (same problem mentioned
|
||||
// in Issue 339 comment #2).
|
||||
|
||||
var bb = getBBox(elem);
|
||||
var bb = svgedit.utilities.getBBox(elem);
|
||||
|
||||
var angle = svgedit.utilities.getRotationAngle(elem);
|
||||
if ((angle && angle % 90) || hasMatrixTransform(getTransformList(elem))) {
|
||||
if ((angle && angle % 90) ||
|
||||
svgedit.math.hasMatrixTransform(getTransformList(elem))) {
|
||||
// Accurate way to get BBox of rotated element in Firefox:
|
||||
// Put element in group and get its BBox
|
||||
|
||||
|
@ -9431,7 +9432,7 @@ this.convertToPath = function(elem, getBBox) {
|
|||
path.parentNode.removeChild(path);
|
||||
return bb;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Function: changeSelectedAttributeNoUndo
|
||||
|
|
|
@ -50,41 +50,27 @@ function transformToString(xform) {
|
|||
return text;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Map of SVGTransformList objects.
|
||||
*/
|
||||
var listMap_ = {};
|
||||
|
||||
svgedit.transformlist.resetListMap = function() {
|
||||
listMap_ = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Parameters:
|
||||
* elem - a DOM Element
|
||||
*/
|
||||
svgedit.transformlist.removeElementFromListMap = function(elem) {
|
||||
if (elem.id && listMap_[elem.id]) {
|
||||
delete listMap_[elem.id];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// **************************************************************************************
|
||||
// SVGTransformList implementation for Webkit
|
||||
// These methods do not currently raise any exceptions.
|
||||
// These methods also do not check that transforms are being inserted or handle if
|
||||
// a transform is already in the list, etc. This is basically implementing as much
|
||||
// of SVGTransformList that we need to get the job done.
|
||||
// These methods also do not check that transforms are being inserted. This is basically
|
||||
// implementing as much of SVGTransformList that we need to get the job done.
|
||||
//
|
||||
// interface SVGEditTransformList {
|
||||
// attribute unsigned long numberOfItems;
|
||||
// void clear ( )
|
||||
// SVGTransform initialize ( in SVGTransform newItem )
|
||||
// SVGTransform getItem ( in unsigned long index )
|
||||
// SVGTransform insertItemBefore ( in SVGTransform newItem, in unsigned long index )
|
||||
// SVGTransform replaceItem ( in SVGTransform newItem, in unsigned long index )
|
||||
// SVGTransform removeItem ( in unsigned long index )
|
||||
// SVGTransform getItem ( in unsigned long index ) (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
|
||||
// SVGTransform insertItemBefore ( in SVGTransform newItem, in unsigned long index ) (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
|
||||
// SVGTransform replaceItem ( in SVGTransform newItem, in unsigned long index ) (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
|
||||
// SVGTransform removeItem ( in unsigned long index ) (DOES NOT THROW DOMException, INDEX_SIZE_ERR)
|
||||
// SVGTransform appendItem ( in SVGTransform newItem )
|
||||
// NOT IMPLEMENTED: SVGTransform createSVGTransformFromMatrix ( in SVGMatrix matrix );
|
||||
// NOT IMPLEMENTED: SVGTransform consolidate ( );
|
||||
|
@ -144,7 +130,25 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
this._list.appendItem(xform);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
this._removeFromOtherLists = function(item) {
|
||||
// Check if this transform is already in a transformlist, and
|
||||
// remove it if so.
|
||||
var found = false;
|
||||
for (var id in listMap_) {
|
||||
var tl = listMap_[id];
|
||||
for (var i = 0, len = tl._xforms.length; i < len; ++i) {
|
||||
if(tl._xforms[i] == item) {
|
||||
found = true;
|
||||
tl.removeItem(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.numberOfItems = 0;
|
||||
this.clear = function() {
|
||||
|
@ -154,6 +158,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
|
||||
this.initialize = function(newItem) {
|
||||
this.numberOfItems = 1;
|
||||
this._removeFromOtherLists(newItem);
|
||||
this._xforms = [newItem];
|
||||
};
|
||||
|
||||
|
@ -168,6 +173,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
var retValue = null;
|
||||
if (index >= 0) {
|
||||
if (index < this.numberOfItems) {
|
||||
this._removeFromOtherLists(newItem);
|
||||
var newxforms = new Array(this.numberOfItems + 1);
|
||||
// TODO: use array copying and slicing
|
||||
for ( var i = 0; i < index; ++i) {
|
||||
|
@ -192,6 +198,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
this.replaceItem = function(newItem, index) {
|
||||
var retValue = null;
|
||||
if (index < this.numberOfItems && index >= 0) {
|
||||
this._removeFromOtherLists(newItem);
|
||||
this._xforms[index] = newItem;
|
||||
retValue = newItem;
|
||||
this._list._update();
|
||||
|
@ -218,6 +225,7 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
};
|
||||
|
||||
this.appendItem = function(newItem) {
|
||||
this._removeFromOtherLists(newItem);
|
||||
this._xforms.push(newItem);
|
||||
this.numberOfItems++;
|
||||
this._list._update();
|
||||
|
@ -226,6 +234,21 @@ svgedit.transformlist.SVGTransformList = function(elem) {
|
|||
};
|
||||
|
||||
|
||||
svgedit.transformlist.resetListMap = function() {
|
||||
listMap_ = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes transforms of the given element from the map.
|
||||
* Parameters:
|
||||
* elem - a DOM Element
|
||||
*/
|
||||
svgedit.transformlist.removeElementFromListMap = function(elem) {
|
||||
if (elem.id && listMap_[elem.id]) {
|
||||
delete listMap_[elem.id];
|
||||
}
|
||||
};
|
||||
|
||||
// Function: getTransformList
|
||||
// Returns an object that behaves like a SVGTransformList for the given DOM element
|
||||
//
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>All SVG-edit Tests</title>
|
||||
<script src='../editor/jquery.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>All SVG-edit Tests</h1>
|
||||
<p>This file frames all SVG-edit test pages. This should only include tests known to work. If a test is broken in this page, it is possible that <em>YOU</em> broke it. Please do not submit code that breaks any of these tests.</p>
|
||||
<iframe src='svgtransformlist_test.html' width='100%' height='300'></iframe>
|
||||
</body>
|
||||
<script>
|
||||
window.setTimeout(function() {
|
||||
var iframes = document.getElementsByTagName('iframe');
|
||||
for (var i = 0, len = iframes.length; i < len; ++i) {
|
||||
var f = iframes[i];
|
||||
f.style.height = f.contentDocument.body.scrollHeight + 'px';
|
||||
}
|
||||
}, 1000);
|
||||
</script>
|
||||
</html>
|
|
@ -0,0 +1,245 @@
|
|||
<html>
|
||||
<head>
|
||||
<link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
|
||||
<script src='../editor/jquery.js'></script>
|
||||
<script>
|
||||
// Mock for browsersupport
|
||||
window.svgedit = {};
|
||||
svgedit.browsersupport = {};
|
||||
svgedit.browsersupport.isWebkit_ = true;
|
||||
svgedit.browsersupport.isWebkit = function() { return svgedit.browsersupport.isWebkit_; }
|
||||
</script>
|
||||
<script type='text/javascript' src='../editor/svgtransformlist.js'></script>
|
||||
<script type='text/javascript' src='qunit/qunit.js'></script>
|
||||
<script type='text/javascript'>
|
||||
$(function() {
|
||||
// log function
|
||||
QUnit.log = function(result, message) {
|
||||
if (window.console && window.console.log) {
|
||||
window.console.log(result +' :: '+ message);
|
||||
}
|
||||
};
|
||||
|
||||
var svgns = 'http://www.w3.org/2000/svg';
|
||||
var svgroot = document.getElementById('svgroot');
|
||||
var svgcontent, rect, circle;
|
||||
|
||||
function setUp() {
|
||||
svgcontent = svgroot.appendChild(document.createElementNS(svgns, 'svg'));
|
||||
rect = svgcontent.appendChild(document.createElementNS(svgns, 'rect'));
|
||||
rect.id = 'r';
|
||||
circle = svgcontent.appendChild(document.createElementNS(svgns, 'circle'));
|
||||
circle.id = 'c';
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
svgedit.transformlist.resetListMap();
|
||||
while (svgroot.hasChildNodes()) {
|
||||
svgroot.removeChild(svgroot.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
module('svgedit.svgtransformlist Module');
|
||||
|
||||
test('Test svgedit.transformlist package', function() {
|
||||
expect(2);
|
||||
|
||||
ok(svgedit.transformlist);
|
||||
ok(svgedit.transformlist.getTransformList);
|
||||
});
|
||||
|
||||
test('Test svgedit.transformlist.getTransformList() function', function() {
|
||||
expect(4);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
var cxform = svgedit.transformlist.getTransformList(circle);
|
||||
|
||||
ok(rxform);
|
||||
ok(cxform);
|
||||
equals(typeof rxform, typeof {});
|
||||
equals(typeof cxform, typeof {});
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.numberOfItems property', function() {
|
||||
expect(2);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
|
||||
equals(typeof rxform.numberOfItems, typeof 0);
|
||||
equals(rxform.numberOfItems, 0);
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.initialize()', function() {
|
||||
expect(7);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
var cxform = svgedit.transformlist.getTransformList(circle);
|
||||
|
||||
var t = svgcontent.createSVGTransform();
|
||||
ok(t);
|
||||
ok(rxform.initialize);
|
||||
equals(typeof rxform.initialize, typeof function(){});
|
||||
rxform.initialize(t);
|
||||
equals(rxform.numberOfItems, 1);
|
||||
equals(cxform.numberOfItems, 0);
|
||||
|
||||
// If a transform was already in a transform list, this should
|
||||
// remove it from its old list and add it to this list.
|
||||
cxform.initialize(t);
|
||||
equals(rxform.numberOfItems, 0);
|
||||
equals(cxform.numberOfItems, 1);
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.appendItem() and getItem()', function() {
|
||||
expect(15);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
var cxform = svgedit.transformlist.getTransformList(circle);
|
||||
|
||||
var t1 = svgcontent.createSVGTransform(),
|
||||
t2 = svgcontent.createSVGTransform(),
|
||||
t3 = svgcontent.createSVGTransform();
|
||||
|
||||
ok(rxform.appendItem);
|
||||
ok(rxform.getItem);
|
||||
equals(typeof rxform.appendItem, typeof function(){});
|
||||
equals(typeof rxform.getItem, typeof function(){});
|
||||
|
||||
rxform.appendItem(t1);
|
||||
rxform.appendItem(t2);
|
||||
rxform.appendItem(t3);
|
||||
|
||||
equals(rxform.numberOfItems, 3);
|
||||
equals(rxform.getItem(0), t1);
|
||||
equals(rxform.getItem(1), t2);
|
||||
equals(rxform.getItem(2), t3);
|
||||
|
||||
ok(!rxform.getItem(-1));
|
||||
ok(!rxform.getItem(3));
|
||||
|
||||
cxform.appendItem(t1);
|
||||
equals(rxform.numberOfItems, 2);
|
||||
equals(rxform.getItem(0), t2);
|
||||
equals(rxform.getItem(1), t3);
|
||||
|
||||
equals(cxform.numberOfItems, 1);
|
||||
equals(cxform.getItem(0), t1);
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.removeItem()', function() {
|
||||
expect(7);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
|
||||
var t1 = svgcontent.createSVGTransform(),
|
||||
t2 = svgcontent.createSVGTransform();
|
||||
ok(rxform.removeItem);
|
||||
equals(typeof rxform.removeItem, typeof function(){});
|
||||
rxform.appendItem(t1);
|
||||
rxform.appendItem(t2);
|
||||
|
||||
var removedTransform = rxform.removeItem(0);
|
||||
equals(rxform.numberOfItems, 1);
|
||||
equals(removedTransform, t1);
|
||||
equals(rxform.getItem(0), t2);
|
||||
|
||||
ok(!rxform.removeItem(-1));
|
||||
ok(!rxform.removeItem(1));
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.replaceItem()', function() {
|
||||
expect(10);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
var cxform = svgedit.transformlist.getTransformList(circle);
|
||||
|
||||
ok(rxform.replaceItem);
|
||||
equals(typeof rxform.replaceItem, typeof function(){});
|
||||
|
||||
var t1 = svgcontent.createSVGTransform(),
|
||||
t2 = svgcontent.createSVGTransform(),
|
||||
t3 = svgcontent.createSVGTransform();
|
||||
|
||||
rxform.appendItem(t1);
|
||||
rxform.appendItem(t2);
|
||||
cxform.appendItem(t3);
|
||||
|
||||
var newItem = rxform.replaceItem(t3, 0);
|
||||
equals(rxform.numberOfItems, 2);
|
||||
equals(newItem, t3);
|
||||
equals(rxform.getItem(0), t3);
|
||||
equals(rxform.getItem(1), t2);
|
||||
equals(cxform.numberOfItems, 0);
|
||||
|
||||
// test replaceItem within a list
|
||||
rxform.appendItem(t1);
|
||||
rxform.replaceItem(t1, 0);
|
||||
equals(rxform.numberOfItems, 2);
|
||||
equals(rxform.getItem(0), t1);
|
||||
equals(rxform.getItem(1), t2);
|
||||
|
||||
tearDown();
|
||||
});
|
||||
|
||||
test('Test SVGTransformList.insertItemBefore()', function() {
|
||||
expect(12);
|
||||
setUp();
|
||||
|
||||
var rxform = svgedit.transformlist.getTransformList(rect);
|
||||
var cxform = svgedit.transformlist.getTransformList(circle);
|
||||
|
||||
ok(rxform.insertItemBefore);
|
||||
equals(typeof rxform.insertItemBefore, typeof function(){});
|
||||
|
||||
var t1 = svgcontent.createSVGTransform(),
|
||||
t2 = svgcontent.createSVGTransform(),
|
||||
t3 = svgcontent.createSVGTransform();
|
||||
|
||||
rxform.appendItem(t1);
|
||||
rxform.appendItem(t2);
|
||||
cxform.appendItem(t3);
|
||||
|
||||
var newItem = rxform.insertItemBefore(t3, 0);
|
||||
equals(rxform.numberOfItems, 3);
|
||||
equals(newItem, t3);
|
||||
equals(rxform.getItem(0), t3);
|
||||
equals(rxform.getItem(1), t1);
|
||||
equals(rxform.getItem(2), t2);
|
||||
equals(cxform.numberOfItems, 0);
|
||||
|
||||
rxform.insertItemBefore(t2, 1);
|
||||
equals(rxform.numberOfItems, 3);
|
||||
equals(rxform.getItem(0), t3);
|
||||
equals(rxform.getItem(1), t2);
|
||||
equals(rxform.getItem(2), t1);
|
||||
tearDown();
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1 id='qunit-header'>Unit Tests for svgtransformlist.js</h1>
|
||||
<h2 id='qunit-banner'></h2>
|
||||
<h2 id='qunit-userAgent'></h2>
|
||||
<ol id='qunit-tests'>
|
||||
</ol>
|
||||
<div id='svgroot' style='visibility:hidden'></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue