869N/A/*
869N/A * @test
869N/A * @bug 4337267
869N/A * @summary test that numeric shaping works in Swing components
869N/A * @author Sergey Groznyh
869N/A * @run main bug4337267
869N/A */
869N/A
869N/Aimport java.awt.Component;
869N/Aimport java.awt.Dimension;
869N/Aimport java.awt.Graphics;
869N/Aimport java.awt.font.NumericShaper;
869N/Aimport java.awt.font.TextAttribute;
869N/Aimport java.awt.image.BufferedImage;
869N/Aimport javax.swing.BoxLayout;
869N/Aimport javax.swing.JComponent;
869N/Aimport javax.swing.JFrame;
873N/Aimport javax.swing.JLabel;
869N/Aimport javax.swing.JPanel;
869N/Aimport javax.swing.JTextArea;
869N/Aimport javax.swing.SwingUtilities;
869N/A
869N/Apublic class bug4337267 {
869N/A TestJPanel p1, p2;
869N/A TestBufferedImage i1, i2;
0N/A JComponent[] printq;
0N/A JFrame window;
0N/A static boolean testFailed = false;
0N/A static boolean done = false;
869N/A
0N/A String shaped =
0N/A "000 (E) 111 (A) \u0641\u0642\u0643 \u0662\u0662\u0662 (E) 333";
0N/A String text = "000 (E) 111 (A) \u0641\u0642\u0643 222 (E) 333";
869N/A
0N/A void run() {
869N/A initUI();
0N/A testTextComponent();
869N/A testNonTextComponentHTML();
869N/A testNonTextComponentPlain();
869N/A
2624N/A doneTask();
869N/A }
48N/A
869N/A void initUI() {
0N/A window = new JFrame("bug4337267");
869N/A window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
716N/A window.setSize(800, 600);
869N/A Component content = createContentPane();
1958N/A window.add(content);
1963N/A window.setVisible(true);
2340N/A }
1954N/A
1954N/A Runnable printComponents = new Runnable() {
1954N/A public void run() {
1954N/A printComponent(printq[0], i1);
1954N/A printComponent(printq[1], i2);
1954N/A }
1954N/A };
1954N/A
2350N/A Runnable compareRasters = new Runnable() {
1954N/A public void run() {
1954N/A assertEquals(p1.image, p2.image);
1954N/A assertEquals(i1, i2);
1954N/A }
0N/A };
0N/A
869N/A void doneTask() {
868N/A final Object monitor = this;
2624N/A SwingUtilities.invokeLater(new Runnable() {
2693N/A public void run() {
2042N/A done = true;
2982N/A synchronized(monitor) {
1007N/A monitor.notify();
2980N/A }
2963N/A }
1503N/A });
2915N/A }
869N/A
2624N/A
2624N/A void fail(String message) {
0N/A testFailed = true;
2270N/A throw new RuntimeException(message);
2270N/A }
2270N/A
2270N/A void assertEquals(Object o1, Object o2) {
2270N/A if ((o1 == null) && (o2 != null)) {
2270N/A fail("Expected null, got " + o2);
2270N/A } else if ((o1 != null) && (o2 == null)) {
0N/A fail("Expected " + o1 + ", got null");
869N/A } else if (!o1.equals(o2)) {
868N/A fail("Expected " + o1 + ", got " + o2);
0N/A }
0N/A }
65N/A
869N/A void testTextComponent() {
868N/A System.out.println("testTextComponent:");
65N/A JTextArea area1 = new JTextArea();
869N/A injectComponent(p1, area1, false);
2624N/A area1.setText(shaped);
2624N/A JTextArea area2 = new JTextArea();
65N/A injectComponent(p2, area2, true);
65N/A area2.setText(text);
65N/A window.repaint();
65N/A printq = new JComponent[] { area1, area2 };
65N/A SwingUtilities.invokeLater(printComponents);
65N/A SwingUtilities.invokeLater(compareRasters);
65N/A }
65N/A
65N/A void testNonTextComponentHTML() {
65N/A System.out.println("testNonTextComponentHTML:");
65N/A JLabel label1 = new JLabel();
65N/A injectComponent(p1, label1, false);
65N/A label1.setText("<html>" + shaped);
2266N/A JLabel label2 = new JLabel();
2266N/A injectComponent(p2, label2, true);
2266N/A label2.setText("<html>" + text);
2266N/A window.repaint();
2266N/A printq = new JComponent[] { label1, label2 };
2624N/A SwingUtilities.invokeLater(printComponents);
2624N/A SwingUtilities.invokeLater(compareRasters);
2266N/A }
2266N/A
2624N/A void testNonTextComponentPlain() {
2266N/A System.out.println("testNonTextComponentHTML:");
2266N/A JLabel label1 = new JLabel();
2266N/A injectComponent(p1, label1, false);
2266N/A label1.setText(shaped);
2266N/A JLabel label2 = new JLabel();
2266N/A injectComponent(p2, label2, true);
2266N/A label2.setText(text);
2266N/A window.repaint();
2266N/A printq = new JComponent[] { label1, label2 };
2266N/A SwingUtilities.invokeLater(printComponents);
2266N/A SwingUtilities.invokeLater(compareRasters);
2266N/A }
2266N/A
2266N/A void setShaping(JComponent c) {
2266N/A c.putClientProperty(TextAttribute.NUMERIC_SHAPING,
2266N/A NumericShaper.getContextualShaper(NumericShaper.ARABIC));
2266N/A }
2266N/A
2266N/A void injectComponent(JComponent p, JComponent c, boolean shape) {
2266N/A if (shape) {
2266N/A setShaping(c);
2266N/A }
2266N/A p.removeAll();
2266N/A p.add(c);
2266N/A }
0N/A
869N/A void printComponent(JComponent c, TestBufferedImage i) {
868N/A Graphics g = i.getGraphics();
0N/A g.setColor(c.getBackground());
0N/A g.fillRect(0, 0, i.getWidth(), i.getHeight());
0N/A c.print(g);
0N/A }
0N/A
2334N/A Component createContentPane() {
0N/A Dimension size = new Dimension(500, 100);
2624N/A i1 = new TestBufferedImage(size.width, size.height,
2624N/A BufferedImage.TYPE_INT_ARGB);
0N/A i2 = new TestBufferedImage(size.width, size.height,
0N/A BufferedImage.TYPE_INT_ARGB);
869N/A p1 = new TestJPanel();
868N/A p1.setPreferredSize(size);
0N/A p2 = new TestJPanel();
0N/A p2.setPreferredSize(size);
869N/A JPanel panel = new JPanel();
0N/A panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
0N/A panel.add(p1);
2624N/A panel.add(p2);
2624N/A
2624N/A return panel;
869N/A }
2624N/A
2624N/A static class TestBufferedImage extends BufferedImage {
2624N/A int MAX_GLITCHES = 0;
2624N/A
2624N/A TestBufferedImage(int width, int height, int imageType) {
2624N/A super(width, height, imageType);
2624N/A }
2624N/A
2624N/A @Override
2624N/A public boolean equals(Object other) {
2624N/A if (! (other instanceof TestBufferedImage)) {
2624N/A return false;
2624N/A }
2624N/A TestBufferedImage image2 = (TestBufferedImage) other;
2624N/A int width = getWidth();
2624N/A int height = getHeight();
2624N/A if ((image2.getWidth() != width) || (image2.getHeight() != height)) {
2624N/A return false;
2624N/A }
2624N/A int glitches = 0;
2624N/A for (int x = 0; x < width; x++) {
2624N/A for (int y = 0; y < height; y++) {
2624N/A int rgb1 = getRGB(x, y);
2624N/A int rgb2 = image2.getRGB(x, y);
2624N/A if (rgb1 != rgb2) {
2624N/A //System.out.println(x+" "+y+" "+rgb1+" "+rgb2);
2624N/A glitches++;
2624N/A }
2624N/A }
2624N/A }
2624N/A return glitches <= MAX_GLITCHES;
0N/A }
0N/A }
869N/A
868N/A static class TestJPanel extends JPanel {
0N/A TestBufferedImage image = createImage(new Dimension(1, 1));
0N/A
2624N/A TestBufferedImage createImage(Dimension d) {
848N/A return new TestBufferedImage(d.width, d.height,
2624N/A BufferedImage.TYPE_INT_ARGB);
2624N/A }
868N/A
848N/A public void setPreferredSize(Dimension size) {
2624N/A super.setPreferredSize(size);
0N/A image = createImage(size);
2624N/A }
2624N/A
0N/A public void paint(Graphics g) {
0N/A Graphics g0 = image.getGraphics();
868N/A super.paint(g0);
2624N/A g.drawImage(image, 0, 0, this);
2222N/A }
2624N/A }
2624N/A
2222N/A
2222N/A
2624N/A public static void main(String[] args) throws Throwable {
2222N/A final bug4337267 test = new bug4337267();
2624N/A SwingUtilities.invokeLater(new Runnable() {
2624N/A public void run() {
2222N/A test.run();
2222N/A }
2624N/A });
868N/A
2624N/A synchronized(test) {
2624N/A while (!done) {
115N/A try {
868N/A test.wait();
868N/A } catch (InterruptedException ex) {
2624N/A // do nothing
868N/A }
2624N/A }
2624N/A }
868N/A
868N/A if (testFailed) {
868N/A throw new RuntimeException("FAIL");
2624N/A }
868N/A
2624N/A System.out.println("OK");
2624N/A }
868N/A}
868N/A