|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 2023, 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 java.awt.Frame; |
| 25 | +import java.awt.Menu; |
| 26 | +import java.awt.MenuBar; |
| 27 | +import java.awt.event.MouseAdapter; |
| 28 | +import java.awt.event.MouseEvent; |
| 29 | +import javax.swing.SwingUtilities; |
| 30 | + |
| 31 | +/* |
| 32 | + * @test |
| 33 | + * @bug 4028130 |
| 34 | + * @summary Test dynamically adding and removing a menu bar |
| 35 | + * @library /java/awt/regtesthelpers |
| 36 | + * @build PassFailJFrame |
| 37 | + * @run main/manual AddRemoveMenuBarTest_1 |
| 38 | + */ |
| 39 | + |
| 40 | +public class AddRemoveMenuBarTest_1 { |
| 41 | + |
| 42 | + private static final String INSTRUCTIONS = """ |
| 43 | + An initially empty frame should appear. |
| 44 | +
|
| 45 | + Click anywhere in the frame to add a menu bar at the top of the frame. |
| 46 | +
|
| 47 | + Click again to replace the menu bar with another menu bar. |
| 48 | +
|
| 49 | + Each menu bar has one (empty) menu, labelled with the |
| 50 | + number of the menu bar appearing. |
| 51 | +
|
| 52 | + After a menubar is added, the frame should not be resized nor repositioned |
| 53 | + on the screen; |
| 54 | +
|
| 55 | + it should have the same size and position. |
| 56 | +
|
| 57 | + Upon test completion, click Pass or Fail appropriately. |
| 58 | + """; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + PassFailJFrame passFailJFrame = new PassFailJFrame.Builder() |
| 62 | + .title("AddRemoveMenuBarTest_1 Instructions") |
| 63 | + .instructions(INSTRUCTIONS) |
| 64 | + .testTimeOut(5) |
| 65 | + .rows(18) |
| 66 | + .columns(45) |
| 67 | + .build(); |
| 68 | + |
| 69 | + SwingUtilities.invokeAndWait(() -> { |
| 70 | + AddRemoveMenuBar_1 frame = new AddRemoveMenuBar_1(); |
| 71 | + |
| 72 | + PassFailJFrame.addTestWindow(frame); |
| 73 | + PassFailJFrame.positionTestWindow(frame, |
| 74 | + PassFailJFrame.Position.HORIZONTAL); |
| 75 | + |
| 76 | + frame.setVisible(true); |
| 77 | + }); |
| 78 | + |
| 79 | + passFailJFrame.awaitAndCheck(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class AddRemoveMenuBar_1 extends Frame { |
| 84 | + int menuCount; |
| 85 | + |
| 86 | + AddRemoveMenuBar_1() { |
| 87 | + super("AddRemoveMenuBar_1"); |
| 88 | + setSize(200, 200); |
| 89 | + menuCount = 0; |
| 90 | + |
| 91 | + addMouseListener(new MouseAdapter() { |
| 92 | + @Override |
| 93 | + public void mouseClicked(MouseEvent e) { |
| 94 | + setMenuBar(); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + void setMenuBar() { |
| 100 | + MenuBar bar = new MenuBar(); |
| 101 | + bar.add(new Menu(Integer.toString(menuCount++))); |
| 102 | + setMenuBar(bar); |
| 103 | + } |
| 104 | +} |
0 commit comments