From b86418d365d515528317c65f020c013afbc8fd19 Mon Sep 17 00:00:00 2001 From: Avik Niyogi Date: Wed, 20 Jul 2016 11:25:57 +0530 Subject: [PATCH] 8160438: javax/swing/plaf/nimbus/8057791/bug8057791.java fails Reviewed-by: alexsch, rchamyal --- .../swing/plaf/nimbus/8057791/bug8057791.java | 203 +++++++++++++----- 1 file changed, 146 insertions(+), 57 deletions(-) diff --git a/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java b/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java index cc31544abc6..d8146b61b09 100644 --- a/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java +++ b/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java @@ -21,20 +21,20 @@ * questions. */ -/* @test - @bug 8057791 + /* @test + @bug 8057791 8160438 @summary Selection in JList is drawn with wrong colors in Nimbus L&F - @author Anton Litvinov @run main bug8057791 */ - import java.awt.Color; +import java.awt.Font; import java.awt.Graphics; +import java.awt.Point; import java.awt.Rectangle; +import java.awt.Robot; import java.awt.image.BufferedImage; -import java.lang.reflect.InvocationTargetException; -import java.util.HashSet; import javax.swing.DefaultListModel; +import javax.swing.JFrame; import javax.swing.JList; import javax.swing.SwingUtilities; import javax.swing.UIManager; @@ -42,59 +42,148 @@ import javax.swing.UnsupportedLookAndFeelException; import javax.swing.plaf.nimbus.NimbusLookAndFeel; public class bug8057791 { - public static void main(String[] args) { - try { - UIManager.setLookAndFeel(new NimbusLookAndFeel()); - SwingUtilities.invokeAndWait(new Runnable() { - @Override - public void run() { - final int listWidth = 50; - final int listHeight = 50; - final int selCellIndex = 0; + private static JFrame frame; + private static JList list; + private static DefaultListModel model; + private static Robot robot; + private static final int SELECTED_INDEX = 0; + private static volatile String errorString = ""; - JList list = new JList(); - list.setSize(listWidth, listHeight); - DefaultListModel listModel = new DefaultListModel(); - listModel.add(selCellIndex, "E"); - list.setModel(listModel); - list.setSelectedIndex(selCellIndex); - - BufferedImage img = new BufferedImage(listWidth, listHeight, - BufferedImage.TYPE_INT_ARGB); - Graphics g = img.getGraphics(); - list.paint(g); - g.dispose(); - - Rectangle cellRect = list.getCellBounds(selCellIndex, selCellIndex); - HashSet cellColors = new HashSet(); - int uniqueColorIndex = 0; - for (int x = cellRect.x; x < (cellRect.x + cellRect.width); x++) { - for (int y = cellRect.y; y < (cellRect.y + cellRect.height); y++) { - Color cellColor = new Color(img.getRGB(x, y), true); - if (cellColors.add(cellColor)) { - System.err.println(String.format("Cell color #%d: %s", - uniqueColorIndex++, cellColor)); - } - } - } - - Color selForegroundColor = list.getSelectionForeground(); - Color selBackgroundColor = list.getSelectionBackground(); - if (!cellColors.contains(new Color(selForegroundColor.getRGB(), true))) { - throw new RuntimeException(String.format( - "Selected cell is drawn without selection foreground color '%s'.", - selForegroundColor)); - } - if (!cellColors.contains(new Color(selBackgroundColor.getRGB(), true))) { - throw new RuntimeException(String.format( - "Selected cell is drawn without selection background color '%s'.", - selBackgroundColor)); - } - } - }); - } catch (UnsupportedLookAndFeelException | InterruptedException | InvocationTargetException e) { - throw new RuntimeException(e); + public static void main(String[] args) throws Exception { + robot = new Robot(); + robot.waitForIdle(); + runSteps(); + if (!errorString.isEmpty()) { + throw new RuntimeException("Error Log:\n" + errorString); } } + + private static void runSteps() throws Exception { + if (tryNimbusLookAndFeel()) { + createUI(); + robot.waitForIdle(); + runColorTestCase(); + robot.waitForIdle(); + cleanUp(); + } + + } + + private static boolean tryNimbusLookAndFeel() + throws Exception { + try { + UIManager.setLookAndFeel(new NimbusLookAndFeel()); + } catch (UnsupportedLookAndFeelException e) { + errorString += e.getMessage(); + return false; + } + return true; + } + + private static void createUI() + throws Exception { + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + list = new JList<>(); + model = new DefaultListModel<>(); + model.add(0, "@@"); + Font font = list.getFont(); + list.setFont( + new Font(font.getFontName(), + Font.BOLD, + (int) (font.getSize2D() * 2))); + list.setModel(model); + list.setSelectedIndex(SELECTED_INDEX); + frame.add(list); + frame.pack(); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + }); + } + + private static void runColorTestCase() throws Exception { + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + Rectangle cellBounds = list.getCellBounds(SELECTED_INDEX, SELECTED_INDEX); + cellBounds.x += list.getLocationOnScreen().x; + cellBounds.y += list.getLocationOnScreen().y; + //positive test cases + Color foregroundColor = list.getSelectionForeground(); + Color backgroundColor = list.getSelectionBackground(); + checkColor(foregroundColor, cellBounds); + checkColor(backgroundColor, cellBounds); + //negative test cases + Color changedForegroundColor = foregroundColor.darker(); + Color changedBackgroundColor = backgroundColor.brighter(); + checkNotColor(changedForegroundColor, cellBounds); + checkNotColor(changedBackgroundColor, cellBounds); + } + }); + } + + private static void checkColor(Color colorCheck, Rectangle bounds) { + if (!findColor(colorCheck, bounds)) { + String error = "[ERROR][" + colorCheck.toString() + + "] Not found in selected cell"; + errorString += error; + } + } + + private static void checkNotColor(Color colorCheck, Rectangle bounds) { + if (findColor(colorCheck, bounds)) { + String error = "[ERROR][" + colorCheck.toString() + + "] is found in selected cell. " + + "Not supposed to be found in negative test case"; + errorString += error; + } + } + + private static boolean findColor(Color colorCheck, Rectangle bounds) { + BufferedImage img = new BufferedImage(bounds.width, + bounds.height, + BufferedImage.TYPE_INT_ARGB); + Graphics g = img.getGraphics(); + list.paint(g); + g.dispose(); + checkLoops: + for (int x = 0; x < bounds.width; x++) { + for (int y = 0; y < bounds.height; y++) { + Point relativePointCheck = new Point(bounds.x + x, bounds.y + y); + robot.mouseMove(relativePointCheck.x, relativePointCheck.y); + Color detectedColor = robot.getPixelColor(relativePointCheck.x, + relativePointCheck.y); + if (detectedColor.equals(colorCheck)) { + return true; + } + if (isMac()) { + //One more chance for Mac due to non-Generic display setting + detectedColor = new Color(img.getRGB(x, y), true); + if (detectedColor.equals(colorCheck)) { + return true; + } + } + } + } + return false; + } + + private static boolean isMac() { + String osName = System.getProperty("os.name"); + return osName.contains("Mac"); + } + + private static void cleanUp() throws Exception { + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + frame.dispose(); + } + }); + } }