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 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  * @key headful
  41  * @bug 6263470
  42  * @summary Tries to change font of MenuBar. Test passes if the font has changed
  43  * fails otherwise.
  44  * @library ../../../../lib/testlibrary
  45  * @build jdk.testlibrary.OSInfo
  46  * @author Vyacheslav.Baranov: area=menu
  47  * @run main MenuBarSetFont
  48  */
  49 public final class MenuBarSetFont {
  50 
  51     private static final Frame frame = new Frame();
  52     private static final MenuBar mb = new MenuBar();
  53     private static volatile boolean clicked;
  54 
  55     private static final class Listener implements ActionListener {
  56         @Override
  57         public void actionPerformed(final ActionEvent e) {
  58             //Click on this button is performed
  59             //_only_ if font of MenuBar is not changed on time
  60             MenuBarSetFont.clicked = true;
  61         }
  62     }
  63 
  64     private static void addMenu() {
  65         mb.add(new Menu("w"));
  66         frame.validate();
  67     }
  68 
  69     public static void main(final String[] args) throws Exception {
  70 
  71         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
  72             System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
  73             return;
  74         }
  75 
  76         //Components initialization.
  77         frame.setMenuBar(mb);
  78         mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
  79 
  80         final Robot r = new Robot();
  81         r.setAutoDelay(200);
  82 
  83         final Button button = new Button("Click Me");
  84         button.addActionListener(new Listener());
  85         frame.setLayout(new CardLayout());
  86         frame.add(button, "First");
  87         frame.setSize(400, 400);
  88         frame.setVisible(true);
  89         sleep(r);
  90 
  91         final int fInsets = frame.getInsets().top;  //Frame insets without menu.
  92         addMenu();
  93         final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
  94         final int menuBarHeight = fMenuInsets - fInsets;
  95         // There is no way to change menubar height on windows. But on windows
  96         // we can try to split menubar in 2 rows.
  97         for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
  98             // Fill whole menubar.
  99             addMenu();
 100         }
 101 
 102         mb.remove(0);
 103         frame.validate();
 104         sleep(r);
 105 
 106         // Test execution.
 107         // On XToolkit, menubar font should be changed to 60.
 108         // On WToolkit, menubar font should be changed to default and menubar
 109         // should be splitted in 2 rows.
 110         mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
 111 
 112         sleep(r);
 113 
 114         final Point pt = frame.getLocation();
 115         r.mouseMove(pt.x + frame.getWidth() / 2,
 116                     pt.y + fMenuInsets + menuBarHeight / 2);
 117         r.mousePress(InputEvent.BUTTON1_MASK);
 118         r.mouseRelease(InputEvent.BUTTON1_MASK);
 119 
 120         sleep(r);
 121         frame.dispose();
 122 
 123         if (clicked) {
 124             fail("Font was not changed");
 125         }
 126     }
 127 
 128     private static void sleep(Robot robot) {
 129         robot.waitForIdle();
 130         try {
 131             Thread.sleep(500L);
 132         } catch (InterruptedException ignored) {
 133         }
 134     }
 135 
 136     private static void fail(final String message) {
 137         throw new RuntimeException(message);
 138     }
 139 }