001 /* 002 * Created on Jan 31, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2008-2010 the original author or authors. 014 */ 015 package org.fest.swing.driver; 016 017 import static org.fest.swing.driver.JPopupMenuElementsAsTextQuery.menuElementsAsText; 018 019 import java.awt.Container; 020 021 import javax.swing.JMenuItem; 022 import javax.swing.JPopupMenu; 023 024 import org.fest.swing.annotation.RunsInEDT; 025 import org.fest.swing.core.GenericTypeMatcher; 026 import org.fest.swing.core.Robot; 027 import org.fest.swing.exception.ComponentLookupException; 028 029 /** 030 * Understands functional testing of <code>{@link JPopupMenu}</code>s: 031 * <ul> 032 * <li>user input simulation</li> 033 * <li>state verification</li> 034 * <li>property value query</li> 035 * </ul> 036 * This class is intended for internal use only. Please use the classes in the package 037 * <code>{@link org.fest.swing.fixture}</code> in your tests. 038 * 039 * @author Yvonne Wang 040 * @author Alex Ruiz 041 */ 042 public class JPopupMenuDriver extends JComponentDriver { 043 044 /** 045 * Creates a new </code>{@link JPopupMenuDriver}</code>. 046 * @param robot the robot to use to simulate user input. 047 */ 048 public JPopupMenuDriver(Robot robot) { 049 super(robot); 050 } 051 052 /** 053 * Returns the contents of the pop-up menu as a <code>String</code> array. 054 * @param popupMenu the target <code>JPopupMenu</code>. 055 * @return the contents of the pop-up menu as a <code>String</code> array. 056 */ 057 @RunsInEDT 058 public String[] menuLabelsOf(JPopupMenu popupMenu) { 059 return menuElementsAsText(popupMenu); 060 } 061 062 /** 063 * Finds a <code>{@link JMenuItem}</code>, contained in the <code>{@link Container}</code>, which name matches 064 * the specified one. 065 * @param popupMenu the target <code>JPopupMenu</code>. 066 * @param name the name to match. 067 * @return the <code>JMenuItem</code> found. 068 * @throws ComponentLookupException if a <code>JMenuItem</code> having a matching name could not be found. 069 * @throws ComponentLookupException if more than one <code>JMenuItem</code> having a matching name is found. 070 */ 071 @RunsInEDT 072 public JMenuItem menuItem(JPopupMenu popupMenu, String name) { 073 return robot.finder().findByName(popupMenu, name, JMenuItem.class, false); 074 } 075 076 /** 077 * Finds a <code>{@link JMenuItem}</code>, contained in the <code>{@link Container}</code>, that matches the 078 * specified search criteria. 079 * @param popupMenu the target <code>JPopupMenu</code>. 080 * @param matcher contains the search criteria for finding a <code>JMenuItem</code>. 081 * @return the <code>JMenuItem</code> found. 082 * @throws ComponentLookupException if a <code>JMenuItem</code> that matches the given search criteria could not be 083 * found. 084 * @throws ComponentLookupException if more than one <code>JMenuItem</code> that matches the given search criteria 085 * is found. 086 */ 087 public JMenuItem menuItem(JPopupMenu popupMenu, GenericTypeMatcher<? extends JMenuItem> matcher) { 088 return robot.finder().find(popupMenu, matcher); 089 } 090 }