java/awt/Focus/ModalDialogInitialFocusTest/ModalDialogInitialFocusTest.java

Print this page


   1 /*
   2   test
   3   @bug       6382750
   4   @summary   Tests that modal dialog doesn't request extra initial focus on show.
   5   @author    anton.tarasov@sun.com: area=awt.focus
   6   @run       applet ModalDialogInitialFocusTest.html
   7 */
   8 
   9 import java.awt.*;
  10 import java.awt.event.*;
  11 import sun.awt.SunToolkit;
  12 import java.applet.Applet;
  13 import java.util.concurrent.atomic.AtomicBoolean;
  14 import java.lang.reflect.InvocationTargetException;
  15 
  16 public class ModalDialogInitialFocusTest extends Applet {
  17     Robot robot;
  18     SunToolkit toolkit;
  19 
  20     Dialog dialog = new Dialog((Window)null, "Test Dialog", Dialog.ModalityType.TOOLKIT_MODAL);
  21     Button button = new Button("button");
  22 
  23     volatile static boolean passed = true;
  24 
  25     public static void main(String[] args) {
  26         ModalDialogInitialFocusTest app = new ModalDialogInitialFocusTest();
  27         app.init();
  28         app.start();
  29     }
  30 
  31     public void init() {
  32         try {
  33             robot = new Robot();
  34         } catch (AWTException e) {
  35             throw new RuntimeException("Error: unable to create robot", e);
  36         }
  37         // Create instructions for the user here, as well as set up
  38         // the environment -- set the layout manager, add buttons,
  39         // etc.
  40         this.setLayout (new BorderLayout ());
  41         Sysout.createDialogWithInstructions(new String[]
  42             {"This is automatic test. Simply wait until it is done."
  43             });
  44     }
  45 
  46     public void start() {
  47         toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  48 
  49         dialog.setLayout(new FlowLayout());
  50         dialog.add(button);
  51         dialog.setBounds(800, 0, 100, 100);
  52 
  53         dialog.addFocusListener(new FocusAdapter() {
  54                 // The only expected FOCUS_GAINED is on the button.
  55                 public void focusGained(FocusEvent e) {
  56                     passed = false;
  57                 }
  58             });
  59 
  60         test();
  61     }
  62 
  63     void test() {
  64         new Thread(new Runnable() {
  65                 public void run() {
  66                   dialog.setVisible(true);
  67                 }
  68             }).start();
  69 
  70         waitTillShown(dialog);
  71 
  72         toolkit.realSync(); // wait for focus events
  73 
  74         dialog.dispose();
  75 
  76         if (passed) {
  77             Sysout.println("Test passed.");
  78         } else {
  79             throw new RuntimeException("Test failed: dialog requests extra focus on show!");
  80         }
  81     }
  82 
  83     void waitTillShown(Component c) {
  84         while (true) {
  85             try {
  86                 Thread.sleep(100);
  87                 c.getLocationOnScreen();
  88                 break;
  89             } catch (InterruptedException ie) {
  90                 ie.printStackTrace();
  91                 break;
  92             } catch (IllegalComponentStateException e) {


   1 /*
   2   test
   3   @bug       6382750
   4   @summary   Tests that modal dialog doesn't request extra initial focus on show.
   5   @author    anton.tarasov@sun.com: area=awt.focus
   6   @run       applet ModalDialogInitialFocusTest.html
   7 */
   8 
   9 import java.awt.*;
  10 import java.awt.event.*;

  11 import java.applet.Applet;
  12 import java.util.concurrent.atomic.AtomicBoolean;
  13 import java.lang.reflect.InvocationTargetException;
  14 
  15 public class ModalDialogInitialFocusTest extends Applet {
  16     Robot robot;

  17 
  18     Dialog dialog = new Dialog((Window)null, "Test Dialog", Dialog.ModalityType.TOOLKIT_MODAL);
  19     Button button = new Button("button");
  20 
  21     volatile static boolean passed = true;
  22 
  23     public static void main(String[] args) {
  24         ModalDialogInitialFocusTest app = new ModalDialogInitialFocusTest();
  25         app.init();
  26         app.start();
  27     }
  28 
  29     public void init() {
  30         try {
  31             robot = new Robot();
  32         } catch (AWTException e) {
  33             throw new RuntimeException("Error: unable to create robot", e);
  34         }
  35         // Create instructions for the user here, as well as set up
  36         // the environment -- set the layout manager, add buttons,
  37         // etc.
  38         this.setLayout (new BorderLayout ());
  39         Sysout.createDialogWithInstructions(new String[]
  40             {"This is automatic test. Simply wait until it is done."
  41             });
  42     }
  43 
  44     public void start() {

  45 
  46         dialog.setLayout(new FlowLayout());
  47         dialog.add(button);
  48         dialog.setBounds(800, 0, 100, 100);
  49 
  50         dialog.addFocusListener(new FocusAdapter() {
  51                 // The only expected FOCUS_GAINED is on the button.
  52                 public void focusGained(FocusEvent e) {
  53                     passed = false;
  54                 }
  55             });
  56 
  57         test();
  58     }
  59 
  60     void test() {
  61         new Thread(new Runnable() {
  62                 public void run() {
  63                   dialog.setVisible(true);
  64                 }
  65             }).start();
  66 
  67         waitTillShown(dialog);
  68 
  69         robot.waitForIdle();
  70 
  71         dialog.dispose();
  72 
  73         if (passed) {
  74             Sysout.println("Test passed.");
  75         } else {
  76             throw new RuntimeException("Test failed: dialog requests extra focus on show!");
  77         }
  78     }
  79 
  80     void waitTillShown(Component c) {
  81         while (true) {
  82             try {
  83                 Thread.sleep(100);
  84                 c.getLocationOnScreen();
  85                 break;
  86             } catch (InterruptedException ie) {
  87                 ie.printStackTrace();
  88                 break;
  89             } catch (IllegalComponentStateException e) {