View Javadoc

1   // Copyright (C) 2006 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.swingui;
23  
24  import javax.swing.JButton;
25  import javax.swing.JLabel;
26  import javax.swing.JMenu;
27  import javax.swing.JMenuBar;
28  import javax.swing.JPopupMenu;
29  import javax.swing.SwingUtilities;
30  
31  import junit.framework.TestCase;
32  
33  
34  /**
35   * Unit tests for {@link MnemonicHeuristics}.
36   *
37   * @author Philip Aston
38   */
39  public class TestMnemonicHeuristics extends TestCase {
40  
41  
42    public void testMnemonicHeuristics() throws Exception {
43      final JMenu menu = new JMenu();
44      final JButton existing1 = new JButton("Hello");
45      existing1.setMnemonic('H');
46      menu.add(existing1);
47      menu.add(new JLabel("Other stuff"));
48      menu.add(new JButton("Goodbye"));
49      menu.validate();
50  
51      final Expectation[] items = {
52          /*
53          new Expectation("Hello", 'L'),
54          new Expectation("", 0),
55          new Expectation("Lovely", 'V'),
56          new Expectation("E_xplicit mnemonic", 'X'),
57          new Expectation("Hello", 'E'),
58          new Expectation("Hello", 'O'),
59          new Expectation("Hello", 0),
60          new Expectation("In 1974", 'I'),
61          new Expectation("I was", 'W'),
62          new Expectation("hospitalised for", 'S'),
63          new Expectation("approaching perfection", 'A'),
64          new Expectation("baby, there's no guidance", 'B'),
65          new Expectation("When Random Rules", 'R'),
66          new Expectation("but before you go I've got to ask", 'T'),
67          new Expectation("You dear about that tan line on your", 'Y'),
68          new Expectation("ring finger", 'N'),
69          new Expectation("Random Rules", 'D'),
70          new Expectation("RĂ¼les that are Random", 'M'),
71          */
72          new Expectation(null, 0),
73      };
74  
75      new MnemonicHeuristics(menu);
76  
77      for (int i = 0; i < items.length; ++i) {
78        final JButton button = new JButton(items[i].getText());
79        menu.add(button);
80  
81        assertEquals(items[i].toString(),
82          items[i].getMnemonic(), button.getMnemonic());
83      }
84    }
85  
86    public void testDynamicBehaviour() throws Exception {
87      final JMenuBar menubar = new JMenuBar();
88  
89      final JButton b1 = new JButton("Hello");
90      final JButton b2 = new JButton("Hello");
91  
92      menubar.add(b1);
93      menubar.add(b2);
94  
95      new MnemonicHeuristics(menubar);
96  
97      assertEquals('H', b1.getMnemonic());
98      assertEquals('L', b2.getMnemonic());
99  
100     b1.setText("Foo");
101 
102     assertEquals('F', b1.getMnemonic());
103     assertEquals('L', b2.getMnemonic());
104 
105     b1.setText("Hello");
106 
107     assertEquals('H', b1.getMnemonic());
108     assertEquals('L', b2.getMnemonic());
109 
110     menubar.add(new JLabel("Something else"));
111 
112     assertEquals('H', b1.getMnemonic());
113     assertEquals('L', b2.getMnemonic());
114 
115     menubar.remove(b1);
116 
117     // b1 no longer part of menubar, updating text should not change mnemonic.
118     b1.setText("Foo");
119     assertEquals('H', b1.getMnemonic());
120     assertEquals('L', b2.getMnemonic());
121 
122     menubar.add(b1);
123     b1.setText(null);
124     assertEquals(0, b1.getMnemonic());
125   }
126 
127   public void testExplicitMnemonics() throws Exception {
128 
129     final JPopupMenu menu = new JPopupMenu();
130 
131     final JButton b1 = new JButton("H_ello world");
132     final JButton b2 = new JButton("Hello _world");
133 
134     menu.add(b1);
135     menu.add(b2);
136 
137     new MnemonicHeuristics(menu);
138 
139     assertEquals(1, b1.getDisplayedMnemonicIndex());
140     assertEquals('E', b1.getMnemonic());
141     assertEquals('W', b2.getMnemonic());
142     assertEquals("Hello world", b2.getText());
143 
144     final JButton b3 = new JButton("Hello_ world");
145     menu.add(b3);
146     assertEquals('H', b3.getMnemonic());
147 
148     // Displayed mnemonic index is only set reliably on text change if
149     // setText() is dispatched in the AWT thread.
150     SwingUtilities.invokeAndWait(
151       new Runnable() { public void run() { b1.setText("_Hello world"); } });
152 
153     // Wait until AWT event queue is empty.
154     SwingUtilities.invokeAndWait(new Runnable() { public void run() { } });
155 
156     assertEquals('H', b1.getMnemonic());
157     assertEquals(0, b1.getDisplayedMnemonicIndex());
158     assertEquals('L', b3.getMnemonic());
159 
160     SwingUtilities.invokeAndWait(
161       new Runnable() { public void run() { b3.setText("_Hello _Bob"); } });
162     SwingUtilities.invokeAndWait(new Runnable() { public void run() { } });
163 
164     assertEquals('B', b3.getMnemonic());
165     assertEquals(6, b3.getDisplayedMnemonicIndex());
166     assertEquals('H', b1.getMnemonic());
167 
168   }
169 
170   private static class Expectation {
171     private final String m_text;
172     private final int m_mnemonic;
173 
174     public Expectation(String text, int mnemonic) {
175       m_text = text;
176       m_mnemonic = mnemonic;
177     }
178 
179     public String getText() {
180       return m_text;
181     }
182 
183     public int getMnemonic() {
184       return m_mnemonic;
185     }
186 
187     public String toString() {
188       return "Expectation(\"" + m_text + "\", '" +
189                                (char)m_mnemonic + "' (" + m_mnemonic + ")" +
190                         ")";
191     }
192   }
193 }