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