1 2 /* 3 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 import org.jtregext.GuiTestListener; 25 import com.sun.swingset3.demos.slider.SliderDemo; 26 import java.awt.Component; 27 import java.awt.event.KeyEvent; 28 import java.util.function.Predicate; 29 30 import static org.testng.AssertJUnit.*; 31 import org.testng.annotations.Test; 32 import org.netbeans.jemmy.ClassReference; 33 import org.netbeans.jemmy.ComponentChooser; 34 import org.netbeans.jemmy.operators.JFrameOperator; 35 import org.netbeans.jemmy.operators.JSliderOperator; 36 import org.netbeans.jemmy.accessibility.AccessibleNameChooser; 37 import static com.sun.swingset3.demos.slider.SliderDemo.*; 38 import org.testng.annotations.Listeners; 39 40 /* 41 * @test 42 * @key headful 43 * @summary Verifies SwingSet3 SliderDemo by moving the sliders through 44 * different means, checking the slider value corresponding to it, 45 * checking maximum and minimum values, checking Snap to tick is working 46 * and checking the presence of ticks and labels. 47 * 48 * @library /sanity/client/lib/jemmy/src 49 * @library /sanity/client/lib/Extensions/src 50 * @library /sanity/client/lib/SwingSet3/src 51 * @modules java.desktop 52 * java.logging 53 * @build org.jemmy2ext.JemmyExt 54 * @build com.sun.swingset3.demos.slider.SliderDemo 55 * @run testng SliderDemoTest 56 */ 57 @Listeners(GuiTestListener.class) 58 public class SliderDemoTest { 59 60 private static final int PLAIN_SLIDER_MINIMUM = -10; 61 private static final int PLAIN_SLIDER_MAXIMUM = 100; 62 private static final int HORIZONTAL_MINOR_TICKS_SLIDER_MINIMUM = 0; 63 private static final int HORIZONTAL_MINOR_TICKS_SLIDER_MAXIMUM = 11; 64 private static final int VERTICAL_MINOR_TICKS_SLIDER_MINIMUM = 0; 65 private static final int VERTICAL_MINOR_TICKS_SLIDER_MAXIMUM = 100; 66 67 @Test 68 public void test() throws Exception { 69 new ClassReference(SliderDemo.class.getCanonicalName()).startApplication(); 70 JFrameOperator frame = new JFrameOperator(DEMO_TITLE); 71 plain(frame, HORIZONTAL_PLAIN_SLIDER); 72 majorTicks(frame, HORIZONTAL_MAJOR_TICKS_SLIDER); 73 minorTicks(frame, HORIZONTAL_MINOR_TICKS_SLIDER); 74 disabled(frame, HORIZONTAL_DISABLED_SLIDER); 75 plain(frame, VERTICAL_PLAIN_SLIDER); 76 majorTicks(frame, VERTICAL_MAJOR_TICKS_SLIDER); 77 minorTicks(frame, VERTICAL_MINOR_TICKS_SLIDER); 78 disabled(frame, VERTICAL_DISABLED_SLIDER); 79 } 80 81 private void plain(JFrameOperator jfo, String accessibleName) { 82 JSliderOperator jso = new JSliderOperator(jfo, 83 new AccessibleNameChooser(accessibleName)); 84 if (accessibleName.equals(HORIZONTAL_PLAIN_SLIDER)) { 85 jso.waitHasFocus(); 86 checkKeyboard(jso); 87 checkMouse(jso); 88 } 89 checkMaximum(jso, PLAIN_SLIDER_MAXIMUM); 90 checkMinimum(jso, PLAIN_SLIDER_MINIMUM); 91 checkMoveForward(jso, 10); 92 } 93 94 private void majorTicks(JFrameOperator jfo, String accessibleName) { 95 JSliderOperator jso = new JSliderOperator(jfo, 96 new AccessibleNameChooser(accessibleName)); 97 checkMoveForward(jso, 40); 98 assertTrue(jso.getPaintTicks()); 99 assertEquals(100, jso.getMajorTickSpacing()); 100 } 101 102 private void minorTicks(JFrameOperator jfo, String accessibleName) { 103 JSliderOperator jso = new JSliderOperator(jfo, 104 new AccessibleNameChooser(accessibleName)); 105 if (accessibleName.equals(HORIZONTAL_MINOR_TICKS_SLIDER)) { 106 checkMaximum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MAXIMUM); 107 checkMinimum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MINIMUM); 108 checkMoveForward(jso, 2); 109 checkSnapToTick(jso, 5, 6); 110 assertEquals(5, jso.getMajorTickSpacing()); 111 assertEquals(1, jso.getMinorTickSpacing()); 112 } else { 113 checkMaximum(jso, VERTICAL_MINOR_TICKS_SLIDER_MAXIMUM); 114 checkMinimum(jso, VERTICAL_MINOR_TICKS_SLIDER_MINIMUM); 115 checkMoveForward(jso, 10); 116 assertEquals(20, jso.getMajorTickSpacing()); 117 assertEquals(5, jso.getMinorTickSpacing()); 118 } 119 assertTrue(jso.getPaintTicks()); 120 assertTrue(jso.getPaintLabels()); 121 } 122 123 private void disabled(JFrameOperator jfo, String accessibleName) 124 throws InterruptedException { 125 JSliderOperator jso = new JSliderOperator(jfo, 126 new AccessibleNameChooser(accessibleName)); 127 int initialvalue; 128 initialvalue = jso.getValue(); 129 jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 10); 130 Thread.sleep(500); 131 assertFalse(jso.hasFocus()); 132 assertEquals(initialvalue, jso.getValue()); 133 } 134 135 private void checkMaximum(JSliderOperator jso, int maxValue) { 136 jso.scrollToMaximum(); 137 waitSliderValue(jso, jSlider -> jSlider.getValue() == maxValue); 138 } 139 140 private void checkMinimum(JSliderOperator jso, int minValue) { 141 jso.scrollToMinimum(); 142 waitSliderValue(jso, jSlider -> jSlider.getValue() == minValue); 143 } 144 145 private void checkKeyboard(JSliderOperator jso) { 146 checkKeyPress(jso, KeyEvent.VK_HOME, 147 jSlider -> jSlider.getValue() == jso.getMinimum()); 148 149 { 150 int expectedValue = jso.getValue() + 1; 151 checkKeyPress(jso, KeyEvent.VK_UP, 152 jSlider -> jSlider.getValue() >= expectedValue); 153 } 154 { 155 int expectedValue = jso.getValue() + 1; 156 checkKeyPress(jso, KeyEvent.VK_RIGHT, 157 jSlider -> jSlider.getValue() >= expectedValue); 158 } 159 { 160 int expectedValue = jso.getValue() + 11; 161 checkKeyPress(jso, KeyEvent.VK_PAGE_UP, 162 jSlider -> jSlider.getValue() >= expectedValue); 163 } 164 165 checkKeyPress(jso, KeyEvent.VK_END, 166 jSlider -> jSlider.getValue() == jso.getMaximum()); 167 168 { 169 int expectedValue = jso.getValue() - 1; 170 checkKeyPress(jso, KeyEvent.VK_DOWN, 171 jSlider -> jSlider.getValue() <= expectedValue); 172 } 173 { 174 int expectedValue = jso.getValue() - 1; 175 checkKeyPress(jso, KeyEvent.VK_LEFT, 176 jSlider -> jSlider.getValue() <= expectedValue); 177 } 178 { 179 int expectedValue = jso.getValue() - 11; 180 checkKeyPress(jso, KeyEvent.VK_PAGE_DOWN, 181 jSlider -> jSlider.getValue() <= expectedValue); 182 } 183 } 184 185 private void checkKeyPress(JSliderOperator jso, int keyCode, 186 Predicate<JSliderOperator> predicate) { 187 jso.pushKey(keyCode); 188 waitSliderValue(jso, predicate); 189 } 190 191 private void waitSliderValue(JSliderOperator jso, 192 Predicate<JSliderOperator> predicate) { 193 jso.waitState(new ComponentChooser() { 194 public boolean checkComponent(Component comp) { 195 return predicate.test(jso); 196 } 197 198 public String getDescription() { 199 return "Wait till Slider attains the specified state."; 200 } 201 }); 202 } 203 204 private void checkMoveForward(JSliderOperator jso, int value) { 205 jso.setValue(jso.getMinimum()); 206 int finalValue = jso.getValue() + value; 207 jso.scrollToValue(finalValue); 208 waitSliderValue(jso, jSlider -> jSlider.getValue() == finalValue); 209 } 210 211 private void checkSnapToTick(JSliderOperator jso, int expectedLower, 212 int expectedHigher) { 213 jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick()); 214 waitSliderValue(jso, jSlider -> jSlider.getValue() == expectedLower 215 || jSlider.getValue() == expectedHigher); 216 jso.releaseMouse(); 217 } 218 219 private void checkMouse(JSliderOperator jso) { 220 // Check mouse dragging by pressing on the center of Slider and then 221 // dragging the mouse till the end of the track. 222 // We set the initial value of the slider as 45, 223 // which is the value of the slider at the middle. 224 jso.setValue((jso.getMaximum() + jso.getMinimum()) / 2); 225 jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick()); 226 jso.dragMouse(jso.getWidth() + 10, jso.getHeight()); 227 waitSliderValue(jso, jSlider -> jSlider.getValue() == jSlider.getMaximum()); 228 jso.releaseMouse(); 229 230 // Check mouse click by clicking on the center of the track 2 times 231 // and waiting till the slider value has changed from its previous 232 // value as a result of the clicks. 233 jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 2); 234 waitSliderValue(jso, jSlider -> jSlider.getValue() != jSlider.getMaximum()); 235 } 236 }