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 com.sun.javafx.css; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.fail; 30 31 import java.io.IOException; 32 import javafx.css.ParsedValue; 33 import javafx.geometry.Insets; 34 import javafx.scene.text.Font; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 39 import com.sun.javafx.css.parser.CSSParser; 40 41 42 public class InsetsTypeTest { 43 44 String[] css; 45 46 Insets[][] expResults; 47 48 // values for top, right, bottom, left 49 final float[][][] fvals = new float[][][] { 50 { {1.0f} }, 51 { {-1.0f, 0.0f, 1.0f, 2.0f} }, 52 { {1.0f}, {2.0f}, {3.0f}, {4.0f} }, 53 { {1.0f, 0.0f, 1.0f, 2.0f}, 54 {2.0f, 0.0f, 1.0f, 2.0f}, 55 {3.0f, 0.0f, 1.0f, 2.0f}, 56 {4.0f, 0.0f, 1.0f, 2.0f} }, 57 { {1.0f}, 58 {2.0f, -1.0f}, 59 {3.0f, 0.0f, 1.0f, 2.0f}, 60 {4.0f, 0.0f, 1.0f} } 61 }; 62 63 @Before 64 public void setup() { 65 css = new String[fvals.length]; 66 expResults = new Insets[fvals.length][]; 67 for (int i=0; i<fvals.length; i++) { 68 StringBuilder sbuf = new StringBuilder(); 69 expResults[i] = new Insets[fvals[i].length]; 70 for (int j=0; j<fvals[i].length; j++) { 71 expResults[i][j] = makeInsets(fvals[i][j]); 72 73 for (int k=0; k<fvals[i][j].length; k++) { 74 sbuf.append(Float.toString(fvals[i][j][k])); 75 sbuf.append("px"); 76 if (k+1 < fvals[i][j].length) sbuf.append(' '); 77 } 78 79 if (j+1 < fvals[i].length) sbuf.append(", "); 80 } 81 css[i] = sbuf.toString(); 82 } 83 } 84 85 public InsetsTypeTest() { 86 } 87 88 final Insets makeInsets(float[] vals) { 89 90 float top = (vals.length > 0) ? vals[0] : 0.0f; 91 float right = (vals.length > 1) ? vals[1] : top; 92 float bottom = (vals.length > 2) ? vals[2] : top; 93 float left = (vals.length > 3) ? vals[3] : right; 94 return new Insets(top, right, bottom, left); 95 } 96 97 void checkInsets(String msg, Insets expResult, Insets result) { 98 assertEquals(msg + "top", expResult.getTop(), result.getTop(), 0.01); 99 assertEquals(msg + "right", expResult.getRight(), result.getRight(), 0.01); 100 assertEquals(msg + "bottom", expResult.getBottom(), result.getBottom(), 0.01); 101 assertEquals(msg + "left", expResult.getLeft(), result.getLeft(), 0.01); 102 } 103 104 @Test 105 public void testConvert() { 106 107 for (int i=0; i<css.length; i++) { 108 109 Stylesheet stylesheet = 110 CSSParser.getInstance().parse("* { -fx-border-insets: " + css[i] + "; }"); 111 112 ParsedValue value = 113 TypeTest.getValueFor(stylesheet, "-fx-border-insets"); 114 115 Insets[] insets = (Insets[]) value.convert(Font.getDefault()); 116 117 //assertEquals(expResults[i].length,insets.length); 118 119 for(int j=0; j<insets.length; j++) { 120 String msg = Integer.toString(i) + "." + Integer.toString(j); 121 checkInsets(msg, expResults[i][j], insets[j]); 122 } 123 } 124 } 125 }