Path: blob/master/test/jdk/javax/swing/JComboBox/8015300/Test8015300.java
41153 views
/*1* Copyright (c) 2013, 2017, 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*/2223import com.sun.java.swing.plaf.windows.WindowsComboBoxUI.WindowsComboBoxEditor;24import java.awt.Toolkit;25import java.awt.event.ItemEvent;26import java.awt.event.ItemListener;27import javax.swing.ComboBoxEditor;28import javax.swing.JComboBox;29import javax.swing.JFrame;30import javax.swing.JTextField;31import javax.swing.UIManager;3233import static javax.swing.SwingUtilities.invokeAndWait;34import static javax.swing.SwingUtilities.windowForComponent;35import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;3637/*38* @test39* @key headful40* @bug 801530041@requires (os.family == "windows")42* @summary Tests that editable combobox selects all text.43* @author Sergey Malenkov44* @library /lib/client/45* @modules java.desktop/com.sun.java.swing.plaf.windows46* @build ExtendedRobot47* @run main Test801530048*/4950public class Test8015300 {51private static final ExtendedRobot robot = createRobot();52private static final String[] ITEMS = {53"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",54"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};5556private static JComboBox<String> combo;5758public static void main(String[] args) throws Exception {59UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();60for (UIManager.LookAndFeelInfo info : array) {61UIManager.setLookAndFeel(info.getClassName());62System.err.println("L&F: " + info.getName());63invokeAndWait(new Runnable() {64@Override65public void run() {66combo = new JComboBox<>(ITEMS);67combo.addItemListener(new ItemListener() {68@Override69public void itemStateChanged(ItemEvent event) {70if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {71ComboBoxEditor editor = combo.getEditor();72Object component = editor.getEditorComponent();73if (component instanceof JTextField) {74JTextField text = (JTextField) component;75boolean selected = null != text.getSelectedText();7677StringBuilder sb = new StringBuilder();78sb.append(" - ").append(combo.getSelectedIndex());79sb.append(": ").append(event.getItem());80if (selected) {81sb.append("; selected");82}83System.err.println(sb);84if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {85throw new Error("unexpected state of text selection");86}87}88}89}90});91JFrame frame = new JFrame(getClass().getSimpleName());92frame.add(combo);93frame.pack();94frame.setLocationRelativeTo(null);95frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);96frame.setVisible(true);97}98});99for (int i = 0; i < ITEMS.length; ++i) {100select(i, true);101select(1, false);102}103invokeAndWait(new Runnable() {104@Override105public void run() {106windowForComponent(combo).dispose();107}108});109}110}111112private static void select(final int index, final boolean editable) throws Exception {113invokeAndWait(new Runnable() {114@Override115public void run() {116combo.setEditable(editable);117combo.setSelectedIndex(index);118}119});120robot.waitForIdle(50);121}122private static ExtendedRobot createRobot() {123try {124ExtendedRobot robot = new ExtendedRobot();125return robot;126}catch(Exception ex) {127ex.printStackTrace();128throw new Error("Unexpected Failure");129}130}131}132133134