0N/A/*
2362N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/*
0N/A * @test
0N/A * @bug 4697612 6244705
0N/A * @author Peter Zhelezniakov
0N/A * @library ../../regtesthelpers
0N/A * @build Util
0N/A * @run main bug4697612
0N/A */
0N/Aimport java.io.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport javax.swing.*;
0N/A
0N/Aimport javax.swing.text.BadLocationException;
0N/Aimport sun.awt.SunToolkit;
0N/A
0N/Apublic class bug4697612 {
0N/A
0N/A static final int FRAME_WIDTH = 300;
0N/A static final int FRAME_HEIGHT = 300;
0N/A static final int FONT_HEIGHT = 16;
0N/A private static volatile int frameHeight;
0N/A private static volatile int fontHeight;
0N/A private static JFrame frame;
0N/A private static JTextArea text;
0N/A private static JScrollPane scroller;
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
0N/A Robot robot = new Robot();
0N/A robot.setAutoDelay(100);
0N/A
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A createAndShowGUI();
0N/A }
0N/A });
0N/A
0N/A toolkit.realSync();
0N/A
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A text.requestFocus();
0N/A }
0N/A });
0N/A
0N/A toolkit.realSync();
0N/A
0N/A // 4697612: pressing PgDn + PgUp should not alter caret position
0N/A Util.hitKeys(robot, KeyEvent.VK_HOME);
0N/A Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);
0N/A
0N/A
0N/A int pos0 = getTextCaretPosition();
0N/A int caretHeight = getTextCaretHeight();
0N/A fontHeight = FONT_HEIGHT;
0N/A
0N/A // iterate two times, for different (even and odd) font height
0N/A for (int i = 0; i < 2; i++) {
0N/A
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A public void run() {
0N/A text.setFont(text.getFont().deriveFont(fontHeight));
0N/A }
0N/A });
0N/A
0N/A frameHeight = FRAME_HEIGHT;
0N/A
0N/A for (int j = 0; j < caretHeight; j++) {
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A public void run() {
0N/A frame.setSize(FRAME_WIDTH, frameHeight);
0N/A }
0N/A });
0N/A
0N/A toolkit.realSync();
0N/A
0N/A Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);
0N/A Util.hitKeys(robot, KeyEvent.VK_PAGE_UP);
0N/A toolkit.realSync();
0N/A
0N/A int pos = getTextCaretPosition();
0N/A if (pos0 != pos) {
0N/A throw new RuntimeException("Failed 4697612: PgDn & PgUp keys scroll by different amounts");
0N/A }
0N/A frameHeight++;
0N/A }
0N/A fontHeight++;
0N/A }
0N/A
0N/A
0N/A // 6244705: pressing PgDn at the very bottom should not scroll
0N/A LookAndFeel laf = UIManager.getLookAndFeel();
0N/A if (laf.getID().equals("Aqua")) {
0N/A Util.hitKeys(robot, KeyEvent.VK_END);
0N/A } else {
0N/A Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_END);
0N/A }
0N/A
0N/A toolkit.realSync();
0N/A
0N/A pos0 = getScrollerViewPosition();
0N/A Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);
0N/A toolkit.realSync();
0N/A
0N/A int pos = getScrollerViewPosition();
0N/A
0N/A if (pos0 != pos) {
0N/A throw new RuntimeException("Failed 6244705: PgDn at the bottom causes scrolling");
0N/A }
0N/A }
0N/A
0N/A private static int getTextCaretPosition() throws Exception {
0N/A final int[] result = new int[1];
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A result[0] = text.getCaretPosition();
0N/A }
0N/A });
0N/A
0N/A return result[0];
0N/A }
0N/A
0N/A private static int getTextCaretHeight() throws Exception {
0N/A final int[] result = new int[1];
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A try {
0N/A int pos0 = text.getCaretPosition();
0N/A Rectangle dotBounds = text.modelToView(pos0);
0N/A result[0] = dotBounds.height;
0N/A } catch (BadLocationException ex) {
0N/A throw new RuntimeException(ex);
0N/A }
0N/A }
0N/A });
0N/A
0N/A return result[0];
0N/A }
0N/A
0N/A private static int getScrollerViewPosition() throws Exception {
0N/A final int[] result = new int[1];
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A result[0] = scroller.getViewport().getViewPosition().y;
0N/A }
0N/A });
0N/A
0N/A return result[0];
0N/A }
0N/A
0N/A private static void createAndShowGUI() {
0N/A frame = new JFrame();
0N/A frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
0N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0N/A
0N/A text = new JTextArea();
0N/A try {
0N/A InputStream is =
0N/A bug4697612.class.getResourceAsStream("bug4697612.txt");
0N/A text.read(new InputStreamReader(is), null);
0N/A } catch (IOException e) {
0N/A throw new Error(e);
0N/A }
0N/A
0N/A scroller = new JScrollPane(text);
0N/A
0N/A frame.getContentPane().add(scroller);
0N/A
0N/A frame.pack();
0N/A frame.setVisible(true);
0N/A }
0N/A}
0N/A