TooMuchWheelRotationEventsTest.java revision 15920:ff61a6fd0349
1/* 2 * Copyright (c) 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 24import java.awt.Color; 25import java.awt.GridBagConstraints; 26import java.awt.GridBagLayout; 27import java.awt.event.ActionEvent; 28import java.awt.event.ActionListener; 29import java.awt.event.WindowAdapter; 30import java.awt.event.WindowEvent; 31import java.util.concurrent.CountDownLatch; 32import java.util.concurrent.TimeUnit; 33import javax.swing.BoxLayout; 34import javax.swing.JButton; 35import javax.swing.JFrame; 36import javax.swing.JPanel; 37import javax.swing.JScrollPane; 38import javax.swing.JTextArea; 39import javax.swing.SwingUtilities; 40 41/* 42 * @test 43 * @bug 8166591 44 * @key headful 45 * @summary [macos 10.12] Trackpad scrolling of text on OS X 10.12 Sierra 46 * is very fast (Trackpad, Retina only) 47 * @run main/manual/othervm TooMuchWheelRotationEventsTest 48 */ 49public class TooMuchWheelRotationEventsTest { 50 51 private static volatile boolean testResult = false; 52 private static volatile CountDownLatch countDownLatch; 53 private static final String INSTRUCTIONS = "INSTRUCTIONS:\n" 54 + "Try to check the issue on Mac OS X 10.12 Sierra with trackpad" 55 + " on Retina display.\n" 56 + "\n" 57 + "If the trackpad is not supported, press PASS\n" 58 + "\n" 59 + "Use the trackpad to slightly scroll the JTextArea horizontally and vertically.\n" 60 + "If the text area is scrolled too fast press FAIL, else press PASS."; 61 62 public static void main(String args[]) throws Exception { 63 countDownLatch = new CountDownLatch(1); 64 65 SwingUtilities.invokeLater(TooMuchWheelRotationEventsTest::createUI); 66 countDownLatch.await(15, TimeUnit.MINUTES); 67 68 if (!testResult) { 69 throw new RuntimeException("Test fails!"); 70 } 71 } 72 73 private static void createUI() { 74 75 final JFrame mainFrame = new JFrame("Trackpad scrolling test"); 76 GridBagLayout layout = new GridBagLayout(); 77 JPanel mainControlPanel = new JPanel(layout); 78 JPanel resultButtonPanel = new JPanel(layout); 79 80 GridBagConstraints gbc = new GridBagConstraints(); 81 82 JPanel testPanel = createTestPanel(); 83 84 gbc.gridx = 0; 85 gbc.gridy = 0; 86 gbc.fill = GridBagConstraints.HORIZONTAL; 87 mainControlPanel.add(testPanel, gbc); 88 89 JTextArea instructionTextArea = new JTextArea(); 90 instructionTextArea.setText(INSTRUCTIONS); 91 instructionTextArea.setEditable(false); 92 instructionTextArea.setBackground(Color.white); 93 94 gbc.gridx = 0; 95 gbc.gridy = 1; 96 gbc.fill = GridBagConstraints.HORIZONTAL; 97 mainControlPanel.add(instructionTextArea, gbc); 98 99 JButton passButton = new JButton("Pass"); 100 passButton.setActionCommand("Pass"); 101 passButton.addActionListener((ActionEvent e) -> { 102 testResult = true; 103 mainFrame.dispose(); 104 countDownLatch.countDown(); 105 106 }); 107 108 JButton failButton = new JButton("Fail"); 109 failButton.setActionCommand("Fail"); 110 failButton.addActionListener(new ActionListener() { 111 @Override 112 public void actionPerformed(ActionEvent e) { 113 mainFrame.dispose(); 114 countDownLatch.countDown(); 115 } 116 }); 117 118 gbc.gridx = 0; 119 gbc.gridy = 0; 120 resultButtonPanel.add(passButton, gbc); 121 122 gbc.gridx = 1; 123 gbc.gridy = 0; 124 resultButtonPanel.add(failButton, gbc); 125 126 gbc.gridx = 0; 127 gbc.gridy = 2; 128 mainControlPanel.add(resultButtonPanel, gbc); 129 130 mainFrame.add(mainControlPanel); 131 mainFrame.pack(); 132 133 mainFrame.addWindowListener(new WindowAdapter() { 134 135 @Override 136 public void windowClosing(WindowEvent e) { 137 mainFrame.dispose(); 138 countDownLatch.countDown(); 139 } 140 }); 141 mainFrame.setVisible(true); 142 } 143 144 private static JPanel createTestPanel() { 145 JPanel panel = new JPanel(); 146 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 147 JTextArea textArea = new JTextArea(20, 20); 148 textArea.setText(getLongString()); 149 JScrollPane scrollPane = new JScrollPane(textArea); 150 panel.add(scrollPane); 151 return panel; 152 } 153 154 private static String getLongString() { 155 156 String lowCaseString = getLongString('a', 'z'); 157 String upperCaseString = getLongString('A', 'Z'); 158 String digitsString = getLongString('0', '9'); 159 160 int repeat = 30; 161 StringBuilder lowCaseBuilder = new StringBuilder(); 162 StringBuilder upperCaseBuilder = new StringBuilder(); 163 StringBuilder digitsBuilder = new StringBuilder(); 164 165 for (int i = 0; i < repeat; i++) { 166 lowCaseBuilder.append(lowCaseString).append(' '); 167 upperCaseBuilder.append(upperCaseString).append(' '); 168 digitsBuilder.append(digitsString).append(' '); 169 } 170 171 StringBuilder builder = new StringBuilder(); 172 for (int i = 0; i < 200; i++) { 173 builder.append(upperCaseBuilder).append('\n') 174 .append(lowCaseBuilder).append('\n') 175 .append(digitsBuilder).append("\n\n\n"); 176 } 177 178 return builder.toString(); 179 } 180 181 private static String getLongString(char c1, char c2) { 182 183 char[] chars = new char[c2 - c1 + 1]; 184 for (char i = c1; i <= c2; i++) { 185 chars[i - c1] = i; 186 } 187 return new String(chars); 188 } 189} 190