Path: blob/master/test/jdk/sanity/client/SwingSet/src/SliderDemoTest.java
41153 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import org.jtregext.GuiTestListener;23import com.sun.swingset3.demos.slider.SliderDemo;24import java.awt.Component;25import java.awt.event.KeyEvent;26import java.util.function.Predicate;2728import javax.swing.UIManager;2930import static org.testng.AssertJUnit.*;3132import org.netbeans.jemmy.drivers.DriverManager;33import org.netbeans.jemmy.drivers.scrolling.KeyboardJSliderScrollDriver;34import org.testng.annotations.BeforeClass;35import org.testng.annotations.Test;36import org.netbeans.jemmy.ClassReference;37import org.netbeans.jemmy.ComponentChooser;38import org.netbeans.jemmy.operators.JFrameOperator;39import org.netbeans.jemmy.operators.JSliderOperator;40import org.netbeans.jemmy.accessibility.AccessibleNameChooser;41import org.netbeans.jemmy.util.LookAndFeel;4243import static com.sun.swingset3.demos.slider.SliderDemo.*;44import org.testng.annotations.Listeners;4546/*47* @test48* @key headful49* @summary Verifies SwingSet3 SliderDemo by moving the sliders through50* different means, checking the slider value corresponding to it,51* checking maximum and minimum values, checking Snap to tick is working52* and checking the presence of ticks and labels.53*54* @library /sanity/client/lib/jemmy/src55* @library /sanity/client/lib/Extensions/src56* @library /sanity/client/lib/SwingSet3/src57* @modules java.desktop58* java.logging59* @build org.jemmy2ext.JemmyExt60* @build com.sun.swingset3.demos.slider.SliderDemo61* @run testng/timeout=600 SliderDemoTest62*/63@Listeners(GuiTestListener.class)64public class SliderDemoTest {6566private static final int PLAIN_SLIDER_MINIMUM = -10;67private static final int PLAIN_SLIDER_MAXIMUM = 100;68private static final int HORIZONTAL_MINOR_TICKS_SLIDER_MINIMUM = 0;69private static final int HORIZONTAL_MINOR_TICKS_SLIDER_MAXIMUM = 11;70private static final int VERTICAL_MINOR_TICKS_SLIDER_MINIMUM = 0;71private static final int VERTICAL_MINOR_TICKS_SLIDER_MAXIMUM = 100;7273@BeforeClass74public void useKeyboardSliderDriver() {75DriverManager.setScrollDriver(new KeyboardJSliderScrollDriver());76}7778@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)79public void test(String lookAndFeel) throws Exception {80UIManager.setLookAndFeel(lookAndFeel);81new ClassReference(SliderDemo.class.getCanonicalName()).startApplication();82JFrameOperator frame = new JFrameOperator(DEMO_TITLE);83plain(frame, HORIZONTAL_PLAIN_SLIDER);84majorTicks(frame, HORIZONTAL_MAJOR_TICKS_SLIDER);85minorTicks(frame, HORIZONTAL_MINOR_TICKS_SLIDER);86disabled(frame, HORIZONTAL_DISABLED_SLIDER);87plain(frame, VERTICAL_PLAIN_SLIDER);88majorTicks(frame, VERTICAL_MAJOR_TICKS_SLIDER);89minorTicks(frame, VERTICAL_MINOR_TICKS_SLIDER);90disabled(frame, VERTICAL_DISABLED_SLIDER);91}9293private void plain(JFrameOperator jfo, String accessibleName) {94JSliderOperator jso = new JSliderOperator(jfo,95new AccessibleNameChooser(accessibleName));96if (accessibleName.equals(HORIZONTAL_PLAIN_SLIDER)) {97jso.waitHasFocus();98checkKeyboard(jso);99checkMouse(jso);100}101checkMaximum(jso, PLAIN_SLIDER_MAXIMUM);102checkMinimum(jso, PLAIN_SLIDER_MINIMUM);103checkMoveForward(jso, 10);104}105106private void majorTicks(JFrameOperator jfo, String accessibleName) {107JSliderOperator jso = new JSliderOperator(jfo,108new AccessibleNameChooser(accessibleName));109checkMoveForward(jso, 40);110assertTrue(jso.getPaintTicks());111assertEquals(100, jso.getMajorTickSpacing());112}113114private void minorTicks(JFrameOperator jfo, String accessibleName) {115JSliderOperator jso = new JSliderOperator(jfo,116new AccessibleNameChooser(accessibleName));117if (accessibleName.equals(HORIZONTAL_MINOR_TICKS_SLIDER)) {118checkMaximum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MAXIMUM);119checkMinimum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MINIMUM);120checkMoveForward(jso, 2);121checkSnapToTick(jso, 5, 6);122assertEquals(5, jso.getMajorTickSpacing());123assertEquals(1, jso.getMinorTickSpacing());124} else {125checkMaximum(jso, VERTICAL_MINOR_TICKS_SLIDER_MAXIMUM);126checkMinimum(jso, VERTICAL_MINOR_TICKS_SLIDER_MINIMUM);127checkMoveForward(jso, 10);128assertEquals(20, jso.getMajorTickSpacing());129assertEquals(5, jso.getMinorTickSpacing());130}131assertTrue(jso.getPaintTicks());132assertTrue(jso.getPaintLabels());133}134135private void disabled(JFrameOperator jfo, String accessibleName)136throws InterruptedException {137JSliderOperator jso = new JSliderOperator(jfo,138new AccessibleNameChooser(accessibleName));139int initialvalue;140initialvalue = jso.getValue();141jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 10);142Thread.sleep(500);143assertFalse(jso.hasFocus());144assertEquals(initialvalue, jso.getValue());145}146147private void checkMaximum(JSliderOperator jso, int maxValue) {148jso.scrollToMaximum();149waitSliderValue(jso, jSlider -> jSlider.getValue() == maxValue,150"value == " + maxValue);151}152153private void checkMinimum(JSliderOperator jso, int minValue) {154jso.scrollToMinimum();155waitSliderValue(jso, jSlider -> jSlider.getValue() == minValue,156"value == " + minValue);157}158159private void checkKeyboard(JSliderOperator jso) {160boolean isMotif = LookAndFeel.isMotif();161checkKeyPress(jso, KeyEvent.VK_HOME,162jSlider -> jSlider.getValue() == jso.getMinimum(),163"value == " + jso.getMinimum());164165{166int expectedValue = jso.getValue() + 1;167checkKeyPress(jso, KeyEvent.VK_UP,168jSlider -> jSlider.getValue() >= expectedValue,169"value >= " + expectedValue);170}171{172int expectedValue = jso.getValue() + 1;173checkKeyPress(jso, KeyEvent.VK_RIGHT,174jSlider -> jSlider.getValue() >= expectedValue,175"value >= " + expectedValue);176}177if (!isMotif) {178int expectedValue = jso.getValue() + 11;179checkKeyPress(jso, KeyEvent.VK_PAGE_UP,180jSlider -> jSlider.getValue() >= expectedValue,181"value >= " + expectedValue);182}183184checkKeyPress(jso, KeyEvent.VK_END,185jSlider -> jSlider.getValue() == jso.getMaximum(),186"value == " + jso.getMaximum());187188{189int expectedValue = jso.getValue() - 1;190checkKeyPress(jso, KeyEvent.VK_DOWN,191jSlider -> jSlider.getValue() <= expectedValue,192"value <= " + expectedValue);193}194{195int expectedValue = jso.getValue() - 1;196checkKeyPress(jso, KeyEvent.VK_LEFT,197jSlider -> jSlider.getValue() <= expectedValue,198"value <= " + expectedValue);199}200if (!isMotif) {201int expectedValue = jso.getValue() - 11;202checkKeyPress(jso, KeyEvent.VK_PAGE_DOWN,203jSlider -> jSlider.getValue() <= expectedValue,204"value <= " + expectedValue);205}206}207208private void checkKeyPress(JSliderOperator jso, int keyCode,209Predicate<JSliderOperator> predicate,210String description) {211jso.pushKey(keyCode);212waitSliderValue(jso, predicate, description);213}214215private void waitSliderValue(JSliderOperator jso,216Predicate<JSliderOperator> predicate, String description) {217jso.waitState(new ComponentChooser() {218public boolean checkComponent(Component comp) {219return predicate.test(jso);220}221222public String getDescription() {223return description;224}225});226}227228private void checkMoveForward(JSliderOperator jso, int value) {229jso.setValue(jso.getMinimum());230int finalValue = jso.getValue() + value;231jso.scrollToValue(finalValue);232waitSliderValue(jso, jSlider -> jSlider.getValue() == finalValue,233"value == " + finalValue);234}235236private void checkSnapToTick(JSliderOperator jso, int expectedLower,237int expectedHigher) {238jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick());239waitSliderValue(jso, jSlider -> jSlider.getValue() == expectedLower240|| jSlider.getValue() == expectedHigher,241"value is either" + expectedLower + " or " + expectedHigher);242jso.releaseMouse();243}244245private void checkMouse(JSliderOperator jso) {246// Check mouse dragging by pressing on the center of Slider and then247// dragging the mouse till the end of the track.248// We set the initial value of the slider as 45,249// which is the value of the slider at the middle.250jso.setValue((jso.getMaximum() + jso.getMinimum()) / 2);251jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick());252jso.dragMouse(jso.getWidth() + 10, jso.getHeight());253waitSliderValue(jso, jSlider -> jSlider.getValue() == jSlider.getMaximum(),254"value == " + jso.getMaximum());255jso.releaseMouse();256257// Check mouse click by clicking on the center of the track 2 times258// and waiting till the slider value has changed from its previous259// value as a result of the clicks.260jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 2);261waitSliderValue(jso, jSlider -> jSlider.getValue() != jSlider.getMaximum(),262"value != " + jso.getMaximum());263}264}265266267