Path: blob/master/test/jdk/javax/swing/JTextArea/4697612/bug4697612.java
41153 views
/*1* Copyright (c) 2012, 2017, 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* @key headful26* @bug 4697612 6244705 719058127* @run main bug469761228*/29import java.awt.Rectangle;30import java.awt.Dimension;31import java.io.InputStream;32import java.awt.Robot;33import java.awt.event.KeyEvent;34import java.io.IOException;35import java.io.InputStreamReader;36import javax.swing.JFrame;37import javax.swing.JScrollPane;38import javax.swing.JTextArea;39import javax.swing.LookAndFeel;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.UnsupportedLookAndFeelException;43import javax.swing.text.BadLocationException;4445public class bug4697612 {4647static final int FRAME_WIDTH = 300;48static final int FRAME_HEIGHT = 300;49static final int FONT_HEIGHT = 16;50private static volatile int frameHeight;51private static volatile int fontHeight;52private static JFrame frame;53private static JTextArea text;54private static JScrollPane scroller;55private static Robot robot;5657private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {58try {59UIManager.setLookAndFeel(laf.getClassName());60} catch (ClassNotFoundException | InstantiationException |61UnsupportedLookAndFeelException | IllegalAccessException e) {62throw new RuntimeException(e);63}64}6566public static void main(String[] args) throws Throwable {67robot = new Robot();68robot.setAutoDelay(100);69for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {70try {71SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));72System.out.println("Test for LookAndFeel " + laf.getClassName());73new bug4697612();74System.out.println("Test passed for LookAndFeel " + laf.getClassName());75} catch (Exception e) {76throw new RuntimeException(e);77}78}79}8081public bug4697612() throws Exception {82try {83SwingUtilities.invokeAndWait(new Runnable() {84@Override85public void run() {86createAndShowGUI();87}88});89robot.waitForIdle();90SwingUtilities.invokeAndWait(new Runnable() {91@Override92public void run() {93text.requestFocus();94}95});96robot.waitForIdle();9798// 4697612: pressing PgDn + PgUp should not alter caret position99robot.keyPress(KeyEvent.VK_HOME);100robot.keyRelease(KeyEvent.VK_HOME);101robot.keyPress(KeyEvent.VK_PAGE_DOWN);102robot.keyRelease(KeyEvent.VK_PAGE_DOWN);103robot.waitForIdle();104105int pos0 = getTextCaretPosition();106int caretHeight = getTextCaretHeight();107fontHeight = FONT_HEIGHT;108109// iterate two times, for different (even and odd) font height110for (int i = 0; i < 2; i++) {111112SwingUtilities.invokeAndWait(new Runnable() {113public void run() {114text.setFont(text.getFont().deriveFont(fontHeight));115}116});117frameHeight = FRAME_HEIGHT;118119for (int j = 0; j < caretHeight; j++) {120121SwingUtilities.invokeAndWait(new Runnable() {122public void run() {123frame.setSize(FRAME_WIDTH, frameHeight);124}125});126robot.waitForIdle();127robot.keyPress(KeyEvent.VK_PAGE_DOWN);128robot.keyRelease(KeyEvent.VK_PAGE_DOWN);129robot.keyPress(KeyEvent.VK_PAGE_UP);130robot.keyRelease(KeyEvent.VK_PAGE_UP);131robot.waitForIdle();132133int pos = getTextCaretPosition();134if (pos0 != pos) {135throw new RuntimeException("Failed 4697612: PgDn & PgUp keys scroll by different amounts");136}137frameHeight++;138}139fontHeight++;140}141142// 6244705: pressing PgDn at the very bottom should not scroll143LookAndFeel laf = UIManager.getLookAndFeel();144if (laf.getID().equals("Aqua")) {145robot.keyPress(KeyEvent.VK_END);146robot.keyRelease(KeyEvent.VK_END);147} else {148robot.keyPress(KeyEvent.VK_CONTROL);149robot.keyPress(KeyEvent.VK_END);150robot.keyRelease(KeyEvent.VK_END);151robot.keyRelease(KeyEvent.VK_CONTROL);152}153robot.waitForIdle();154robot.delay(1000);155156pos0 = getScrollerViewPosition();157robot.keyPress(KeyEvent.VK_PAGE_DOWN);158robot.keyRelease(KeyEvent.VK_PAGE_DOWN);159robot.waitForIdle();160161int pos = getScrollerViewPosition();162163if (pos0 != pos) {164System.out.println("pos0 " + pos0 + " pos " + pos);165throw new RuntimeException("Failed 6244705: PgDn at the bottom causes scrolling");166}167} finally {168SwingUtilities.invokeAndWait(() -> frame.dispose());169}170}171172private static int getTextCaretPosition() throws Exception {173final int[] result = new int[1];174SwingUtilities.invokeAndWait(new Runnable() {175176@Override177public void run() {178result[0] = text.getCaretPosition();179}180});181182return result[0];183}184185private static int getTextCaretHeight() throws Exception {186final int[] result = new int[1];187SwingUtilities.invokeAndWait(new Runnable() {188189@Override190public void run() {191try {192int pos0 = text.getCaretPosition();193Rectangle dotBounds = text.modelToView(pos0);194result[0] = dotBounds.height;195} catch (BadLocationException ex) {196throw new RuntimeException(ex);197}198}199});200201return result[0];202}203204private static int getScrollerViewPosition() throws Exception {205final int[] result = new int[1];206SwingUtilities.invokeAndWait(new Runnable() {207208@Override209public void run() {210result[0] = scroller.getViewport().getViewPosition().y;211}212});213214return result[0];215}216217private static void createAndShowGUI() {218frame = new JFrame();219frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);220frame.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));221frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);222223text = new JTextArea();224try {225InputStream is =226bug4697612.class.getResourceAsStream("bug4697612.txt");227text.read(new InputStreamReader(is), null);228} catch (IOException e) {229throw new Error(e);230}231232scroller = new JScrollPane(text);233234frame.getContentPane().add(scroller);235236frame.pack();237frame.setVisible(true);238}239}240241242