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