1 /*
   2  * Copyright (c) 2011, 2016, 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 /*
  25   @test
  26   @key headful
  27   @bug 7050935
  28   @summary closed/java/awt/Choice/WheelEventsConsumed/WheelEventsConsumed.html fails on win32
  29   @library ../../regtesthelpers
  30   @author Oleg Pekhovskiy: area=awt-choice
  31   @build Util
  32   @run main ChoiceMouseWheelTest
  33 */
  34 
  35 import test.java.awt.regtesthelpers.Util;
  36 
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 
  40 public class ChoiceMouseWheelTest extends Frame {
  41 
  42     private volatile boolean itemChanged = false;
  43     private volatile boolean wheelMoved = false;
  44     private volatile boolean frameExited = false;
  45 
  46     public static void main(String[] args) {
  47         new ChoiceMouseWheelTest();
  48     }
  49 
  50     ChoiceMouseWheelTest() {
  51         super("ChoiceMouseWheelTest");
  52         setLayout(new FlowLayout());
  53 
  54         Choice choice = new Choice();
  55 
  56         addWindowListener(new WindowAdapter() {
  57             @Override
  58             public void windowClosing(WindowEvent e) {
  59                 System.exit(0);
  60             }
  61         });
  62 
  63         for(Integer i = 0; i < 50; i++) {
  64             choice.add(i.toString());
  65         }
  66 
  67         choice.addItemListener(new ItemListener() {
  68             public void itemStateChanged(ItemEvent e) {
  69                 itemChanged = true;
  70             }
  71         });
  72         choice.addMouseWheelListener(new MouseWheelListener() {
  73             public void mouseWheelMoved(MouseWheelEvent e) {
  74                 wheelMoved = true;
  75             }
  76         });
  77 
  78         addMouseListener(new MouseAdapter() {
  79             @Override
  80             public void mouseExited(MouseEvent e) {
  81                 frameExited = true;
  82             }
  83         });
  84 
  85         add(choice);
  86         setSize(200, 300);
  87         setVisible(true);
  88         toFront();
  89 
  90         try {
  91             Robot robot = new Robot();
  92             robot.setAutoDelay(20);
  93             Util.waitForIdle(robot);
  94 
  95             Point pt = choice.getLocationOnScreen();
  96             Dimension size = choice.getSize();
  97             int x = pt.x + size.width / 3;
  98             robot.mouseMove(x, pt.y + size.height / 2);
  99 
 100             // Test mouse wheel over the choice
 101             String name = Toolkit.getDefaultToolkit().getClass().getName();
 102 
 103             // mouse wheel doesn't work for the choice on X11 and Mac, so skip it
 104             if(!name.equals("sun.awt.X11.XToolkit")
 105                && !name.equals("sun.lwawt.macosx.LWCToolkit")) {
 106                 robot.mouseWheel(1);
 107                 Util.waitForIdle(robot);
 108 
 109                 if(!wheelMoved || !itemChanged) {
 110                     throw new RuntimeException("Mouse Wheel over the choice failed!");
 111                 }
 112             }
 113 
 114             // Test mouse wheel over the drop-down list
 115             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 116             Util.waitForIdle(robot);
 117             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 118             Util.waitForIdle(robot);
 119 
 120             int y = getLocationOnScreen().y + getSize().height;
 121             while(!frameExited && y >= 0) { // move to the bottom of drop-down list
 122                 robot.mouseMove(x, --y);
 123                 Util.waitForIdle(robot);
 124             }
 125 
 126             if(x < 0) {
 127                 throw new RuntimeException("Could not enter drop-down list!");
 128             }
 129 
 130             y -= choice.getHeight() / 2;
 131             robot.mouseMove(x, y); // move to the last visible item in the drop-down list
 132             Util.waitForIdle(robot);
 133 
 134             robot.mouseWheel(choice.getItemCount()); // wheel to the last item
 135             Util.waitForIdle(robot);
 136 
 137             // click the last item
 138             itemChanged = false;
 139             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 140             Util.waitForIdle(robot);
 141             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 142             Util.waitForIdle(robot);
 143 
 144             if(!itemChanged || choice.getSelectedIndex() != choice.getItemCount() - 1) {
 145                 throw new RuntimeException("Mouse Wheel scroll position error!");
 146             }
 147 
 148             dispose();
 149         } catch (AWTException e) {
 150             throw new RuntimeException("AWTException occurred - problem creating robot!");
 151         }
 152     }
 153 }
 154