modules/graphics/src/test/java/test/javafx/scene/transform/AffineOperationsTest.java

Print this page
rev 9250 : 8134762: Refactor Javafx graphics module tests for clear separation of tests
Reviewed-by:


   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 javafx.scene.transform;
  27 
  28 //import java.awt.geom.AffineTransform;
  29 //import com.sun.javafx.geom.transform.Affine3D;
  30 import java.util.Collection;
  31 import javafx.beans.Observable;
  32 import org.junit.runners.Parameterized.Parameters;
  33 import org.junit.runners.Parameterized;
  34 import java.util.Arrays;
  35 import javafx.geometry.Point3D;
  36 import org.junit.Test;
  37 import org.junit.runner.RunWith;
  38 import com.sun.javafx.test.TransformHelper;
  39 import javafx.beans.InvalidationListener;
  40 import javafx.event.EventHandler;
  41 import javafx.geometry.Point2D;










  42 import static org.junit.Assert.*;
  43 
  44 @RunWith(Parameterized.class)
  45 public class AffineOperationsTest {
  46 
  47     private static final Affine identity = new Affine();
  48     private static final Affine translate = new Affine(1, 0, 2,
  49                                                        0, 1, 3);
  50     private static final Affine scale = new Affine(4, 0, 0,
  51                                                    0, 5, 0);
  52     private static final Affine sc_tr = new Affine(6, 0, 8,
  53                                                    0, 7, 9);
  54     private static final Affine shear = new Affine( 0, 10, 0,
  55                                                    11,  0, 0);
  56     private static final Affine sh_tr = new Affine( 0, 12, 14,
  57                                                    13,  0, 15);
  58     private static final Affine sh_sc_simple = new Affine( 1, 18, 0,
  59                                                           19, 1, 0);
  60     private static final Affine sh_sc = new Affine(16, 18, 0,
  61                                                    19, 17, 0);


 183     public AffineOperationsTest(Affine a) {
 184         this.affine = a;
 185     }
 186 
 187     private int eventCounter;
 188     private int listener1Counter;
 189     private int listener2Counter;
 190     private double memMyx, memTy;
 191 
 192     private void assertAffineOk(Transform expected, Affine a) {
 193         TransformHelper.assertMatrix(a, expected);
 194         assertStateOk(a);
 195     }
 196 
 197     private void assertAffineOk(String message, Transform expected, Affine a) {
 198         TransformHelper.assertMatrix(message, a, expected);
 199         assertStateOk(message, a);
 200     }
 201 
 202     private void assertStateOk(Affine a) {
 203         TransformHelper.assertStateOk(a, a.getState3d(), a.getState2d());
 204     }
 205 
 206     private void assertStateOk(String message, Affine a) {
 207         TransformHelper.assertStateOk(message, a, a.getState3d(), a.getState2d());
 208     }
 209 
 210     private void testOperationIsAtomic(Affine a,
 211             final Runnable op, final Runnable check) {
 212 
 213         a.setOnTransformChanged(event -> {
 214             eventCounter++;
 215             check.run();
 216         });
 217 
 218         a.myxProperty().addListener(observable -> {
 219             listener1Counter++;
 220             check.run();
 221         });
 222 
 223         a.tyProperty().addListener(observable -> {
 224             listener2Counter++;
 225             check.run();
 226         });
 227 
 228         memMyx = a.getMyx();
 229         memTy = a.getTy();
 230         eventCounter = 0;
 231         listener1Counter = 0;
 232         listener2Counter = 0;
 233         op.run();
 234 
 235         assertTrue(listener1Counter == (memMyx == a.getMyx() ? 0 : 1));
 236         assertTrue(listener2Counter == (memTy == a.getTy() ? 0 : 1));
 237         assertEquals(1, eventCounter);
 238         assertFalse(a.atomicChangeRuns());
 239     }
 240 
 241     @Test
 242     public void SetToTransformShouldBeAtomic() {
 243         final Affine a = affine.clone();
 244         final Shear sh = new Shear(12, 15);
 245 
 246         testOperationIsAtomic(a,
 247                 () -> a.setToTransform(sh),
 248                 () -> assertAffineOk(sh, a)
 249         );
 250     }
 251 
 252     @Test
 253     public void SetToTransform2DShouldBeAtomic() {
 254         final Affine a = affine.clone();
 255         final Shear sh = new Shear(12, 15);
 256 
 257         testOperationIsAtomic(a,
 258                 () -> a.setToTransform(1, 12, 0, 15, 1, 0),


2341                 return;
2342             }
2343         } catch (IndexOutOfBoundsException e) {
2344             if (iob) {
2345                 // ok
2346                 return;
2347             }
2348         }
2349 
2350         if (iae) {
2351             fail("Should have thrown IAE");
2352         }
2353         if (iob) {
2354             fail("Should have thrown IOB");
2355         }
2356     }
2357 
2358     @Test
2359     public void testSetElement() {
2360         Affine a = affine.clone();
2361         boolean is2d = affine.computeIs2D();
2362 
2363         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 0, a.getMxx(), !is2d, false);
2364         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 1, a.getMxy(), !is2d, false);
2365         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 2, a.getTx(), !is2d, false);
2366         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 0, a.getMyx(), !is2d, false);
2367         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 1, a.getMyy(), !is2d, false);
2368         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 2, a.getTy(), !is2d, false);
2369         assertSetElement(a, MatrixType.MT_2D_2x3, -1, 0, 0, !is2d, true);
2370         assertSetElement(a, MatrixType.MT_2D_2x3, 2, 0, -1000, !is2d, true);
2371         assertSetElement(a, MatrixType.MT_2D_2x3, 2, 1, -1000, !is2d, true);
2372         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 3, 0, !is2d, true);
2373         assertSetElement(a, MatrixType.MT_2D_2x3, 0, -1, 0, !is2d, true);
2374         if (is2d) {
2375             assertAffineOk(new Affine(
2376                     affine.getMxx() + 1000,
2377                     affine.getMxy() + 1000,
2378                     affine.getMxz(),
2379                     affine.getTx() + 1000,
2380                     affine.getMyx() + 1000,
2381                     affine.getMyy() + 1000,




   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 test.javafx.scene.transform;
  27 
  28 //import java.awt.geom.AffineTransform;
  29 //import com.sun.javafx.geom.transform.Affine3D;
  30 import java.util.Collection;
  31 import javafx.beans.Observable;
  32 import org.junit.runners.Parameterized.Parameters;
  33 import org.junit.runners.Parameterized;
  34 import java.util.Arrays;
  35 import javafx.geometry.Point3D;
  36 import org.junit.Test;
  37 import org.junit.runner.RunWith;
  38 import test.com.sun.javafx.test.TransformHelper;
  39 import javafx.beans.InvalidationListener;
  40 import javafx.event.EventHandler;
  41 import javafx.geometry.Point2D;
  42 import javafx.scene.transform.Affine;
  43 import javafx.scene.transform.AffineShim;
  44 import javafx.scene.transform.MatrixType;
  45 import javafx.scene.transform.NonInvertibleTransformException;
  46 import javafx.scene.transform.Rotate;
  47 import javafx.scene.transform.Scale;
  48 import javafx.scene.transform.Shear;
  49 import javafx.scene.transform.Transform;
  50 import javafx.scene.transform.TransformShim;
  51 import javafx.scene.transform.Translate;
  52 import static org.junit.Assert.*;
  53 
  54 @RunWith(Parameterized.class)
  55 public class AffineOperationsTest {
  56 
  57     private static final Affine identity = new Affine();
  58     private static final Affine translate = new Affine(1, 0, 2,
  59                                                        0, 1, 3);
  60     private static final Affine scale = new Affine(4, 0, 0,
  61                                                    0, 5, 0);
  62     private static final Affine sc_tr = new Affine(6, 0, 8,
  63                                                    0, 7, 9);
  64     private static final Affine shear = new Affine( 0, 10, 0,
  65                                                    11,  0, 0);
  66     private static final Affine sh_tr = new Affine( 0, 12, 14,
  67                                                    13,  0, 15);
  68     private static final Affine sh_sc_simple = new Affine( 1, 18, 0,
  69                                                           19, 1, 0);
  70     private static final Affine sh_sc = new Affine(16, 18, 0,
  71                                                    19, 17, 0);


 193     public AffineOperationsTest(Affine a) {
 194         this.affine = a;
 195     }
 196 
 197     private int eventCounter;
 198     private int listener1Counter;
 199     private int listener2Counter;
 200     private double memMyx, memTy;
 201 
 202     private void assertAffineOk(Transform expected, Affine a) {
 203         TransformHelper.assertMatrix(a, expected);
 204         assertStateOk(a);
 205     }
 206 
 207     private void assertAffineOk(String message, Transform expected, Affine a) {
 208         TransformHelper.assertMatrix(message, a, expected);
 209         assertStateOk(message, a);
 210     }
 211 
 212     private void assertStateOk(Affine a) {
 213         TransformHelper.assertStateOk(a, AffineShim.getState3d(a), AffineShim.getState2d(a));
 214     }
 215 
 216     private void assertStateOk(String message, Affine a) {
 217         TransformHelper.assertStateOk(message, a, AffineShim.getState3d(a), AffineShim.getState2d(a));
 218     }
 219 
 220     private void testOperationIsAtomic(Affine a,
 221             final Runnable op, final Runnable check) {
 222 
 223         a.setOnTransformChanged(event -> {
 224             eventCounter++;
 225             check.run();
 226         });
 227 
 228         a.myxProperty().addListener(observable -> {
 229             listener1Counter++;
 230             check.run();
 231         });
 232 
 233         a.tyProperty().addListener(observable -> {
 234             listener2Counter++;
 235             check.run();
 236         });
 237 
 238         memMyx = a.getMyx();
 239         memTy = a.getTy();
 240         eventCounter = 0;
 241         listener1Counter = 0;
 242         listener2Counter = 0;
 243         op.run();
 244 
 245         assertTrue(listener1Counter == (memMyx == a.getMyx() ? 0 : 1));
 246         assertTrue(listener2Counter == (memTy == a.getTy() ? 0 : 1));
 247         assertEquals(1, eventCounter);
 248         assertFalse(AffineShim.atomicChangeRuns(a));
 249     }
 250 
 251     @Test
 252     public void SetToTransformShouldBeAtomic() {
 253         final Affine a = affine.clone();
 254         final Shear sh = new Shear(12, 15);
 255 
 256         testOperationIsAtomic(a,
 257                 () -> a.setToTransform(sh),
 258                 () -> assertAffineOk(sh, a)
 259         );
 260     }
 261 
 262     @Test
 263     public void SetToTransform2DShouldBeAtomic() {
 264         final Affine a = affine.clone();
 265         final Shear sh = new Shear(12, 15);
 266 
 267         testOperationIsAtomic(a,
 268                 () -> a.setToTransform(1, 12, 0, 15, 1, 0),


2351                 return;
2352             }
2353         } catch (IndexOutOfBoundsException e) {
2354             if (iob) {
2355                 // ok
2356                 return;
2357             }
2358         }
2359 
2360         if (iae) {
2361             fail("Should have thrown IAE");
2362         }
2363         if (iob) {
2364             fail("Should have thrown IOB");
2365         }
2366     }
2367 
2368     @Test
2369     public void testSetElement() {
2370         Affine a = affine.clone();
2371         boolean is2d = TransformShim.computeIs2D(affine);
2372 
2373         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 0, a.getMxx(), !is2d, false);
2374         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 1, a.getMxy(), !is2d, false);
2375         assertSetElement(a, MatrixType.MT_2D_2x3, 0, 2, a.getTx(), !is2d, false);
2376         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 0, a.getMyx(), !is2d, false);
2377         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 1, a.getMyy(), !is2d, false);
2378         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 2, a.getTy(), !is2d, false);
2379         assertSetElement(a, MatrixType.MT_2D_2x3, -1, 0, 0, !is2d, true);
2380         assertSetElement(a, MatrixType.MT_2D_2x3, 2, 0, -1000, !is2d, true);
2381         assertSetElement(a, MatrixType.MT_2D_2x3, 2, 1, -1000, !is2d, true);
2382         assertSetElement(a, MatrixType.MT_2D_2x3, 1, 3, 0, !is2d, true);
2383         assertSetElement(a, MatrixType.MT_2D_2x3, 0, -1, 0, !is2d, true);
2384         if (is2d) {
2385             assertAffineOk(new Affine(
2386                     affine.getMxx() + 1000,
2387                     affine.getMxy() + 1000,
2388                     affine.getMxz(),
2389                     affine.getTx() + 1000,
2390                     affine.getMyx() + 1000,
2391                     affine.getMyy() + 1000,