Path: blob/master/test/jdk/javax/swing/JComponent/4337267/bug4337267.java
41153 views
/*1* Copyright (c) 2009, 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 433726727* @summary test that numeric shaping works in Swing components28* @author Sergey Groznyh29* @run main bug433726730*/3132import java.awt.Component;33import java.awt.Dimension;34import java.awt.Graphics;35import java.awt.font.NumericShaper;36import java.awt.font.TextAttribute;37import java.awt.image.BufferedImage;38import javax.swing.BoxLayout;39import javax.swing.JComponent;40import javax.swing.JFrame;41import javax.swing.JLabel;42import javax.swing.JPanel;43import javax.swing.JTextArea;44import javax.swing.SwingUtilities;4546public class bug4337267 {47TestJPanel p1, p2;48TestBufferedImage i1, i2;49JComponent[] printq;50static JFrame window;51static boolean testFailed = false;5253String shaped =54"000 (E) 111 (A) \u0641\u0642\u0643 \u0662\u0662\u0662 (E) 333";55String text = "000 (E) 111 (A) \u0641\u0642\u0643 222 (E) 333";5657void run() {58initUI();59testTextComponent();60testNonTextComponentHTML();61testNonTextComponentPlain();6263}6465void initUI() {66window = new JFrame("bug4337267");67window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);68window.setSize(800, 600);69Component content = createContentPane();70window.add(content);71window.setVisible(true);72}7374void fail(String message) {75testFailed = true;76throw new RuntimeException(message);77}7879void assertEquals(Object o1, Object o2) {80if ((o1 == null) && (o2 != null)) {81fail("Expected null, got " + o2);82} else if ((o1 != null) && (o2 == null)) {83fail("Expected " + o1 + ", got null");84} else if (!o1.equals(o2)) {85fail("Expected " + o1 + ", got " + o2);86}87}8889void testTextComponent() {90System.out.println("testTextComponent:");91JTextArea area1 = new JTextArea();92injectComponent(p1, area1, false);93area1.setText(shaped);94JTextArea area2 = new JTextArea();95injectComponent(p2, area2, true);96area2.setText(text);97window.repaint();98printq = new JComponent[] { area1, area2 };99printComponent(printq[0], i1);100printComponent(printq[1], i2);101assertEquals(p1.image, p2.image);102assertEquals(i1, i2);103}104105void testNonTextComponentHTML() {106System.out.println("testNonTextComponentHTML:");107JLabel label1 = new JLabel();108injectComponent(p1, label1, false);109label1.setText("<html>" + shaped);110JLabel label2 = new JLabel();111injectComponent(p2, label2, true);112label2.setText("<html>" + text);113window.repaint();114printq = new JComponent[] { label1, label2 };115printComponent(printq[0], i1);116printComponent(printq[1], i2);117assertEquals(p1.image, p2.image);118assertEquals(i1, i2);119}120121void testNonTextComponentPlain() {122System.out.println("testNonTextComponentPlain:");123JLabel label1 = new JLabel();124injectComponent(p1, label1, false);125label1.setText(shaped);126JLabel label2 = new JLabel();127injectComponent(p2, label2, true);128label2.setText(text);129window.repaint();130printq = new JComponent[] { label1, label2 };131printComponent(printq[0], i1);132printComponent(printq[1], i2);133assertEquals(p1.image, p2.image);134assertEquals(i1, i2);135}136137void setShaping(JComponent c) {138c.putClientProperty(TextAttribute.NUMERIC_SHAPING,139NumericShaper.getContextualShaper(NumericShaper.ARABIC));140}141142void injectComponent(JComponent p, JComponent c, boolean shape) {143if (shape) {144setShaping(c);145}146p.removeAll();147p.add(c);148}149150void printComponent(JComponent c, TestBufferedImage i) {151Graphics g = i.getGraphics();152g.setColor(c.getBackground());153g.fillRect(0, 0, i.getWidth(), i.getHeight());154c.print(g);155}156157Component createContentPane() {158Dimension size = new Dimension(500, 100);159i1 = new TestBufferedImage(size.width, size.height,160BufferedImage.TYPE_INT_ARGB);161i2 = new TestBufferedImage(size.width, size.height,162BufferedImage.TYPE_INT_ARGB);163p1 = new TestJPanel();164p1.setPreferredSize(size);165p2 = new TestJPanel();166p2.setPreferredSize(size);167JPanel panel = new JPanel();168panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));169panel.add(p1);170panel.add(p2);171172return panel;173}174175static class TestBufferedImage extends BufferedImage {176int MAX_GLITCHES = 0;177178TestBufferedImage(int width, int height, int imageType) {179super(width, height, imageType);180}181182@Override183public boolean equals(Object other) {184if (! (other instanceof TestBufferedImage)) {185return false;186}187TestBufferedImage image2 = (TestBufferedImage) other;188int width = getWidth();189int height = getHeight();190if ((image2.getWidth() != width) || (image2.getHeight() != height)) {191return false;192}193int glitches = 0;194for (int x = 0; x < width; x++) {195for (int y = 0; y < height; y++) {196int rgb1 = getRGB(x, y);197int rgb2 = image2.getRGB(x, y);198if (rgb1 != rgb2) {199//System.out.println(x+" "+y+" "+rgb1+" "+rgb2);200glitches++;201}202}203}204return glitches <= MAX_GLITCHES;205}206}207208static class TestJPanel extends JPanel {209TestBufferedImage image = createImage(new Dimension(1, 1));210211TestBufferedImage createImage(Dimension d) {212return new TestBufferedImage(d.width, d.height,213BufferedImage.TYPE_INT_ARGB);214}215216public void setPreferredSize(Dimension size) {217super.setPreferredSize(size);218image = createImage(size);219}220221public void paint(Graphics g) {222Graphics g0 = image.getGraphics();223super.paint(g0);224g.drawImage(image, 0, 0, this);225} }226227228229public static void main(String[] args) throws Exception {230try {231final bug4337267 test = new bug4337267();232SwingUtilities.invokeAndWait(new Runnable() {233public void run() {234test.run();235}236});237238if (testFailed) {239throw new RuntimeException("FAIL");240}241242System.out.println("OK");243} finally {244if (window != null) SwingUtilities.invokeAndWait(() -> window.dispose());245}246}247}248249250