1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.scene.transform; 27 28 import com.sun.javafx.test.TransformHelper; 29 import javafx.scene.transform.Transform; 30 import com.sun.javafx.geom.transform.Affine3D; 31 import java.util.LinkedList; 32 import java.util.List; 33 import javafx.scene.transform.TransformOperationsTest; 34 import javafx.scene.transform.Translate; 35 import static org.junit.Assert.*; 36 37 import org.junit.Test; 38 39 public class TransformUtilsTest { 40 @Test 41 public void shouldCreateCorrectImmutableTransform() { 42 Transform t = TransformUtils.immutableTransform( 43 1, 2, 3, 4, 44 5, 6, 7, 8, 45 9, 10, 11, 12); 46 47 TransformHelper.assertMatrix(t, 48 1, 2, 3, 4, 49 5, 6, 7, 8, 50 9, 10, 11, 12); 51 } 52 53 @Test 54 public void immutableTransformShouldApplyCorrectly() { 55 Affine3D a = new Affine3D(); 56 a.translate(10, 20); 57 58 Transform t = TransformUtils.immutableTransform( 59 1, 2, 3, 4, 60 5, 6, 7, 8, 61 9, 10, 11, 12); 62 63 t.impl_apply(a); 64 65 TransformHelper.assertMatrix(a, 66 1, 2, 3, 14, 67 5, 6, 7, 28, 68 9, 10, 11, 12); 69 } 70 71 @Test 72 public void immutableTransformShouldCopyCorrectly() { 73 Transform src = TransformUtils.immutableTransform( 74 1, 2, 3, 4, 75 5, 6, 7, 8, 76 9, 10, 11, 12); 77 78 Transform t = src.clone(); 79 80 TransformHelper.assertMatrix(t, 81 1, 2, 3, 4, 82 5, 6, 7, 8, 83 9, 10, 11, 12); 84 } 85 86 @Test public void testImmutableTransformToString() { 87 final Transform trans = TransformUtils.immutableTransform( 88 1, 2, 3, 4, 89 5, 6, 7, 8, 90 9, 10, 11, 12); 91 92 String s = trans.toString(); 93 94 assertNotNull(s); 95 assertFalse(s.isEmpty()); 96 } 97 98 @Test public void testImmutableTransformState() { 99 int counter = 0; 100 for (Object o : TransformOperationsTest.getParams()) { 101 Object[] arr = (Object[]) o; 102 if (arr[0] instanceof TransformUtils.ImmutableTransform) { 103 TransformUtils.ImmutableTransform t = 104 (TransformUtils.ImmutableTransform) arr[0]; 105 106 TransformHelper.assertStateOk("Checking state of transform #" + 107 (counter++) + " of TransformOperationsTest", t, 108 t.getState3d(), t.getState2d()); 109 } 110 } 111 } 112 113 @Test public void testReusedImmutableTransform() { 114 int counter = 0; 115 for (Object o : TransformOperationsTest.getParams()) { 116 Object[] arr = (Object[]) o; 117 if (arr[0] instanceof TransformUtils.ImmutableTransform) { 118 119 Transform t = (Transform) arr[0]; 120 121 122 // reusing 123 Transform reuse = TransformUtils.immutableTransform( 124 new Translate(10, 20)); 125 126 Transform returned = TransformUtils.immutableTransform(reuse, t); 127 128 assertSame("Checking reusing immutable transform to values of #" 129 + counter + " of TransformOperationsTest", reuse, returned); 130 131 TransformHelper.assertStateOk( 132 "Checking reusing immutable transform to values of #" 133 + counter + " of TransformOperationsTest", 134 (TransformUtils.ImmutableTransform) returned, 135 ((TransformUtils.ImmutableTransform) returned).getState3d(), 136 ((TransformUtils.ImmutableTransform) returned).getState2d()); 137 138 TransformHelper.assertMatrix("Checking reusing immutable " 139 + "transform to values of #" + counter 140 + " of TransformOperationsTest", returned, t); 141 142 // creating new 143 Transform returned2 = TransformUtils.immutableTransform(null, t); 144 145 assertNotSame("Checking reusing immutable transform to values of #" 146 + counter + " of TransformOperationsTest", returned2, t); 147 148 TransformHelper.assertStateOk( 149 "Checking reusing immutable transform to values of #" 150 + counter + " of TransformOperationsTest", 151 (TransformUtils.ImmutableTransform) returned2, 152 ((TransformUtils.ImmutableTransform) returned2).getState3d(), 153 ((TransformUtils.ImmutableTransform) returned2).getState2d()); 154 155 TransformHelper.assertMatrix("Checking reusing immutable " 156 + "transform to values of #" + counter 157 + " of TransformOperationsTest", returned2, t); 158 159 counter++; 160 } 161 } 162 } 163 164 @Test public void testConcatenatedImmutableTransform() { 165 166 List<TransformUtils.ImmutableTransform> ts = new LinkedList<>(); 167 for (Object o : TransformOperationsTest.getParams()) { 168 Object[] arr = (Object[]) o; 169 if (arr[0] instanceof TransformUtils.ImmutableTransform) { 170 ts.add((TransformUtils.ImmutableTransform) arr[0]); 171 } 172 } 173 174 int outer = 0; 175 for (TransformUtils.ImmutableTransform t1 : ts) { 176 int inner = 0; 177 for (TransformUtils.ImmutableTransform t2 : ts) { 178 int orig = 0; 179 // reusing 180 for (TransformUtils.ImmutableTransform t3 : ts) { 181 Transform clone = t3.clone(); 182 Transform conc = TransformUtils.immutableTransform( 183 clone, t1, t2); 184 185 assertSame("Checking state of concatenation of " 186 + "transform #" + outer + " and #" + inner + 187 " reusing #" + orig + 188 " of TransformOperationsTest", clone, conc); 189 TransformHelper.assertStateOk( 190 "Checking state of concatenation of " 191 + "transform #" + outer + " and #" + inner + 192 " reusing #" + orig + 193 " of TransformOperationsTest", 194 (TransformUtils.ImmutableTransform) conc, 195 ((TransformUtils.ImmutableTransform) conc).getState3d(), 196 ((TransformUtils.ImmutableTransform) conc).getState2d()); 197 TransformHelper.assertMatrix( 198 "Checking state of concatenation of " 199 + "transform #" + outer + " and #" + inner + 200 " reusing #" + orig + 201 " of TransformOperationsTest", conc, 202 TransformHelper.concatenate(t1, t2)); 203 orig++; 204 } 205 206 // creating new 207 Transform conc2 = TransformUtils.immutableTransform( 208 null, t1, t2); 209 210 assertNotSame("Checking state of concatenation of " 211 + "transform #" + outer + " and #" + inner + 212 " of TransformOperationsTest", conc2, t1); 213 assertNotSame("Checking state of concatenation of " 214 + "transform #" + outer + " and #" + inner + 215 " of TransformOperationsTest", conc2, t2); 216 TransformHelper.assertStateOk( 217 "Checking state of concatenation of " 218 + "transform #" + outer + " and #" + inner + 219 " of TransformOperationsTest", 220 (TransformUtils.ImmutableTransform) conc2, 221 ((TransformUtils.ImmutableTransform) conc2).getState3d(), 222 ((TransformUtils.ImmutableTransform) conc2).getState2d()); 223 TransformHelper.assertMatrix( 224 "Checking state of concatenation of " 225 + "transform #" + outer + " and #" + inner + 226 " of TransformOperationsTest", conc2, 227 TransformHelper.concatenate(t1, t2)); 228 inner++; 229 } 230 outer++; 231 } 232 } 233 }