8160438: javax/swing/plaf/nimbus/8057791/bug8057791.java fails
authoraniyogi
Wed, 20 Jul 2016 11:25:57 +0530 (2016-07-20)
changeset 40126 9006a75adf47
parent 39873 76907ae1b680
child 40127 2720cd366dc2
8160438: javax/swing/plaf/nimbus/8057791/bug8057791.java fails Reviewed-by: alexsch, rchamyal
jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java
--- a/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java	Mon Jul 18 15:43:30 2016 -0500
+++ b/jdk/test/javax/swing/plaf/nimbus/8057791/bug8057791.java	Wed Jul 20 11:25:57 2016 +0530
@@ -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.plaf.nimbus.NimbusLookAndFeel;
 
 public class bug8057791 {
-    public static void main(String[] args) {
+
+    private static JFrame frame;
+    private static JList<String> list;
+    private static DefaultListModel<String> model;
+    private static Robot robot;
+    private static final int SELECTED_INDEX = 0;
+    private static volatile String errorString = "";
+
+    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());
-
-            SwingUtilities.invokeAndWait(new Runnable() {
-                @Override
-                public void run() {
-                    final int listWidth = 50;
-                    final int listHeight = 50;
-                    final int selCellIndex = 0;
+        } catch (UnsupportedLookAndFeelException e) {
+            errorString += e.getMessage();
+            return false;
+        }
+        return true;
+    }
 
-                    JList<String> list = new JList<String>();
-                    list.setSize(listWidth, listHeight);
-                    DefaultListModel<String> listModel = new DefaultListModel<String>();
-                    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();
+    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);
+            }
+        });
+    }
 
-                    Rectangle cellRect = list.getCellBounds(selCellIndex, selCellIndex);
-                    HashSet<Color> cellColors = new HashSet<Color>();
-                    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));
-                            }
-                        }
-                    }
+    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;
+        }
+    }
 
-                    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));
+    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;
                     }
                 }
-            });
-        } catch (UnsupportedLookAndFeelException | InterruptedException | InvocationTargetException e) {
-            throw new RuntimeException(e);
+            }
         }
+        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();
+            }
+        });
     }
 }