jdk/test/sanity/client/SwingSet/src/SliderDemoTest.java
changeset 44716 60901aefa352
parent 44128 23aaaabafcf9
equal deleted inserted replaced
44715:a52b758eed5c 44716:60901aefa352
       
     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             checkKeyboard(jso);
       
    86             checkMouse(jso);
       
    87         }
       
    88         checkMaximum(jso, PLAIN_SLIDER_MAXIMUM);
       
    89         checkMinimum(jso, PLAIN_SLIDER_MINIMUM);
       
    90         checkMoveForward(jso, 10);
       
    91     }
       
    92 
       
    93     private void majorTicks(JFrameOperator jfo, String accessibleName) {
       
    94         JSliderOperator jso = new JSliderOperator(jfo,
       
    95                 new AccessibleNameChooser(accessibleName));
       
    96         checkMoveForward(jso, 40);
       
    97         assertTrue(jso.getPaintTicks());
       
    98         assertEquals(100, jso.getMajorTickSpacing());
       
    99     }
       
   100 
       
   101     private void minorTicks(JFrameOperator jfo, String accessibleName) {
       
   102         JSliderOperator jso = new JSliderOperator(jfo,
       
   103                 new AccessibleNameChooser(accessibleName));
       
   104         if (accessibleName.equals(HORIZONTAL_MINOR_TICKS_SLIDER)) {
       
   105             checkMaximum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MAXIMUM);
       
   106             checkMinimum(jso, HORIZONTAL_MINOR_TICKS_SLIDER_MINIMUM);
       
   107             checkMoveForward(jso, 2);
       
   108             checkSnapToTick(jso, 5, 6);
       
   109             assertEquals(5, jso.getMajorTickSpacing());
       
   110             assertEquals(1, jso.getMinorTickSpacing());
       
   111         } else {
       
   112             checkMaximum(jso, VERTICAL_MINOR_TICKS_SLIDER_MAXIMUM);
       
   113             checkMinimum(jso, VERTICAL_MINOR_TICKS_SLIDER_MINIMUM);
       
   114             checkMoveForward(jso, 10);
       
   115             assertEquals(20, jso.getMajorTickSpacing());
       
   116             assertEquals(5, jso.getMinorTickSpacing());
       
   117         }
       
   118         assertTrue(jso.getPaintTicks());
       
   119         assertTrue(jso.getPaintLabels());
       
   120     }
       
   121 
       
   122     private void disabled(JFrameOperator jfo, String accessibleName)
       
   123             throws InterruptedException {
       
   124         JSliderOperator jso = new JSliderOperator(jfo,
       
   125                 new AccessibleNameChooser(accessibleName));
       
   126         int initialvalue;
       
   127         initialvalue = jso.getValue();
       
   128         jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 10);
       
   129         Thread.sleep(500);
       
   130         assertFalse(jso.hasFocus());
       
   131         assertEquals(initialvalue, jso.getValue());
       
   132     }
       
   133 
       
   134     private void checkMaximum(JSliderOperator jso, int maxValue) {
       
   135         jso.scrollToMaximum();
       
   136         waitSliderValue(jso, jSlider -> jSlider.getValue() == maxValue);
       
   137     }
       
   138 
       
   139     private void checkMinimum(JSliderOperator jso, int minValue) {
       
   140         jso.scrollToMinimum();
       
   141         waitSliderValue(jso, jSlider -> jSlider.getValue() == minValue);
       
   142     }
       
   143 
       
   144     private void checkKeyboard(JSliderOperator jso) {
       
   145         checkKeyPress(jso, KeyEvent.VK_HOME,
       
   146                 jSlider -> jSlider.getValue() == jso.getMinimum());
       
   147 
       
   148         {
       
   149             int expectedValue = jso.getValue() + 1;
       
   150             checkKeyPress(jso, KeyEvent.VK_UP,
       
   151                     jSlider -> jSlider.getValue() >= expectedValue);
       
   152         }
       
   153         {
       
   154             int expectedValue = jso.getValue() + 1;
       
   155             checkKeyPress(jso, KeyEvent.VK_RIGHT,
       
   156                     jSlider -> jSlider.getValue() >= expectedValue);
       
   157         }
       
   158         {
       
   159             int expectedValue = jso.getValue() + 11;
       
   160             checkKeyPress(jso, KeyEvent.VK_PAGE_UP,
       
   161                     jSlider -> jSlider.getValue() >= expectedValue);
       
   162         }
       
   163 
       
   164         checkKeyPress(jso, KeyEvent.VK_END,
       
   165                 jSlider -> jSlider.getValue() == jso.getMaximum());
       
   166 
       
   167         {
       
   168             int expectedValue = jso.getValue() - 1;
       
   169             checkKeyPress(jso, KeyEvent.VK_DOWN,
       
   170                     jSlider -> jSlider.getValue() <= expectedValue);
       
   171         }
       
   172         {
       
   173             int expectedValue = jso.getValue() - 1;
       
   174             checkKeyPress(jso, KeyEvent.VK_LEFT,
       
   175                     jSlider -> jSlider.getValue() <= expectedValue);
       
   176         }
       
   177         {
       
   178             int expectedValue = jso.getValue() - 11;
       
   179             checkKeyPress(jso, KeyEvent.VK_PAGE_DOWN,
       
   180                     jSlider -> jSlider.getValue() <= expectedValue);
       
   181         }
       
   182     }
       
   183 
       
   184     private void checkKeyPress(JSliderOperator jso, int keyCode,
       
   185             Predicate<JSliderOperator> predicate) {
       
   186         jso.pushKey(keyCode);
       
   187         waitSliderValue(jso, predicate);
       
   188     }
       
   189 
       
   190     private void waitSliderValue(JSliderOperator jso,
       
   191             Predicate<JSliderOperator> predicate) {
       
   192         jso.waitState(new ComponentChooser() {
       
   193             public boolean checkComponent(Component comp) {
       
   194                 return predicate.test(jso);
       
   195             }
       
   196 
       
   197             public String getDescription() {
       
   198                 return "Wait till Slider attains the specified state.";
       
   199             }
       
   200         });
       
   201     }
       
   202 
       
   203     private void checkMoveForward(JSliderOperator jso, int value) {
       
   204         jso.setValue(jso.getMinimum());
       
   205         int finalValue = jso.getValue() + value;
       
   206         jso.scrollToValue(finalValue);
       
   207         waitSliderValue(jso, jSlider -> jSlider.getValue() == finalValue);
       
   208     }
       
   209 
       
   210     private void checkSnapToTick(JSliderOperator jso, int expectedLower,
       
   211             int expectedHigher) {
       
   212         jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick());
       
   213         waitSliderValue(jso, jSlider -> jSlider.getValue() == expectedLower
       
   214                 || jSlider.getValue() == expectedHigher);
       
   215         jso.releaseMouse();
       
   216     }
       
   217 
       
   218     private void checkMouse(JSliderOperator jso) {
       
   219         // Check mouse dragging by pressing on the center of Slider and then
       
   220         // dragging the mouse till the end of the track.
       
   221         // We set the initial value of the slider as 45,
       
   222         // which is the value of the slider at the middle.
       
   223         jso.setValue((jso.getMaximum() + jso.getMinimum()) / 2);
       
   224         jso.pressMouse(jso.getCenterXForClick(), jso.getCenterYForClick());
       
   225         jso.dragMouse(jso.getWidth() + 10, jso.getHeight());
       
   226         waitSliderValue(jso, jSlider -> jSlider.getValue() == jSlider.getMaximum());
       
   227         jso.releaseMouse();
       
   228 
       
   229         // Check mouse click by clicking on the center of the track 2 times
       
   230         // and waiting till the slider value has changed from its previous
       
   231         // value as a result of the clicks.
       
   232         jso.clickMouse(jso.getCenterXForClick(), jso.getCenterYForClick(), 2);
       
   233         waitSliderValue(jso, jSlider -> jSlider.getValue() != jSlider.getMaximum());
       
   234     }
       
   235 }