|
| 1 | +/* |
| 2 | + * Copyright (c) 2011, 2024, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6502052 |
| 27 | + * @summary Menu cells must resize if font changes (XToolkit) |
| 28 | + * @requires os.family == "linux" |
| 29 | + * @library /java/awt/regtesthelpers |
| 30 | + * @build PassFailJFrame |
| 31 | + * @run main/manual CellsResize |
| 32 | + */ |
| 33 | + |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Font; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.GridLayout; |
| 38 | +import java.awt.Menu; |
| 39 | +import java.awt.MenuBar; |
| 40 | +import java.awt.MenuComponent; |
| 41 | +import java.awt.MenuItem; |
| 42 | +import java.awt.Panel; |
| 43 | +import java.awt.PopupMenu; |
| 44 | +import java.awt.Toolkit; |
| 45 | +import java.awt.event.MouseAdapter; |
| 46 | +import java.awt.event.MouseEvent; |
| 47 | + |
| 48 | +public class CellsResize { |
| 49 | + private static Frame frame; |
| 50 | + private static MenuBar menuBar; |
| 51 | + private static PopupMenu popupMenu; |
| 52 | + private static Menu barSubMenu; |
| 53 | + private static Menu popupSubMenu; |
| 54 | + private static boolean fontMultiplied = false; |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + String INSTRUCTIONS = """ |
| 58 | + 1. Open all nested menus in menu bar. |
| 59 | + 2. Click on "popup-menu" button to show popup-menus. |
| 60 | + 3. Open all nested menus in popup-menu. |
| 61 | + 4. Click on "big-font" button (to make all menus have a |
| 62 | + bigger font). |
| 63 | + 5. Open all nested menus again (as described in 1, 2, 3). |
| 64 | + 6. If all menu items use a bigger font now and their labels fit |
| 65 | + into menu-item size, press "pass", otherwise press "fail". |
| 66 | + """; |
| 67 | + |
| 68 | + PassFailJFrame.builder() |
| 69 | + .title("Test Instructions") |
| 70 | + .instructions(INSTRUCTIONS) |
| 71 | + .rows((int) INSTRUCTIONS.lines().count() + 2) |
| 72 | + .columns(35) |
| 73 | + .testUI(CellsResize::createUI) |
| 74 | + .logArea(5) |
| 75 | + .build() |
| 76 | + .awaitAndCheck(); |
| 77 | + } |
| 78 | + |
| 79 | + public static Frame createUI () { |
| 80 | + if (!checkToolkit()) { |
| 81 | + new RuntimeException("Toolkit check failed."); |
| 82 | + } |
| 83 | + frame = new Frame("MenuBar Cell Resize Test"); |
| 84 | + |
| 85 | + popupMenu = new PopupMenu(); |
| 86 | + popupMenu.add(createMenu(false)); |
| 87 | + |
| 88 | + frame.add(popupMenu); |
| 89 | + |
| 90 | + menuBar = new MenuBar(); |
| 91 | + menuBar.add(createMenu(true)); |
| 92 | + |
| 93 | + frame.setMenuBar(menuBar); |
| 94 | + |
| 95 | + Button bp = new Button("popup-menu"); |
| 96 | + bp.addMouseListener(new MouseAdapter() { |
| 97 | + public void mouseReleased(MouseEvent e) { |
| 98 | + popupMenu.show(e.getComponent(), e.getX(), e.getY()); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + Button bf = new Button("big-font"); |
| 103 | + bf.addMouseListener(new MouseAdapter() { |
| 104 | + public void mouseReleased(MouseEvent e) { |
| 105 | + bigFont(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + Panel panel = new Panel(); |
| 110 | + panel.setLayout(new GridLayout(2, 1)); |
| 111 | + panel.add(bp); |
| 112 | + panel.add(bf); |
| 113 | + |
| 114 | + frame.add(panel); |
| 115 | + frame.setSize(300, 300); |
| 116 | + return frame; |
| 117 | + } |
| 118 | + |
| 119 | + static boolean checkToolkit() { |
| 120 | + String toolkitName = Toolkit.getDefaultToolkit().getClass().getName(); |
| 121 | + return toolkitName.equals("sun.awt.X11.XToolkit"); |
| 122 | + } |
| 123 | + |
| 124 | + static Menu createMenu(boolean bar) { |
| 125 | + Menu menu1 = new Menu("Menu-1"); |
| 126 | + Menu menu11 = new Menu("Menu-11"); |
| 127 | + menu1.add(menu11); |
| 128 | + if (bar) { |
| 129 | + barSubMenu = menu11; |
| 130 | + } else { |
| 131 | + popupSubMenu = menu11; |
| 132 | + } |
| 133 | + menu11.add(new MenuItem("MenuItem")); |
| 134 | + return menu1; |
| 135 | + } |
| 136 | + |
| 137 | + static void bigFont() { |
| 138 | + if (fontMultiplied) { |
| 139 | + return; |
| 140 | + } else { |
| 141 | + fontMultiplied = true; |
| 142 | + } |
| 143 | + |
| 144 | + multiplyFont(barSubMenu, 7); |
| 145 | + multiplyFont(popupSubMenu, 7); |
| 146 | + |
| 147 | + // NOTE: if previous two are moved below following |
| 148 | + // two, they get their font multiplied twice. |
| 149 | + |
| 150 | + multiplyFont(menuBar, 5); |
| 151 | + multiplyFont(popupMenu, 5); |
| 152 | + } |
| 153 | + |
| 154 | + static void multiplyFont(MenuComponent comp, int times) { |
| 155 | + Font font = comp.getFont(); |
| 156 | + float size = font.getSize() * times; |
| 157 | + comp.setFont(font.deriveFont(size)); |
| 158 | + } |
| 159 | +} |
0 commit comments