File: CustomComboBoxFocusTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (336 lines) | stat: -rw-r--r-- 9,650 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2015, 2017, 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.
 */

/**
 * @test
 * @key headful
 * @bug     8073001 8081764
 * @summary Test verifies that combo box with custom editor renders
 *          focus ring around arrow button correctly.
 * @library /lib/testlibrary
 * @build jdk.testlibrary.OSInfo
 * @run     main CustomComboBoxFocusTest
 */

import java.awt.AWTException;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CountDownLatch;
import javax.imageio.ImageIO;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import jdk.testlibrary.OSInfo;

public class CustomComboBoxFocusTest {

    private static CustomComboBoxFocusTest test = null;

    public static void main(String[] args) {
        if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
            System.out.println("Only Mac platform test. Test is skipped for other OS.");
            return;
        }

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    test = new CustomComboBoxFocusTest();
                }
            });
        } catch (InterruptedException | InvocationTargetException e ) {
            throw new RuntimeException("Test failed.", e);
        }

        SwingUtilities.invokeLater(test.init);

        try {
            System.out.println("Wait for screenshots...");
            test.testDone.await();
        } catch (InterruptedException e) {
            throw new RuntimeException("Test failed.", e);
        }
        System.out.println("Compare screenshots...");
        if (!test.match()) {
            throw new RuntimeException("Test failed.");
        }
        System.out.println("Test passed.");
    }

    private final JComboBox<String> ref = new JComboBox<String>() {
        public String toString() {
            return "reference";
        }
    };

    private final JComboBox<String> custom = new JComboBox<String>() {
        public String toString() {
            return "custom";
        }
    };

    private final JFrame frame;

    private CountDownLatch testDone = new CountDownLatch(1);

    private Robot robot;

    public CustomComboBoxFocusTest() {
        frame = new JFrame(System.getProperty("java.version"));

        try {
            robot = new Robot(frame.getGraphicsConfiguration().getDevice());

        } catch (AWTException e) {
            throw new RuntimeException("Test failed.", e);
        }
    }

    private boolean match() {
        final BufferedImage a = captureRef.img;
        final BufferedImage b = captureCustom.img;

        final int w = Math.min(a.getWidth(), b.getWidth());
        final int h = Math.min(a.getHeight(), b.getHeight());

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (a.getRGB(x, y) != b.getRGB(x, y)) {
                    return false;
                }
            }
        }
        return true;
    }

    private JComboBox<String> getReference() {
        return ref;
    }

    private JComboBox<String> getCustom() {
        return custom;
    }

    private JFrame getFrame() {
        return frame;
    }

    private static abstract class Step implements Runnable {
        final public void run() {
            doStep();

            final Step next = nextStep();

            if (next != null) {
                SwingUtilities.invokeLater(next);
            }
        }

        public abstract void doStep();

        public abstract Step nextStep();
    }

    private final Step init = new Step() {
        public void doStep() {
            final JFrame f = getFrame();

            final JPanel p = new JPanel(new GridLayout(4, 1, 20, 20));

            JComboBox<String> r = getReference();
            r.setEditable(true);
            r.addItem("One");

            JComboBox<String> c = getCustom();
            c.setEditable(true);
            c.addItem("One");
            final ComboBoxEditor e = new ComboBoxEditor() {
                private JTextField text = new JTextField();

                @Override
                public Component getEditorComponent() {
                    return this.text;
                }

                @Override
                public void setItem(Object o) {
                    text.setText(o == null ? "" : o.toString());
                }

                @Override
                public Object getItem() {
                    return text.getText();
                }

                @Override
                public void selectAll() {
                    text.selectAll();
                }

                @Override
                public void addActionListener(ActionListener actionListener) {
                    text.addActionListener(actionListener);
                }

                @Override
                public void removeActionListener(ActionListener actionListener) {
                    text.removeActionListener(actionListener);
                }
            };
            c.setEditor(e);

            p.add(new JLabel("Reference"));
            p.add(r);
            p.add(c);
            p.add(new JLabel("Custom"));

            f.add(p);

            f.pack();
            f.setVisible(true);
        }

        public Step nextStep() {
            return focusRef;
        }
    };

    private class FocusStep extends Step {
        private final JComboBox<String> target;
        private final Step focusHandler;
        private final Step next;

        public FocusStep(JComboBox<String> t, Step h, Step n) {
            target = t;
            focusHandler = h;
            next = n;
        }

        public void doStep() {
            System.out.println("Request focus on " + target);
            final Component c = target.getEditor().getEditorComponent();

            c.addFocusListener(new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                    SwingUtilities.invokeLater(focusHandler);
                }

                @Override
                public void focusLost(FocusEvent e) {

                }
            });

            c.requestFocus();

        }

        public Step nextStep() {
            return next;
        }
    }


    private class CaptureStep extends Step {
        private final JComboBox<String> target;
        private BufferedImage img;
        private String fname;
        private final Step next;

        private final int timeout = 2000;

        public CaptureStep(JComboBox<String> t, String name, Step n) {
            target = t;
            next = n;
            fname = name;
        }

        public void doStep() {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException e) {
            }
            System.out.println("Capture sceeenshot of " + target);

            Rectangle bounds = target.getBounds();
            Point p = target.getLocationOnScreen();
            System.out.println("Target bounds: " + bounds);
            System.out.println("Target location: " + p);

            bounds.x = p.x;
            bounds.y = p.y;

            img = robot.createScreenCapture(bounds);

            try {
                ImageIO.write(img, "PNG", new File(fname + ".png"));
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

        }

        public Step nextStep() {
            return next;
        }
    }

    private final Step done = new Step() {
        public void doStep() {
            JFrame f = getFrame();
            if (f != null) {
                f.dispose();
            }
            System.out.println("Done");

            testDone.countDown();
        }

        public Step nextStep() {
            return null;
        }
    };

    private final CaptureStep captureCustom = new CaptureStep(getCustom(), "cb_custom", done);

    private final FocusStep focusCustom = new FocusStep(getCustom(), captureCustom, null);

    private final CaptureStep captureRef = new CaptureStep(getReference(), "cb_ref", focusCustom);

    private final FocusStep focusRef = new FocusStep(getReference(), captureRef, null);
}