Path: blob/master/test/jdk/javax/swing/JTextArea/JTextAreaOrientationTest.java
41152 views
/*1* Copyright (c) 2021, 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 471067526* @key headful27* @summary Verify JTextArea.setComponentOrientation honours RIGHT_TO_LEFT orientation.28* @run main JTextAreaOrientationTest29*/30import java.awt.ComponentOrientation;31import java.awt.Graphics;32import java.awt.Rectangle;33import java.awt.image.BufferedImage;34import java.io.File;35import javax.imageio.ImageIO;36import javax.swing.JFrame;37import javax.swing.JTextArea;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;4142import static java.awt.image.BufferedImage.TYPE_INT_RGB;4344public class JTextAreaOrientationTest {4546static JFrame frame;47static Rectangle bounds;4849public static boolean compareBufferedImages(BufferedImage bufferedImage0, BufferedImage bufferedImage1) {50int width = bufferedImage0.getWidth();51int height = bufferedImage0.getHeight();5253if (width != bufferedImage1.getWidth() || height != bufferedImage1.getHeight()) {54return false;55}5657for (int y = 0; y < height; y++) {58for (int x = 0; x < width; x++) {59if (bufferedImage0.getRGB(x, y) != bufferedImage1.getRGB(x, y)) {60return false;61}62}63}6465return true;66}6768private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {69try {70UIManager.setLookAndFeel(laf.getClassName());71} catch (UnsupportedLookAndFeelException ignored) {72System.out.println("Unsupported L&F: " + laf.getClassName());73} catch (ClassNotFoundException | InstantiationException74| IllegalAccessException e) {75throw new RuntimeException(e);76}77}7879public static void main(String[] args) throws Exception {80for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {81System.out.println("Testing L&F: " + laf.getClassName());82SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));8384BufferedImage left = test(ComponentOrientation.LEFT_TO_RIGHT);85ImageIO.write(left, "png", new File("JTextAreaTest-left.png"));86BufferedImage right = test(ComponentOrientation.RIGHT_TO_LEFT);87ImageIO.write(right, "png", new File("JTextAreaTest-right.png"));88if (compareBufferedImages(left, right)) {89throw new RuntimeException("Orientation change is not effected");90}91}92}9394private static BufferedImage test(ComponentOrientation orientation) throws Exception {95SwingUtilities.invokeAndWait(() -> {96frame = new JFrame("Swing JTextArea component");97JTextArea ta = new JTextArea();98ta.setText("Swing JTextArea component");99ta.setName("jtext");100ta.setComponentOrientation(orientation);101frame.getContentPane().add(ta);102frame.setSize(300, 100);103frame.setUndecorated(true);104frame.setLocationRelativeTo(null);105frame.setVisible(true);106});107Thread.sleep(1000);108SwingUtilities.invokeAndWait(() -> {109bounds = frame.getBounds();110});111BufferedImage img = new BufferedImage(bounds.width, bounds.height, TYPE_INT_RGB);112Graphics g = img.getGraphics();113frame.paint(g);114g.dispose();115Thread.sleep(1000);116SwingUtilities.invokeAndWait(() -> frame.dispose());117return img;118}119}120121122