Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComponent/4337267/bug4337267.java
38918 views
/*1* Copyright (c) 2009, 2013, 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 433726726* @summary test that numeric shaping works in Swing components27* @author Sergey Groznyh28* @run main bug433726729*/3031import java.awt.Component;32import java.awt.Dimension;33import java.awt.Graphics;34import java.awt.font.NumericShaper;35import java.awt.font.TextAttribute;36import java.awt.image.BufferedImage;37import javax.swing.BoxLayout;38import javax.swing.JComponent;39import javax.swing.JFrame;40import javax.swing.JLabel;41import javax.swing.JPanel;42import javax.swing.JTextArea;43import javax.swing.SwingUtilities;4445public class bug4337267 {46TestJPanel p1, p2;47TestBufferedImage i1, i2;48JComponent[] printq;49JFrame window;50static boolean testFailed = false;51static boolean done = 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();6263doneTask();64}6566void initUI() {67window = new JFrame("bug4337267");68window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);69window.setSize(800, 600);70Component content = createContentPane();71window.add(content);72window.setVisible(true);73}7475Runnable printComponents = new Runnable() {76public void run() {77printComponent(printq[0], i1);78printComponent(printq[1], i2);79}80};8182Runnable compareRasters = new Runnable() {83public void run() {84assertEquals(p1.image, p2.image);85assertEquals(i1, i2);86}87};8889void doneTask() {90final Object monitor = this;91SwingUtilities.invokeLater(new Runnable() {92public void run() {93done = true;94synchronized(monitor) {95monitor.notify();96}97}98});99}100101102void fail(String message) {103testFailed = true;104throw new RuntimeException(message);105}106107void assertEquals(Object o1, Object o2) {108if ((o1 == null) && (o2 != null)) {109fail("Expected null, got " + o2);110} else if ((o1 != null) && (o2 == null)) {111fail("Expected " + o1 + ", got null");112} else if (!o1.equals(o2)) {113fail("Expected " + o1 + ", got " + o2);114}115}116117void testTextComponent() {118System.out.println("testTextComponent:");119JTextArea area1 = new JTextArea();120injectComponent(p1, area1, false);121area1.setText(shaped);122JTextArea area2 = new JTextArea();123injectComponent(p2, area2, true);124area2.setText(text);125window.repaint();126printq = new JComponent[] { area1, area2 };127SwingUtilities.invokeLater(printComponents);128SwingUtilities.invokeLater(compareRasters);129}130131void testNonTextComponentHTML() {132System.out.println("testNonTextComponentHTML:");133JLabel label1 = new JLabel();134injectComponent(p1, label1, false);135label1.setText("<html>" + shaped);136JLabel label2 = new JLabel();137injectComponent(p2, label2, true);138label2.setText("<html>" + text);139window.repaint();140printq = new JComponent[] { label1, label2 };141SwingUtilities.invokeLater(printComponents);142SwingUtilities.invokeLater(compareRasters);143}144145void testNonTextComponentPlain() {146System.out.println("testNonTextComponentHTML:");147JLabel label1 = new JLabel();148injectComponent(p1, label1, false);149label1.setText(shaped);150JLabel label2 = new JLabel();151injectComponent(p2, label2, true);152label2.setText(text);153window.repaint();154printq = new JComponent[] { label1, label2 };155SwingUtilities.invokeLater(printComponents);156SwingUtilities.invokeLater(compareRasters);157}158159void setShaping(JComponent c) {160c.putClientProperty(TextAttribute.NUMERIC_SHAPING,161NumericShaper.getContextualShaper(NumericShaper.ARABIC));162}163164void injectComponent(JComponent p, JComponent c, boolean shape) {165if (shape) {166setShaping(c);167}168p.removeAll();169p.add(c);170}171172void printComponent(JComponent c, TestBufferedImage i) {173Graphics g = i.getGraphics();174g.setColor(c.getBackground());175g.fillRect(0, 0, i.getWidth(), i.getHeight());176c.print(g);177}178179Component createContentPane() {180Dimension size = new Dimension(500, 100);181i1 = new TestBufferedImage(size.width, size.height,182BufferedImage.TYPE_INT_ARGB);183i2 = new TestBufferedImage(size.width, size.height,184BufferedImage.TYPE_INT_ARGB);185p1 = new TestJPanel();186p1.setPreferredSize(size);187p2 = new TestJPanel();188p2.setPreferredSize(size);189JPanel panel = new JPanel();190panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));191panel.add(p1);192panel.add(p2);193194return panel;195}196197static class TestBufferedImage extends BufferedImage {198int MAX_GLITCHES = 0;199200TestBufferedImage(int width, int height, int imageType) {201super(width, height, imageType);202}203204@Override205public boolean equals(Object other) {206if (! (other instanceof TestBufferedImage)) {207return false;208}209TestBufferedImage image2 = (TestBufferedImage) other;210int width = getWidth();211int height = getHeight();212if ((image2.getWidth() != width) || (image2.getHeight() != height)) {213return false;214}215int glitches = 0;216for (int x = 0; x < width; x++) {217for (int y = 0; y < height; y++) {218int rgb1 = getRGB(x, y);219int rgb2 = image2.getRGB(x, y);220if (rgb1 != rgb2) {221//System.out.println(x+" "+y+" "+rgb1+" "+rgb2);222glitches++;223}224}225}226return glitches <= MAX_GLITCHES;227}228}229230static class TestJPanel extends JPanel {231TestBufferedImage image = createImage(new Dimension(1, 1));232233TestBufferedImage createImage(Dimension d) {234return new TestBufferedImage(d.width, d.height,235BufferedImage.TYPE_INT_ARGB);236}237238public void setPreferredSize(Dimension size) {239super.setPreferredSize(size);240image = createImage(size);241}242243public void paint(Graphics g) {244Graphics g0 = image.getGraphics();245super.paint(g0);246g.drawImage(image, 0, 0, this);247}248}249250251252public static void main(String[] args) throws Throwable {253final bug4337267 test = new bug4337267();254SwingUtilities.invokeLater(new Runnable() {255public void run() {256test.run();257}258});259260synchronized(test) {261while (!done) {262try {263test.wait();264} catch (InterruptedException ex) {265// do nothing266}267}268}269270if (testFailed) {271throw new RuntimeException("FAIL");272}273274System.out.println("OK");275}276}277278279