1 /*
   2  * Copyright (c) 2010, 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 test.javafx.scene.paint;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import javafx.scene.paint.Color;
  33 import javafx.scene.paint.CycleMethod;
  34 import javafx.scene.paint.LinearGradient;
  35 import javafx.scene.paint.RadialGradient;
  36 import javafx.scene.paint.Stop;
  37 
  38 import org.junit.Test;
  39 
  40 public class StopListTest {
  41 
  42     private final Color color1 = new Color(0.0, 0.0, 0.0, 0.0);
  43     private final Color color2 = new Color(0.5, 0.5, 0.5, 0.5);
  44     private final Color color3 = new Color(1.0, 1.0, 1.0, 1.0);
  45     private final Stop zerostop = new Stop(0.0, color1);
  46     private final Stop stop1 = new Stop(0.1, color1);
  47     private final Stop stop2 = new Stop(0.2, color2);
  48     private final Stop stop3 = new Stop(0.3, color3);
  49     private final Stop onestop = new Stop(1.0, color3);
  50 
  51     static List<Stop> normalize(Stop... stops) {
  52         LinearGradient lg = new LinearGradient(0, 0, 1, 1, true,
  53                                                CycleMethod.NO_CYCLE, stops);
  54         RadialGradient rg = new RadialGradient(0, 0, 0, 0, 1, true,
  55                                                CycleMethod.NO_CYCLE, stops);
  56         assertEquals(lg.getStops(), rg.getStops());
  57         return lg.getStops();
  58     }
  59 
  60     static List<Stop> normalize(List<Stop> stops) {
  61         LinearGradient lg = new LinearGradient(0, 0, 1, 1, true,
  62                                                CycleMethod.NO_CYCLE, stops);
  63         RadialGradient rg = new RadialGradient(0, 0, 0, 0, 1, true,
  64                                                CycleMethod.NO_CYCLE, stops);
  65         assertEquals(lg.getStops(), rg.getStops());
  66         return lg.getStops();
  67     }
  68 
  69     @Test
  70     public void testNormalizeStopsEmpty() {
  71         List<Stop> zeroOneList = Arrays.asList(
  72             new Stop(0.0, Color.TRANSPARENT),
  73             new Stop(1.0, Color.TRANSPARENT)
  74         );
  75 
  76         assertEquals(zeroOneList, normalize((Stop[]) null));
  77         assertEquals(zeroOneList, normalize((List<Stop>) null));
  78         assertEquals(zeroOneList, normalize(new Stop(0.5, null)));
  79         assertEquals(zeroOneList, normalize(new Stop[0]));
  80         assertEquals(zeroOneList, normalize(new Stop[] { null }));
  81         assertEquals(zeroOneList, normalize(new Stop[] { null, null, null }));
  82     }
  83 
  84     @Test
  85     public void testNormalizeOneStop() {
  86         Stop[] justzero = new Stop[] { zerostop };
  87         Stop[] justone = new Stop[] { onestop };
  88         Stop[] justmid = new Stop[] { stop2 };
  89         List<Stop> allZeroList = Arrays.asList(zerostop, new Stop(1.0, color1));
  90         List<Stop> allOneList = Arrays.asList(new Stop(0.0, color3), onestop);
  91         List<Stop> allColor2List = Arrays.asList(
  92             new Stop(0.0, color2),
  93             new Stop(1.0, color2)
  94         );
  95 
  96         assertEquals(allZeroList, normalize(justzero));
  97         assertEquals(allOneList, normalize(justone));
  98         assertEquals(allColor2List, normalize(justmid));
  99     }
 100 
 101     @Test
 102     public void testNormalizeStopsNonEmpty() {
 103         Stop[] noNull = new Stop[] { stop1, stop2, stop3 };
 104         Stop[] nullFirst = new Stop[] { null, null, stop1, stop2, stop3 };
 105         Stop[] nullMiddle = new Stop[] { stop1, null, null, stop2, stop3 };
 106         Stop[] nullLast = new Stop[] { stop1, stop2, stop3, null, null };
 107         Stop[] manyNulls = new Stop[] { null, stop1, null, null, stop2, null,
 108                 stop3, null };
 109         List<Stop> noNullList =
 110             Arrays.asList(zerostop, stop1, stop2, stop3, onestop);
 111 
 112         assertEquals(noNullList, normalize(noNull));
 113         assertEquals(noNullList, normalize(nullFirst));
 114         assertEquals(noNullList, normalize(nullMiddle));
 115         assertEquals(noNullList, normalize(nullLast));
 116         assertEquals(noNullList, normalize(manyNulls));
 117     }
 118 
 119     @Test
 120     public void testNormalizeStopsDuplicated() {
 121         Stop[] dupzeros = new Stop[] { zerostop, zerostop, zerostop,
 122                                        stop1, stop2, stop3 };
 123         Stop[] onedup = new Stop[] { stop1, stop1, stop2, stop2, stop3, stop3 };
 124         Stop[] twodups = new Stop[] { stop1, stop1, stop1,
 125                                       stop2, stop2, stop2,
 126                                       stop3, stop3, stop3 };
 127         List<Stop> singleList =
 128             Arrays.asList(zerostop, stop1, stop2, stop3, onestop);
 129         List<Stop> dupList =
 130             Arrays.asList(zerostop,
 131                           stop1, stop1, stop2, stop2, stop3, stop3,
 132                           onestop);
 133 
 134         assertEquals(singleList, normalize(dupzeros));
 135         assertEquals(dupList, normalize(onedup));
 136         assertEquals(dupList, normalize(twodups));
 137     }
 138 
 139     @Test
 140     public void testNormalizeStopsNonUnsorted() {
 141         Stop[] unordered = new Stop[] { stop2, stop3, stop1 };
 142         Stop[] unordereddups = new Stop[] { stop3, stop2, stop1,
 143                                             stop2, stop3, stop1 };
 144         List<Stop> sortedList =
 145             Arrays.asList(zerostop, stop1, stop2, stop3, onestop);
 146         List<Stop> dupSortedList =
 147             Arrays.asList(zerostop,
 148                           stop1, stop1, stop2, stop2, stop3, stop3,
 149                           onestop);
 150 
 151         assertEquals(sortedList, normalize(unordered));
 152         assertEquals(dupSortedList, normalize(unordereddups));
 153     }
 154 }