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