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/*
24  @test
25  @key headful
26  @bug       6240202
27  @summary   Tests that non-focusable List in a Window generates ActionEvent.
28  @author    anton.tarasov@sun.com: area=awt-list
29  @run       main NofocusListDblClickTest
30*/
31
32import java.awt.*;
33import java.awt.event.*;
34import java.util.concurrent.atomic.AtomicInteger;
35import javax.swing.SwingUtilities;
36
37public class NofocusListDblClickTest {
38    static final int EXPECTED_ACTION_COUNT = 2;
39    static Robot robot;
40    static final AtomicInteger actionPerformed = new AtomicInteger(0);
41    static List lst;
42
43    public static void main(String[] args) throws Exception {
44        SwingUtilities.invokeAndWait(new Runnable() {
45            public void run() {
46                createAndShowGUI();
47            }
48        });
49        robot = new Robot();
50        robot.setAutoDelay(50);
51        robot.waitForIdle();
52        Thread.sleep(1000);
53
54        // ACTION_PERFORMED event happens only on even clicks
55        clickTwiceOn(lst);
56        Thread.sleep(500);
57        clickTwiceOn(lst);
58        robot.waitForIdle();
59        Thread.sleep(1000);
60
61        synchronized (actionPerformed) {
62            if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {
63                try {
64                    actionPerformed.wait(3000);
65                } catch (InterruptedException e) {
66                    System.out.println("Interrupted unexpectedly!");
67                    throw new RuntimeException(e);
68                }
69            }
70        }
71
72        if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {
73            System.out.println("No ActionEvent was generated. " + actionPerformed.get());
74            throw new RuntimeException("Test failed!");
75        }
76
77        System.out.println("Test passed.");
78    }
79
80    public static void createAndShowGUI() {
81        Frame f = new Frame("Owner");
82        Window w = new Window(f);
83        lst = new List(3, true);
84        //this.setLayout (new BorderLayout ());
85        f.setBounds(800, 0, 100, 100);
86        w.setLocation(800, 150);
87
88        lst.add("item 0");
89        lst.add("item 1");
90        lst.add("item 2");
91
92        lst.setFocusable(false);
93
94        lst.addActionListener(new ActionListener() {
95                public void actionPerformed(ActionEvent e) {
96                    System.out.println(e.toString());
97                    synchronized (actionPerformed) {
98                        if (EXPECTED_ACTION_COUNT == actionPerformed.incrementAndGet()) {
99                            actionPerformed.notifyAll();
100                        }
101                    }
102                }
103            });
104
105        w.add(lst);
106        w.pack();
107
108        f.setVisible(true);
109        w.setVisible(true);
110    }
111
112    static void clickTwiceOn(Component c) throws Exception {
113        Point p = c.getLocationOnScreen();
114        Dimension d = c.getSize();
115
116        if (c instanceof Frame) {
117            robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
118        } else {
119            robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
120        }
121
122        robot.mousePress(InputEvent.BUTTON1_MASK);
123        robot.mouseRelease(InputEvent.BUTTON1_MASK);
124        Thread.sleep(20);
125        robot.mousePress(InputEvent.BUTTON1_MASK);
126        robot.mouseRelease(InputEvent.BUTTON1_MASK);
127    }
128}
129