--- /dev/null 2016-01-25 00:13:44.000000000 +0400 +++ new/test/javax/swing/UIDefaults/8132119/bug8132119.java 2016-01-25 00:13:44.000000000 +0400 @@ -0,0 +1,442 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.GraphicsEnvironment; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.font.FontRenderContext; +import java.awt.font.NumericShaper; +import java.awt.font.TextAttribute; +import java.awt.font.TextLayout; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.UIManager.LookAndFeelInfo; +import javax.swing.plaf.basic.BasicLookAndFeel; +import javax.swing.plaf.TextUIDrawing; +import javax.swing.plaf.metal.MetalLookAndFeel; + +/** + * @test + * @bug 8132119 + * @author Alexandr Scherbatiy + * @summary Provide public API for text related methods in SwingUtilities2 + */ +public class bug8132119 { + + private static final int WIDTH = 150; + private static final int HEIGHT = 50; + private static final Color DEFAULT_DRAW_COLOR = Color.RED; + private static final Color TEST_DRAW_COLOR = Color.PINK; + private static final Color BACKGROUND_COLOR = Color.GREEN; + private static final NumericShaper NUMERIC_SHAPER = NumericShaper.getShaper( + NumericShaper.ARABIC); + private static final String TEXT_UI_DRAWING_PROPERTY = "uiDrawing.text"; + + public static void main(String[] args) throws Exception { + SwingUtilities.invokeAndWait(bug8132119::testDrawStringMethods); + } + + private static void testDrawStringMethods() { + testTextDrawingProperty(); + testDefaultTextDrawing(); + testCustomTextDrawing(); + } + + private static void testDefaultTextDrawing() { + setMetalLAF(); + testStringWidth(); + testStringClip(); + testDrawEmptyString(); + testDrawString(false, DEFAULT_DRAW_COLOR); + testDrawString(true, DEFAULT_DRAW_COLOR); + checkNullArguments(); + } + + private static void testCustomTextDrawing() { + setMetalLAF(); + setCustomTextDrawing(); + testDrawString(false, TEST_DRAW_COLOR); + testDrawString(true, TEST_DRAW_COLOR); + } + + private static void testTextDrawingProperty() { + try { + LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels(); + + for (LookAndFeelInfo info : infos) { + UIManager.setLookAndFeel(info.getClassName()); + Class cls = UIManager.getLookAndFeel().getClass(); + if (!BasicLookAndFeel.class.isAssignableFrom(cls)) { + continue; + } + + Object textDrawing = UIManager.get(TEXT_UI_DRAWING_PROPERTY); + if (!(textDrawing instanceof TextUIDrawing)) { + throw new RuntimeException( + String.format("%s property is not set for %s!", + TEXT_UI_DRAWING_PROPERTY, info.getClassName())); + } + + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static void testStringWidth() { + + String str = "12345678910\u036F"; + JComponent comp = createComponent(str); + Font font = comp.getFont(); + FontMetrics fontMetrics = comp.getFontMetrics(font); + int stringWidth = getTextDrawing().getStringWidth(comp, fontMetrics, str); + + if (stringWidth == fontMetrics.stringWidth(str)) { + throw new RuntimeException("Numeric shaper is not used!"); + } + + if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) { + throw new RuntimeException("Wrong text width!"); + } + } + + private static void testStringClip() { + + String str = "1234567890"; + JComponent comp = createComponent(str); + FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont()); + + TextUIDrawing textDrawing = getTextDrawing(); + + int width = textDrawing.getStringWidth(comp, fontMetrics, str); + + String clip = textDrawing.getClippedString(comp, fontMetrics, str, width); + checkClippedString(str, clip, str); + + clip = textDrawing.getClippedString(comp, fontMetrics, str, width + 1); + checkClippedString(str, clip, str); + + clip = textDrawing.getClippedString(comp, fontMetrics, str, -1); + checkClippedString(str, clip, "..."); + + clip = textDrawing.getClippedString(comp, fontMetrics, str, 0); + checkClippedString(str, clip, "..."); + + clip = textDrawing.getClippedString(comp, fontMetrics, + str, width - width / str.length()); + int endIndex = str.length() - 3; + checkClippedString(str, clip, str.substring(0, endIndex) + "..."); + } + + private static void checkClippedString(String str, String res, String golden) { + if (!golden.equals(res)) { + throw new RuntimeException(String.format("The string '%s' is not " + + "properly clipped. The result is '%s' instead of '%s'", + str, res, golden)); + } + } + + private static void testDrawEmptyString() { + JLabel label = new JLabel(); + BufferedImage buffImage = createBufferedImage(50, 50); + Graphics2D g2 = buffImage.createGraphics(); + g2.setColor(DEFAULT_DRAW_COLOR); + TextUIDrawing textDrawing = getTextDrawing(); + textDrawing.drawString(null, g2, null, 0, 0); + textDrawing.drawString(label, g2, null, 0, 0); + textDrawing.drawString(null, g2, "", 0, 0); + textDrawing.drawString(label, g2, "", 0, 0); + textDrawing.drawStringUnderlineCharAt(null, g2, null, 3, 0, 0); + textDrawing.drawStringUnderlineCharAt(label, g2, null, 3, 0, 0); + textDrawing.drawStringUnderlineCharAt(null, g2, "", 3, 0, 0); + textDrawing.drawStringUnderlineCharAt(label, g2, "", 3, 0, 0); + g2.dispose(); + checkImageIsEmpty(buffImage); + } + + private static void testDrawString(boolean underlined, Color drawColor) { + String str = "AOB"; + JComponent comp = createComponent(str, underlined); + + BufferedImage buffImage = createBufferedImage(WIDTH, HEIGHT); + Graphics2D g2 = buffImage.createGraphics(); + + comp.paint(g2); + g2.dispose(); + + FontMetrics fontMetrices = comp.getFontMetrics(comp.getFont()); + TextUIDrawing textDrawing = getTextDrawing(); + int width = textDrawing.getStringWidth(comp, fontMetrices, str); + int xx = width / 2 + 6; + + checkImageContainsSymbol(buffImage, xx, underlined ? 3 : 2, drawColor); + } + + private static void checkNullArguments() { + + Graphics g = null; + try { + String text = "Test"; + JComponent component = new JLabel(text); + BufferedImage img = createBufferedImage(100, 100); + g = img.createGraphics(); + checkNullArguments(component, g, text); + } finally { + g.dispose(); + } + } + + private static void checkNullArguments(JComponent comp, Graphics g, + String text) { + + checkNullArgumentsDrawString(comp, g, text); + checkNullArgumentsDrawStringUnderlineCharAt(comp, g, text); + checkNullArgumentsGetClippedString(comp, text); + checkNullArgumentsGetStringWidth(comp, text); + } + + private static void checkNullArgumentsDrawString(JComponent comp, Graphics g, + String text) { + + int x = 50; + int y = 50; + TextUIDrawing textDrawing = getTextDrawing(); + textDrawing.drawString(null, g, text, x, y); + textDrawing.drawString(comp, g, null, x, y); + + try { + textDrawing.drawString(comp, null, text, x, y); + } catch (NullPointerException e) { + return; + } + + throw new RuntimeException("NPE is not thrown"); + } + + private static void checkNullArgumentsDrawStringUnderlineCharAt( + JComponent comp, Graphics g, String text) { + + int x = 50; + int y = 50; + TextUIDrawing textDrawing = getTextDrawing(); + textDrawing.drawStringUnderlineCharAt(null, g, text, 1, x, y); + textDrawing.drawStringUnderlineCharAt(comp, g, null, 1, x, y); + + try { + textDrawing.drawStringUnderlineCharAt(comp, null, text, 1, x, y); + } catch (NullPointerException e) { + return; + } + + throw new RuntimeException("NPE is not thrown"); + } + + private static void checkNullArgumentsGetClippedString( + JComponent comp, String text) { + + FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont()); + TextUIDrawing textDrawing = getTextDrawing(); + textDrawing.getClippedString(null, fontMetrics, text, 1); + String result = textDrawing.getClippedString(comp, fontMetrics, null, 1); + if (!"".equals(result)) { + throw new RuntimeException("Empty string is not returned!"); + } + + try { + textDrawing.getClippedString(comp, null, text, 1); + } catch (NullPointerException e) { + return; + } + + throw new RuntimeException("NPE is not thrown"); + } + + private static void checkNullArgumentsGetStringWidth(JComponent comp, + String text) { + + FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont()); + TextUIDrawing textDrawing = getTextDrawing(); + textDrawing.getStringWidth(null, fontMetrics, text); + int result = textDrawing.getStringWidth(comp, fontMetrics, null); + + if (result != 0) { + throw new RuntimeException("The string length is not 0"); + } + + try { + textDrawing.getStringWidth(comp, null, text); + } catch (NullPointerException e) { + return; + } + + throw new RuntimeException("NPE is not thrown"); + } + + private static void setMetalLAF() { + try { + UIManager.setLookAndFeel(new MetalLookAndFeel()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static JComponent createComponent(String str) { + return createComponent(str, false); + } + + private static JComponent createComponent(String str, boolean underline) { + JLabel comp = new JLabel(str); + + if (underline) { + comp.setDisplayedMnemonicIndex(1); + UIManager.put("Button.showMnemonics", true); + } + comp.setSize(WIDTH, HEIGHT); + comp.putClientProperty(TextAttribute.NUMERIC_SHAPING, NUMERIC_SHAPER); + comp.setFont(getFont()); + comp.setForeground(DEFAULT_DRAW_COLOR); + return comp; + } + + private static Font getFont() { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String[] fontNames = ge.getAvailableFontFamilyNames(); + String fontName = fontNames[0]; + for (String name : fontNames) { + if ("Dialog".equals(name)) { + fontName = name; + break; + } + } + return new Font(fontName, Font.PLAIN, 28); + } + + private static int getLayoutWidth(String text, Font font, NumericShaper shaper) { + HashMap map = new HashMap(); + map.put(TextAttribute.FONT, font); + map.put(TextAttribute.NUMERIC_SHAPING, shaper); + FontRenderContext frc = new FontRenderContext(null, false, false); + TextLayout layout = new TextLayout(text, map, frc); + return (int) layout.getAdvance(); + } + + private static TextUIDrawing getTextDrawing() { + return (TextUIDrawing) UIManager.get(TEXT_UI_DRAWING_PROPERTY); + } + + private static void setCustomTextDrawing() { + UIManager.put(TEXT_UI_DRAWING_PROPERTY, new TestLAFTextDrawing()); + } + + private static void checkImageIsEmpty(BufferedImage buffImage) { + int background = BACKGROUND_COLOR.getRGB(); + + for (int i = 0; i < buffImage.getWidth(); i++) { + for (int j = 0; j < buffImage.getHeight(); j++) { + if (background != buffImage.getRGB(i, j)) { + throw new RuntimeException("Image is not empty!"); + } + } + } + } + + private static void checkImageContainsSymbol(BufferedImage buffImage, + int x, int intersections, Color drawColor) { + boolean hasDrawColor = false; + int background = BACKGROUND_COLOR.getRGB(); + boolean isBackground = true; + int backgroundChangesCount = 0; + + for (int y = 0; y < buffImage.getHeight(); y++) { + int imgRGB = buffImage.getRGB(x, y); + if (!(isBackground ^ (background != imgRGB))) { + isBackground = !isBackground; + backgroundChangesCount++; + } + + if (!hasDrawColor && imgRGB == drawColor.getRGB()) { + hasDrawColor = true; + } + } + if (backgroundChangesCount != intersections * 2) { + throw new RuntimeException("String is not properly drawn!"); + } + if (!hasDrawColor) { + throw new RuntimeException("Wrong draw color!"); + } + } + + private static BufferedImage createBufferedImage(int width, int height) { + BufferedImage bufffImage = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + + Graphics2D g = bufffImage.createGraphics(); + g.setColor(BACKGROUND_COLOR); + g.fillRect(0, 0, width, height); + g.dispose(); + return bufffImage; + } + + private static class TestLAFTextDrawing implements TextUIDrawing { + + @Override + public void drawString(JComponent c, Graphics g, String string, int x, int y) { + drawStringUnderlineCharAt(c, g, string, -1, x, y); + } + + @Override + public void drawStringUnderlineCharAt(JComponent c, Graphics g, + String string, int underlinedIndex, int x, int y) { + g.setColor(BACKGROUND_COLOR); + g.fillRect(0, 0, c.getWidth(), c.getHeight()); + g.setColor(TEST_DRAW_COLOR); + g.drawString(string, x, y); + if (underlinedIndex > -1) { + int w = WIDTH; + int h = 8; + g.drawLine(x, y + h, x + w, y + h); + } + } + + @Override + public String getClippedString(JComponent c, FontMetrics fm, + String string, int availTextWidth) { + return string.substring(0, string.length() - 3) + "***"; + } + + @Override + public int getStringWidth(JComponent c, FontMetrics fm, String string) { + return fm.stringWidth(string); + } + } +}