test/java/awt/MenuBar/MenuBarSetFont/MenuBarSetFont.java

Print this page




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import sun.awt.SunToolkit;
  25 
  26 import java.awt.Button;
  27 import java.awt.CardLayout;
  28 import java.awt.Font;
  29 import java.awt.Frame;
  30 import java.awt.Menu;
  31 import java.awt.MenuBar;
  32 import java.awt.Point;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.ActionListener;
  37 import java.awt.event.InputEvent;
  38 


  39 /**
  40  * @test
  41  * @bug 6263470
  42  * @summary Tries to change font of MenuBar. Test passes if the font has changed
  43  * fails otherwise.


  44  * @author Vyacheslav.Baranov: area=menu
  45  * @run main MenuBarSetFont
  46  */
  47 public final class MenuBarSetFont {
  48 
  49     private static final Frame frame = new Frame();
  50     private static final MenuBar mb = new MenuBar();
  51     private static volatile boolean clicked;
  52 
  53     private static final class Listener implements ActionListener {
  54         @Override
  55         public void actionPerformed(final ActionEvent e) {
  56             //Click on this button is performed
  57             //_only_ if font of MenuBar is not changed on time
  58             MenuBarSetFont.clicked = true;
  59         }
  60     }
  61 
  62     private static void addMenu() {
  63         mb.add(new Menu("w"));
  64         frame.validate();
  65     }
  66 
  67     public static void main(final String[] args) throws Exception {
  68 
  69         if (sun.awt.OSInfo.getOSType() == sun.awt.OSInfo.OSType.MACOSX) {
  70             System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
  71             return;
  72         }
  73 
  74         //Components initialization.
  75         frame.setMenuBar(mb);
  76         mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
  77 



  78         final Button button = new Button("Click Me");
  79         button.addActionListener(new Listener());
  80         frame.setLayout(new CardLayout());
  81         frame.add(button, "First");
  82         frame.setSize(400, 400);
  83         frame.setVisible(true);
  84         sleep();
  85 
  86         final int fInsets = frame.getInsets().top;  //Frame insets without menu.
  87         addMenu();
  88         final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
  89         final int menuBarHeight = fMenuInsets - fInsets;
  90         // There is no way to change menubar height on windows. But on windows
  91         // we can try to split menubar in 2 rows.
  92         for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
  93             // Fill whole menubar.
  94             addMenu();
  95         }
  96 
  97         mb.remove(0);
  98         frame.validate();
  99         sleep();
 100 
 101         // Test execution.
 102         // On XToolkit, menubar font should be changed to 60.
 103         // On WToolkit, menubar font should be changed to default and menubar
 104         // should be splitted in 2 rows.
 105         mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
 106         sleep();
 107 
 108         final Robot r = new Robot();
 109         r.setAutoDelay(200);
 110         final Point pt = frame.getLocation();
 111         r.mouseMove(pt.x + frame.getWidth() / 2,
 112                     pt.y + fMenuInsets + menuBarHeight / 2);
 113         r.mousePress(InputEvent.BUTTON1_MASK);
 114         r.mouseRelease(InputEvent.BUTTON1_MASK);
 115 
 116         sleep();
 117         frame.dispose();
 118 
 119         if (clicked) {
 120             fail("Font was not changed");
 121         }
 122     }
 123 
 124     private static void sleep() {
 125         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 126         try {
 127             Thread.sleep(500L);
 128         } catch (InterruptedException ignored) {
 129         }
 130     }
 131 
 132     private static void fail(final String message) {
 133         throw new RuntimeException(message);
 134     }
 135 }


   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 


  24 import java.awt.Button;
  25 import java.awt.CardLayout;
  26 import java.awt.Font;
  27 import java.awt.Frame;
  28 import java.awt.Menu;
  29 import java.awt.MenuBar;
  30 import java.awt.Point;
  31 import java.awt.Robot;

  32 import java.awt.event.ActionEvent;
  33 import java.awt.event.ActionListener;
  34 import java.awt.event.InputEvent;
  35 
  36 import jdk.testlibrary.OSInfo;
  37 
  38 /**
  39  * @test
  40  * @bug 6263470
  41  * @summary Tries to change font of MenuBar. Test passes if the font has changed
  42  * fails otherwise.
  43  * @library ../../../../lib/testlibrary
  44  * @build jdk.testlibrary.OSInfo
  45  * @author Vyacheslav.Baranov: area=menu
  46  * @run main MenuBarSetFont
  47  */
  48 public final class MenuBarSetFont {
  49 
  50     private static final Frame frame = new Frame();
  51     private static final MenuBar mb = new MenuBar();
  52     private static volatile boolean clicked;
  53 
  54     private static final class Listener implements ActionListener {
  55         @Override
  56         public void actionPerformed(final ActionEvent e) {
  57             //Click on this button is performed
  58             //_only_ if font of MenuBar is not changed on time
  59             MenuBarSetFont.clicked = true;
  60         }
  61     }
  62 
  63     private static void addMenu() {
  64         mb.add(new Menu("w"));
  65         frame.validate();
  66     }
  67 
  68     public static void main(final String[] args) throws Exception {
  69 
  70         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
  71             System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
  72             return;
  73         }
  74 
  75         //Components initialization.
  76         frame.setMenuBar(mb);
  77         mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
  78 
  79         final Robot r = new Robot();
  80         r.setAutoDelay(200);
  81 
  82         final Button button = new Button("Click Me");
  83         button.addActionListener(new Listener());
  84         frame.setLayout(new CardLayout());
  85         frame.add(button, "First");
  86         frame.setSize(400, 400);
  87         frame.setVisible(true);
  88         sleep(r);
  89 
  90         final int fInsets = frame.getInsets().top;  //Frame insets without menu.
  91         addMenu();
  92         final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
  93         final int menuBarHeight = fMenuInsets - fInsets;
  94         // There is no way to change menubar height on windows. But on windows
  95         // we can try to split menubar in 2 rows.
  96         for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
  97             // Fill whole menubar.
  98             addMenu();
  99         }
 100 
 101         mb.remove(0);
 102         frame.validate();
 103         sleep(r);
 104 
 105         // Test execution.
 106         // On XToolkit, menubar font should be changed to 60.
 107         // On WToolkit, menubar font should be changed to default and menubar
 108         // should be splitted in 2 rows.
 109         mb.setFont(new Font("Helvetica", Font.ITALIC, 60));

 110 
 111         sleep(r);
 112 
 113         final Point pt = frame.getLocation();
 114         r.mouseMove(pt.x + frame.getWidth() / 2,
 115                     pt.y + fMenuInsets + menuBarHeight / 2);
 116         r.mousePress(InputEvent.BUTTON1_MASK);
 117         r.mouseRelease(InputEvent.BUTTON1_MASK);
 118 
 119         sleep(r);
 120         frame.dispose();
 121 
 122         if (clicked) {
 123             fail("Font was not changed");
 124         }
 125     }
 126 
 127     private static void sleep(Robot robot) {
 128         robot.waitForIdle();
 129         try {
 130             Thread.sleep(500L);
 131         } catch (InterruptedException ignored) {
 132         }
 133     }
 134 
 135     private static void fail(final String message) {
 136         throw new RuntimeException(message);
 137     }
 138 }