Bug 705236 part 2 - Refactor test_smilTiming.xhtml and add more tests; r=dholbert

This commit is contained in:
Brian Birtles 2012-02-24 09:45:40 +09:00
parent 51354a911b
commit 91bea1eb43

View File

@ -18,55 +18,158 @@
/** Test for SMIL timing **/
/* Global Variables */
const svgns="http://www.w3.org/2000/svg";
var svg = document.getElementById("svg");
var circle = document.getElementById('circle');
const svgns = "http://www.w3.org/2000/svg";
var gSvg = document.getElementById("svg");
var gCircle = document.getElementById('circle');
SimpleTest.waitForExplicitFinish();
function createAnim() {
function main() {
ok(gSvg.animationsPaused(), "should be paused by <svg> load handler");
is(gSvg.getCurrentTime(), 0, "should be paused at 0 in <svg> load handler");
var testCases = Array();
// List syntax
testCases.push(StartTimeTest('3', 3));
testCases.push(StartTimeTest('3;', 3));
testCases.push(StartTimeTest('3; ', 3));
testCases.push(StartTimeTest('3 ; ', 3));
testCases.push(StartTimeTest('3;;', 'none'));
testCases.push(StartTimeTest('3;; ', 'none'));
testCases.push(StartTimeTest(';3', 'none'));
testCases.push(StartTimeTest(' ;3', 'none'));
testCases.push(StartTimeTest('3;4', 3));
testCases.push(StartTimeTest(' 3 ; 4 ', 3));
// List syntax on end times
testCases.push({
'attr' : { 'begin': '0s',
'end': '1s; 2s' },
'times': [ [ 0, 0 ],
[ 1, -100 ] ]
});
testCases.push({
'attr' : { 'begin': '0s',
'end': '1s; 2s; ' },
'times': [ [ 0, 0 ],
[ 1, -100 ] ]
});
testCases.push({
'attr' : { 'begin': '0s',
'end': '3s; 2s' },
'times': [ [ 0, 0 ],
[ 1, 10 ],
[ 2, -100 ] ]
});
// Simple case
testCases.push({
'attr' : { 'begin': '3s' },
'times': [ [ 0, -100 ],
[ 4, 10 ] ]
});
// Multiple begins
testCases.push({
'attr' : { 'begin': '2s; 6s',
'dur': '2s' },
'times': [ [ 0, -100 ],
[ 3, 50 ],
[ 4, -100 ],
[ 7, 50 ],
[ 8, -100 ] ]
});
// Negative begins
testCases.push({
'attr' : { 'begin': '-3s; 1s ; 4s',
'dur': '2s ',
'fill': 'freeze' },
'times': [ [ 0, -100 ],
[ 0.5, -100 ],
[ 1, 0 ],
[ 2, 50 ],
[ 3, 100 ],
[ 5, 50 ] ]
});
// Sorting
testCases.push({
'attr' : { 'begin': '-3s; 110s; 1s; 4s; -5s; -10s',
'end': '111s; -5s; -15s; 6s; -5s; 1.2s',
'dur': '2s ',
'fill': 'freeze' },
'times': [ [ 0, -100 ],
[ 1, 0 ],
[ 2, 10 ],
[ 4, 0 ],
[ 5, 50 ],
[ 109, 100 ],
[ 110, 0 ],
[ 112, 50 ] ]
});
for (var i = 0; i < testCases.length; i++) {
gSvg.setCurrentTime(0);
var test = testCases[i];
// Generate string version of params for output messages
var params = "";
for (var name in test.attr) {
params += name + '="' + test.attr[name] + '" ';
}
params = params.trim();
// Create animation elements
var anim = createAnim(test.attr);
// Run samples
if ('times' in test) {
for (var j = 0; j < test.times.length; j++) {
var curSample = test.times[j];
checkSample(curSample[0], curSample[1], params);
}
}
// Check start time
if ('startTime' in test) {
is(getStartTime(anim), test.startTime,
"Got unexpected start time for " + params);
}
anim.parentNode.removeChild(anim);
}
SimpleTest.finish();
}
function createAnim(attr) {
var anim = document.createElementNS(svgns,'animate');
anim.setAttribute('attributeName','cx');
anim.setAttribute('from','0');
anim.setAttribute('to','100');
anim.setAttribute('dur','10s');
anim.setAttribute('begin','indefinite');
return circle.appendChild(anim);
}
function removeAnim(anim) {
anim.parentNode.removeChild(anim);
}
function main() {
ok(svg.animationsPaused(), "should be paused by <svg> load handler");
is(svg.getCurrentTime(), 0, "should be paused at 0 in <svg> load handler");
var tests =
[ testListSyntax,
testOffsetStartup,
testMultipleBegins,
testNegativeBegins,
testSorting
];
for (var i = 0; i < tests.length; i++) {
var anim = createAnim();
svg.setCurrentTime(0);
tests[i](anim);
removeAnim(anim);
for (name in attr) {
anim.setAttribute(name, attr[name]);
}
SimpleTest.finish();
return gCircle.appendChild(anim);
}
function checkSample(time, expectedValue) {
svg.setCurrentTime(time);
is(circle.cx.animVal.value, expectedValue);
function checkSample(time, expectedValue, params) {
gSvg.setCurrentTime(time);
var msg = "Unexpected sample value for " + params +
" at t=" + time + ": ";
is(gCircle.cx.animVal.value, expectedValue);
}
function getStartTime(anim) {
var startTime;
try {
startTime = anim.getStartTime();
// We round start times to 3 decimal places to make comparisons simpler
startTime = parseFloat(startTime.toFixed(3));
} catch(e) {
if (e.code == DOMException.INVALID_STATE_ERR) {
startTime = 'none';
@ -77,67 +180,9 @@ function getStartTime(anim) {
return startTime;
}
function testListSyntax() {
var specs = [ [ '0', 0 ],
[ '3;', 3 ],
[ '3; ', 3 ],
[ '3 ; ', 3 ],
[ '3;;', 'none' ],
[ '3;; ', 'none' ],
[ ';3', 'none' ],
[ ' ;3', 'none' ],
[ '3;4', 3 ],
[ ' 3 ; 4 ', 3 ] ];
for (var i = 0; i < specs.length; i++) {
var anim = createAnim()
anim.setAttribute('begin', specs[i][0]);
is(getStartTime(anim), specs[i][1],
"Got unexpected start time for " + specs[i][0]);
removeAnim(anim);
}
}
function testOffsetStartup(anim) {
anim.setAttribute('begin', '3s');
checkSample(0,-100);
checkSample(4,10);
}
function testMultipleBegins(anim) {
anim.setAttribute('begin', '2s; 6s');
anim.setAttribute('dur', ' 2s');
checkSample(0,-100);
checkSample(3,50);
checkSample(4,-100);
checkSample(7,50);
checkSample(8,-100);
}
function testNegativeBegins(anim) {
anim.setAttribute('begin', '-3s; 1s ; 4s');
anim.setAttribute('dur', '2s ');
anim.setAttribute('fill', 'freeze');
checkSample(0,-100);
checkSample(0.5,-100);
checkSample(1,0);
checkSample(2,50);
checkSample(3,100);
checkSample(5,50);
}
function testSorting(anim) {
anim.setAttribute('begin', '-3s; 110s; 1s; 4s; -5s; -10s');
anim.setAttribute('end', '111s; -5s; -15s; 6s; -5s; 1.2s');
anim.setAttribute('dur', '2s ');
anim.setAttribute('fill', 'freeze');
checkSample(0,-100);
checkSample(1,0);
checkSample(2,10);
checkSample(4,0);
checkSample(5,50);
checkSample(109,100);
checkSample(110,0);
checkSample(112,50);
function StartTimeTest(beginSpec, expectedStartTime) {
return { 'attr' : { 'begin': beginSpec },
'startTime': expectedStartTime };
}
window.addEventListener("load", main, false);