Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
15   87   8   3
6   39   0.53   5
5     1.6  
1    
 
  JMenuItemMatcher       Line # 36 15 0% 8 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Sep 5, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11    * specific language governing permissions and limitations under the License.
12    *
13    * Copyright @2007-2010 the original author or authors.
14    */
15    package org.fest.swing.driver;
16   
17    import static org.fest.swing.driver.AbstractButtonTextQuery.textOf;
18    import static org.fest.util.Objects.areEqual;
19    import static org.fest.util.Strings.*;
20   
21    import java.awt.Component;
22   
23    import javax.swing.JMenuItem;
24    import javax.swing.JPopupMenu;
25   
26    import org.fest.swing.annotation.RunsInCurrentThread;
27    import org.fest.swing.core.ComponentMatcher;
28   
29    /**
30    * Matches a <code>{@link JMenuItem}</code> given a simple label or a menu path of the format "menu|submenu|menuitem",
31    * for example "File|Open|Can of worms". Adapted from Abbot's own <code>JMenuItemMatcher</code>.
32    *
33    * @author Yvonne Wang
34    * @author Alex Ruiz
35    */
 
36    public class JMenuItemMatcher implements ComponentMatcher {
37   
38    private static final String SEPARATOR = "|";
39   
40    private final String label;
41   
42    /**
43    * Creates a new </code>{@link JMenuItemMatcher}</code>.
44    * @param path the path of the menu to match.
45    */
 
46  7 toggle public JMenuItemMatcher(String... path) {
47  7 this.label = join(path).with(SEPARATOR);
48    }
49   
50    /**
51    * Indicates whether the given component is a <code>{@link JMenuItem}</code> whose text matches the path specified
52    * in this matcher.
53    * <p>
54    * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
55    * responsible for calling this method from the EDT.
56    * </p>
57    * @param c the component to verify.
58    * @return <code>true</code> if the component matches, <code>false</code> otherwise.
59    */
 
60  45 toggle @RunsInCurrentThread
61    public boolean matches(Component c) {
62  32 if (!(c instanceof JMenuItem)) return false;
63  13 JMenuItem menuItem = (JMenuItem) c;
64  13 String text = menuItem.getText();
65  13 return areEqual(label, text) || areEqual(label, pathOf(menuItem));
66    }
67   
 
68  19 toggle @RunsInCurrentThread
69    private String pathOf(JMenuItem menuItem) {
70  19 Component parent = parentOrInvokerOf(menuItem);
71  19 if (parent instanceof JMenuItem)
72  7 return concat(pathOf((JMenuItem)parent), SEPARATOR, textOf(menuItem));
73  12 return textOf(menuItem);
74    }
75   
 
76  19 toggle @RunsInCurrentThread
77    private Component parentOrInvokerOf(JMenuItem menuItem) {
78  19 Component parent = menuItem.getParent();
79  19 if (parent instanceof JPopupMenu)
80  10 parent = ((JPopupMenu)parent).getInvoker();
81  19 return parent;
82    }
83   
 
84  2 toggle @Override public String toString() {
85  2 return concat(getClass().getName(), "[", "label=", quote(label), "]");
86    }
87    }