3434import javax .swing .border .*;
3535import java .awt .*;
3636import java .awt .event .*;
37+ import java .awt .image .BufferedImage ;
38+ import java .lang .reflect .InvocationTargetException ;
3739
3840/**
3941 * This presents two dialogs, each with two possible default buttons. The
4547 * to double-check that the removed code didn't negatively affect how default
4648 * buttons are repainted.
4749 */
48- public class RootPaneDefaultButtonTest extends JDialog {
50+ public class RootPaneDefaultButtonTest {
4951
50- record ButtonRenderingExpectation (JButton button ,
51- boolean appearAsDefault ) {}
52+ static class TestDialog extends JDialog {
5253
53- public static void main (String [] args ) throws Exception {
54- if (!System .getProperty ("os.name" ).contains ("OS X" )) {
55- System .out .println ("This test is for MacOS only." );
56- return ;
54+ JButton button1 = new JButton ("Button 1" );
55+ JButton button2 = new JButton ("Button 2" );
56+
57+ TestDialog () {
58+ getContentPane ().setLayout (new BorderLayout ());
59+ getContentPane ().add (createPushButtonRow (), BorderLayout .SOUTH );
60+ setUndecorated (true );
61+ pack ();
5762 }
5863
59- RootPaneDefaultButtonTest window1 = new RootPaneDefaultButtonTest ();
60- RootPaneDefaultButtonTest window2 = new RootPaneDefaultButtonTest ();
64+ private JPanel createPushButtonRow () {
65+ JPanel p = new JPanel (new GridLayout (1 , 2 ));
66+ p .add (button1 );
67+ p .add (button2 );
68+ p .setBorder (new EmptyBorder (5 ,5 ,5 ,5 ));
69+ return p ;
70+ }
71+ }
6172
62- SwingUtilities .invokeAndWait (new Runnable () {
63- @ Override
64- public void run () {
65- Rectangle r1 = new Rectangle (0 , 20 ,
73+ /**
74+ * We want 2 dialogs: one in the foreground and one not in the foreground
75+ */
76+ static class TestScene {
77+ boolean isButton1Default , isButton2Default ;
78+
79+ TestScene (boolean isButton1Default , boolean isButton2Default ) {
80+ this .isButton1Default = isButton1Default ;
81+ this .isButton2Default = isButton2Default ;
82+ }
83+
84+ void run () throws Exception {
85+ SwingUtilities .invokeAndWait (() -> {
86+ System .out .println (
87+ "Testing isButton1Default = " + isButton1Default +
88+ " isButton2Default = " + isButton2Default );
89+ TestDialog window1 = new TestDialog ();
90+ TestDialog window2 = new TestDialog ();
91+
92+ if (isButton1Default ) {
93+ window1 .getRootPane ().setDefaultButton (window1 .button1 );
94+ }
95+ if (isButton2Default ) {
96+ window1 .getRootPane ().setDefaultButton (window1 .button2 );
97+ }
98+
99+ Rectangle r1 = new Rectangle (0 , 100 ,
66100 window1 .getWidth (), window1 .getHeight ());
67101 window1 .setBounds (r1 );
68102
69- Rectangle r2 = new Rectangle ((int ) (r1 .getMaxX () + 10 ), 20 ,
103+ Rectangle r2 = new Rectangle ((int ) (r1 .getMaxX () + 10 ), 100 ,
70104 window2 .getWidth (), window2 .getHeight ());
71105 window2 .setBounds (r2 );
72106
73107 window1 .setVisible (true );
74108 window2 .setVisible (true );
75- }
76- });
77-
78- Robot robot = new Robot ();
79-
80- test (robot , window1 .radioButton1 ,
81- new ButtonRenderingExpectation (window1 .button1 , true ),
82- new ButtonRenderingExpectation (window1 .button2 , false ),
83- new ButtonRenderingExpectation (window2 .button1 , false ),
84- new ButtonRenderingExpectation (window2 .button2 , false ));
85-
86- test (robot , window1 .radioButton2 ,
87- new ButtonRenderingExpectation (window1 .button1 , false ),
88- new ButtonRenderingExpectation (window1 .button2 , true ),
89- new ButtonRenderingExpectation (window2 .button1 , false ),
90- new ButtonRenderingExpectation (window2 .button2 , false ));
91-
92- test (robot , window1 .radioButton3 ,
93- new ButtonRenderingExpectation (window1 .button1 , false ),
94- new ButtonRenderingExpectation (window1 .button2 , false ),
95- new ButtonRenderingExpectation (window2 .button1 , false ),
96- new ButtonRenderingExpectation (window2 .button2 , false ));
97-
98- test (robot , window2 .radioButton1 ,
99- new ButtonRenderingExpectation (window1 .button1 , false ),
100- new ButtonRenderingExpectation (window1 .button2 , false ),
101- new ButtonRenderingExpectation (window2 .button1 , true ),
102- new ButtonRenderingExpectation (window2 .button2 , false ));
103-
104- test (robot , window2 .radioButton2 ,
105- new ButtonRenderingExpectation (window1 .button1 , false ),
106- new ButtonRenderingExpectation (window1 .button2 , false ),
107- new ButtonRenderingExpectation (window2 .button1 , false ),
108- new ButtonRenderingExpectation (window2 .button2 , true ));
109-
110- test (robot , window2 .radioButton3 ,
111- new ButtonRenderingExpectation (window1 .button1 , false ),
112- new ButtonRenderingExpectation (window1 .button2 , false ),
113- new ButtonRenderingExpectation (window2 .button1 , false ),
114- new ButtonRenderingExpectation (window2 .button2 , false ));
115-
116- System .out .println ("Test passed successfully" );
117- }
118109
119- private static void test (Robot robot , AbstractButton buttonToClick ,
120- ButtonRenderingExpectation ... expectations )
121- throws Exception {
122- robot .delay (100 );
123-
124- Point mouseLoc = buttonToClick .getLocationOnScreen ();
125- robot .mouseMove (mouseLoc .x + buttonToClick .getSize ().width / 2 ,
126- mouseLoc .y + buttonToClick .getSize ().height / 2 );
127- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
128- robot .delay (20 );
129- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
130-
131- robot .delay (100 );
132-
133- // the colors may change depending on your system's appearance.
134- // Depending on how you've configured "Appearance" in the
135- // System Settings app: the default button may be blue (the default),
136- // red, purple, etc. So instead of checking for a specific color: we'll
137- // make sure 3-4 are the same color, and one is significantly
138- // different.
139- Color defaultColor = null ;
140- Color nonDefaultColor = null ;
141-
142- for (ButtonRenderingExpectation expectation : expectations ) {
143- int x = expectation .button .getLocationOnScreen ().x + 20 ;
144- int y = expectation .button .getLocationOnScreen ().y + 10 ;
145-
146- // this mouseMove is optional, but it helps debug this test to see
147- // where we're sampling the pixel color from:
148- robot .mouseMove (x , y );
149-
150- Color c = robot .getPixelColor (x , y );
151- if (expectation .appearAsDefault ) {
152- if (defaultColor == null ) {
153- defaultColor = c ;
154- } else {
155- throw new IllegalStateException (
156- "there should only be at most 1 default button" );
157- }
158- } else {
159- if (nonDefaultColor == null ) {
160- nonDefaultColor = c ;
161- } else if (!isSimilar (nonDefaultColor , c )) {
162- throw new IllegalStateException (
163- "these two colors should match: " + c + ", " +
164- nonDefaultColor );
110+ Rectangle sum = new Rectangle ();
111+ sum .add (r1 );
112+ sum .add (r2 );
113+ BufferedImage bi = new BufferedImage (sum .width , sum .height ,
114+ BufferedImage .TYPE_INT_ARGB );
115+ Graphics2D g = bi .createGraphics ();
116+ window1 .paint (g .create (r1 .x , r1 .y , r1 .width , r1 .height ));
117+ window2 .paint (g .create (r2 .x , r2 .y , r2 .width , r2 .height ));
118+ g .dispose ();
119+
120+ // the exact colors may change depending on your system's
121+ // appearance. Depending on how you've configured "Appearance"
122+ // in the System Settings app: the default button may be blue
123+ // (the default), red, purple, etc. So instead of checking for
124+ // a specific color: we'll make sure 3-4 are the same color,
125+ // and one is significantly different.
126+ Color defaultColor = null ;
127+ Color nonDefaultColor = null ;
128+
129+ JButton [] buttons = new JButton [] {window1 .button1 ,
130+ window1 .button2 , window2 .button1 , window2 .button2 };
131+
132+ try {
133+ for (int a = 0 ; a < buttons .length ; a ++) {
134+ try {
135+ JButton b = buttons [a ];
136+
137+ Point p = b .getLocationOnScreen ();
138+ int x = p .x + 20 ;
139+ int y = p .y + 10 ;
140+
141+ Color c = new Color (bi .getRGB (x - sum .x , y - sum .y ));
142+ if (b .isDefaultButton ()) {
143+ if (defaultColor == null ) {
144+ defaultColor = c ;
145+ } else {
146+ throw new IllegalStateException (
147+ "there should only be at most 1 " +
148+ "default button" );
149+ }
150+ } else {
151+ if (nonDefaultColor == null ) {
152+ nonDefaultColor = c ;
153+ } else if (!isSimilar (nonDefaultColor , c )) {
154+ throw new IllegalStateException (
155+ "these two colors should match: " + c +
156+ ", " + nonDefaultColor );
157+ }
158+ }
159+
160+ if (defaultColor != null && nonDefaultColor != null &&
161+ isSimilar (defaultColor , nonDefaultColor )) {
162+ throw new IllegalStateException (
163+ "The default button and non-default " +
164+ "buttons should look " +
165+ "different: " + defaultColor +
166+ " matches " + nonDefaultColor );
167+ }
168+ } catch (Exception e ) {
169+ System .err .println ("a = " + a );
170+ throw e ;
171+ }
172+ }
173+ } finally {
174+ System .out .println ("defaultColor = " + defaultColor +
175+ " nonDefaultColor = " + nonDefaultColor );
176+
177+ window1 .dispose ();
178+ window2 .dispose ();
165179 }
166- }
167- }
168180
169- if (defaultColor != null && isSimilar (defaultColor , nonDefaultColor )) {
170- throw new IllegalStateException (
171- "The default button and non-default buttons should " +
172- "look different: " + defaultColor + " matches " +
173- nonDefaultColor );
181+ System .out .println ("Test passed successfully\n " );
182+ });
174183 }
175184 }
176185
@@ -187,63 +196,14 @@ private static boolean isSimilar(Color c1, Color c2) {
187196 return true ;
188197 }
189198
190- JRadioButton radioButton1 = new JRadioButton (
191- "\" Button 1\" is the default button" );
192- JRadioButton radioButton2 = new JRadioButton (
193- "\" Button 2\" is the default button" );
194- JRadioButton radioButton3 = new JRadioButton ("No default button" );
195-
196- JButton button1 = new JButton ("Button 1" );
197- JButton button2 = new JButton ("Button 2" );
198-
199- public RootPaneDefaultButtonTest () {
200- getContentPane ().setLayout (new BorderLayout ());
201- getContentPane ().add (createRadioButtonPanel (), BorderLayout .NORTH );
202- getContentPane ().add (createPushButtonRow (), BorderLayout .SOUTH );
203- pack ();
204-
205- radioButton1 .addActionListener (new ActionListener () {
206- @ Override
207- public void actionPerformed (ActionEvent e ) {
208- getRootPane ().setDefaultButton (button1 );
209- }
210- });
211-
212- radioButton2 .addActionListener (new ActionListener () {
213- @ Override
214- public void actionPerformed (ActionEvent e ) {
215- getRootPane ().setDefaultButton (button2 );
216- }
217- });
218-
219- radioButton3 .addActionListener (new ActionListener () {
220- @ Override
221- public void actionPerformed (ActionEvent e ) {
222- getRootPane ().setDefaultButton (null );
223- }
224- });
225-
226- ButtonGroup g = new ButtonGroup ();
227- g .add (radioButton1 );
228- g .add (radioButton2 );
229- g .add (radioButton3 );
230- radioButton1 .doClick ();
231- }
232-
233- private JPanel createPushButtonRow () {
234- JPanel p = new JPanel (new GridLayout (1 , 2 ));
235- p .add (button1 );
236- p .add (button2 );
237- p .setBorder (new EmptyBorder (5 ,5 ,5 ,5 ));
238- return p ;
239- }
199+ public static void main (String [] args ) throws Exception {
200+ if (!System .getProperty ("os.name" ).contains ("OS X" )) {
201+ System .out .println ("This test is for MacOS only." );
202+ return ;
203+ }
240204
241- private JPanel createRadioButtonPanel () {
242- JPanel p = new JPanel (new GridLayout (3 , 1 ));
243- p .add (radioButton1 );
244- p .add (radioButton2 );
245- p .add (radioButton3 );
246- p .setBorder (new EmptyBorder (5 ,5 ,5 ,5 ));
247- return p ;
205+ new TestScene (true , false ).run ();
206+ new TestScene (false , true ).run ();
207+ new TestScene (false , false ).run ();
248208 }
249209}
0 commit comments