Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/MenuBar/MenuBarSetFont/MenuBarSetFont.java
38828 views
/*1* Copyright (c) 2011, 2013, 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.testlibrary.OSInfo;3637/**38* @test39* @bug 626347040* @summary Tries to change font of MenuBar. Test passes if the font has changed41* fails otherwise.42* @library ../../../../lib/testlibrary43* @build jdk.testlibrary.OSInfo44* @author Vyacheslav.Baranov: area=menu45* @run main MenuBarSetFont46*/47public final class MenuBarSetFont {4849private static final Frame frame = new Frame();50private static final MenuBar mb = new MenuBar();51private static volatile boolean clicked;5253private static final class Listener implements ActionListener {54@Override55public void actionPerformed(final ActionEvent e) {56//Click on this button is performed57//_only_ if font of MenuBar is not changed on time58MenuBarSetFont.clicked = true;59}60}6162private static void addMenu() {63mb.add(new Menu("w"));64frame.validate();65}6667public static void main(final String[] args) throws Exception {6869if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {70System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");71return;72}7374//Components initialization.75frame.setMenuBar(mb);76mb.setFont(new Font("Helvetica", Font.ITALIC, 5));7778final Robot r = new Robot();79r.setAutoDelay(200);8081final Button button = new Button("Click Me");82button.addActionListener(new Listener());83frame.setLayout(new CardLayout());84frame.add(button, "First");85frame.setSize(400, 400);86frame.setVisible(true);87sleep(r);8889final int fInsets = frame.getInsets().top; //Frame insets without menu.90addMenu();91final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.92final int menuBarHeight = fMenuInsets - fInsets;93// There is no way to change menubar height on windows. But on windows94// we can try to split menubar in 2 rows.95for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {96// Fill whole menubar.97addMenu();98}99100mb.remove(0);101frame.validate();102sleep(r);103104// Test execution.105// On XToolkit, menubar font should be changed to 60.106// On WToolkit, menubar font should be changed to default and menubar107// should be splitted in 2 rows.108mb.setFont(new Font("Helvetica", Font.ITALIC, 60));109110sleep(r);111112final Point pt = frame.getLocation();113r.mouseMove(pt.x + frame.getWidth() / 2,114pt.y + fMenuInsets + menuBarHeight / 2);115r.mousePress(InputEvent.BUTTON1_MASK);116r.mouseRelease(InputEvent.BUTTON1_MASK);117118sleep(r);119frame.dispose();120121if (clicked) {122fail("Font was not changed");123}124}125126private static void sleep(Robot robot) {127robot.waitForIdle();128try {129Thread.sleep(500L);130} catch (InterruptedException ignored) {131}132}133134private static void fail(final String message) {135throw new RuntimeException(message);136}137}138139140