|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8016524 |
| 27 | + * @requires (os.family=="mac") |
| 28 | + * @key headful |
| 29 | + * @summary Tests whether the bottom line of JTableHeader border is visible for MacOS default LAF |
| 30 | + * @run main JTHeaderBorderTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Graphics2D; |
| 34 | +import javax.imageio.ImageIO; |
| 35 | +import javax.swing.JFrame; |
| 36 | +import javax.swing.JTable; |
| 37 | +import javax.swing.JScrollPane; |
| 38 | +import javax.swing.SwingUtilities; |
| 39 | +import javax.swing.UIManager; |
| 40 | +import javax.swing.UnsupportedLookAndFeelException; |
| 41 | +import java.awt.image.BufferedImage; |
| 42 | +import java.io.File; |
| 43 | +import java.io.IOException; |
| 44 | + |
| 45 | +import static java.awt.image.BufferedImage.TYPE_INT_ARGB; |
| 46 | + |
| 47 | +public class JTHeaderBorderTest { |
| 48 | + |
| 49 | + private static JFrame frame; |
| 50 | + private static JTable table; |
| 51 | + private static JScrollPane scrollableTable; |
| 52 | + |
| 53 | + private static final int FRAME_HT = 300; |
| 54 | + private static final int FRAME_WT = 300; |
| 55 | + private static final int TABLE_COLS = 3; |
| 56 | + private static final int TABLE_ROWS = 2; |
| 57 | + private static final int Y_OFFSET_START = 30; |
| 58 | + private static final int Y_OFFSET_END = 55; |
| 59 | + private static final int X_OFFSET = 25; |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + |
| 63 | + try { |
| 64 | + try { |
| 65 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 66 | + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
| 67 | + | UnsupportedLookAndFeelException e) { |
| 68 | + throw new RuntimeException("Unsupported Look&Feel Class"); |
| 69 | + } |
| 70 | + |
| 71 | + SwingUtilities.invokeAndWait(() -> { |
| 72 | + table = new JTable(TABLE_ROWS, TABLE_COLS); |
| 73 | + scrollableTable = new JScrollPane(table); |
| 74 | + |
| 75 | + frame = new JFrame(); |
| 76 | + frame.getContentPane().add(scrollableTable); |
| 77 | + frame.setSize(FRAME_WT, FRAME_HT); |
| 78 | + frame.setLocationRelativeTo(null); |
| 79 | + frame.setVisible(true); |
| 80 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 81 | + |
| 82 | + // paint JFrame to BufferedImage |
| 83 | + BufferedImage image = new BufferedImage(FRAME_WT, FRAME_HT, TYPE_INT_ARGB); |
| 84 | + Graphics2D graphics2D = image.createGraphics(); |
| 85 | + frame.paint(graphics2D); |
| 86 | + graphics2D.dispose(); |
| 87 | + |
| 88 | + int tableColor = table.getBackground().getRGB(); |
| 89 | + int headerColor = table.getTableHeader().getBackground().getRGB(); |
| 90 | + //at start pixelColor initialized to table header background color |
| 91 | + int pixelColor = headerColor; |
| 92 | + boolean isBottomLineVisible = false; |
| 93 | + |
| 94 | + // scan table header region to check if bottom border of JTableHeader is visible |
| 95 | + for (int y = Y_OFFSET_START; y <= Y_OFFSET_END; y++) { |
| 96 | + pixelColor = image.getRGB(X_OFFSET, y); |
| 97 | + System.out.println("Y offset: "+ y + " Color: "+ (Integer.toHexString(image.getRGB(X_OFFSET, y)))); |
| 98 | + if (pixelColor != tableColor || pixelColor != headerColor) { |
| 99 | + isBottomLineVisible = true; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + // throw Runtime Exception if border is not visible in the scanned region |
| 104 | + if (!isBottomLineVisible) { |
| 105 | + saveImage(image, "JTableHeader.png"); |
| 106 | + throw new RuntimeException("JTableHeader Bottom Border not visible"); |
| 107 | + } |
| 108 | + }); |
| 109 | + } finally { |
| 110 | + if (frame != null) { |
| 111 | + SwingUtilities.invokeAndWait(()-> frame.dispose()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + // to save the BufferedImage as .png in the event the test fails (for debugging purpose) |
| 116 | + private static void saveImage(BufferedImage image, String filename) { |
| 117 | + try { |
| 118 | + ImageIO.write(image, "png", new File(filename)); |
| 119 | + } catch (IOException e) { |
| 120 | + e.printStackTrace(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments