001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.KeyEvent;
023: import java.util.Enumeration;
024: import java.util.HashSet;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * MenuBarTest
030: */
031: public class MenuBarTest extends TestCase {
032:
033: MenuBar mb;
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: mb = new MenuBar();
039: Menu file = new Menu("File");
040: mb.add(file);
041: file.add("New");
042: file.add("Open ...");
043: file.add("Save");
044: file.addSeparator();
045: file.add("Exit");
046: file.getItem(0).setShortcut(newShortcut(KeyEvent.VK_N));
047: file.getItem(1).setShortcut(newShortcut(KeyEvent.VK_O));
048: file.getItem(2).setShortcut(newShortcut(KeyEvent.VK_S));
049:
050: Menu edit = new Menu("Edit");
051: mb.add(edit);
052: edit.add("Cut");
053: edit.add("Copy");
054: edit.add("Paste");
055: edit.getItem(0).setShortcut(newShortcut(KeyEvent.VK_X));
056: edit.getItem(1).setShortcut(newShortcut(KeyEvent.VK_C));
057: edit.getItem(2).setShortcut(newShortcut(KeyEvent.VK_V));
058:
059: Menu help = new Menu("Help");
060: help.add("About");
061: mb.add(help);
062: mb.setHelpMenu(help);
063: }
064:
065: private MenuShortcut newShortcut(int vkey) {
066: return new MenuShortcut(vkey, false);
067: }
068:
069: public void testShortcuts() {
070: Enumeration<MenuShortcut> sh = mb.shortcuts();
071: assertNotNull(sh);
072: HashSet<MenuShortcut> s = new HashSet<MenuShortcut>();
073: while (sh.hasMoreElements()) {
074: s.add(sh.nextElement());
075: }
076: checkShortcut(s, KeyEvent.VK_N);
077: checkShortcut(s, KeyEvent.VK_O);
078: checkShortcut(s, KeyEvent.VK_S);
079: checkShortcut(s, KeyEvent.VK_X);
080: checkShortcut(s, KeyEvent.VK_C);
081: checkShortcut(s, KeyEvent.VK_V);
082: assertEquals(0, s.size());
083: }
084:
085: private void checkShortcut(HashSet<MenuShortcut> s, int vkey) {
086: MenuShortcut ms = newShortcut(vkey);
087: assertTrue(s.contains(ms));
088: s.remove(ms);
089: }
090:
091: public void testGetShortcutItem() {
092: checkShortcutItem("New", KeyEvent.VK_N);
093: checkShortcutItem("Open ...", KeyEvent.VK_O);
094: checkShortcutItem("Save", KeyEvent.VK_S);
095: checkShortcutItem("Cut", KeyEvent.VK_X);
096: checkShortcutItem("Copy", KeyEvent.VK_C);
097: checkShortcutItem("Paste", KeyEvent.VK_V);
098: checkShortcutItemNotExists(KeyEvent.VK_N, true);
099: checkShortcutItemNotExists(KeyEvent.VK_Z, false);
100: checkShortcutItemNotExists(KeyEvent.VK_Z, true);
101: }
102:
103: private void checkShortcutItem(String label, int vkey) {
104: MenuShortcut ms = newShortcut(vkey);
105: MenuItem mi = mb.getShortcutMenuItem(ms);
106: assertNotNull(mi);
107: assertEquals(label, mi.getLabel());
108: }
109:
110: private void checkShortcutItemNotExists(int vkey, boolean shift) {
111: MenuShortcut ms = new MenuShortcut(vkey, shift);
112: MenuItem mi = mb.getShortcutMenuItem(ms);
113: assertNull(mi);
114: }
115:
116: public void testRemove1() {
117: MenuBar m = new MenuBar();
118: m.remove(null);
119: assertTrue(true);
120: }
121:
122: public void testSetHelpMenu() {
123: MenuBar m = new MenuBar();
124: m.setHelpMenu(null);
125: assertTrue(true);
126: }
127: }
|