24
24
/*
25
25
* @test
26
26
* @bug 8015854
27
- * @requires (os.family == "mac")
28
- * @summary Tests HTML image as JButton text for unwanted padding on macOS Aqua LAF
27
+ * @summary Tests HTML image as JButton text for unwanted padding
29
28
* @run main HtmlButtonImageTest
30
29
*/
31
30
41
40
import javax .swing .JButton ;
42
41
import javax .swing .SwingUtilities ;
43
42
import javax .swing .UIManager ;
43
+ import javax .swing .UnsupportedLookAndFeelException ;
44
44
45
45
import static java .awt .image .BufferedImage .TYPE_INT_ARGB ;
46
46
@@ -60,19 +60,38 @@ public final class HtmlButtonImageTest {
60
60
private static final int maxX = centerX + (SQUARE_WIDTH / 2 );
61
61
private static final int maxY = centerY + (SQUARE_HEIGHT / 2 );
62
62
63
+ private static boolean supportedLaf ;
64
+ private static int failCount = 0 ;
65
+ private static String currentLaf = new String ();
66
+ private static StringBuffer failedLafs = new StringBuffer ();
67
+
68
+
63
69
public static void main (String [] args ) throws Exception {
64
- UIManager .setLookAndFeel ("com.apple.laf.AquaLookAndFeel" );
65
70
testDir = Path .of (System .getProperty ("test.classes" , "." ));
66
71
generateRedSquare ();
67
72
68
- SwingUtilities .invokeAndWait (HtmlButtonImageTest ::createButton );
69
- SwingUtilities .invokeAndWait (HtmlButtonImageTest ::paintButton );
73
+ for (UIManager .LookAndFeelInfo laf : UIManager .getInstalledLookAndFeels ()) {
74
+ SwingUtilities .invokeAndWait (() -> setLookAndFeel (laf ));
75
+ if (supportedLaf ) {
76
+ currentLaf = laf .getName ();
77
+ SwingUtilities .invokeAndWait (HtmlButtonImageTest ::createButton );
78
+ SwingUtilities .invokeAndWait (HtmlButtonImageTest ::paintButton );
79
+
80
+ testImageCentering (image .getRGB (centerX , centerY ),
81
+ image .getRGB (minX , minY ),
82
+ image .getRGB (minX , maxY ),
83
+ image .getRGB (maxX , minY ),
84
+ image .getRGB (maxX , maxY ));
85
+ }
86
+ }
70
87
71
- testImageCentering (image .getRGB (centerX , centerY ),
72
- image .getRGB (minX , minY ),
73
- image .getRGB (minX , maxY ),
74
- image .getRGB (maxX , minY ),
75
- image .getRGB (maxX , maxY ));
88
+ if (!failedLafs .isEmpty ()) {
89
+ if (failCount > 1 ) {
90
+ failedLafs .setLength (failedLafs .length () - 2 );
91
+ }
92
+ throw new RuntimeException ("HTML image not centered in button " +
93
+ "for these L&F's: " + failedLafs );
94
+ }
76
95
}
77
96
78
97
private static void generateRedSquare () throws IOException {
@@ -105,11 +124,22 @@ private static boolean checkRedColor(int rgb) {
105
124
private static void testImageCentering (int ... colors ) throws IOException {
106
125
for (int c : colors ) {
107
126
if (!checkRedColor (c )) {
108
- ImageIO .write (image , "png" ,
109
- new File (testDir + "/fail_image.png" ));
110
- throw new RuntimeException ("HTML image not centered in button" );
127
+ failCount ++;
128
+ ImageIO .write (image , "png" , new File (testDir + "/fail_image_" +
129
+ currentLaf .replaceAll ("[^\\ w\\ s]" ,"" ) + ".png" ));
130
+ failedLafs .append (currentLaf + ", " );
131
+ break ;
111
132
}
112
133
}
113
- System .out .println ("Passed" );
134
+ }
135
+
136
+ private static void setLookAndFeel (UIManager .LookAndFeelInfo laf ) {
137
+ try {
138
+ UIManager .setLookAndFeel (laf .getClassName ());
139
+ supportedLaf = true ;
140
+ } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
141
+ InstantiationException | IllegalAccessException e ) {
142
+ supportedLaf = false ;
143
+ }
114
144
}
115
145
}
0 commit comments