Path: blob/master/test/jdk/javax/swing/JFileChooser/JFileChooserCombolistSelection/JFileChooserCombolistSelection.java
41153 views
/*1* Copyright (c) 2015, 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*/2223/*24@test25@bug 4674075 823358426@summary Checks if keyboard traversal of JFileChooser's "Look in" combobox is broken27@modules java.desktop28@requires (os.family == "windows")29@key headful30@run main/manual JFileChooserCombolistSelection31*/32import java.awt.Panel;33import java.awt.TextArea;34import java.awt.event.ActionEvent;35import java.awt.event.ActionListener;36import javax.swing.JFileChooser;37import javax.swing.SwingUtilities;38import javax.swing.UIManager;39import javax.swing.JFrame;40import javax.swing.JDialog;41import javax.swing.JButton;4243public class JFileChooserCombolistSelection {4445private static boolean theTestPassed;46private static boolean testGeneratedInterrupt;47private static Thread mainThread;48private static int sleepTime = 90000;49public static JFileChooser fileChooser;5051private static void init() throws Exception {5253SwingUtilities.invokeAndWait(new Runnable() {54@Override55public void run() {56String[] instructions57= {58"Activate the \"Look in\" combobox's popup by "59+"clicking on its arrow button."60+ "Then navigate in it using keyboard "61+"(ie UP and DOWN arrow keys)."62+ "When navigating, take a notice, "63+"if the contents of the file list changes."64+ "If yes, then press \"Fail\", "65+"otherwise press \"Passed\"."66};67Sysout.createDialogWithInstructions(instructions);68Sysout.printInstructions(instructions);69}70});71}7273/**74* ***************************************************75* Standard Test Machinery Section DO NOT modify anything in this section --76* it's a standard chunk of code which has all of the synchronisation77* necessary for the test harness. By keeping it the same in all tests, it78* is easier to read and understand someone else's test, as well as insuring79* that all tests behave correctly with the test harness. There is a section80* following this for test-defined classes81*/82public static void main(String args[]) throws Exception {83String osName = System.getProperty("os.name");84if(!osName.toLowerCase().contains("win")) {85System.out.println("The test was skipped because it is sensible only for Windows.");86return;87}88try {89UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");90} catch(Exception ex) {91return;92}93mainThread = Thread.currentThread();94try {95init();96} catch (Exception ex) {97return;98}99try {100mainThread.sleep(sleepTime);101} catch (InterruptedException ex) {102Sysout.dispose();103if (!theTestPassed && testGeneratedInterrupt) {104throw new RuntimeException("Test Failed");105}106}107if (!testGeneratedInterrupt) {108Sysout.dispose();109throw new RuntimeException("Test Failed");110}111}112113public static synchronized void pass() {114theTestPassed = true;115testGeneratedInterrupt = true;116mainThread.interrupt();117}118119public static synchronized void fail() {120theTestPassed = false;121testGeneratedInterrupt = true;122mainThread.interrupt();123}124}125126/**127* This is part of the standard test machinery. It creates a dialog (with the128* instructions), and is the interface for sending text messages to the user. To129* print the instructions, send an array of strings to Sysout.createDialog130* WithInstructions method. Put one line of instructions per array entry. To131* display a message for the tester to see, simply call Sysout.println with the132* string to be displayed. This mimics System.out.println but works within the133* test harness as well as standalone.134*/135class Sysout {136137private static TestDialog dialog;138private static JFrame frame;139140public static void createDialogWithInstructions(String[] instructions) {141frame = new JFrame();142dialog = new TestDialog(frame, "Instructions");143dialog.printInstructions(instructions);144dialog.setVisible(true);145println("Any messages for the tester will display here.");146}147148public static void printInstructions(String[] instructions) {149dialog.printInstructions(instructions);150}151152public static void println(String messageIn) {153dialog.displayMessage(messageIn);154}155156public static void dispose() {157Sysout.println("Shutting down the Java process..");158if(JFileChooserCombolistSelection.fileChooser != null) {159JFileChooserCombolistSelection.fileChooser.cancelSelection();160}161frame.dispose();162dialog.dispose();163}164}165166/**167* This is part of the standard test machinery. It provides a place for the test168* instructions to be displayed, and a place for interactive messages to the169* user to be displayed. To have the test instructions displayed, see Sysout. To170* have a message to the user be displayed, see Sysout. Do not call anything in171* this dialog directly.172*/173class TestDialog extends JDialog {174175private TextArea instructionsText;176private TextArea messageText;177private int maxStringLength = 80;178private Panel buttonP = new Panel();179private JButton run = new JButton("Run");180private JButton passB = new JButton("Pass");181private JButton failB = new JButton("Fail");182183public TestDialog(JFrame frame, String name) {184super(frame, name);185frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);186int scrollBoth = TextArea.SCROLLBARS_BOTH;187instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);188add("North", instructionsText);189190messageText = new TextArea("", 5, maxStringLength, scrollBoth);191add("Center", messageText);192193buttonP.add("East", run);194buttonP.add("East", passB);195buttonP.add("West", failB);196passB.setEnabled(false);197failB.setEnabled(false);198add("South", buttonP);199200run.addActionListener(new ActionListener() {201202@Override203public void actionPerformed(ActionEvent ae) {204JFileChooserCombolistSelection.fileChooser = new JFileChooser();205JFileChooserCombolistSelection.fileChooser.showOpenDialog(null);206passB.setEnabled(true);207failB.setEnabled(true);208}209});210211passB.addActionListener(new ActionListener() {212213@Override214public void actionPerformed(ActionEvent ae) {215JFileChooserCombolistSelection.pass();216}217});218219failB.addActionListener(new ActionListener() {220221@Override222public void actionPerformed(ActionEvent ae) {223JFileChooserCombolistSelection.fail();224}225});226pack();227228setVisible(true);229}230231public void printInstructions(String[] instructions) {232instructionsText.setText("");233234String printStr, remainingStr;235for (String instruction : instructions) {236remainingStr = instruction;237while (remainingStr.length() > 0) {238if (remainingStr.length() >= maxStringLength) {239int posOfSpace = remainingStr.240lastIndexOf(' ', maxStringLength - 1);241242if (posOfSpace <= 0) {243posOfSpace = maxStringLength - 1;244}245246printStr = remainingStr.substring(0, posOfSpace + 1);247remainingStr = remainingStr.substring(posOfSpace + 1);248} else {249printStr = remainingStr;250remainingStr = "";251}252instructionsText.append(printStr + "\n");253}254}255256}257258public void displayMessage(String messageIn) {259messageText.append(messageIn + "\n");260}261}262263264