test/java/awt/Window/AlwaysOnTop/TestAlwaysOnTopBeforeShow.java

Print this page




  23 /*
  24 @test
  25 @bug 6236247
  26 @summary Test that setting of always-on-top flags before showing window works
  27 @author dom@sparc.spb.su: area=awt.toplevel
  28 @run main TestAlwaysOnTopBeforeShow
  29 */
  30 
  31 /**
  32  * TestAlwaysOnTopBeforeShow.java
  33  *
  34  * summary:  Test that always-on-top works in the following situations:
  35  * - when set on a window before showing
  36  * - when set on a child dialog
  37  * - that it doesn't generate focus event when set on an invisible window
  38  */
  39 
  40 import java.awt.*;
  41 import java.awt.event.*;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import sun.awt.SunToolkit;
  44 
  45 
  46 //*** global search and replace TestAlwaysOnTopBeforeShow with name of the test ***
  47 
  48 public class TestAlwaysOnTopBeforeShow
  49 {
  50 
  51     //*** test-writer defined static variables go here ***
  52 
  53     private static AtomicBoolean focused = new AtomicBoolean();
  54     private static AtomicBoolean pressed = new AtomicBoolean();
  55     private static volatile Object pressedTarget;

  56     private static void init()
  57     {
  58         //*** Create instructions for the user here ***
  59 
  60         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  61                 public void eventDispatched(AWTEvent e) {
  62                     if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  63                         synchronized(pressed) {
  64                             pressed.set(true);
  65                             pressedTarget = e.getSource();
  66                             pressed.notifyAll();
  67                         }
  68                     }
  69                 }
  70             }, AWTEvent.MOUSE_EVENT_MASK);
  71 
  72         Frame f = new Frame("always-on-top");
  73         f.setBounds(0, 0, 200, 200);
  74         f.addFocusListener(new FocusAdapter() {
  75                 public void focusGained(FocusEvent e) {


 106         d.pack();
 107         d.setBounds(0, 0, 100, 100);
 108 
 109         waitForIdle(1000);
 110         checkOnTop(f, f2, location.x + size.width / 2, location.y + size.height / 2);
 111         waitForIdle(1000);
 112 
 113         focused.set(false);
 114         f.setVisible(false);
 115         f.setAlwaysOnTop(false);
 116         waitForIdle(1000);
 117         if (focused.get()) {
 118             throw new RuntimeException("Always-on-top generated focus event");
 119         }
 120 
 121         TestAlwaysOnTopBeforeShow.pass();
 122 
 123     }//End  init()
 124 
 125     private static void waitForIdle(int mls) {
 126         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 127         try {




 128             Thread.sleep(mls);
 129         } catch (InterruptedException e) {
 130             e.printStackTrace();
 131         }
 132     }
 133 
 134     static void waitFocused(Window w, AtomicBoolean b) {
 135         try {
 136             synchronized(b) {
 137                 if (w.isFocusOwner()) {
 138                     return;
 139                 }
 140                 b.wait(3000);
 141             }
 142         } catch (Exception e) {
 143             throw new RuntimeException(e);
 144         }
 145         if (!w.isFocusOwner()) {
 146             throw new RuntimeException("Can't make " + w + " focus owner");
 147         }
 148     }
 149 




  23 /*
  24 @test
  25 @bug 6236247
  26 @summary Test that setting of always-on-top flags before showing window works
  27 @author dom@sparc.spb.su: area=awt.toplevel
  28 @run main TestAlwaysOnTopBeforeShow
  29 */
  30 
  31 /**
  32  * TestAlwaysOnTopBeforeShow.java
  33  *
  34  * summary:  Test that always-on-top works in the following situations:
  35  * - when set on a window before showing
  36  * - when set on a child dialog
  37  * - that it doesn't generate focus event when set on an invisible window
  38  */
  39 
  40 import java.awt.*;
  41 import java.awt.event.*;
  42 import java.util.concurrent.atomic.AtomicBoolean;

  43 
  44 
  45 //*** global search and replace TestAlwaysOnTopBeforeShow with name of the test ***
  46 
  47 public class TestAlwaysOnTopBeforeShow
  48 {
  49 
  50     //*** test-writer defined static variables go here ***
  51 
  52     private static AtomicBoolean focused = new AtomicBoolean();
  53     private static AtomicBoolean pressed = new AtomicBoolean();
  54     private static volatile Object pressedTarget;
  55     private static Robot robot = null;
  56     private static void init()
  57     {
  58         //*** Create instructions for the user here ***
  59 
  60         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  61                 public void eventDispatched(AWTEvent e) {
  62                     if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  63                         synchronized(pressed) {
  64                             pressed.set(true);
  65                             pressedTarget = e.getSource();
  66                             pressed.notifyAll();
  67                         }
  68                     }
  69                 }
  70             }, AWTEvent.MOUSE_EVENT_MASK);
  71 
  72         Frame f = new Frame("always-on-top");
  73         f.setBounds(0, 0, 200, 200);
  74         f.addFocusListener(new FocusAdapter() {
  75                 public void focusGained(FocusEvent e) {


 106         d.pack();
 107         d.setBounds(0, 0, 100, 100);
 108 
 109         waitForIdle(1000);
 110         checkOnTop(f, f2, location.x + size.width / 2, location.y + size.height / 2);
 111         waitForIdle(1000);
 112 
 113         focused.set(false);
 114         f.setVisible(false);
 115         f.setAlwaysOnTop(false);
 116         waitForIdle(1000);
 117         if (focused.get()) {
 118             throw new RuntimeException("Always-on-top generated focus event");
 119         }
 120 
 121         TestAlwaysOnTopBeforeShow.pass();
 122 
 123     }//End  init()
 124 
 125     private static void waitForIdle(int mls) {

 126         try {
 127             if(robot == null) {
 128                 robot = new Robot();
 129             }
 130             robot.waitForIdle();
 131             Thread.sleep(mls);
 132         } catch (Exception e) {
 133             e.printStackTrace();
 134         }
 135     }
 136 
 137     static void waitFocused(Window w, AtomicBoolean b) {
 138         try {
 139             synchronized(b) {
 140                 if (w.isFocusOwner()) {
 141                     return;
 142                 }
 143                 b.wait(3000);
 144             }
 145         } catch (Exception e) {
 146             throw new RuntimeException(e);
 147         }
 148         if (!w.isFocusOwner()) {
 149             throw new RuntimeException("Can't make " + w + " focus owner");
 150         }
 151     }
 152