Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/8015300/Test8015300.java
38853 views
/*1* Copyright (c) 2013, 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;32import sun.awt.SunToolkit;3334import static javax.swing.SwingUtilities.invokeAndWait;35import static javax.swing.SwingUtilities.windowForComponent;36import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;3738/*39* @test40* @bug 801530041* @summary Tests that editable combobox select all text42* @author Sergey Malenkov43* @library ../../../../lib/testlibrary/44* @build ExtendedRobot45* @run main Test801530046*/4748public class Test8015300 {49private static final ExtendedRobot robot = createRobot();50private static final String[] ITEMS = {51"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",52"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};5354private static JComboBox<String> combo;5556public static void main(String[] args) throws Exception {57UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();58for (UIManager.LookAndFeelInfo info : array) {59UIManager.setLookAndFeel(info.getClassName());60System.err.println("L&F: " + info.getName());61invokeAndWait(new Runnable() {62@Override63public void run() {64combo = new JComboBox<>(ITEMS);65combo.addItemListener(new ItemListener() {66@Override67public void itemStateChanged(ItemEvent event) {68if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {69ComboBoxEditor editor = combo.getEditor();70Object component = editor.getEditorComponent();71if (component instanceof JTextField) {72JTextField text = (JTextField) component;73boolean selected = null != text.getSelectedText();7475StringBuilder sb = new StringBuilder();76sb.append(" - ").append(combo.getSelectedIndex());77sb.append(": ").append(event.getItem());78if (selected) {79sb.append("; selected");80}81System.err.println(sb);82if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {83throw new Error("unexpected state of text selection");84}85}86}87}88});89JFrame frame = new JFrame(getClass().getSimpleName());90frame.add(combo);91frame.pack();92frame.setLocationRelativeTo(null);93frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);94frame.setVisible(true);95}96});97for (int i = 0; i < ITEMS.length; ++i) {98select(i, true);99select(1, false);100}101invokeAndWait(new Runnable() {102@Override103public void run() {104windowForComponent(combo).dispose();105}106});107}108}109110private static void select(final int index, final boolean editable) throws Exception {111invokeAndWait(new Runnable() {112@Override113public void run() {114combo.setEditable(editable);115combo.setSelectedIndex(index);116}117});118robot.waitForIdle(50);119}120private static ExtendedRobot createRobot() {121try {122ExtendedRobot robot = new ExtendedRobot();123return robot;124}catch(Exception ex) {125ex.printStackTrace();126throw new Error("Unexpected Failure");127}128}129}130131132