11/*
2- * Copyright (c) 2002, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2002, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525import java .awt .Choice ;
2626import java .awt .FlowLayout ;
2727import java .awt .Frame ;
28+ import java .awt .GraphicsConfiguration ;
29+ import java .awt .GraphicsEnvironment ;
2830import java .awt .Point ;
2931import java .awt .Robot ;
3032import java .awt .Window ;
3133import java .awt .event .FocusAdapter ;
3234import java .awt .event .FocusEvent ;
3335import java .awt .event .InputEvent ;
3436import java .awt .event .KeyEvent ;
37+ import java .awt .image .BufferedImage ;
38+ import java .io .File ;
39+ import java .io .IOException ;
3540import java .util .concurrent .CountDownLatch ;
3641import java .util .concurrent .TimeUnit ;
3742
43+ import javax .imageio .ImageIO ;
44+
3845/**
3946 * @test
4047 * @bug 4478780
@@ -49,12 +56,12 @@ public class AccessibleChoiceTest {
4956 Button def = new Button ("default owner" );
5057 CountDownLatch go = new CountDownLatch (1 );
5158
52- public static void main (final String [] args ) {
59+ public static void main (final String [] args ) throws IOException {
5360 AccessibleChoiceTest app = new AccessibleChoiceTest ();
5461 app .test ();
5562 }
5663
57- private void test () {
64+ private void test () throws IOException {
5865 try {
5966 init ();
6067 start ();
@@ -77,7 +84,7 @@ public void focusGained(FocusEvent e) {
7784 win .add (choice );
7885 }
7986
80- public void start () {
87+ public void start () throws IOException {
8188 frame .setVisible (true );
8289 win .pack ();
8390 win .setLocation (100 , 200 );
@@ -89,15 +96,16 @@ public void start () {
8996 } catch (Exception ex ) {
9097 throw new RuntimeException ("Can't create robot" );
9198 }
92- robot .delay (2000 );
99+ robot .waitForIdle ();
100+ robot .delay (1000 );
93101 robot .setAutoDelay (150 );
94102 robot .setAutoWaitForIdle (true );
95103
96104 // Focus default button and wait till it gets focus
97105 Point loc = def .getLocationOnScreen ();
98106 robot .mouseMove (loc .x +2 , loc .y +2 );
99- robot .mousePress (InputEvent .BUTTON1_MASK );
100- robot .mouseRelease (InputEvent .BUTTON1_MASK );
107+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
108+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
101109
102110 try {
103111 go .await (1 , TimeUnit .SECONDS );
@@ -120,10 +128,13 @@ public void start () {
120128 robot .keyPress (KeyEvent .VK_DOWN );
121129 robot .keyRelease (KeyEvent .VK_DOWN );
122130
131+ robot .delay (500 );
132+
123133 String osName = System .getProperty ("os.name" ).toLowerCase ();
124134 if (osName .startsWith ("mac" )) {
125135 robot .keyPress (KeyEvent .VK_DOWN );
126136 robot .keyRelease (KeyEvent .VK_DOWN );
137+ robot .delay (500 );
127138 robot .keyPress (KeyEvent .VK_ENTER );
128139 robot .keyRelease (KeyEvent .VK_ENTER );
129140 }
@@ -132,6 +143,16 @@ public void start () {
132143
133144 // On success second item should be selected
134145 if (choice .getSelectedItem () != choice .getItem (1 )) {
146+ // Print out os name to check if mac conditional is relevant
147+ System .err .println ("Failed on os: " + osName );
148+
149+ // Save image to better debug the status of test when failing
150+ GraphicsConfiguration ge = GraphicsEnvironment
151+ .getLocalGraphicsEnvironment ().getDefaultScreenDevice ()
152+ .getDefaultConfiguration ();
153+ BufferedImage failImage = robot .createScreenCapture (ge .getBounds ());
154+ ImageIO .write (failImage , "png" , new File ("failImage.png" ));
155+
135156 throw new RuntimeException ("Choice can't be controlled by keyboard" );
136157 }
137158 }
0 commit comments