1 /*
   2   @test
   3   @bug       7125044
   4   @summary   Tests default focus traversal policy in Swing toplevel windows.
   5   @author    anton.tarasov@sun.com: area=awt.focus
   6   @run       main InitialFTP_Swing
   7 */
   8 
   9 import java.awt.FlowLayout;
  10 import java.awt.FocusTraversalPolicy;
  11 import java.awt.Window;
  12 import javax.swing.JButton;
  13 import javax.swing.JFrame;
  14 import javax.swing.JList;
  15 import javax.swing.JTextArea;
  16 import javax.swing.LayoutFocusTraversalPolicy;
  17 
  18 public class InitialFTP_Swing {
  19     public static void main(String[] args) {
  20         SwingFrame f0 = new SwingFrame("frame0");
  21         f0.setVisible(true);
  22 
  23         test(f0, LayoutFocusTraversalPolicy.class);
  24         
  25         SwingFrame f1 = new SwingFrame("frame1");
  26         f1.setVisible(true);        
  27         
  28         test(f1, LayoutFocusTraversalPolicy.class);
  29         
  30         System.out.println("Test passed.");
  31     }
  32     
  33     public static void test(Window win, Class<? extends FocusTraversalPolicy> expectedPolicy) {
  34         FocusTraversalPolicy ftp = win.getFocusTraversalPolicy();
  35         
  36         System.out.println("Tested window:    " + win + "\n" +
  37                            "Expected policy:  " + expectedPolicy + "\n" +
  38                            "Effective policy: " + ftp.getClass());
  39         
  40         if (!expectedPolicy.equals(ftp.getClass())) {
  41             throw new RuntimeException("Test failed: unexpected focus policy");
  42         }        
  43     }
  44 }
  45 
  46 class SwingFrame extends JFrame {
  47     JButton button = new JButton("button");
  48     JTextArea text = new JTextArea("qwerty");
  49     JList list = new JList(new String[] {"one", "two", "three"});
  50     
  51     public SwingFrame(String title) {
  52         super(title);
  53 
  54         this.setLayout(new FlowLayout());
  55         this.add(button);
  56         this.add(text);
  57         this.add(list);
  58         this.pack();
  59     }
  60 }