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 java.util.Arrays;
  29 import java.util.List;
  30 import com.sun.javafx.tk.Toolkit;
  31 import javafx.scene.paint.Color;
  32 import javafx.scene.paint.CycleMethod;
  33 import javafx.scene.paint.LinearGradient;
  34 import javafx.scene.paint.Stop;
  35 import org.junit.Test;
  36 import static org.junit.Assert.assertEquals;
  37 import static org.junit.Assert.assertFalse;
  38 import static org.junit.Assert.assertNotNull;
  39 import static org.junit.Assert.assertSame;
  40 import static org.junit.Assert.assertTrue;
  41 import static org.junit.Assert.fail;
  42 
  43 public class LinearGradientTest {
  44 
  45     private final Color color1 = Color.rgb(0, 0, 0);
  46     private final Color color2 = Color.rgb(255, 255, 255);
  47     private final Stop stop1 = new Stop(0.1, color1);
  48     private final Stop stop2 = new Stop(0.2, color2);
  49     private final Stop[] noStop = new Stop[0];
  50     private final Stop[] oneStop = new Stop[] { stop1 };
  51     private final Stop[] twoStops = new Stop[] { stop1, stop2 };
  52     private final Stop[] twoStopsWithNulls =
  53             new Stop[] { stop1, null, stop2, null };
  54     private final List<Stop> normalizedTwoStops = Arrays.asList(
  55         new Stop(0.0, color1),
  56         stop1, stop2,
  57         new Stop(1.0, color2)
  58     );
  59 
  60     @Test
  61     public void testLinearGradient() {
  62         LinearGradient gradient = new LinearGradient(1f, 2f, 3f, 4f, true,
  63                 CycleMethod.REPEAT, twoStops);
  64 
  65         assertEquals(1f, gradient.getStartX(), 0.0001);
  66         assertEquals(2f, gradient.getStartY(), 0.0001);
  67         assertEquals(3f, gradient.getEndX(), 0.0001);
  68         assertEquals(4f, gradient.getEndY(), 0.0001);
  69         assertTrue(gradient.isProportional());
  70         assertEquals(CycleMethod.REPEAT, gradient.getCycleMethod());
  71         assertEquals(normalizedTwoStops, gradient.getStops());
  72     }
  73 
  74     @Test
  75     public void testGetStopsNullsRemoved() {
  76         LinearGradient gradient = new LinearGradient(0, 0, 1, 1, true,
  77                 CycleMethod.NO_CYCLE, twoStopsWithNulls);
  78 
  79         assertEquals(normalizedTwoStops, gradient.getStops());
  80     }
  81 
  82     @Test(expected=UnsupportedOperationException.class)
  83     public void testGetStopsCannotChangeGradient() {
  84         LinearGradient gradient = new LinearGradient(0, 0, 1, 1, true,
  85                 CycleMethod.NO_CYCLE, twoStopsWithNulls);
  86 
  87         List<Stop> returned = gradient.getStops();
  88         returned.set(0, stop2);
  89     }
  90 
  91     @Test
  92     public void testEquals() {
  93         LinearGradient basic = new LinearGradient(1, 2, 3, 4, true,
  94                 CycleMethod.REPEAT, twoStops);
  95         LinearGradient equal = new LinearGradient(1, 2, 3, 4, true,
  96                 CycleMethod.REPEAT, twoStopsWithNulls);
  97         LinearGradient beginX = new LinearGradient(2, 2, 3, 4, true,
  98                 CycleMethod.REPEAT, twoStops);
  99         LinearGradient beginY = new LinearGradient(1, 1, 3, 4, true,
 100                 CycleMethod.REPEAT, twoStops);
 101         LinearGradient endX = new LinearGradient(1, 2, 4, 4, true,
 102                 CycleMethod.REPEAT, twoStops);
 103         LinearGradient endY = new LinearGradient(1, 2, 3, 3, true,
 104                 CycleMethod.REPEAT, twoStops);
 105         LinearGradient proportional = new LinearGradient(1, 2, 3, 4, false,
 106                 CycleMethod.REPEAT, twoStops);
 107         LinearGradient cycleMethod = new LinearGradient(1, 2, 3, 4, true,
 108                 CycleMethod.REFLECT, twoStops);
 109         LinearGradient stops = new LinearGradient(1, 2, 3, 4, true,
 110                 CycleMethod.REPEAT, oneStop);
 111 
 112         assertFalse(basic.equals(null));
 113         assertFalse(basic.equals(color1));
 114         assertFalse(basic.equals(color2));
 115         assertTrue(basic.equals(basic));
 116         assertTrue(basic.equals(equal));
 117         assertFalse(basic.equals(beginX));
 118         assertFalse(basic.equals(beginY));
 119         assertFalse(basic.equals(endX));
 120         assertFalse(basic.equals(endY));
 121         assertFalse(basic.equals(proportional));
 122         assertFalse(basic.equals(cycleMethod));
 123         assertFalse(basic.equals(stops));
 124     }
 125 
 126     @Test
 127     public void testHashCode() {
 128         LinearGradient basic = new LinearGradient(1, 2, 3, 4, true,
 129                 CycleMethod.REPEAT, twoStops);
 130         LinearGradient equal = new LinearGradient(1, 2, 3, 4, true,
 131                 CycleMethod.REPEAT, twoStops);
 132         LinearGradient different = new LinearGradient(1, 2, 3, 4, false,
 133                 CycleMethod.REPEAT, twoStops);
 134         LinearGradient different2 = new LinearGradient(1, 2, 3, 4, true,
 135                 CycleMethod.REPEAT, oneStop);
 136 
 137         int code = basic.hashCode();
 138         int second = basic.hashCode();
 139         assertTrue(code == second);
 140         assertTrue(code == equal.hashCode());
 141         assertFalse(code == different.hashCode());
 142         assertFalse(code == different2.hashCode());
 143     }
 144 
 145     @Test
 146     public void testToString() {
 147         LinearGradient empty = new LinearGradient(1, 2, 3, 4, true,
 148                 CycleMethod.REPEAT, noStop);
 149         LinearGradient nonempty = new LinearGradient(1, 2, 3, 4, true,
 150                 CycleMethod.REPEAT, twoStops);
 151 
 152         String s = empty.toString();
 153         assertNotNull(s);
 154         assertFalse(s.isEmpty());
 155 
 156         s = nonempty.toString();
 157         assertNotNull(s);
 158         assertFalse(s.isEmpty());
 159     }
 160     
 161     @Test
 162     public void testToStringEquals() {
 163         LinearGradient lg =
 164                 LinearGradient.valueOf("linear-gradient(from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%)");
 165         assertEquals(lg, LinearGradient.valueOf(lg.toString()));
 166         
 167         lg = LinearGradient.valueOf("linear-gradient(from 100px 0 to 200px 0px, red 0px, blue 50px,  black 100px)");        
 168         assertEquals(lg, LinearGradient.valueOf(lg.toString()));
 169 
 170         lg = LinearGradient.valueOf("linear-gradient(to top, red  0%, blue  30%,black 100%)");
 171         assertEquals(lg, LinearGradient.valueOf(lg.toString()));
 172         
 173         lg = LinearGradient.valueOf("linear-gradient(from 10% 20% to 30% 40%, ff00ff 0%, 0xffffff 30%,black 100%)");
 174         assertEquals(lg, LinearGradient.valueOf(lg.toString()));
 175     }
 176 
 177     @Test
 178     public void testImpl_getPlatformPaint() {
 179         LinearGradient gradient = new LinearGradient(1, 2, 3, 4, true,
 180                 CycleMethod.REPEAT, noStop);
 181         
 182         Object paint = Toolkit.getPaintAccessor().getPlatformPaint(gradient);
 183         assertNotNull(paint);
 184         assertSame(paint, Toolkit.getPaintAccessor().getPlatformPaint(gradient));
 185     }
 186 
 187     @Test(expected=NullPointerException.class)
 188     public void testValueOfNullValue() {
 189         LinearGradient.valueOf(null);
 190     }
 191 
 192     @Test(expected=IllegalArgumentException.class)
 193     public void testValueOfEmpty() {
 194         LinearGradient.valueOf("");
 195     }
 196 
 197     @Test
 198     public void testValueOfIllegalNotations() {
 199         try {
 200             LinearGradient.valueOf("abcd");
 201             fail("IllegalArgument should have been thrown.");
 202         } catch (IllegalArgumentException iae) {
 203             // expected
 204         }
 205 
 206         try {
 207             LinearGradient.valueOf("linear-gradient()");
 208             fail("IllegalArgument should have been thrown.");
 209         } catch (IllegalArgumentException iae) {
 210             // expected
 211         }
 212 
 213         // rgb( must end with ')'
 214         try {
 215             LinearGradient.valueOf("linear-gradient(hsl(240,100%,100%), rgb(250, 0, 0)");
 216             fail("IllegalArgument should have been thrown.");
 217         } catch (IllegalArgumentException iae) {
 218             // expected
 219         }
 220         
 221         // stop can't be empty
 222         try {
 223             LinearGradient.valueOf("linear-gradient(from 0 100 to 100 100, red  0% ,,   black 100%)");
 224             fail("IllegalArgument should have been thrown.");
 225         } catch (IllegalArgumentException iae) {
 226             // expected
 227         }
 228 
 229         try {
 230             LinearGradient.valueOf("linear-gradient(from 0 100 to 100 100, red  0% ,black 100%,   )");
 231             fail("IllegalArgument should have been thrown.");
 232         } catch (IllegalArgumentException iae) {
 233             // expected
 234         }
 235         
 236        // token can't be empty
 237         try {
 238             LinearGradient.valueOf("linear-gradient(, from 0 100 to 100 100, red  0% ,   black 100%)");
 239             fail("IllegalArgument should have been thrown.");
 240         } catch (IllegalArgumentException iae) {
 241             // expected
 242         }
 243 
 244         try {
 245             LinearGradient.valueOf("linear-gradient(from 0 100 to 100 100, ,repeat, red  0% ,   black 100%)");
 246             fail("IllegalArgument should have been thrown.");
 247         } catch (IllegalArgumentException iae) {
 248             // expected
 249         }
 250 
 251     }
 252     
 253     @Test
 254     public void testValueOfRelativeAbsoluteMixed() {
 255         try {
 256             LinearGradient.valueOf("linear-gradient(from 0px 100% to 100% 100%, red  0% , blue 30%,  black 100%)");
 257             fail("IllegalArgument should have been thrown.");
 258         } catch (IllegalArgumentException iae) {
 259             // expected
 260         }
 261 
 262         try {
 263             LinearGradient.valueOf("linear-gradient(from 0% 100% to 100px 100%, red  0% , blue 30%,  black 100%)");
 264             fail("IllegalArgument should have been thrown.");
 265         } catch (IllegalArgumentException iae) {
 266             // expected
 267         }
 268 
 269         try {
 270             LinearGradient.valueOf("linear-gradient(from 0% 100% to 100% 100%, red  0% , blue 30px,  black 100%)");
 271             fail("IllegalArgument should have been thrown.");
 272         } catch (IllegalArgumentException iae) {
 273             // expected
 274         }
 275 
 276         try {
 277             LinearGradient.valueOf("linear-gradient(from 0 100 to 100 100%, red  0% , blue 30px,  black 100%)");
 278             fail("IllegalArgument should have been thrown.");
 279         } catch (IllegalArgumentException iae) {
 280             // expected
 281         }
 282 
 283     }
 284 
 285     @Test
 286     public void testValueOfCycleMethod() {
 287         LinearGradient actual = LinearGradient.valueOf("linear-gradient(repeat, red  0%, blue  30%,black 100%)");
 288         LinearGradient expected = new LinearGradient(0, 0, 0, 1,
 289                 true, CycleMethod.REPEAT,
 290                 new Stop(0, Color.RED),
 291                 new Stop(.3, Color.BLUE),
 292                 new Stop(1.0, Color.BLACK));
 293         assertEquals(expected, actual);
 294         
 295         actual = LinearGradient.valueOf("linear-gradient(reflect, red  0%, blue 30%, black 100%)");
 296         expected = new LinearGradient(0, 0, 0, 1,
 297                 true, CycleMethod.REFLECT,
 298                 new Stop(0, Color.RED),
 299                 new Stop(.3, Color.BLUE),
 300                 new Stop(1.0, Color.BLACK));
 301         assertEquals(expected, actual);
 302     }
 303 
 304     @Test
 305     public void testValueOfFromTo() {
 306         LinearGradient actual =
 307                 LinearGradient.valueOf("linear-gradient(from 10% 20% to 30% 40%, red  0%, blue  30%,black 100%)");
 308         LinearGradient expected = new LinearGradient(0.1, 0.2, 0.3, 0.4,
 309                 true, CycleMethod.NO_CYCLE,
 310                 new Stop(0, Color.RED),
 311                 new Stop(.3, Color.BLUE),
 312                 new Stop(1.0, Color.BLACK));
 313         assertEquals(expected, actual);
 314         
 315         actual =
 316                 LinearGradient.valueOf("linear-gradient(from 10px 20px to 30px 40px, red  0%, blue 30%, black 100%)");
 317         expected = new LinearGradient(10, 20, 30, 40,
 318                 false, CycleMethod.NO_CYCLE,
 319                 new Stop(0, Color.RED),
 320                 new Stop(.3, Color.BLUE),
 321                 new Stop(1.0, Color.BLACK));
 322         assertEquals(expected, actual);
 323     }
 324 
 325     @Test
 326     public void testValueOfTo() {
 327         LinearGradient actual =
 328                 LinearGradient.valueOf("linear-gradient(to top, red  0%, blue  30%,black 100%)");
 329         LinearGradient expected = new LinearGradient(0, 1, 0, 0,
 330                 true, CycleMethod.NO_CYCLE,
 331                 new Stop(0, Color.RED),
 332                 new Stop(.3, Color.BLUE),
 333                 new Stop(1.0, Color.BLACK));
 334         assertEquals(expected, actual);
 335         
 336         actual =
 337                 LinearGradient.valueOf("linear-gradient(to bottom, red  0%, blue  30%,black 100%)");
 338         expected = new LinearGradient(0, 0, 0, 1,
 339                 true, CycleMethod.NO_CYCLE,
 340                 new Stop(0, Color.RED),
 341                 new Stop(.3, Color.BLUE),
 342                 new Stop(1.0, Color.BLACK));
 343         assertEquals(expected, actual);
 344         
 345         actual =
 346                 LinearGradient.valueOf("linear-gradient(to left, red  0%, blue  30%,black 100%)");
 347         expected = new LinearGradient(1, 0, 0, 0,
 348                 true, CycleMethod.NO_CYCLE,
 349                 new Stop(0, Color.RED),
 350                 new Stop(.3, Color.BLUE),
 351                 new Stop(1.0, Color.BLACK));
 352         assertEquals(expected, actual);
 353         
 354         actual =
 355                 LinearGradient.valueOf("linear-gradient(to right, red  0%, blue  30%,black 100%)");
 356         expected = new LinearGradient(0, 0, 1, 0,
 357                 true, CycleMethod.NO_CYCLE,
 358                 new Stop(0, Color.RED),
 359                 new Stop(.3, Color.BLUE),
 360                 new Stop(1.0, Color.BLACK));
 361         assertEquals(expected, actual);
 362         
 363         actual =
 364                 LinearGradient.valueOf("linear-gradient(to bottom left, red  0%, blue  30%,black 100%)");
 365         expected = new LinearGradient(1, 0, 0, 1,
 366                 true, CycleMethod.NO_CYCLE,
 367                 new Stop(0, Color.RED),
 368                 new Stop(.3, Color.BLUE),
 369                 new Stop(1.0, Color.BLACK));
 370         assertEquals(expected, actual);
 371         
 372         actual =
 373                 LinearGradient.valueOf("linear-gradient(to right top, red  0%, blue  30%,black 100%)");
 374         expected = new LinearGradient(0, 1, 1, 0,
 375                 true, CycleMethod.NO_CYCLE,
 376                 new Stop(0, Color.RED),
 377                 new Stop(.3, Color.BLUE),
 378                 new Stop(1.0, Color.BLACK));
 379         assertEquals(expected, actual);
 380     }
 381     
 382     @Test
 383     public void testValueOfRelativeValues() {
 384         LinearGradient actual =
 385                 LinearGradient.valueOf("linear-gradient(from 0% 0% to 100% 100%, red  0% , blue 30%,  black 100%)");
 386         LinearGradient expected = new LinearGradient(0, 0, 1, 1,
 387                 true, CycleMethod.NO_CYCLE,
 388                 new Stop(0, Color.RED),
 389                 new Stop(.3, Color.BLUE),
 390                 new Stop(1.0, Color.BLACK));
 391         assertEquals(expected, actual);
 392     }
 393     
 394     @Test
 395     public void testValueOfAbsoluteValues() {
 396         LinearGradient actual =
 397                 LinearGradient.valueOf("linear-gradient(from 100px 0 to 200px 0px, red 0px, blue 50px,  black 100px)");
 398         LinearGradient expected = new LinearGradient(100, 0, 200, 0,
 399                 false, CycleMethod.NO_CYCLE,
 400                 new Stop(0, Color.RED),
 401                 new Stop(.5, Color.BLUE),
 402                 new Stop(1.0, Color.BLACK));
 403         assertEquals(expected, actual);
 404     }
 405     
 406     @Test
 407     public void testValueOfColor() {
 408         LinearGradient actual =
 409                 LinearGradient.valueOf("linear-gradient(rgb(0,0,255), rgb(255, 0, 0))");
 410         LinearGradient expected = new LinearGradient(0, 0, 0, 1,
 411                 true, CycleMethod.NO_CYCLE,
 412                 new Stop(0, Color.BLUE),
 413                 new Stop(1.0, Color.RED));
 414         assertEquals(expected, actual);
 415     }
 416 
 417     @Test
 418     public void testValueOfDefaultsToBottom() {
 419         LinearGradient actual =
 420                 LinearGradient.valueOf("linear-gradient(red  0%, blue 30%, black 100%)");
 421         LinearGradient expected = new LinearGradient(0, 0, 0, 1,
 422                 true, CycleMethod.NO_CYCLE,
 423                 new Stop(0, Color.RED),
 424                 new Stop(.3, Color.BLUE),
 425                 new Stop(1.0, Color.BLACK));
 426         assertEquals(expected, actual);
 427     }
 428 
 429     @Test
 430     public void testValueOfStopsNormalizeLargerValues() {
 431         LinearGradient actual =
 432                 LinearGradient.valueOf("linear-gradient(red  10%, blue 9%, red 8%, black 100%)");
 433         LinearGradient expected = new LinearGradient(0, 0, 0, 1,
 434                 true, CycleMethod.NO_CYCLE,
 435                 new Stop(0.1, Color.RED),
 436                 new Stop(0.1, Color.BLUE),
 437                 new Stop(0.1, Color.RED),
 438                 new Stop(1.0, Color.BLACK));
 439         assertEquals(expected, actual);
 440     }
 441 }