1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   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  * @key headful
  42  * @bug 6263470
  43  * @summary Tries to change font of MenuBar. Test passes if the font has changed
  44  * fails otherwise.
  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 (sun.awt.OSInfo.getOSType() == sun.awt.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 Button button = new Button("Click Me");
  80         button.addActionListener(new Listener());
  81         frame.setLayout(new CardLayout());
  82         frame.add(button, "First");
  83         frame.setSize(400, 400);
  84         frame.setVisible(true);
  85         sleep();
  86 
  87         final int fInsets = frame.getInsets().top;  //Frame insets without menu.
  88         addMenu();
  89         final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
  90         final int menuBarHeight = fMenuInsets - fInsets;
  91         // There is no way to change menubar height on windows. But on windows
  92         // we can try to split menubar in 2 rows.
  93         for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
  94             // Fill whole menubar.
  95             addMenu();
  96         }
  97 
  98         mb.remove(0);
  99         frame.validate();
 100         sleep();
 101 
 102         // Test execution.
 103         // On XToolkit, menubar font should be changed to 60.
 104         // On WToolkit, menubar font should be changed to default and menubar
 105         // should be splitted in 2 rows.
 106         mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
 107         sleep();
 108 
 109         final Robot r = new Robot();
 110         r.setAutoDelay(200);
 111         final Point pt = frame.getLocation();
 112         r.mouseMove(pt.x + frame.getWidth() / 2,
 113                     pt.y + fMenuInsets + menuBarHeight / 2);
 114         r.mousePress(InputEvent.BUTTON1_MASK);
 115         r.mouseRelease(InputEvent.BUTTON1_MASK);
 116 
 117         sleep();
 118         frame.dispose();
 119 
 120         if (clicked) {
 121             fail("Font was not changed");
 122         }
 123     }
 124 
 125     private static void sleep() {
 126         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 127         try {
 128             Thread.sleep(500L);
 129         } catch (InterruptedException ignored) {
 130         }
 131     }
 132 
 133     private static void fail(final String message) {
 134         throw new RuntimeException(message);
 135     }
 136 }