Path: blob/master/test/jdk/java/awt/MenuBar/MenuBarSetFont/MenuBarSetFont.java
41155 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Button;24import java.awt.CardLayout;25import java.awt.Font;26import java.awt.Frame;27import java.awt.Menu;28import java.awt.MenuBar;29import java.awt.Point;30import java.awt.Robot;31import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;33import java.awt.event.InputEvent;3435import jdk.test.lib.Platform;3637/**38* @test39* @key headful40* @bug 626347041* @summary Tries to change font of MenuBar. Test passes if the font has changed42* fails otherwise.43* @library /test/lib44* @build jdk.test.lib.Platform45* @author Vyacheslav.Baranov: area=menu46* @run main MenuBarSetFont47*/48public final class MenuBarSetFont {4950private static final Frame frame = new Frame();51private static final MenuBar mb = new MenuBar();52private static volatile boolean clicked;5354private static final class Listener implements ActionListener {55@Override56public void actionPerformed(final ActionEvent e) {57//Click on this button is performed58//_only_ if font of MenuBar is not changed on time59MenuBarSetFont.clicked = true;60}61}6263private static void addMenu() {64mb.add(new Menu("w"));65frame.validate();66}6768public static void main(final String[] args) throws Exception {6970if (Platform.isOSX()) {71System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");72return;73}7475//Components initialization.76frame.setMenuBar(mb);77mb.setFont(new Font("Helvetica", Font.ITALIC, 5));7879final Robot r = new Robot();80r.setAutoDelay(200);8182final Button button = new Button("Click Me");83button.addActionListener(new Listener());84frame.setLayout(new CardLayout());85frame.add(button, "First");86frame.setSize(400, 400);87frame.setVisible(true);88sleep(r);8990final int fInsets = frame.getInsets().top; //Frame insets without menu.91addMenu();92final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.93final int menuBarHeight = fMenuInsets - fInsets;94// There is no way to change menubar height on windows. But on windows95// we can try to split menubar in 2 rows.96for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {97// Fill whole menubar.98addMenu();99}100101mb.remove(0);102frame.validate();103sleep(r);104105// Test execution.106// On XToolkit, menubar font should be changed to 60.107// On WToolkit, menubar font should be changed to default and menubar108// should be splitted in 2 rows.109mb.setFont(new Font("Helvetica", Font.ITALIC, 60));110111sleep(r);112113final Point pt = frame.getLocation();114r.mouseMove(pt.x + frame.getWidth() / 2,115pt.y + fMenuInsets + menuBarHeight / 2);116r.mousePress(InputEvent.BUTTON1_MASK);117r.mouseRelease(InputEvent.BUTTON1_MASK);118119sleep(r);120frame.dispose();121122if (clicked) {123fail("Font was not changed");124}125}126127private static void sleep(Robot robot) {128robot.waitForIdle();129try {130Thread.sleep(500L);131} catch (InterruptedException ignored) {132}133}134135private static void fail(final String message) {136throw new RuntimeException(message);137}138}139140141