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 com.sun.javafx.scene.control.infrastructure; 27 28 import javafx.beans.InvalidationListener; 29 import javafx.beans.value.ObservableValue; 30 import javafx.collections.ListChangeListener; 31 import javafx.collections.ObservableList; 32 import javafx.css.PseudoClass; 33 import javafx.scene.chart.Axis; 34 import javafx.scene.control.Control; 35 import javafx.scene.control.MenuItem; 36 import javafx.scene.control.Pagination; 37 import javafx.scene.control.PopupControl; 38 import javafx.scene.control.Tab; 39 import java.lang.reflect.Field; 40 import java.util.ArrayList; 41 import java.util.List; 42 import java.util.Set; 43 import com.sun.javafx.binding.ExpressionHelperUtility; 44 import static org.junit.Assert.assertFalse; 45 import static org.junit.Assert.assertTrue; 46 47 public final class ControlTestUtils { 48 private ControlTestUtils() { } 49 /********************************************************************* 50 * Following 2 methods are for Tab * 51 ********************************************************************/ 52 public static void assertStyleClassContains(Tab tab, String styleClass) { 53 assertStyleClassContains( 54 "The style class " + styleClass + " was not set on the Tab " + tab, 55 tab, styleClass); 56 } 57 58 public static void assertStyleClassContains(String message, Tab tab, String styleClass) { 59 assertTrue(message, tab.getStyleClass().contains(styleClass)); 60 } 61 62 /********************************************************************* 63 * Following 2 methods are for MenuItem * 64 ********************************************************************/ 65 public static void assertStyleClassContains(MenuItem mi, String styleClass) { 66 assertStyleClassContains( 67 "The style class " + styleClass + " was not set on the MenuItem " + mi, 68 mi, styleClass); 69 } 70 71 public static void assertStyleClassContains(String message, MenuItem mi, String styleClass) { 72 assertTrue(message, mi.getStyleClass().contains(styleClass)); 73 } 74 75 /********************************************************************* 76 * Following 2 methods are for Popup controls like Tooltip etc * 77 ********************************************************************/ 78 public static void assertStyleClassContains(PopupControl control, String styleClass) { 79 assertStyleClassContains( 80 "The style class " + styleClass + " was not set on the Popupcontrol " + control, 81 control, styleClass); 82 } 83 84 public static void assertStyleClassContains(String message, PopupControl control, String styleClass) { 85 assertTrue(message, control.getStyleClass().contains(styleClass)); 86 } 87 88 /**************************************************************************** 89 * Following 2 methods are for normal controls like Button, TextField etc * 90 ****************************************************************************/ 91 public static void assertStyleClassContains(Control control, String styleClass) { 92 assertStyleClassContains( 93 "The style class " + styleClass + " was not set on the control " + control, 94 control, styleClass); 95 } 96 97 public static void assertStyleClassContains(String message, Control control, String styleClass) { 98 assertTrue(message, control.getStyleClass().contains(styleClass)); 99 } 100 101 /**************************************************************************** 102 * Following 4 methods are for normal controls like Button, TextField etc * 103 ****************************************************************************/ 104 public static void assertPseudoClassExists(Control control, String pseudoClass) { 105 assertPseudoClassExists( 106 "The pseudo class " + pseudoClass + " was not set on control " + control, 107 control, pseudoClass); 108 } 109 110 public static void assertPseudoClassExists(String message, Control control, String pseudoClass) { 111 Set<PseudoClass> allStates = control.getPseudoClassStates(); 112 PseudoClass state = PseudoClass.getPseudoClass(pseudoClass); 113 assertTrue(message, allStates.contains(state)); 114 } 115 116 public static void assertPseudoClassDoesNotExist(Control control, String pseudoClass) { 117 assertPseudoClassDoesNotExist( 118 "The pseudo class " + pseudoClass + " was unexpectedly set on control " + control, 119 control, pseudoClass); 120 } 121 122 public static void assertPseudoClassDoesNotExist(String message, Control control, String pseudoClass) { 123 Set<PseudoClass> allStates = control.getPseudoClassStates(); 124 PseudoClass state = PseudoClass.getPseudoClass(pseudoClass); 125 assertFalse(message, allStates.contains(state)); 126 } 127 128 /**************************************************************************** 129 * Following 4 methods are for axis type like ValueAxis, NumberAxis, CategoryAxis etc* 130 ****************************************************************************/ 131 public static void assertPseudoClassExists(Axis axis, String pseudoClass) { 132 assertPseudoClassExists( 133 "The pseudo class " + pseudoClass + " was not set on axis " + axis, 134 axis, pseudoClass); 135 } 136 137 public static void assertPseudoClassExists(String message, Axis axis, String pseudoClass) { 138 Set<PseudoClass> allStates = axis.getPseudoClassStates(); 139 PseudoClass state = PseudoClass.getPseudoClass(pseudoClass); 140 assertTrue(message, allStates.contains(state)); 141 } 142 143 public static void assertPseudoClassDoesNotExist(Axis axis, String pseudoClass) { 144 assertPseudoClassDoesNotExist( 145 "The pseudo class " + pseudoClass + " was unexpectedly set on axis " + axis, 146 axis, pseudoClass); 147 } 148 149 public static void assertPseudoClassDoesNotExist(String message, Axis axis, String pseudoClass) { 150 Set<PseudoClass> allStates = axis.getPseudoClassStates(); 151 PseudoClass state = PseudoClass.getPseudoClass(pseudoClass); 152 assertFalse(message, allStates.contains(state)); 153 } 154 155 /********************************************************************* 156 * Following 2 methods are for the Pagination controls * 157 ********************************************************************/ 158 public static void assertStyleClassContains(Pagination control, String styleClass) { 159 assertStyleClassContains( 160 "The style class " + styleClass + " was not set on the Pagination " + control, 161 control, styleClass); 162 } 163 164 public static void assertStyleClassContains(String message, Pagination control, String styleClass) { 165 assertTrue(message, control.getStyleClass().contains(styleClass)); 166 } 167 168 public static void assertListenerListContains(ObservableList list, ListChangeListener listener) { 169 assertListenerListContains("The listener " + listener + " was not contained in " + list, list, listener); 170 } 171 172 public static void assertListenerListContains(String message, ObservableList list, ListChangeListener listener) { 173 // ListenerList listeners = getListenerList(list); 174 // assertTrue(message, listeners != null && listeners.contains(listener)); 175 } 176 177 public static void assertListenerListDoesNotContain(ObservableList list, ListChangeListener listener) { 178 assertListenerListDoesNotContain("The listener " + listener + " was contained in " + list, list, listener); 179 } 180 181 public static void assertListenerListDoesNotContain(String message, ObservableList list, ListChangeListener listener) { 182 // ListenerList listeners = getListenerList(list); 183 // assertTrue(message, listeners == null || !listeners.contains(listener)); 184 } 185 186 public static ListChangeListener getListChangeListener(Object bean, String fieldName) { 187 return (ListChangeListener) getListener(bean, fieldName); 188 } 189 190 public static void assertValueListenersContains(ObservableValue value, InvalidationListener listener) { 191 assertValueListenersContains("The listener " + listener + " was not contained in " + value, value, listener); 192 } 193 194 public static void assertValueListenersContains(String message, ObservableValue value, InvalidationListener listener) { 195 List listeners = getObservableValueListeners(value); 196 assertTrue(message, listeners != null && listeners.contains(listener)); 197 } 198 199 public static void assertValueListenersDoesNotContain(ObservableValue value, InvalidationListener listener) { 200 assertValueListenersDoesNotContain("The listener " + listener + " was contained in " + value, value, listener); 201 } 202 203 public static void assertValueListenersDoesNotContain(String message, ObservableValue value, InvalidationListener listener) { 204 List listeners = getObservableValueListeners(value); 205 assertTrue(message, listeners == null || !listeners.contains(listener)); 206 } 207 208 public static int getListenerCount(ObservableValue value) { 209 return getObservableValueListeners(value).size(); 210 } 211 212 public static InvalidationListener getInvalidationListener(Object bean, String fieldName) { 213 return (InvalidationListener) getListener(bean, fieldName); 214 } 215 216 private static Object getListener(Object bean, String fieldName) { 217 try { 218 Class clazz = bean.getClass(); 219 Field field = clazz.getDeclaredField(fieldName); 220 field.setAccessible(true); 221 return field.get(bean); 222 } catch (Exception e) { 223 e.printStackTrace(); 224 } 225 return null; 226 } 227 228 private static List getObservableValueListeners(ObservableValue value) { 229 ArrayList results = new ArrayList(); 230 results.addAll(ExpressionHelperUtility.getChangeListeners(value)); 231 results.addAll(ExpressionHelperUtility.getInvalidationListeners(value)); 232 return results; 233 } 234 }