1 /* 2 * Copyright (c) 1998, 2017, 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 import java.awt.*; 25 import java.awt.event.*; 26 27 /** 28 * @test 29 * @key headful 30 * @bug 4449139 31 * @summary test MouseWheelEvent generation by Scrollbar component 32 */ 33 34 public final class ScrollbarMouseWheelTest 35 implements MouseWheelListener, WindowListener { 36 37 final static String tk = Toolkit.getDefaultToolkit().getClass().getName(); 38 final static int REPS = 5; 39 // There is a bug on Windows: 4616935. 40 // Wheel events comes to every component in the hierarchy so we should 41 // check a platform. 42 // There are two scrollbars within one Panel and both accept 5 clicks, so 43 // Panel would accept 5*2 clicks on Windows. 44 final static int PANEL_REPS = tk.equals("sun.awt.windows.WToolkit")? REPS * 2: REPS; 45 46 Scrollbar sb1; 47 Scrollbar sb2; 48 Panel pnl; 49 class Sema { 50 boolean flag; 51 boolean getVal() { return flag;} 52 void setVal(boolean b) { flag = b;} 53 } 54 Sema sema = new Sema(); 55 56 Robot robot; 57 58 int sb1upevents, sb2upevents, pnlupevents; 59 int sb1downevents, sb2downevents, pnldownevents; 60 61 public static void main(final String[] args) { 62 new ScrollbarMouseWheelTest().test(); 63 } 64 65 public void test() { 66 // Move mouse to upper-right area 67 try { 68 robot = new Robot(); 69 } catch (AWTException e) { 70 System.out.println("Problem creating Robot. FAIL."); 71 throw new RuntimeException("Problem creating Robot. FAIL."); 72 73 } 74 75 robot.setAutoDelay(500); 76 robot.setAutoWaitForIdle(true); 77 78 // Show test Frame 79 Frame frame = new Frame("ScrollbarMouseWheelTest"); 80 frame.addWindowListener(this); 81 pnl = new Panel(); 82 pnl.setLayout(new GridLayout(1, 2)); 83 pnl.addMouseWheelListener(this); 84 sb1 = new Scrollbar(); 85 sb1.addMouseWheelListener(this); 86 pnl.add(sb1); 87 sb2 = new Scrollbar(); 88 pnl.add(sb2); 89 frame.add(pnl); 90 frame.setSize(200, 400); 91 frame.setLocationRelativeTo(null); 92 frame.setVisible(true); 93 frame.toFront(); 94 95 // When Frame is active, start testing (handled in windowActivated()) 96 while (true) { 97 synchronized (sema) { 98 if (sema.getVal()) { 99 break; 100 } 101 } 102 } 103 // up on sb1 104 testComp(sb1, true); 105 // down on sb1 106 testComp(sb1, false); 107 // up on sb2 108 testComp(sb2, true); 109 // down on sb2 110 testComp(sb2, false); 111 frame.dispose(); 112 System.out.println("Test done."); 113 if (sb1upevents == REPS && 114 sb2upevents == 0 && 115 pnlupevents == PANEL_REPS && 116 sb1downevents == REPS && 117 sb2downevents == 0 && 118 pnldownevents == PANEL_REPS) { 119 System.out.println("PASSED."); 120 } else { 121 System.out.println("Test Failed:" + 122 "\n\tsb1upevents =" + sb1upevents + 123 "\n\tsb2upevents = " + sb2upevents + 124 "\n\tpnlupevents = " + pnlupevents + 125 "\n\tsb1downevents =" + sb1downevents + 126 "\n\tsb2downevents = " + sb2downevents + 127 "\n\tpnldownevents = " + pnldownevents); 128 throw new RuntimeException("Test FAILED."); 129 } 130 } 131 132 public void testComp(Component comp, boolean up) { 133 Point loc = comp.getLocationOnScreen(); 134 robot.mouseMove(loc.x + comp.getWidth() / 2, 135 loc.y + comp.getHeight() / 2); 136 for (int loop = 0; loop < REPS; loop++) { 137 System.out.println("Robot.mouseWheel() on " + comp.getName()); 138 robot.mouseWheel(up ? -1 : 1); 139 } 140 } 141 142 public void mouseWheelMoved(MouseWheelEvent mwe) { 143 Component src = mwe.getComponent(); 144 System.out.println("mouseWheelMoved() on " + src.getName()); 145 if (mwe.getWheelRotation() == -1) { 146 if (src == sb1) { 147 sb1upevents++; 148 } else if (src == sb2) { 149 sb2upevents++; 150 } else if (src == pnl) { 151 pnlupevents++; 152 } else { 153 System.out.println("weird source component"); 154 } 155 } else if (mwe.getWheelRotation() == 1) { 156 if (src == sb1) { 157 sb1downevents++; 158 } else if (src == sb2) { 159 sb2downevents++; 160 } else if (src == pnl) { 161 pnldownevents++; 162 } else { 163 System.out.println("weird source component"); 164 } 165 } else { 166 System.out.println("weird wheel rotation"); 167 } 168 } 169 170 public void windowActivated(WindowEvent we) { 171 synchronized (sema) { 172 sema.setVal(true); 173 } 174 } 175 176 public void windowClosed(WindowEvent we) {} 177 public void windowClosing(WindowEvent we) {} 178 public void windowDeactivated(WindowEvent we) {} 179 public void windowDeiconified(WindowEvent we) {} 180 public void windowIconified(WindowEvent we) {} 181 public void windowOpened(WindowEvent we) {} 182 }// class ScrollbarMouseWheelTest