MenuBarSetFont.java revision 9330:8b1f1c2a400f
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
24import sun.awt.SunToolkit;
25
26import java.awt.Button;
27import java.awt.CardLayout;
28import java.awt.Font;
29import java.awt.Frame;
30import java.awt.Menu;
31import java.awt.MenuBar;
32import java.awt.Point;
33import java.awt.Robot;
34import java.awt.Toolkit;
35import java.awt.event.ActionEvent;
36import java.awt.event.ActionListener;
37import 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 */
47public 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
69        if (sun.awt.OSInfo.getOSType() == sun.awt.OSInfo.OSType.MACOSX) {
70            System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
71            return;
72        }
73
74        //Components initialization.
75        frame.setMenuBar(mb);
76        mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
77
78        final Button button = new Button("Click Me");
79        button.addActionListener(new Listener());
80        frame.setLayout(new CardLayout());
81        frame.add(button, "First");
82        frame.setSize(400, 400);
83        frame.setVisible(true);
84        sleep();
85
86        final int fInsets = frame.getInsets().top;  //Frame insets without menu.
87        addMenu();
88        final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
89        final int menuBarHeight = fMenuInsets - fInsets;
90        // There is no way to change menubar height on windows. But on windows
91        // we can try to split menubar in 2 rows.
92        for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
93            // Fill whole menubar.
94            addMenu();
95        }
96
97        mb.remove(0);
98        frame.validate();
99        sleep();
100
101        // Test execution.
102        // On XToolkit, menubar font should be changed to 60.
103        // On WToolkit, menubar font should be changed to default and menubar
104        // should be splitted in 2 rows.
105        mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
106        sleep();
107
108        final Robot r = new Robot();
109        r.setAutoDelay(200);
110        final Point pt = frame.getLocation();
111        r.mouseMove(pt.x + frame.getWidth() / 2,
112                    pt.y + fMenuInsets + menuBarHeight / 2);
113        r.mousePress(InputEvent.BUTTON1_MASK);
114        r.mouseRelease(InputEvent.BUTTON1_MASK);
115
116        sleep();
117        frame.dispose();
118
119        if (clicked) {
120            fail("Font was not changed");
121        }
122    }
123
124    private static void sleep() {
125        ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
126        try {
127            Thread.sleep(500L);
128        } catch (InterruptedException ignored) {
129        }
130    }
131
132    private static void fail(final String message) {
133        throw new RuntimeException(message);
134    }
135}
136