Test8015300.java revision 11018:66682f651425
1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import com.sun.java.swing.plaf.windows.WindowsComboBoxUI.WindowsComboBoxEditor;
25import java.awt.Toolkit;
26import java.awt.event.ItemEvent;
27import java.awt.event.ItemListener;
28import javax.swing.ComboBoxEditor;
29import javax.swing.JComboBox;
30import javax.swing.JFrame;
31import javax.swing.JTextField;
32import javax.swing.UIManager;
33import sun.awt.SunToolkit;
34
35import static javax.swing.SwingUtilities.invokeAndWait;
36import static javax.swing.SwingUtilities.windowForComponent;
37import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
38
39/*
40 * @test
41 * @bug 8015300
42 * @summary Tests that editable combobox select all text
43 * @author Sergey Malenkov
44 * @library ../../../../lib/testlibrary/
45 * @build ExtendedRobot
46 * @run main Test8015300
47 */
48
49public class Test8015300 {
50    private static final ExtendedRobot robot = createRobot();
51    private static final String[] ITEMS = {
52            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
53            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
54
55    private static JComboBox<String> combo;
56
57    public static void main(String[] args) throws Exception {
58        UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
59        for (UIManager.LookAndFeelInfo info : array) {
60            UIManager.setLookAndFeel(info.getClassName());
61            System.err.println("L&F: " + info.getName());
62            invokeAndWait(new Runnable() {
63                @Override
64                public void run() {
65                    combo = new JComboBox<>(ITEMS);
66                    combo.addItemListener(new ItemListener() {
67                        @Override
68                        public void itemStateChanged(ItemEvent event) {
69                            if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
70                                ComboBoxEditor editor = combo.getEditor();
71                                Object component = editor.getEditorComponent();
72                                if (component instanceof JTextField) {
73                                    JTextField text = (JTextField) component;
74                                    boolean selected = null != text.getSelectedText();
75
76                                    StringBuilder sb = new StringBuilder();
77                                    sb.append(" - ").append(combo.getSelectedIndex());
78                                    sb.append(": ").append(event.getItem());
79                                    if (selected) {
80                                        sb.append("; selected");
81                                    }
82                                    System.err.println(sb);
83                                    if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
84                                        throw new Error("unexpected state of text selection");
85                                    }
86                                }
87                            }
88                        }
89                    });
90                    JFrame frame = new JFrame(getClass().getSimpleName());
91                    frame.add(combo);
92                    frame.pack();
93                    frame.setLocationRelativeTo(null);
94                    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
95                    frame.setVisible(true);
96                }
97            });
98            for (int i = 0; i < ITEMS.length; ++i) {
99                select(i, true);
100                select(1, false);
101            }
102            invokeAndWait(new Runnable() {
103                @Override
104                public void run() {
105                    windowForComponent(combo).dispose();
106                }
107            });
108        }
109    }
110
111    private static void select(final int index, final boolean editable) throws Exception {
112        invokeAndWait(new Runnable() {
113            @Override
114            public void run() {
115                combo.setEditable(editable);
116                combo.setSelectedIndex(index);
117            }
118        });
119        robot.waitForIdle(50);
120    }
121    private static ExtendedRobot createRobot() {
122         try {
123             ExtendedRobot robot = new ExtendedRobot();
124             return robot;
125         }catch(Exception ex) {
126             ex.printStackTrace();
127             throw new Error("Unexpected Failure");
128         }
129    }
130}
131