modules/graphics/src/test/java/test/javafx/animation/StrokeTransitionTest.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.animation;
  27 





  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertNull;
  31 import static org.junit.Assert.assertTrue;
  32 import javafx.scene.Group;
  33 import javafx.scene.paint.Color;
  34 import javafx.scene.paint.LinearGradient;
  35 import javafx.scene.paint.Paint;
  36 import javafx.scene.paint.Stop;
  37 import javafx.scene.shape.Rectangle;
  38 import javafx.scene.shape.Shape;
  39 import javafx.util.Duration;
  40 
  41 import org.junit.Before;
  42 import org.junit.Test;
  43 
  44 public class StrokeTransitionTest {
  45         
  46         private static Duration DEFAULT_DURATION = Duration.millis(400);
  47         private static Interpolator DEFAULT_INTERPOLATOR = Interpolator.EASE_BOTH;


 104                 assertNull(t0.getShape());
 105                 assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 106                 assertNull(t0.getOnFinished());
 107                 
 108                 // duration, shape, and values
 109                 t0 = new StrokeTransition(TWO_SECS, shape, Color.BLACK, Color.WHITE);
 110                 assertEquals(TWO_SECS, t0.getDuration());
 111                 assertColorEquals(Color.BLACK, t0.getFromValue());
 112                 assertColorEquals(Color.WHITE, t0.getToValue());
 113                 assertEquals(shape, t0.getShape());
 114                 assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 115                 assertNull(t0.getOnFinished());
 116         }
 117 
 118         @Test
 119         public void testInterpolate() {
 120                 final Color fromValue = Color.color(0.2, 0.3, 0.7, 0.1);
 121                 final Color toValue = Color.color(0.8, 0.4, 0.2, 0.9);
 122                 final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 123                 
 124                 assertTrue(t0.impl_startable(true));
 125                 t0.impl_start(true);
 126                 t0.interpolate(0.0);
 127                 assertColorEquals(Color.color(0.2, 0.3, 0.7, 0.1), shape.getStroke());
 128                 t0.interpolate(0.4);
 129                 assertColorEquals(Color.color(0.44, 0.34, 0.5, 0.42), shape.getStroke());
 130                 t0.interpolate(1.0);
 131                 assertColorEquals(Color.color(0.8, 0.4, 0.2, 0.9), shape.getStroke());
 132         t0.impl_finished();
 133         }
 134         
 135         @Test
 136         public void testRedValueCombinations() {
 137                 final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, null, Color.WHITE);
 138                 final double originalRed = 0.6;
 139                 final double fromRed = 0.4;
 140                 final Color originalValue = Color.color(originalRed, 1.0, 1.0);
 141                 final Color fromValue = Color.color(fromRed, 1.0, 1.0);
 142                 
 143                 // no from value set
 144                 shape.setStroke(originalValue);
 145                 t0.setFromValue(null);
 146                 assertTrue(t0.impl_startable(true));
 147                 t0.impl_start(true);
 148                 t0.interpolate(0.0);
 149                 assertColorEquals(originalValue, shape.getStroke());
 150                 t0.impl_finished();
 151                 
 152                 // from-value set
 153                 shape.setStroke(originalValue);
 154                 t0.setFromValue(fromValue);
 155                 assertTrue(t0.impl_startable(true));
 156                 t0.impl_start(true);
 157                 t0.interpolate(0.0);
 158                 assertColorEquals(fromValue, shape.getStroke());
 159                 t0.impl_finished();
 160         }
 161         
 162     @Test
 163     public void testGetTargetNode() {
 164         final Color fromValue = Color.color(0.0, 0.4, 0.8, 1.0);
 165                 final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 166         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 167         ft.setInterpolator(Interpolator.LINEAR);
 168         final Shape shape2 = new Rectangle();
 169         final ParallelTransition pt = new ParallelTransition();
 170         pt.getChildren().add(ft);
 171         pt.setNode(shape2);
 172         shape.setStroke(Color.WHITE);
 173         shape2.setStroke(Color.WHITE);
 174 
 175         // node set, parent set
 176         assertTrue(ft.impl_startable(true));
 177         ft.impl_start(true);
 178         ft.interpolate(0.5);
 179         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 180         assertColorEquals(Color.WHITE, shape2.getStroke());
 181         ft.impl_finished();
 182 
 183         // node null, parent set
 184         ft.setShape(null);
 185         assertTrue(ft.impl_startable(true));
 186         ft.impl_start(true);
 187         ft.interpolate(0.4);
 188         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 189         assertColorEquals(Color.color(0.4, 0.56, 0.72, 0.76), shape2.getStroke());
 190         ft.impl_finished();
 191         
 192         // node null, parent not shape set
 193         pt.setNode(new Group());
 194         assertFalse(ft.impl_startable(true));
 195 
 196         // node null, parent null
 197         pt.setNode(null);
 198         assertFalse(ft.impl_startable(true));
 199     }
 200 
 201     @Test
 202     public void testCachedValues() {
 203         final Color fromValue = Color.color(0.0, 0.4, 0.8, 0.2);
 204         final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 205         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 206         ft.setInterpolator(Interpolator.LINEAR);
 207 
 208         // start
 209         assertTrue(ft.impl_startable(true));
 210         ft.impl_start(true);
 211         ft.setFromValue(Color.WHITE);
 212         ft.interpolate(0.5);
 213         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.3), shape.getStroke());
 214         ft.impl_finished();
 215                 ft.setFromValue(fromValue);
 216 
 217         // end
 218         assertTrue(ft.impl_startable(true));
 219         ft.impl_start(true);
 220         ft.setToValue(Color.BLACK);
 221         ft.interpolate(0.2);
 222         assertColorEquals(Color.color(0.2, 0.48, 0.76, 0.24), shape.getStroke());
 223         ft.impl_finished();
 224         ft.setToValue(toValue);
 225 
 226         // shape
 227         assertTrue(ft.impl_startable(true));
 228         ft.impl_start(true);
 229         ft.setShape(null);
 230         ft.interpolate(0.7);
 231         assertColorEquals(Color.color(0.7, 0.68, 0.66, 0.34), shape.getStroke());
 232         ft.impl_finished();
 233         ft.setShape(shape);
 234 
 235         // interpolator
 236         assertTrue(ft.impl_startable(true));
 237         ft.impl_start(true);
 238         ft.setInterpolator(null);
 239         ft.interpolate(0.1);
 240         assertColorEquals(Color.color(0.1, 0.44, 0.78, 0.22), shape.getStroke());
 241         ft.impl_finished();
 242         ft.setInterpolator(Interpolator.LINEAR);
 243     }
 244 
 245         @Test
 246         public void testStartable() {
 247                 final StrokeTransition t0 = new StrokeTransition(Duration.ONE, shape, Color.WHITE, Color.BLACK);
 248                 final Paint paint2 = new LinearGradient(0, 0, 1, 1, false, null,
 249                 new Stop[] { new Stop(0, Color.RED) });
 250                 assertTrue(t0.impl_startable(true));
 251                 
 252                 // duration is 0
 253                 t0.setDuration(Duration.ZERO);
 254                 assertFalse(t0.impl_startable(true));
 255                 t0.setDuration(Duration.ONE);
 256                 assertTrue(t0.impl_startable(true));
 257                 
 258                 // shape is null
 259                 t0.setShape(null);
 260                 assertFalse(t0.impl_startable(true));
 261                 t0.setShape(shape);
 262                 assertTrue(t0.impl_startable(true));
 263                 
 264                 // interpolator is null
 265                 t0.setInterpolator(null);
 266                 assertFalse(t0.impl_startable(true));
 267                 t0.setInterpolator(Interpolator.LINEAR);
 268                 assertTrue(t0.impl_startable(true));
 269                 
 270                 // fromValue
 271                 t0.setFromValue(null);
 272                 shape.setStroke(paint2);
 273                 assertFalse(t0.impl_startable(true));
 274                 shape.setStroke(Color.BLACK);
 275                 assertTrue(t0.impl_startable(true));
 276                 t0.setFromValue(Color.WHITE);
 277                 shape.setStroke(paint2);
 278                 assertTrue(t0.impl_startable(true));
 279                 
 280                 // toValue
 281                 t0.setToValue(null);
 282                 assertFalse(t0.impl_startable(true));
 283                 t0.setToValue(Color.BLACK);
 284                 assertTrue(t0.impl_startable(true));
 285         }
 286 
 287         @Test
 288         public void testEvaluateStartValue() {
 289                 final StrokeTransition t0 = new StrokeTransition(Duration.INDEFINITE, shape, null, Color.WHITE);
 290                 
 291                 // first run
 292                 shape.setStroke(Color.GREY);
 293                 assertTrue(t0.impl_startable(true));
 294                 t0.impl_start(true);
 295                 shape.setStroke(Color.TRANSPARENT);
 296                 t0.interpolate(0.0);
 297                 assertColorEquals(Color.GREY, shape.getStroke());
 298                 t0.impl_finished();
 299                 
 300                 // second run
 301                 shape.setStroke(Color.BLACK);
 302                 assertTrue(t0.impl_startable(true));
 303                 t0.impl_start(true);
 304                 shape.setStroke(Color.WHITE);
 305                 t0.interpolate(0.0);
 306                 assertColorEquals(Color.BLACK, shape.getStroke());
 307                 t0.impl_finished();
 308         }
 309 
 310 }


   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.animation;
  27 
  28 import javafx.animation.AnimationShim;
  29 import javafx.animation.Interpolator;
  30 import javafx.animation.ParallelTransition;
  31 import javafx.animation.StrokeTransition;
  32 import javafx.animation.TransitionShim;
  33 import static org.junit.Assert.assertEquals;
  34 import static org.junit.Assert.assertFalse;
  35 import static org.junit.Assert.assertNull;
  36 import static org.junit.Assert.assertTrue;
  37 import javafx.scene.Group;
  38 import javafx.scene.paint.Color;
  39 import javafx.scene.paint.LinearGradient;
  40 import javafx.scene.paint.Paint;
  41 import javafx.scene.paint.Stop;
  42 import javafx.scene.shape.Rectangle;
  43 import javafx.scene.shape.Shape;
  44 import javafx.util.Duration;
  45 
  46 import org.junit.Before;
  47 import org.junit.Test;
  48 
  49 public class StrokeTransitionTest {
  50         
  51         private static Duration DEFAULT_DURATION = Duration.millis(400);
  52         private static Interpolator DEFAULT_INTERPOLATOR = Interpolator.EASE_BOTH;


 109                 assertNull(t0.getShape());
 110                 assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 111                 assertNull(t0.getOnFinished());
 112                 
 113                 // duration, shape, and values
 114                 t0 = new StrokeTransition(TWO_SECS, shape, Color.BLACK, Color.WHITE);
 115                 assertEquals(TWO_SECS, t0.getDuration());
 116                 assertColorEquals(Color.BLACK, t0.getFromValue());
 117                 assertColorEquals(Color.WHITE, t0.getToValue());
 118                 assertEquals(shape, t0.getShape());
 119                 assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 120                 assertNull(t0.getOnFinished());
 121         }
 122 
 123         @Test
 124         public void testInterpolate() {
 125                 final Color fromValue = Color.color(0.2, 0.3, 0.7, 0.1);
 126                 final Color toValue = Color.color(0.8, 0.4, 0.2, 0.9);
 127                 final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 128                 
 129                 assertTrue(AnimationShim.impl_startable(t0,true));
 130                 AnimationShim.impl_start(t0,true);
 131                 TransitionShim.interpolate(t0,0.0);
 132                 assertColorEquals(Color.color(0.2, 0.3, 0.7, 0.1), shape.getStroke());
 133                 TransitionShim.interpolate(t0,0.4);
 134                 assertColorEquals(Color.color(0.44, 0.34, 0.5, 0.42), shape.getStroke());
 135                 TransitionShim.interpolate(t0,1.0);
 136                 assertColorEquals(Color.color(0.8, 0.4, 0.2, 0.9), shape.getStroke());
 137         AnimationShim.impl_finished(t0);
 138         }
 139         
 140         @Test
 141         public void testRedValueCombinations() {
 142                 final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, null, Color.WHITE);
 143                 final double originalRed = 0.6;
 144                 final double fromRed = 0.4;
 145                 final Color originalValue = Color.color(originalRed, 1.0, 1.0);
 146                 final Color fromValue = Color.color(fromRed, 1.0, 1.0);
 147                 
 148                 // no from value set
 149                 shape.setStroke(originalValue);
 150                 t0.setFromValue(null);
 151                 assertTrue(AnimationShim.impl_startable(t0,true));
 152                 AnimationShim.impl_start(t0,true);
 153                 TransitionShim.interpolate(t0,0.0);
 154                 assertColorEquals(originalValue, shape.getStroke());
 155                 AnimationShim.impl_finished(t0);
 156                 
 157                 // from-value set
 158                 shape.setStroke(originalValue);
 159                 t0.setFromValue(fromValue);
 160                 assertTrue(AnimationShim.impl_startable(t0,true));
 161                 AnimationShim.impl_start(t0,true);
 162                 TransitionShim.interpolate(t0,0.0);
 163                 assertColorEquals(fromValue, shape.getStroke());
 164                 AnimationShim.impl_finished(t0);
 165         }
 166         
 167     @Test
 168     public void testGetTargetNode() {
 169         final Color fromValue = Color.color(0.0, 0.4, 0.8, 1.0);
 170                 final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 171         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 172         ft.setInterpolator(Interpolator.LINEAR);
 173         final Shape shape2 = new Rectangle();
 174         final ParallelTransition pt = new ParallelTransition();
 175         pt.getChildren().add(ft);
 176         pt.setNode(shape2);
 177         shape.setStroke(Color.WHITE);
 178         shape2.setStroke(Color.WHITE);
 179 
 180         // node set, parent set
 181         assertTrue(AnimationShim.impl_startable(ft,true));
 182         AnimationShim.impl_start(ft,true);
 183         TransitionShim.interpolate(ft,0.5);
 184         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 185         assertColorEquals(Color.WHITE, shape2.getStroke());
 186         AnimationShim.impl_finished(ft);
 187 
 188         // node null, parent set
 189         ft.setShape(null);
 190         assertTrue(AnimationShim.impl_startable(ft,true));
 191         AnimationShim.impl_start(ft,true);
 192         TransitionShim.interpolate(ft,0.4);
 193         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 194         assertColorEquals(Color.color(0.4, 0.56, 0.72, 0.76), shape2.getStroke());
 195         AnimationShim.impl_finished(ft);
 196         
 197         // node null, parent not shape set
 198         pt.setNode(new Group());
 199         assertFalse(AnimationShim.impl_startable(ft,true));
 200 
 201         // node null, parent null
 202         pt.setNode(null);
 203         assertFalse(AnimationShim.impl_startable(ft,true));
 204     }
 205 
 206     @Test
 207     public void testCachedValues() {
 208         final Color fromValue = Color.color(0.0, 0.4, 0.8, 0.2);
 209         final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 210         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 211         ft.setInterpolator(Interpolator.LINEAR);
 212 
 213         // start
 214         assertTrue(AnimationShim.impl_startable(ft,true));
 215         AnimationShim.impl_start(ft,true);
 216         ft.setFromValue(Color.WHITE);
 217         TransitionShim.interpolate(ft,0.5);
 218         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.3), shape.getStroke());
 219         AnimationShim.impl_finished(ft);
 220                 ft.setFromValue(fromValue);
 221 
 222         // end
 223         assertTrue(AnimationShim.impl_startable(ft,true));
 224         AnimationShim.impl_start(ft,true);
 225         ft.setToValue(Color.BLACK);
 226         TransitionShim.interpolate(ft,0.2);
 227         assertColorEquals(Color.color(0.2, 0.48, 0.76, 0.24), shape.getStroke());
 228         AnimationShim.impl_finished(ft);
 229         ft.setToValue(toValue);
 230 
 231         // shape
 232         assertTrue(AnimationShim.impl_startable(ft,true));
 233         AnimationShim.impl_start(ft,true);
 234         ft.setShape(null);
 235         TransitionShim.interpolate(ft,0.7);
 236         assertColorEquals(Color.color(0.7, 0.68, 0.66, 0.34), shape.getStroke());
 237         AnimationShim.impl_finished(ft);
 238         ft.setShape(shape);
 239 
 240         // interpolator
 241         assertTrue(AnimationShim.impl_startable(ft,true));
 242         AnimationShim.impl_start(ft,true);
 243         ft.setInterpolator(null);
 244         TransitionShim.interpolate(ft,0.1);
 245         assertColorEquals(Color.color(0.1, 0.44, 0.78, 0.22), shape.getStroke());
 246         AnimationShim.impl_finished(ft);
 247         ft.setInterpolator(Interpolator.LINEAR);
 248     }
 249 
 250         @Test
 251         public void testStartable() {
 252                 final StrokeTransition t0 = new StrokeTransition(Duration.ONE, shape, Color.WHITE, Color.BLACK);
 253                 final Paint paint2 = new LinearGradient(0, 0, 1, 1, false, null,
 254                 new Stop[] { new Stop(0, Color.RED) });
 255                 assertTrue(AnimationShim.impl_startable(t0,true));
 256                 
 257                 // duration is 0
 258                 t0.setDuration(Duration.ZERO);
 259                 assertFalse(AnimationShim.impl_startable(t0,true));
 260                 t0.setDuration(Duration.ONE);
 261                 assertTrue(AnimationShim.impl_startable(t0,true));
 262                 
 263                 // shape is null
 264                 t0.setShape(null);
 265                 assertFalse(AnimationShim.impl_startable(t0,true));
 266                 t0.setShape(shape);
 267                 assertTrue(AnimationShim.impl_startable(t0,true));
 268                 
 269                 // interpolator is null
 270                 t0.setInterpolator(null);
 271                 assertFalse(AnimationShim.impl_startable(t0,true));
 272                 t0.setInterpolator(Interpolator.LINEAR);
 273                 assertTrue(AnimationShim.impl_startable(t0,true));
 274                 
 275                 // fromValue
 276                 t0.setFromValue(null);
 277                 shape.setStroke(paint2);
 278                 assertFalse(AnimationShim.impl_startable(t0,true));
 279                 shape.setStroke(Color.BLACK);
 280                 assertTrue(AnimationShim.impl_startable(t0,true));
 281                 t0.setFromValue(Color.WHITE);
 282                 shape.setStroke(paint2);
 283                 assertTrue(AnimationShim.impl_startable(t0,true));
 284                 
 285                 // toValue
 286                 t0.setToValue(null);
 287                 assertFalse(AnimationShim.impl_startable(t0,true));
 288                 t0.setToValue(Color.BLACK);
 289                 assertTrue(AnimationShim.impl_startable(t0,true));
 290         }
 291 
 292         @Test
 293         public void testEvaluateStartValue() {
 294                 final StrokeTransition t0 = new StrokeTransition(Duration.INDEFINITE, shape, null, Color.WHITE);
 295                 
 296                 // first run
 297                 shape.setStroke(Color.GREY);
 298                 assertTrue(AnimationShim.impl_startable(t0,true));
 299                 AnimationShim.impl_start(t0,true);
 300                 shape.setStroke(Color.TRANSPARENT);
 301                 TransitionShim.interpolate(t0,0.0);
 302                 assertColorEquals(Color.GREY, shape.getStroke());
 303                 AnimationShim.impl_finished(t0);
 304                 
 305                 // second run
 306                 shape.setStroke(Color.BLACK);
 307                 assertTrue(AnimationShim.impl_startable(t0,true));
 308                 AnimationShim.impl_start(t0,true);
 309                 shape.setStroke(Color.WHITE);
 310                 TransitionShim.interpolate(t0,0.0);
 311                 assertColorEquals(Color.BLACK, shape.getStroke());
 312                 AnimationShim.impl_finished(t0);
 313         }
 314 
 315 }