1 /* 2 * Copyright (c) 2012, 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 * @bug 4697612 6244705 27 * @author Peter Zhelezniakov 28 * @library ../../regtesthelpers 29 * @build Util 30 * @run main bug4697612 31 */ 32 import java.io.*; 33 import java.awt.*; 34 import java.awt.event.*; 35 import javax.swing.*; 36 37 import javax.swing.text.BadLocationException; 38 import sun.awt.SunToolkit; 39 40 public class bug4697612 { 41 42 static final int FRAME_WIDTH = 300; 43 static final int FRAME_HEIGHT = 300; 44 static final int FONT_HEIGHT = 16; 45 private static volatile int frameHeight; 46 private static volatile int fontHeight; 47 private static JFrame frame; 48 private static JTextArea text; 49 private static JScrollPane scroller; 50 51 public static void main(String[] args) throws Throwable { 52 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 53 Robot robot = new Robot(); 54 robot.setAutoDelay(100); 55 56 SwingUtilities.invokeAndWait(new Runnable() { 57 58 @Override 59 public void run() { 60 createAndShowGUI(); 61 } 62 }); 63 64 toolkit.realSync(); 65 66 SwingUtilities.invokeAndWait(new Runnable() { 67 68 @Override 69 public void run() { 70 text.requestFocus(); 71 } 72 }); 73 74 toolkit.realSync(); 75 76 // 4697612: pressing PgDn + PgUp should not alter caret position 77 Util.hitKeys(robot, KeyEvent.VK_HOME); 78 Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN); 79 80 81 int pos0 = getTextCaretPosition(); 82 int caretHeight = getTextCaretHeight(); 83 fontHeight = FONT_HEIGHT; 84 85 // iterate two times, for different (even and odd) font height 86 for (int i = 0; i < 2; i++) { 87 88 SwingUtilities.invokeAndWait(new Runnable() { 89 90 public void run() { 91 text.setFont(text.getFont().deriveFont(fontHeight)); 92 } 93 }); 94 95 frameHeight = FRAME_HEIGHT; 96 97 for (int j = 0; j < caretHeight; j++) { 98 SwingUtilities.invokeAndWait(new Runnable() { 99 100 public void run() { 101 frame.setSize(FRAME_WIDTH, frameHeight); 102 } 103 }); 104 105 toolkit.realSync(); 106 107 Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN); 108 Util.hitKeys(robot, KeyEvent.VK_PAGE_UP); 109 toolkit.realSync(); 110 111 int pos = getTextCaretPosition(); 112 if (pos0 != pos) { 113 throw new RuntimeException("Failed 4697612: PgDn & PgUp keys scroll by different amounts"); 114 } 115 frameHeight++; 116 } 117 fontHeight++; 118 } 119 120 121 // 6244705: pressing PgDn at the very bottom should not scroll 122 LookAndFeel laf = UIManager.getLookAndFeel(); 123 if (laf.getID().equals("Aqua")) { 124 Util.hitKeys(robot, KeyEvent.VK_END); 125 } else { 126 Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_END); 127 } 128 129 toolkit.realSync(); 130 131 pos0 = getScrollerViewPosition(); 132 Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN); 133 toolkit.realSync(); 134 135 int pos = getScrollerViewPosition(); 136 137 if (pos0 != pos) { 138 throw new RuntimeException("Failed 6244705: PgDn at the bottom causes scrolling"); 139 } 140 } 141 142 private static int getTextCaretPosition() throws Exception { 143 final int[] result = new int[1]; 144 SwingUtilities.invokeAndWait(new Runnable() { 145 146 @Override 147 public void run() { 148 result[0] = text.getCaretPosition(); 149 } 150 }); 151 152 return result[0]; 153 } 154 155 private static int getTextCaretHeight() throws Exception { 156 final int[] result = new int[1]; 157 SwingUtilities.invokeAndWait(new Runnable() { 158 159 @Override 160 public void run() { 161 try { 162 int pos0 = text.getCaretPosition(); 163 Rectangle dotBounds = text.modelToView(pos0); 164 result[0] = dotBounds.height; 165 } catch (BadLocationException ex) { 166 throw new RuntimeException(ex); 167 } 168 } 169 }); 170 171 return result[0]; 172 } 173 174 private static int getScrollerViewPosition() throws Exception { 175 final int[] result = new int[1]; 176 SwingUtilities.invokeAndWait(new Runnable() { 177 178 @Override 179 public void run() { 180 result[0] = scroller.getViewport().getViewPosition().y; 181 } 182 }); 183 184 return result[0]; 185 } 186 187 private static void createAndShowGUI() { 188 frame = new JFrame(); 189 frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 190 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 191 192 text = new JTextArea(); 193 try { 194 InputStream is = 195 bug4697612.class.getResourceAsStream("bug4697612.txt"); 196 text.read(new InputStreamReader(is), null); 197 } catch (IOException e) { 198 throw new Error(e); 199 } 200 201 scroller = new JScrollPane(text); 202 203 frame.getContentPane().add(scroller); 204 205 frame.pack(); 206 frame.setVisible(true); 207 } 208 }