changeset 1093:f5158988671f

Tests for transformations.
author Pavel Safrata
date Fri, 18 May 2012 15:37:12 +0200
parents afd6ff987c7f
children 6f6711e369dd
files javafx-ui-common/test/unit/com/sun/javafx/scene/transform/TrasformUtilsTest.java javafx-ui-common/test/unit/com/sun/javafx/test/TransformHelper.java javafx-ui-common/test/unit/javafx/scene/transform/AffineTest.java javafx-ui-common/test/unit/javafx/scene/transform/RotateTest.java javafx-ui-common/test/unit/javafx/scene/transform/ScaleTest.java javafx-ui-common/test/unit/javafx/scene/transform/ShearTest.java javafx-ui-common/test/unit/javafx/scene/transform/TransformTest.java javafx-ui-common/test/unit/javafx/scene/transform/Transform_properties_Test.java javafx-ui-common/test/unit/javafx/scene/transform/TranslateTest.java
diffstat 9 files changed, 441 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/javafx-ui-common/test/unit/com/sun/javafx/scene/transform/TrasformUtilsTest.java	Fri May 18 15:37:12 2012 +0200
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.sun.javafx.scene.transform;
+
+import com.sun.javafx.test.TransformHelper;
+import javafx.scene.transform.Transform;
+import com.sun.javafx.geom.transform.Affine3D;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TrasformUtilsTest {
+    @Test
+    public void shouldCreateCorrectImmutableTransform() {
+        Transform t = TransformUtils.immutableTransform(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        TransformHelper.assertMatrix(t,
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+    }
+
+    @Test
+    public void immutableTransformShouldApplyCorrectly() {
+        Affine3D a = new Affine3D();
+        a.translate(10, 20);
+
+        Transform t = TransformUtils.immutableTransform(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        t.impl_apply(a);
+
+        TransformHelper.assertMatrix(a,
+                1,  2,  3, 14,
+                5,  6,  7, 28,
+                9, 10, 11, 12);
+    }
+
+    @Test
+    public void immutableTransformShouldCopyCorrectly() {
+        Transform src = TransformUtils.immutableTransform(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        Transform t = src.impl_copy();
+
+        TransformHelper.assertMatrix(t,
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+    }
+
+    @Test public void testImmutableTransformToString() {
+        final Transform trans = TransformUtils.immutableTransform(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+        
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
+
+}
--- a/javafx-ui-common/test/unit/com/sun/javafx/test/TransformHelper.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/com/sun/javafx/test/TransformHelper.java	Fri May 18 15:37:12 2012 +0200
@@ -26,6 +26,10 @@
 
 import javafx.scene.transform.Transform;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import com.sun.javafx.geom.transform.Affine3D;
+import javafx.scene.Node;
+import javafx.scene.transform.Affine;
 
 public class TransformHelper {
     public static void assertMatrix(Transform matrix,
@@ -46,4 +50,21 @@
         assertEquals(tz, matrix.getTz(), 0.00001);
     }
 
+    public static void assertMatrix(Affine3D matrix,
+            double mxx, double mxy, double mxz, double tx,
+            double myx, double myy, double myz, double ty,
+            double mzx, double mzy, double mzz, double tz) {
+        assertEquals(mxx, matrix.getMxx(), 0.00001);
+        assertEquals(mxy, matrix.getMxy(), 0.00001);
+        assertEquals(mxz, matrix.getMxz(), 0.00001);
+        assertEquals(tx, matrix.getMxt(), 0.00001);
+        assertEquals(myx, matrix.getMyx(), 0.00001);
+        assertEquals(myy, matrix.getMyy(), 0.00001);
+        assertEquals(myz, matrix.getMyz(), 0.00001);
+        assertEquals(ty, matrix.getMyt(), 0.00001);
+        assertEquals(mzx, matrix.getMzx(), 0.00001);
+        assertEquals(mzy, matrix.getMzy(), 0.00001);
+        assertEquals(mzz, matrix.getMzz(), 0.00001);
+        assertEquals(tz, matrix.getMzt(), 0.00001);
+    }
 }
--- a/javafx-ui-common/test/unit/javafx/scene/transform/AffineTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/AffineTest.java	Fri May 18 15:37:12 2012 +0200
@@ -29,10 +29,12 @@
 
 import org.junit.Assert;
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 import com.sun.javafx.test.TransformHelper;
 import com.sun.javafx.geom.transform.Affine2D;
 import com.sun.javafx.geom.transform.Affine3D;
+import com.sun.javafx.scene.transform.TransformUtils;
 
 public class AffineTest {
 
@@ -78,11 +80,11 @@
 
     @Test public void testGetters() {
         final Affine trans = new Affine(
-                1,  2,  3,  4,
+                0.5,  2,  3,  4,
                 5,  6,  7,  8,
                 9, 10, 11, 12);
         TransformHelper.assertMatrix(trans,
-                1,  2,  3,  4,
+                0.5,  2,  3,  4,
                 5,  6,  7,  8,
                 9, 10, 11, 12);
 
@@ -105,6 +107,87 @@
                  4,  3,  2, 1);
     }
 
+    @Test public void testConstructingIdentityTransform() {
+        final Affine trans = new Affine(
+                1, 0, 0, 0,
+                0, 1, 0, 0,
+                0, 0, 1, 0);
+
+        TransformHelper.assertMatrix(trans,
+                1, 0, 0, 0,
+                0, 1, 0, 0,
+                0, 0, 1, 0);
+
+        trans.setMxx(12);
+        trans.setMxy(11);
+        trans.setMxz(10);
+        trans.setTx(9);
+        trans.setMyx(8);
+        trans.setMyy(7);
+        trans.setMyz(6);
+        trans.setTy(5);
+        trans.setMzx(4);
+        trans.setMzy(3);
+        trans.setMzz(2);
+        trans.setTz(1);
+
+        TransformHelper.assertMatrix(trans,
+                12, 11, 10, 9,
+                 8,  7,  6, 5,
+                 4,  3,  2, 1);
+    }
+
+    @Test public void testSettingTransform() {
+        final Affine trans = new Affine(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        TransformHelper.assertMatrix(trans,
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        Transform it = TransformUtils.immutableTransform(
+                12, 11, 10, 9,
+                 8,  7,  6, 5,
+                 4,  3,  2, 1);
+
+        trans.setTransform(it);
+
+        TransformHelper.assertMatrix(trans,
+                12, 11, 10, 9,
+                 8,  7,  6, 5,
+                 4,  3,  2, 1);
+    }
+
+    @Test
+    public void testCopying() {
+        final Affine trans = new Affine(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        Transform copy = trans.impl_copy();
+
+        TransformHelper.assertMatrix(copy,
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+    }
+
+    @Test public void testToString() {
+        final Affine trans = new Affine(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
+
     @Test public void testBoundPropertySynced() throws Exception {
 
         TransformTest.checkDoublePropertySynced(createTransform(), "mxx" , 2.0);
--- a/javafx-ui-common/test/unit/javafx/scene/transform/RotateTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/RotateTest.java	Fri May 18 15:37:12 2012 +0200
@@ -25,6 +25,7 @@
 package javafx.scene.transform;
 
 import static javafx.scene.transform.TransformTest.assertTx;
+import static org.junit.Assert.*;
 
 import java.lang.reflect.Method;
 
@@ -166,6 +167,59 @@
         checkObjectPropertySynced("axis", new Point3D(1, 0, 0));
     }
 
+    @Test
+    public void testCopying() {
+        final Rotate trans = new Rotate();
+
+        trans.setAngle(45);
+        trans.setPivotX(66);
+        trans.setPivotY(77);
+        trans.setPivotZ(88);
+        trans.setAxis(new Point3D(20, 30, 40));
+
+        Transform copy = trans.impl_copy();
+
+        final Point3D a = new Point3D(20.0 / Math.sqrt(2900.0),
+                                30.0 / Math.sqrt(2900.0),
+                                40.0 / Math.sqrt(2900.0));
+        double sin = Math.sin(Math.PI / 4);
+        double cos = Math.cos(Math.PI / 4);
+
+        TransformHelper.assertMatrix(copy,
+                cos + a.getX() * a.getX() * (1 - cos), //mxx
+                a.getX() * a.getY() * (1 - cos) - a.getZ() * sin, //mxy
+                a.getX() * a.getZ() * (1 - cos) + a.getY() * sin, //mxz
+                66
+                    - 66 * (cos + a.getX() * a.getX() * (1 - cos))
+                    - 77 * (a.getX() * a.getY() * (1 - cos) - a.getZ() * sin)
+                    - 88 * (a.getX() * a.getZ() * (1 - cos) + a.getY() * sin), //tx
+                a.getY() * a.getX() * (1 - cos) + a.getZ() * sin, //myx
+                cos + a.getY() * a.getY() * (1 - cos), //myy
+                a.getY() * a.getZ() * (1 - cos) - a.getX() * sin, //myz
+                77
+                    - 66 * (a.getY() * a.getX() * (1 - cos) + a.getZ() * sin)
+                    - 77 * (cos + a.getY() * a.getY() * (1 - cos))
+                    - 88 * (a.getY() * a.getZ() * (1 - cos) - a.getX() * sin), //ty
+                a.getZ() * a.getX() * (1 - cos) - a.getY() * sin, //mzx
+                a.getZ() * a.getY() * (1 - cos) + a.getX() * sin, //mzy
+                cos + a.getZ() * a.getZ() * (1 - cos), //mzz
+                88
+                    - 66 * (a.getZ() * a.getX() * (1 - cos) - a.getY() * sin)
+                    - 77 * (a.getZ() * a.getY() * (1 - cos) + a.getX() * sin)
+                    - 88 * (cos + a.getZ() * a.getZ() * (1 - cos)) //tz
+
+                );
+    }
+
+    @Test public void testToString() {
+        final Rotate trans = new Rotate(40);
+
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
+
     private void checkObjectPropertySynced(String propertyName, Object val)
             throws Exception {
 
--- a/javafx-ui-common/test/unit/javafx/scene/transform/ScaleTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/ScaleTest.java	Fri May 18 15:37:12 2012 +0200
@@ -30,6 +30,7 @@
 import org.junit.Assert;
 import org.junit.Test;
 
+import static org.junit.Assert.*;
 import com.sun.javafx.test.TransformHelper;
 import com.sun.javafx.geom.transform.Affine2D;
 import com.sun.javafx.geom.transform.Affine3D;
@@ -126,6 +127,26 @@
         assertTx(n, expT);
     }
 
+    @Test
+    public void testCopying() {
+        final Scale trans = new Scale(34, 67, 10, 66, 77, 5);
+
+        Transform copy = trans.impl_copy();
+
+        TransformHelper.assertMatrix(copy,
+                34,  0,  0, -2178,
+                 0, 67,  0, -5082,
+                 0,  0, 10,   -45);
+    }
+
+    @Test public void testToString() {
+        final Scale trans = new Scale(5, 8);
+
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
 
     @Test public void testBoundPropertySynced_PivotX() throws Exception {
         TransformTest.checkDoublePropertySynced(new Scale(3, 3, 0), "pivotX", 200.0);
--- a/javafx-ui-common/test/unit/javafx/scene/transform/ShearTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/ShearTest.java	Fri May 18 15:37:12 2012 +0200
@@ -28,6 +28,7 @@
 import javafx.scene.shape.Rectangle;
 
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 import com.sun.javafx.test.TransformHelper;
 import com.sun.javafx.geom.transform.Affine2D;
@@ -102,6 +103,27 @@
                  0,  0, 1,      0);
     }
 
+    @Test
+    public void testCopying() {
+        final Shear trans = new Shear(34, 67, 66, 77);
+
+        Transform copy = trans.impl_copy();
+
+        TransformHelper.assertMatrix(copy,
+                 1, 34, 0, -34*77,
+                67,  1, 0, -67*66,
+                 0,  0, 1,      0);
+    }
+
+    @Test public void testToString() {
+        final Shear trans = new Shear(8, 15);
+
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
+
     @Test public void testBoundPropertySynced_X() throws Exception {
         TransformTest.checkDoublePropertySynced(new Shear(3, 3, 0, 0), "x", 123.0);
     }
--- a/javafx-ui-common/test/unit/javafx/scene/transform/TransformTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/TransformTest.java	Fri May 18 15:37:12 2012 +0200
@@ -36,11 +36,15 @@
 
 import org.junit.Assert;
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 import com.sun.javafx.geom.transform.Affine2D;
 import com.sun.javafx.geom.transform.Affine3D;
 import com.sun.javafx.geom.transform.BaseTransform;
 import com.sun.javafx.pgstub.StubRectangle;
+import com.sun.javafx.scene.transform.TransformUtils;
+import com.sun.javafx.test.TransformHelper;
+import javafx.geometry.Point3D;
 
 
 public class TransformTest {
@@ -143,7 +147,38 @@
         final Affine3D affine3D = new Affine3D(11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333);
         assertTx(n, affine3D);
     }
+
+    @Test
+    public void defaultTransformShouldBeIdentity() {
+        final Transform t = new Transform() {
+            @Override
+            public void impl_apply(com.sun.javafx.geom.transform.Affine3D ad) {}
+            @Override
+            public Transform impl_copy() { return null; }
+        };
+
+        TransformHelper.assertMatrix(t,
+                1, 0, 0, 0,
+                0, 1, 0, 0,
+                0, 0, 1, 0);
+    }
     
+    @Test
+    public void testTransform() {
+        Transform t = TransformUtils.immutableTransform(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        Point3D point = new Point3D(2, 5, 7);
+
+        Point3D transformed = t.impl_transform(point);
+
+        assertEquals(37, transformed.getX(), 0.0001);
+        assertEquals(97, transformed.getY(), 0.0001);
+        assertEquals(157, transformed.getZ(), 0.0001);
+    }
+
     static void checkDoublePropertySynced(Transform tr, String propertyName, double val)
         throws Exception {
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/Transform_properties_Test.java	Fri May 18 15:37:12 2012 +0200
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package javafx.scene.transform;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import com.sun.javafx.test.PropertiesTestBase;
+import javafx.geometry.Point3D;
+
+@RunWith(Parameterized.class)
+public class Transform_properties_Test extends PropertiesTestBase {
+    @Parameters
+    public static Collection data() {
+        final Affine a = new Affine(
+                1,  2,  3,  4,
+                5,  6,  7,  8,
+                9, 10, 11, 12);
+
+        final Rotate r = new Rotate();
+        final Shear s = new Shear();
+        final Translate t = new Translate();
+        final Scale c = new Scale();
+
+        return Arrays.asList(new Object[] {
+            config(a, "mxx", 10.0, 20.0),
+            config(a, "mxy", 10.0, 20.0),
+            config(a, "mxz", 10.0, 20.0),
+            config(a, "tx", 10.0, 20.0),
+            config(a, "myx", 10.0, 20.0),
+            config(a, "myy", 10.0, 20.0),
+            config(a, "myz", 10.0, 20.0),
+            config(a, "ty", 10.0, 20.0),
+            config(a, "mzx", 10.0, 20.0),
+            config(a, "mzy", 10.0, 20.0),
+            config(a, "mzz", 10.0, 20.0),
+            config(a, "tz", 10.0, 20.0),
+            config(r, "angle", 10.0, 20.0),
+            config(r, "axis", new Point3D(10, 20, 30), new Point3D(30, 20, 10)),
+            config(r, "pivotX", 10.0, 20.0),
+            config(r, "pivotY", 10.0, 20.0),
+            config(r, "pivotZ", 10.0, 20.0),
+            config(s, "x", 10.0, 20.0),
+            config(s, "y", 10.0, 20.0),
+            config(s, "pivotX", 10.0, 20.0),
+            config(s, "pivotY", 10.0, 20.0),
+            config(t, "x", 10.0, 20.0),
+            config(t, "y", 10.0, 20.0),
+            config(t, "z", 10.0, 20.0),
+            config(c, "x", 10.0, 20.0),
+            config(c, "y", 10.0, 20.0),
+            config(c, "z", 10.0, 20.0),
+            config(c, "pivotX", 10.0, 20.0),
+            config(c, "pivotY", 10.0, 20.0),
+            config(c, "pivotZ", 10.0, 20.0),
+        });
+    }
+
+    public Transform_properties_Test(final Configuration configuration) {
+        super(configuration);
+    }
+}
--- a/javafx-ui-common/test/unit/javafx/scene/transform/TranslateTest.java	Fri May 18 12:51:15 2012 +0200
+++ b/javafx-ui-common/test/unit/javafx/scene/transform/TranslateTest.java	Fri May 18 15:37:12 2012 +0200
@@ -29,6 +29,7 @@
 
 import org.junit.Assert;
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 import com.sun.javafx.test.TransformHelper;
 import com.sun.javafx.geom.transform.BaseTransform;
@@ -74,6 +75,27 @@
                 0, 0, 1, 33);
     }
 
+    @Test
+    public void testCopying() {
+        final Translate trans = new Translate(34, 67, 33);
+
+        Transform copy = trans.impl_copy();
+
+        TransformHelper.assertMatrix(copy,
+                1, 0, 0, 34,
+                0, 1, 0, 67,
+                0, 0, 1, 33);
+    }
+
+    @Test public void testToString() {
+        final Translate trans = new Translate(8, 15);
+
+        String s = trans.toString();
+
+        assertNotNull(s);
+        assertFalse(s.isEmpty());
+    }
+
     @Test public void testBoundPropertySynced_X() throws Exception {
         TransformTest.checkDoublePropertySynced(new Translate(3, 3, 0), "x", 22.0);
     }