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