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  * @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 }