/* * 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.
*/
publicenum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}
public PassFailJFrame(String instructions) throws InterruptedException,
InvocationTargetException { this(instructions, TEST_TIMEOUT);
}
public PassFailJFrame(String instructions, long testTimeOut) throws
InterruptedException, InvocationTargetException { this(TITLE, instructions, testTimeOut);
}
public PassFailJFrame(String title, String instructions, long testTimeOut) throws InterruptedException,
InvocationTargetException { this(title, instructions, testTimeOut, ROWS, COLUMNS);
}
/** * Constructs a JFrame with a given title & serves as test instructional * frame where the user follows the specified test instruction in order * to test the test case & mark the test pass or fail. If the expected * result is seen then the user click on the 'Pass' button else click * on the 'Fail' button and the reason for the failure should be * specified in the JDialog JTextArea. * * @param title title of the Frame. * @param instructions the instruction for the tester on how to test * and what is expected (pass) and what is not * expected (fail). * @param testTimeOut test timeout where time is specified in minutes. * @param rows number of visible rows of the JTextArea where the * instruction is show. * @param columns Number of columns of the instructional * JTextArea * @throws InterruptedException exception thrown when thread is * interrupted * @throws InvocationTargetException if an exception is thrown while * creating the test instruction frame on * EDT
*/ public PassFailJFrame(String title, String instructions, long testTimeOut, int rows, int columns) throws InterruptedException,
InvocationTargetException { if (isEventDispatchThread()) {
createUI(title, instructions, testTimeOut, rows, columns);
} else {
invokeAndWait(() -> createUI(title, instructions, testTimeOut,
rows, columns));
}
}
privatestaticvoid createUI(String title, String instructions, long testTimeOut, int rows, int columns) {
frame = new JFrame(title);
frame.setLayout(new BorderLayout());
JTextArea instructionsText = new JTextArea(instructions, rows, columns);
instructionsText.setEditable(false);
instructionsText.setLineWrap(true);
long tTimeout = TimeUnit.MINUTES.toMillis(testTimeOut);
final JLabel testTimeoutLabel = new JLabel(String.format("Test " + "timeout: %s", convertMillisToTimeStr(tTimeout)), JLabel.CENTER); finallong startTime = System.currentTimeMillis();
timer.setDelay(1000);
timer.addActionListener((e) -> { long leftTime = tTimeout - (System.currentTimeMillis() - startTime); if ((leftTime < 0) || failed) {
timer.stop();
testFailedReason = FAILURE_REASON
+ "Timeout User did not perform testing.";
timeout = true;
latch.countDown();
}
testTimeoutLabel.setText(String.format("Test timeout: %s", convertMillisToTimeStr(leftTime)));
});
timer.start();
frame.add(testTimeoutLabel, BorderLayout.NORTH);
frame.add(new JScrollPane(instructionsText), BorderLayout.CENTER);
/** * Wait for the user decision i,e user selects pass or fail button. * If user does not select pass or fail button then the test waits for * the specified timeoutMinutes period and the test gets timeout. * Note: This method should be called from main() thread * * @throws InterruptedException exception thrown when thread is * interrupted * @throws InvocationTargetException if an exception is thrown while * disposing of frames on EDT
*/ publicvoid awaitAndCheck() throws InterruptedException, InvocationTargetException { if (isEventDispatchThread()) { thrownew IllegalStateException("awaitAndCheck() should not be called on EDT");
}
latch.await();
invokeAndWait(PassFailJFrame::disposeWindows);
if (timeout) { thrownew RuntimeException(testFailedReason);
}
/** * Dispose all the window(s) i,e both the test instruction frame and * the window(s) that is added via addTestWindow(Window testWindow)
*/ privatestaticsynchronizedvoid disposeWindows() { for (Window win : windowList) {
win.dispose();
}
}
/** * Read the test failure reason and add the reason to the test result * example in the jtreg .jtr file.
*/ privatestaticvoid getFailureReason() { final JDialog dialog = new JDialog(frame, "Test Failure ", true);
dialog.setTitle("Failure reason");
JPanel jPanel = new JPanel(new BorderLayout());
JTextArea jTextArea = new JTextArea(5, 20);
/** * Approximately positions the instruction frame relative to the test * window as specified by the {@code position} parameter. If {@code testWindow} * is {@code null}, only the instruction frame is positioned according to * {@code position} parameter. * <p>This method should be called before making the test window visible * to avoid flickering.</p> * * @param testWindow test window that the test created. * May be {@code null}. * * @param position position must be one of: * <ul> * <li>{@code HORIZONTAL} - the test instruction frame is positioned * such that its right edge aligns with screen's horizontal center * and the test window (if not {@code null}) is placed to the right * of the instruction frame.</li> * * <li>{@code VERTICAL} - the test instruction frame is positioned * such that its bottom edge aligns with the screen's vertical center * and the test window (if not {@code null}) is placed below the * instruction frame.</li> * * <li>{@code TOP_LEFT_CORNER} - the test instruction frame is positioned * such that its top left corner is at the top left corner of the screen * and the test window (if not {@code null}) is placed to the right of * the instruction frame.</li> * </ul>
*/ publicstaticvoid positionTestWindow(Window testWindow, Position position) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Get the screen insets to position the frame by taking into // account the location of taskbar/menubars on screen.
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
/** * Ensures the frame location is updated by the window manager * if it adjusts the frame location after {@code setLocation}. * * @see #positionTestWindow
*/ privatestaticvoid syncLocationToWindowManager() {
Toolkit.getDefaultToolkit().sync(); try { Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/** * Returns the current position and size of the test instruction frame. * This method can be used in scenarios when custom positioning of * multiple test windows w.r.t test instruction frame is necessary, * at test-case level and the desired configuration is not available * as a {@code Position} option. * * @return Rectangle bounds of test instruction frame * @see #positionTestWindow * * @throws InterruptedException exception thrown when thread is * interrupted * @throws InvocationTargetException if an exception is thrown while * obtaining frame bounds on EDT
*/ publicstatic Rectangle getInstructionFrameBounds() throws InterruptedException, InvocationTargetException { final Rectangle[] bounds = {null};
/** * Add the testWindow to the windowList so that test instruction frame * and testWindow and any other windows used in this test is disposed * via disposeWindows(). * * @param testWindow testWindow that needs to be disposed
*/ publicstaticsynchronizedvoid addTestWindow(Window testWindow) {
windowList.add(testWindow);
}
/** * Forcibly fail the test and provide a reason. * * @param reason the reason why the test is failed
*/ publicstaticvoid forceFail(String reason) {
failed = true;
testFailedReason = FAILURE_REASON + reason;
latch.countDown();
}
}
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.