test_SVGMatrix.xhtml (5432B)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <title>Test SVGMatrix behavior</title> 4 <script src="/tests/SimpleTest/SimpleTest.js"></script> 5 <script type="text/javascript" src="matrixUtils.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 <body> 9 <p id="display"></p> 10 <div id="content"> 11 <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="1" id="svg"> 12 <g id="g" transform="translate(10, 20)"/> 13 </svg> 14 </div> 15 <pre id="test"> 16 <script class="testbody" type="text/javascript"> 17 <![CDATA[ 18 19 SimpleTest.waitForExplicitFinish(); 20 21 function main() { 22 let tests = 23 [ testCreateMatrix, 24 testMultiply, 25 testInverse, 26 testTranslate, 27 testScale, 28 testScaleNonUniform, 29 testRotate, 30 testRotateFromVector, 31 testFlipX, 32 testFlipY, 33 testSkewX, 34 testSkewY, 35 ]; 36 for (let i = 0; i < tests.length; i++) { 37 tests[i](); 38 } 39 SimpleTest.finish(); 40 } 41 42 function testCreateMatrix() { 43 let svg = $("svg"); 44 let m = svg.createSVGMatrix(); 45 46 // Should be initialised to identity 47 cmpMatrix(m, [1, 0, 0, 1, 0, 0], 48 "createSVGMatrix should produce identity matrix"); 49 50 // Should return a new object each time; 51 ok(m != svg.createSVGMatrix(), 52 "Got identical objects when creating new matrix"); 53 } 54 55 // SVGMatrix multiply(in SVGMatrix secondMatrix); 56 function testMultiply() { 57 // This is the example from SVG 1.1 section 7.5 58 let m1 = createMatrix(1, 0, 0, 1, 50, 90); 59 let m2 = createMatrix(0.707, -0.707, 0.707, 0.707, 0, 0); 60 let result = m1.multiply(m2).multiply({a:1, b: 0, c: 0, d: 1, e: 130, f: 160}); 61 roughCmpMatrix(result, [0.707, -0.707, 0.707, 0.707, 255.03, 111.21], 62 "Unexpected result after multiplying matrices"); 63 64 // Check orig matrices are unchanged 65 cmpMatrix(m1, [1, 0, 0, 1, 50, 90], "Matrix changed after multiplication"); 66 roughCmpMatrix(m2, [0.707, -0.707, 0.707, 0.707, 0, 0], 67 "Matrix changed after multiplication"); 68 } 69 70 // SVGMatrix inverse() raises(SVGException); 71 function testInverse() { 72 // Test inversion 73 let m = createMatrix(2, 0, 0, 4, 110, -50); 74 roughCmpMatrix(m.inverse(), [0.5, 0, 0, 0.25, -55, 12.5], 75 "Unexpected result after inverting matrix"); 76 77 // Test non-invertable 78 m = createMatrix(0, 0, 1, 0, 0, 0); 79 try { 80 m.inverse(); 81 ok(false, "Failed to throw exception when inverting singular matrix"); 82 } catch (e) { 83 is(e.name, "InvalidStateError", 84 "Got unexpected exception " + e + ", expected InvalidStateError"); 85 } 86 } 87 88 // SVGMatrix translate(in float x, in float y); 89 function testTranslate() { 90 let m = createMatrix(2, 0, 0, 1, 120, 100); 91 roughCmpMatrix(m.translate(100, -50), [2, 0, 0, 1, 320, 50], 92 "Unexpected result after translate"); 93 } 94 95 // SVGMatrix scale(in float scaleFactor); 96 function testScale() { 97 let m = createMatrix(2, 0, 0, 1, 120, 100); 98 roughCmpMatrix(m.scale(0.5), [1, 0, 0, 0.5, 120, 100], 99 "Unexpected result after scale"); 100 } 101 102 // SVGMatrix scaleNonUniform(in float scaleFactorX, in float scaleFactorY); 103 function testScaleNonUniform() { 104 let m = createMatrix(2, 0, 0, 1, 120, 100); 105 roughCmpMatrix(m.scaleNonUniform(0.5, -3), [1, 0, 0, -3, 120, 100], 106 "Unexpected result after scaleNonUniform"); 107 } 108 109 // SVGMatrix rotate(in float angle); 110 function testRotate() { 111 let m = createMatrix(2, 0, 0, 1, 120, 100); 112 roughCmpMatrix(m.rotate(45), 113 [2 * Math.cos(Math.PI / 4), Math.sin(Math.PI / 4), 114 2 * -Math.sin(Math.PI / 4), Math.cos(Math.PI / 4), 115 120, 100], 116 "Unexpected result after rotate"); 117 } 118 119 // SVGMatrix rotateFromVector(in float x, in float y) raises(SVGException); 120 function testRotateFromVector() { 121 let m = createMatrix(2, 0, 0, 1, 120, 100); 122 // Make a 150 degree angle 123 let result = m.rotateFromVector(-2, 1.1547); 124 roughCmpMatrix(result, 125 [2 * Math.cos(5 * Math.PI / 6), Math.sin(5 * Math.PI / 6), 126 2 * -Math.sin(5 * Math.PI / 6), Math.cos(5 * Math.PI / 6), 127 120, 100], 128 "Unexpected result after rotateFromVector"); 129 130 // Test bad input (1) 131 try { 132 m.rotateFromVector(1, 0); 133 ok(false, "Failed to throw exception with zero coord for rotateFromVector"); 134 } catch (e) { 135 is(e.name, "InvalidAccessError", 136 "Got unexpected exception " + e + ", expected TypeError"); 137 } 138 139 // Test bad input (2) 140 try { 141 m.rotateFromVector(0, 1); 142 ok(false, "Failed to throw exception with zero coord for rotateFromVector"); 143 } catch (e) { } 144 } 145 146 // SVGMatrix flipX(); 147 function testFlipX() { 148 let m = createMatrix(1, 2, 3, 4, 5, 6); 149 cmpMatrix(m.flipX(), [-1, -2, 3, 4, 5, 6], "Unexpected result after flipX"); 150 } 151 152 // SVGMatrix flipY(); 153 function testFlipY() { 154 let m = createMatrix(1, 2, 3, 4, 5, 6); 155 cmpMatrix(m.flipY(), [1, 2, -3, -4, 5, 6], "Unexpected result after flipY"); 156 } 157 158 // SVGMatrix skewX(in float angle); 159 function testSkewX() { 160 let m = createMatrix(2, 0, 0, 1, 120, 100); 161 roughCmpMatrix(m.skewX(30), [2, 0, 2 * Math.tan(Math.PI / 6), 1, 120, 100], 162 "Unexpected result after skewX"); 163 } 164 165 // SVGMatrix skewY(in float angle); 166 function testSkewY() { 167 let m = createMatrix(2, 0, 0, 1, 120, 100); 168 roughCmpMatrix(m.skewY(30), [2, Math.tan(Math.PI / 6), 0, 1, 120, 100], 169 "Unexpected result after skewY"); 170 } 171 172 window.addEventListener("load", main); 173 174 ]]> 175 </script> 176 </pre> 177 </body> 178 </html>