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