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