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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
* Copyright (c) 2002, 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 4670051
* @summary Checks whether JSpinner with a SpinnerDateModel
* spins the field where cursor is located.
* @run main DateFieldUnderCursorTest
*/
public class DateFieldUnderCursorTest {
private static final Calendar expected = Calendar.getInstance();
private static final Calendar actual = Calendar.getInstance();
private static Robot robot;
private static JSpinner spinner;
private static Date initValue;
private static Date upValue;
private static Date downValue;
private static JFrame frame;
private static boolean passed = true;
private static volatile Point spinnerUpButtonCenter;
private static volatile Point spinnerDownButtonCenter;
private static volatile Date spinnerValue;
public static void main(String[] s) throws Exception {
runTest();
}
public static void runTest() throws Exception {
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();
});
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));
});
// Cursor at Day field.
// Increment Day
initValue = getSpinnerValue();
mousePressOnUpButton();
upValue = getSpinnerValue();
verifyDayIncrement();
// Decrement Day
initValue = getSpinnerValue();
mousePressOnDownButton();
downValue = getSpinnerValue();
verifyDayDecrement();
// Cursor at Month Field
pressRightArrowKey();
// Increment Month
initValue = getSpinnerValue();
mousePressOnUpButton();
upValue = getSpinnerValue();
verifyMonthIncrement();
// Decrement Month
initValue = getSpinnerValue();
mousePressOnDownButton();
downValue = getSpinnerValue();
verifyMonthDecrement();
// Cursor at Year Field
pressRightArrowKey();
// Increment Year
initValue = getSpinnerValue();
mousePressOnUpButton();
upValue = getSpinnerValue();
verifyYearIncrement();
// Decrement Year
initValue = getSpinnerValue();
mousePressOnDownButton();
downValue = getSpinnerValue();
verifyYearDecrement();
if (passed) {
System.out.println("Test Passed");
} else {
throw new RuntimeException("Test Failed as one or more cases failed");
}
} finally {
SwingUtilities.invokeAndWait(DateFieldUnderCursorTest::disposeFrame);
}
}
}
private static Date getSpinnerValue() throws Exception {
SwingUtilities.invokeAndWait(() -> spinnerValue = (Date) spinner.getValue());
return spinnerValue;
}
public static void pressRightArrowKey() {
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
}
public static void mousePressOnUpButton() {
robot.mouseMove(spinnerUpButtonCenter.x, spinnerUpButtonCenter.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
public static void mousePressOnDownButton() {
robot.mouseMove(spinnerDownButtonCenter.x, spinnerDownButtonCenter.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
public static boolean compareDates() {
return (expected.get(Calendar.DATE) == actual.get(Calendar.DATE))
&& (expected.get(Calendar.MONTH) == actual.get(Calendar.MONTH))
&& (expected.get(Calendar.YEAR) == actual.get(Calendar.YEAR));
}
private static void checkResult() {
if (compareDates()) {
System.out.println(" Case Passed");
} else {
passed = false;
System.out.println(" Case Failed because the expected: " + expected.getTime()
+ " and actual: " + actual.getTime() + " outputs do not match.");
}
}
private static void updateCalendarObjects(Date finalValue) {
expected.setTime(initValue);
actual.setTime(finalValue);
}
/**
* Verifying that JSpinner increments the date field when cursor is on date field
*/
private static void verifyDayIncrement() {
System.out.print("verifyDateIncrement");
updateCalendarObjects(upValue);
expected.add(Calendar.DATE, 1);
checkResult();
}
/**
* Verifying that JSpinner decrements the date field when cursor is on date field
*/
private static void verifyDayDecrement() {
System.out.print("verifyDateDecrement");
updateCalendarObjects(downValue);
expected.add(Calendar.DATE, -1);
checkResult();
}
/**
* Verifying that JSpinner increments the month field when cursor is on month field
*/
private static void verifyMonthIncrement() {
System.out.print("verifyMonthIncrement");
updateCalendarObjects(upValue);
expected.add(Calendar.MONTH, 1);
checkResult();
}
/**
* Verifying that JSpinner decrements the month field when cursor is on month field
*/
private static void verifyMonthDecrement() {
System.out.print("verifyMonthDecrement");
updateCalendarObjects(downValue);
expected.add(Calendar.MONTH, -1);
checkResult();
}
/**
* Verifying that, JSpinner decrements the year field when the cursor is on year field.
*/
private static void verifyYearDecrement() {
System.out.print("verifyYearDecrement");
updateCalendarObjects(downValue);
expected.add(Calendar.YEAR, -1);
checkResult();
}
/**
* Verifying that JSpinner increments the year field when cursor is on year field
*/
private static void verifyYearIncrement() {
System.out.print("verifyYearIncrement");
updateCalendarObjects(upValue);
expected.add(Calendar.YEAR, 1);
checkResult();
}
private static void createUI() {
frame = new JFrame();
JPanel panel = new JPanel();
spinner = new JSpinner(new SpinnerDateModel());
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.pack();
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
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 disposeFrame() {
if (frame != null) {
frame.dispose();
frame = null;
}
}
}
|