1 /*
   2  * Copyright (c) 2011, 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.scene.control.skin;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertSame;
  30 
  31 import java.util.Arrays;
  32 import java.util.Collection;
  33 
  34 import javafx.scene.Node;
  35 import javafx.scene.control.ContentDisplay;
  36 import javafx.scene.control.Label;
  37 import javafx.scene.control.skin.LabelSkin;
  38 import javafx.scene.paint.Color;
  39 import javafx.scene.paint.Paint;
  40 import javafx.scene.shape.Rectangle;
  41 import javafx.scene.text.Font;
  42 import javafx.scene.text.Text;
  43 import javafx.scene.text.TextAlignment;
  44 
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 import org.junit.runner.RunWith;
  48 import org.junit.runners.Parameterized;
  49 import org.junit.runners.Parameterized.Parameters;
  50 
  51 import javafx.scene.control.SkinBaseAccessor;
  52 
  53 
  54 /**
  55  * A parameterized test suite for testing that a LabelSkin,
  56  * created with various different configurations, is setup
  57  * correctly.
  58  */
  59 @RunWith(Parameterized.class)
  60 public class LabelSkinCreationTest {
  61     private Paint fill;
  62     private Font font;
  63     private TextAlignment align;
  64     private boolean underline;
  65     private boolean wrapText;
  66     private ContentDisplay contentDisplay;
  67     private Node graphic;
  68     
  69     private Label label;
  70     private LabelSkin skin;
  71     private Text text;
  72 
  73     @SuppressWarnings("rawtypes")
  74     @Parameters public static Collection implementations() {
  75         Rectangle rect = new Rectangle();
  76         rect.setWidth(25);
  77         rect.setHeight(25);
  78         return Arrays.asList(new Object[][] {
  79             // standard configuration
  80             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,   null },
  81             // specify only the fill
  82             { Color.RED,   Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,   null },
  83             // specify only the font
  84             { Color.BLACK, Font.font("Arial", 64), TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,   null },
  85             // specify only the align
  86             { Color.BLACK, Font.getDefault(),      TextAlignment.JUSTIFY, false, false, ContentDisplay.LEFT,   null },
  87             // specify only the underline
  88             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    true,  false, ContentDisplay.LEFT,   null },
  89             // specify only the wrapText
  90             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, true,  ContentDisplay.LEFT,   null },
  91             // specify only the contentDisplay
  92             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.BOTTOM, null },
  93             // specify only the graphic
  94             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,   rect },
  95             // specify every type of content display with null graphic
  96             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.BOTTOM,       null },
  97             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.CENTER,       null },
  98             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.GRAPHIC_ONLY, null },
  99             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,         null },
 100             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.RIGHT,        null },
 101             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.TEXT_ONLY,    null },
 102             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.TOP,          null },
 103             // specify every type of content display with non-null graphic
 104             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.BOTTOM,       rect },
 105             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.CENTER,       rect },
 106             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.GRAPHIC_ONLY, rect },
 107             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.LEFT,         rect },
 108             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.RIGHT,        rect },
 109             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.TEXT_ONLY,    rect },
 110             { Color.BLACK, Font.getDefault(),      TextAlignment.LEFT,    false, false, ContentDisplay.TOP,          rect },
 111         });
 112     }
 113 
 114     public LabelSkinCreationTest(
 115             Paint fill,
 116             Font font,
 117             TextAlignment align,
 118             boolean underline,
 119             boolean wrapText,
 120             ContentDisplay contentDisplay,
 121             Node graphic)
 122     {
 123         this.fill = fill;
 124         this.font = font;
 125         this.align = align;
 126         this.underline = underline;
 127         this.wrapText = wrapText;
 128         this.contentDisplay = contentDisplay;
 129         this.graphic = graphic;
 130     }
 131     
 132     @Before public void setup() {
 133         label = new Label();
 134         label.setTextFill(fill);
 135         label.setFont(font);
 136         label.setTextAlignment(align);
 137         label.setUnderline(underline);
 138         label.setWrapText(wrapText);
 139         label.setContentDisplay(contentDisplay);
 140         label.setGraphic(graphic);
 141         label.setText("*");
 142         label.resize(30, 30);
 143         skin = new LabelSkin(label);
 144         label.setSkin(skin);
 145         text = skin.text;
 146     }
 147     
 148     @Test public void labelWasInitializedCorrectly() {
 149         assertSame(label.getTextFill(), text.getFill());
 150         assertSame(label.getFont(), text.getFont());
 151         assertSame(label.getTextAlignment(), text.getTextAlignment());
 152         assertEquals(label.isUnderline(), text.isUnderline());
 153 
 154         // The wrapping width is based on layout after updateDisplayedText() is called from layoutChildren().
 155         //assertTrue(label.isWrapText() ? text.getWrappingWidth() > 0 : text.getWrappingWidth() == 0);
 156         
 157         // Now test the children
 158         if (label.getContentDisplay() == ContentDisplay.GRAPHIC_ONLY) {
 159             // 1 child, graphic, if it is not null, otherwise 0
 160             if (label.getGraphic() == null) {
 161                 assertEquals(0, SkinBaseAccessor.getChildren(skin).size());
 162             } else {
 163                 assertEquals(1, SkinBaseAccessor.getChildren(skin).size());
 164                 assertEquals(label.getGraphic(), SkinBaseAccessor.getChildren(skin).get(0));
 165             }
 166         } else if (label.getContentDisplay() == ContentDisplay.TEXT_ONLY) {
 167             // 1 child, text
 168             assertEquals(1, SkinBaseAccessor.getChildren(skin).size());
 169             assertEquals(text, SkinBaseAccessor.getChildren(skin).get(0));
 170         } else {
 171             if (label.getGraphic() == null) {
 172                 // 1 child, text
 173                 assertEquals(1, SkinBaseAccessor.getChildren(skin).size());
 174                 assertEquals(text, SkinBaseAccessor.getChildren(skin).get(0));
 175             } else {
 176                 // 2 children, graphic + text
 177                 assertEquals(2, SkinBaseAccessor.getChildren(skin).size());
 178                 assertEquals(label.getGraphic(), SkinBaseAccessor.getChildren(skin).get(0));
 179                 assertEquals(text, SkinBaseAccessor.getChildren(skin).get(1));
 180             }
 181         }
 182         
 183         // TODO test that if there is a graphic, that the appropriate listeners are added
 184     }
 185 }