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