1 /*
   2  * Copyright (c) 2012, 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.scene.control.skin;
  27 
  28 import com.sun.javafx.pgstub.StubImageLoaderFactory;
  29 import com.sun.javafx.pgstub.StubPlatformImageInfo;
  30 import com.sun.javafx.pgstub.StubToolkit;
  31 import com.sun.javafx.tk.Toolkit;
  32 import javafx.css.StyleConverter;
  33 import javafx.css.CssMetaData;
  34 import java.util.ArrayList;
  35 
  36 import java.util.Arrays;
  37 import java.util.Collection;
  38 import java.util.List;
  39 import javafx.beans.value.WritableValue;
  40 import javafx.css.Styleable;
  41 import javafx.geometry.Insets;
  42 import javafx.geometry.Pos;
  43 import javafx.scene.Cursor;
  44 import javafx.scene.Node;
  45 import javafx.scene.control.ContentDisplay;
  46 import javafx.scene.control.Label;
  47 import javafx.scene.control.Labeled;
  48 import javafx.scene.control.OverrunStyle;
  49 import javafx.scene.effect.BlendMode;
  50 import javafx.scene.effect.ColorAdjust;
  51 import javafx.scene.paint.Color;
  52 import javafx.scene.text.Font;
  53 import javafx.scene.text.TextAlignment;
  54 import javax.swing.GroupLayout;
  55 
  56 import org.junit.BeforeClass;
  57 import org.junit.runner.RunWith;
  58 import org.junit.runners.Parameterized;
  59 import org.junit.runners.Parameterized.Parameters;
  60 
  61 import org.junit.Before;
  62 import org.junit.Ignore;
  63 import org.junit.Test;
  64 import static org.junit.Assert.*;
  65 
  66 @RunWith(Parameterized.class)
  67 public class LabeledImplTest {
  68 
  69     @BeforeClass
  70     public static void configureImageLoaderFactory() {
  71         final StubImageLoaderFactory imageLoaderFactory =
  72                 ((StubToolkit) Toolkit.getToolkit()).getImageLoaderFactory();
  73         imageLoaderFactory.reset();
  74         imageLoaderFactory.registerImage(LabeledImpl.class.getResource("caspian/center-btn.png").toExternalForm(),
  75                 new StubPlatformImageInfo(32, 32));
  76     }
  77 
  78     private static final Labeled LABELED = new Label("label");
  79     private static final LabeledImpl LABELED_IMPL = new LabeledImpl(LABELED);
  80     
  81     private static class Configuration {
  82         final WritableValue source;
  83         final WritableValue mirror;
  84         final Object value;
  85         Configuration(WritableValue source, WritableValue mirror, Object value) {
  86             this.source = source;
  87             this.mirror = mirror;
  88             this.value = value;
  89         }        
  90     }
  91     
  92     private static Configuration config(CssMetaData styleable) {
  93         WritableValue source = styleable.getStyleableProperty(LABELED);
  94         WritableValue mirror   = styleable.getStyleableProperty(LABELED_IMPL);
  95         Object value = null;
  96         if (source != null && mirror != null) {
  97             final String prop = styleable.getProperty();
  98             if ("-fx-cursor".equals(prop)) {
  99                 value = Cursor.HAND;                
 100             } else if ("-fx-effect".equals(prop)) {
 101                 value = new ColorAdjust(.5, .5, .5, .5);
 102             } else if ("-fx-focus-traversable".equals(prop)) {
 103                 value = Boolean.FALSE;
 104             } else if ("-fx-opacity".equals(prop)) {
 105                 value = .5;
 106             } else if ("-fx-blend-mode".equals(prop)) {
 107                 value = BlendMode.RED;
 108             } else if ("-fx-rotate".equals(prop)) {
 109                 value = .5;
 110             } else if ("-fx-scale-x".equals(prop)) {
 111                 value = .5;
 112             } else if ("-fx-scale-y".equals(prop)) {
 113                 value = .5;
 114             } else if ("-fx-scale-z".equals(prop)) {
 115                 value = .5;
 116             } else if ("-fx-translate-x".equals(prop)) {
 117                 value = .5;
 118             } else if ("-fx-translate-y".equals(prop)) {
 119                 value = .5;
 120             } else if ("-fx-translate-z".equals(prop)) {
 121                 value = .5;
 122             } else if ("visibility".equals(prop)) {
 123                 value = Boolean.FALSE;
 124             } else if ("-fx-font".equals(prop)) {
 125                 value = Font.font("Amble", 15);
 126             } else if ("-fx-alignment".equals(prop)) {
 127                 value = Pos.TOP_CENTER;
 128             } else if ("-fx-text-alignment".equals(prop)) {
 129                 value = TextAlignment.RIGHT;
 130             } else if ("-fx-text-fill".equals(prop)) {
 131                 value = Color.RED;
 132             } else if ("-fx-text-overrun".equals(prop)) {
 133                 value = OverrunStyle.LEADING_WORD_ELLIPSIS;
 134             } else if ("-fx-wrap-text".equals(prop)) {
 135                 value = Boolean.TRUE;
 136             } else if ("-fx-graphic".equals(prop)) {
 137                 value = LabeledImpl.class.getResource("caspian/center-btn.png").toExternalForm();
 138             } else if ("-fx-underline".equals(prop)) {
 139                 value = Boolean.TRUE;
 140             } else if ("-fx-content-display".equals(prop)) {
 141                 value = ContentDisplay.GRAPHIC_ONLY;
 142             } else if ("-fx-label-padding".equals(prop)) {
 143                 value = new Insets(1,2,3,4);
 144             } else if ("-fx-graphic-text-gap".equals(prop)) {
 145                 value = .5;
 146             } else if ("-fx-ellipsis-string".equals(prop)) {
 147                 value = "...";
 148             } else if ("-fx-line-spacing".equals(prop)) {
 149                 value = 0.0;
 150             } else {
 151                 fail(prop + " not accounted for");
 152                 return null;
 153             }          
 154             
 155             return new Configuration(source, mirror, value);
 156         }
 157 
 158         fail();
 159         return null;
 160     }
 161     
 162     private final Configuration configuration;
 163 
 164     @Parameters
 165     public static Collection<Configuration[]> data() {
 166 
 167         Collection<Configuration[]> data = new ArrayList<Configuration[]>();
 168         
 169         List<CssMetaData<? extends Styleable, ?>> styleables = LabeledImpl.StyleableProperties.STYLEABLES_TO_MIRROR;
 170         for(CssMetaData<? extends Styleable, ?> styleable : styleables) {
 171             
 172             // LabeledImpl doesn't track -fx-skin since the Labeled
 173             // isn't necessarily a Label
 174             if ("-fx-skin".equals(styleable.getProperty())) continue;
 175             
 176             Configuration[] config = new Configuration[] { config(styleable) };
 177             if (config != null) data.add(config);
 178         }
 179         
 180         data.add( new Configuration[] { 
 181             new Configuration(LABELED.textProperty(), LABELED_IMPL.textProperty(), "TEST 1 2 3")
 182         });
 183         
 184         return data;
 185     }
 186     
 187     @Test 
 188     public void testMirrorReflectsSource() {
 189         final WritableValue source = configuration.source;
 190         final WritableValue mirror = configuration.mirror;
 191         final Object expected = configuration.value;
 192         
 193         source.setValue(expected);
 194         assertEquals(mirror.toString(), expected, mirror.getValue());
 195     }
 196 
 197     public LabeledImplTest(Configuration configuration) {
 198         this.configuration = configuration;
 199     }
 200 
 201     static {
 202     }
 203 }