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 test.com.sun.javafx.test; 27 28 import java.util.Iterator; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import static org.junit.Assert.fail; 32 import javafx.scene.Node; 33 34 import org.junit.Test; 35 36 import javafx.css.StyleConverter; 37 import javafx.css.CssMetaData; 38 import java.util.List; 39 import javafx.css.Styleable; 40 import javafx.css.StyleableProperty; 41 42 public abstract class CssMethodsTestBase { 43 private final Configuration configuration; 44 45 public CssMethodsTestBase(final Configuration configuration) { 46 this.configuration = configuration; 47 } 48 49 50 @Test // This _must_ be the first test! 51 public void testCssDefaultSameAsPropertyDefault() { 52 configuration.cssDefaultsTest(); 53 } 54 55 @Test 56 public void testCSSPropertyAndCSSPropertyReferenceEachOther() { 57 configuration.cssPropertyReferenceIntegrityTest(); 58 } 59 60 @Test 61 public void testCssSettable() throws Exception { 62 configuration.cssSettableTest(); 63 } 64 65 @Test 66 public void testCssSet() { 67 configuration.cssSetTest(); 68 } 69 70 public static Object[] config( 71 final Node node, 72 final String propertyName, 73 final Object initialValue, 74 final String cssPropertyKey, 75 final Object cssPropertyValue) { 76 return config(new Configuration(node, 77 propertyName, 78 initialValue, 79 cssPropertyKey, 80 cssPropertyValue)); 81 } 82 83 public static Object[] config( 84 final Node node, 85 final String propertyName, 86 final Object initialValue, 87 final String cssPropertyKey, 88 final Object cssPropertyValue, 89 final Object expectedFinalValue) { 90 return config(new Configuration(node, 91 propertyName, 92 initialValue, 93 cssPropertyKey, 94 cssPropertyValue, 95 expectedFinalValue)); 96 } 97 98 public static Object[] config( 99 final Node node, 100 final String propertyName, 101 final Object initialValue, 102 final String cssPropertyKey, 103 final Object cssPropertyValue, 104 final ValueComparator comparator) { 105 return config(new Configuration(node, 106 propertyName, 107 initialValue, 108 cssPropertyKey, 109 cssPropertyValue, 110 comparator)); 111 } 112 113 public static Object[] config(final Configuration configuration) { 114 return new Object[] { configuration }; 115 } 116 117 private static CssMetaData<? extends Styleable, ?> getCssMetaData(Node node, String cssProperty) { 118 119 List<CssMetaData<? extends Styleable, ?>> styleables = node.getCssMetaData(); 120 for(CssMetaData<? extends Styleable, ?> styleable : styleables) { 121 if (styleable.getProperty().equals(cssProperty)) { 122 return styleable; 123 } 124 } 125 fail(node.toString() + ": CSSProperty" + cssProperty + " not found"); 126 return null; 127 } 128 129 public static class Configuration { 130 private static final StyleConverter<String, Object> TEST_TYPE = 131 new StyleConverter<String, Object>(); 132 133 private static final CssMetaData<Node, Object> UNDEFINED_KEY = 134 new CssMetaData<Node,Object>("U-N-D-E-F-I-N-E-D", TEST_TYPE, "") { 135 136 @Override 137 public boolean isSettable(Node n) { 138 return false; 139 } 140 141 @Override 142 public StyleableProperty<Object> getStyleableProperty(Node n) { 143 return null; 144 } 145 }; 146 147 private final Node node; 148 149 private final PropertyReference nodePropertyReference; 150 151 private final Object initialValue; 152 153 private final Object defaultValue; 154 155 private final CssMetaData cssPropertyKey; 156 157 private final Object cssPropertyValue; 158 159 private final Object expectedFinalValue; 160 161 private final ValueComparator comparator; 162 163 public Configuration(final Node node, 164 final String propertyName, 165 final Object initialValue, 166 final String cssPropertyKey, 167 final Object cssPropertyValue) { 168 this(node, 169 propertyName, 170 initialValue, 171 cssPropertyKey, 172 cssPropertyValue, 173 ValueComparator.DEFAULT); 174 } 175 176 public Configuration(final Node node, 177 final String propertyName, 178 final Object initialValue, 179 final String cssPropertyKey, 180 final Object cssPropertyValue, 181 final Object finalExpectedValue) { 182 this(node, 183 propertyName, 184 initialValue, 185 getCssMetaData(node, cssPropertyKey), 186 cssPropertyValue, 187 finalExpectedValue, 188 ValueComparator.DEFAULT); 189 } 190 191 public Configuration(final Node node, 192 final String propertyName, 193 final Object initialValue, 194 final String cssPropertyKey, 195 final Object cssPropertyValue, 196 final ValueComparator comparator) { 197 this(node, 198 propertyName, 199 initialValue, 200 getCssMetaData(node, cssPropertyKey), 201 cssPropertyValue, 202 cssPropertyValue, 203 comparator); 204 } 205 206 public Configuration(final Node node, 207 final String propertyName, 208 final Object initialValue, 209 final CssMetaData cssPropertyKey, 210 final Object cssPropertyValue, 211 final Object expectedFinalValue, 212 final ValueComparator comparator) { 213 this.node = node; 214 this.nodePropertyReference = 215 PropertyReference.createForBean(node.getClass(), 216 propertyName); 217 this.initialValue = initialValue; 218 this.defaultValue = this.nodePropertyReference.getValue(this.node); 219 this.cssPropertyKey = cssPropertyKey; 220 this.cssPropertyValue = cssPropertyValue; 221 this.expectedFinalValue = expectedFinalValue; 222 this.comparator = comparator; 223 } 224 225 public void cssSettableTest() throws Exception { 226 assertFalse(UNDEFINED_KEY.isSettable(node)); 227 assertTrue(cssPropertyKey.isSettable(node)); 228 229 final Object propertyModel = BindingHelper.getPropertyModel( 230 node, nodePropertyReference); 231 assertTrue(cssPropertyKey.isSettable(node)); 232 233 final Class<?> typeClass = nodePropertyReference.getValueType(); 234 235 final Object variable = BindingHelper.createVariable(typeClass); 236 BindingHelper.setWritableValue(typeClass, variable, initialValue); 237 238 BindingHelper.bind(typeClass, propertyModel, variable); 239 assertFalse(cssPropertyKey.isSettable(node)); 240 241 BindingHelper.unbind(typeClass, propertyModel); 242 assertTrue(cssPropertyKey.isSettable(node)); 243 } 244 245 public void cssSetTest() { 246 nodePropertyReference.setValue(node, initialValue); 247 StyleableProperty styleableProperty = cssPropertyKey.getStyleableProperty(node); 248 styleableProperty.applyStyle(null, cssPropertyValue); 249 250 final Object nodePropertyValue = 251 nodePropertyReference.getValue(node); 252 comparator.assertEquals(expectedFinalValue, 253 nodePropertyValue); 254 } 255 256 public void cssDefaultsTest() { 257 258 // is the cssInitialValue the same as the node property's default? 259 final Object cssInitialValue = 260 cssPropertyKey.getInitialValue(node); 261 262 ValueComparator.DEFAULT.assertEquals(defaultValue, cssInitialValue); 263 } 264 265 public void cssPropertyReferenceIntegrityTest() { 266 267 StyleableProperty prop = cssPropertyKey.getStyleableProperty(node); 268 269 CssMetaData styleable = prop.getCssMetaData(); 270 271 ValueComparator.DEFAULT.assertEquals(cssPropertyKey, styleable); 272 } 273 } 274 }