1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.ComponentOrientation;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import static javax.swing.UIManager.getInstalledLookAndFeels;
/*
* @test
* @key headful
* @bug 4515999
* @summary Check whether incrementing dates via the keyboard (up/down) gives
* the same results as using mouse press on the arrow buttons in a JSpinner.
* @run main JSpinnerMouseAndKeyPressTest
*/
public class JSpinnerMouseAndKeyPressTest {
// 2 days in milliseconds
private static final int EXPECTED_VALUE_2_DAYS = 2 * 24 * 60 * 60 * 1000;
private static JFrame frame;
private static JSpinner spinner;
private static volatile Point spinnerUpButtonCenter;
private static volatile Point spinnerDownButtonCenter;
private static volatile Date spinnerValue;
public static void main(String[] s) throws Exception {
runTest();
}
private static void setLookAndFeel(final String laf) {
try {
UIManager.setLookAndFeel(laf);
System.out.println("LookAndFeel: " + laf);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static void createUI() {
frame = new JFrame();
JPanel panel = new JPanel();
spinner = new JSpinner();
spinner.setModel(new DateModel());
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "dd/MM/yy");
spinner.setEditor(editor);
spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(spinner);
frame.add(panel);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void runTest() throws Exception {
Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(100);
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
.map(UIManager.LookAndFeelInfo::getClassName)
.collect(Collectors.toList());
for (final String laf : lafs) {
try {
SwingUtilities.invokeAndWait(() -> {
setLookAndFeel(laf);
createUI();
});
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
Point loc = spinner.getLocationOnScreen();
int editorWidth = spinner.getEditor().getWidth();
int buttonWidth = spinner.getWidth() - editorWidth;
int quarterHeight = spinner.getHeight() / 4;
spinnerUpButtonCenter = new Point(loc.x + editorWidth
+ (buttonWidth / 2),
loc.y + quarterHeight);
spinnerDownButtonCenter = new Point(spinnerUpButtonCenter.x,
loc.y + (3 * quarterHeight));
});
// Mouse press use-case
// Move Mouse pointer to UP button center and click it
robot.mouseMove(spinnerUpButtonCenter.x, spinnerUpButtonCenter.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
updateSpinnerValue();
long upValue = spinnerValue.getTime();
// Move Mouse pointer to DOWN button center and click it
robot.mouseMove(spinnerDownButtonCenter.x, spinnerDownButtonCenter.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
updateSpinnerValue();
long downValue = spinnerValue.getTime();
long mouseIncrement = upValue - downValue;
// Key press use-case
// Up Key press
robot.keyPress(KeyEvent.VK_UP);
robot.keyRelease(KeyEvent.VK_UP);
updateSpinnerValue();
upValue = spinnerValue.getTime();
// Down Key press
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
updateSpinnerValue();
downValue = spinnerValue.getTime();
long keyIncrement = upValue - downValue;
if ((keyIncrement == EXPECTED_VALUE_2_DAYS) &&
(mouseIncrement == EXPECTED_VALUE_2_DAYS)) {
System.out.println("Test passed");
} else {
throw new RuntimeException("Test failed because keyIncrement: " +
keyIncrement + " and mouseIncrement: " +
mouseIncrement + " should match with the expected value " +
EXPECTED_VALUE_2_DAYS + " for LnF " + laf);
}
} finally {
SwingUtilities.invokeAndWait(JSpinnerMouseAndKeyPressTest::disposeFrame);
}
}
}
private static void updateSpinnerValue() throws Exception {
SwingUtilities.invokeAndWait(() -> spinnerValue = (Date) spinner.getValue());
}
private static void disposeFrame() {
if (frame != null) {
frame.dispose();
frame = null;
}
}
private static class DateModel extends SpinnerDateModel {
private final Calendar cal = Calendar.getInstance();
@Override
public Object getNextValue() {
cal.setTime(getDate());
cal.add(Calendar.DAY_OF_MONTH, 2); // Increment two days
return cal.getTime();
}
@Override
public Object getPreviousValue() {
cal.setTime(getDate());
cal.add(Calendar.DAY_OF_MONTH, -2); // Decrement two days
return cal.getTime();
}
}
}
|