Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTextArea/4697612/bug4697612.java
38853 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4697612 624470526* @author Peter Zhelezniakov27* @library ../../regtesthelpers28* @build Util29* @run main bug469761230*/31import java.io.*;32import java.awt.*;33import java.awt.event.*;34import javax.swing.*;3536import javax.swing.text.BadLocationException;3738public class bug4697612 {3940static final int FRAME_WIDTH = 300;41static final int FRAME_HEIGHT = 300;42static final int FONT_HEIGHT = 16;43private static volatile int frameHeight;44private static volatile int fontHeight;45private static JFrame frame;46private static JTextArea text;47private static JScrollPane scroller;4849public static void main(String[] args) throws Throwable {50Robot robot = new Robot();51robot.setAutoDelay(100);5253SwingUtilities.invokeAndWait(new Runnable() {5455@Override56public void run() {57createAndShowGUI();58}59});6061robot.waitForIdle();6263SwingUtilities.invokeAndWait(new Runnable() {6465@Override66public void run() {67text.requestFocus();68}69});7071robot.waitForIdle();7273// 4697612: pressing PgDn + PgUp should not alter caret position74Util.hitKeys(robot, KeyEvent.VK_HOME);75Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);767778int pos0 = getTextCaretPosition();79int caretHeight = getTextCaretHeight();80fontHeight = FONT_HEIGHT;8182// iterate two times, for different (even and odd) font height83for (int i = 0; i < 2; i++) {8485SwingUtilities.invokeAndWait(new Runnable() {8687public void run() {88text.setFont(text.getFont().deriveFont(fontHeight));89}90});9192frameHeight = FRAME_HEIGHT;9394for (int j = 0; j < caretHeight; j++) {95SwingUtilities.invokeAndWait(new Runnable() {9697public void run() {98frame.setSize(FRAME_WIDTH, frameHeight);99}100});101102robot.waitForIdle();103104Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);105Util.hitKeys(robot, KeyEvent.VK_PAGE_UP);106robot.waitForIdle();107108int pos = getTextCaretPosition();109if (pos0 != pos) {110throw new RuntimeException("Failed 4697612: PgDn & PgUp keys scroll by different amounts");111}112frameHeight++;113}114fontHeight++;115}116117118// 6244705: pressing PgDn at the very bottom should not scroll119LookAndFeel laf = UIManager.getLookAndFeel();120if (laf.getID().equals("Aqua")) {121Util.hitKeys(robot, KeyEvent.VK_END);122} else {123Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_END);124}125126robot.waitForIdle();127128pos0 = getScrollerViewPosition();129Util.hitKeys(robot, KeyEvent.VK_PAGE_DOWN);130robot.waitForIdle();131132int pos = getScrollerViewPosition();133134if (pos0 != pos) {135throw new RuntimeException("Failed 6244705: PgDn at the bottom causes scrolling");136}137}138139private static int getTextCaretPosition() throws Exception {140final int[] result = new int[1];141SwingUtilities.invokeAndWait(new Runnable() {142143@Override144public void run() {145result[0] = text.getCaretPosition();146}147});148149return result[0];150}151152private static int getTextCaretHeight() throws Exception {153final int[] result = new int[1];154SwingUtilities.invokeAndWait(new Runnable() {155156@Override157public void run() {158try {159int pos0 = text.getCaretPosition();160Rectangle dotBounds = text.modelToView(pos0);161result[0] = dotBounds.height;162} catch (BadLocationException ex) {163throw new RuntimeException(ex);164}165}166});167168return result[0];169}170171private static int getScrollerViewPosition() throws Exception {172final int[] result = new int[1];173SwingUtilities.invokeAndWait(new Runnable() {174175@Override176public void run() {177result[0] = scroller.getViewport().getViewPosition().y;178}179});180181return result[0];182}183184private static void createAndShowGUI() {185frame = new JFrame();186frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);187frame.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));188frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);189190text = new JTextArea();191try {192InputStream is =193bug4697612.class.getResourceAsStream("bug4697612.txt");194text.read(new InputStreamReader(is), null);195} catch (IOException e) {196throw new Error(e);197}198199scroller = new JScrollPane(text);200201frame.getContentPane().add(scroller);202203frame.pack();204frame.setVisible(true);205}206}207208209