View Javadoc

1   // Copyright (C) 2008 - 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 java.awt.event.ActionEvent;
25  import java.util.HashMap;
26  
27  import javax.swing.Action;
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  
31  import net.grinder.console.common.Resources;
32  import net.grinder.console.common.StubResources;
33  import junit.framework.TestCase;
34  
35  
36  /**
37   * Unit tests for {@link CustomAction}.
38   *
39   * @author Philip Aston
40   */
41  public class TestCustomAction extends TestCase {
42  
43    private static ImageIcon s_image1 = new ImageIcon();
44    private static ImageIcon s_image2 = new ImageIcon();
45  
46    private static Resources s_resources =
47      new StubResources<Object>(
48        new HashMap<String, Object>() { {
49          put("blah.label", "lah");
50          put("blah.rollover-image", s_image2);
51          put("x.label", "X");
52          put("x.tip", "X Tip");
53          put("x.image", s_image1);
54        } }
55      );
56  
57    private static class MyAction extends CustomAction {
58  
59      public MyAction(String key) {
60        super(s_resources, key);
61      }
62  
63      public MyAction(String key, boolean isDialogAction) {
64        super(s_resources, key, isDialogAction);
65      }
66  
67      public void actionPerformed(ActionEvent e) {
68      }
69  
70      public void firePropertyChange(
71        String propertyName, Object oldValue, Object newValue) {
72        super.firePropertyChange(propertyName, oldValue, newValue);
73      }
74    }
75  
76    public void testConstruction() throws Exception {
77      final MyAction action1 = new MyAction("blah");
78      assertEquals("lah", action1.getValue(Action.NAME));
79      assertNull(action1.getValue(Action.SHORT_DESCRIPTION));
80      assertNull(action1.getValue(Action.SMALL_ICON));
81      assertSame(s_image2, action1.getValue(CustomAction.ROLLOVER_ICON));
82      assertEquals("blah", action1.getKey());
83  
84      final MyAction action2 = new MyAction("blah", true);
85      assertEquals("lah...", action2.getValue(Action.NAME));
86  
87      final MyAction action3 = new MyAction("notthere", true);
88      assertNull(action3.getValue(Action.NAME));
89  
90      final MyAction action4 = new MyAction("x");
91      assertEquals("X", action4.getValue(Action.NAME));
92      assertEquals("X Tip", action4.getValue(Action.SHORT_DESCRIPTION));
93      assertSame(s_image1, action4.getValue(Action.SMALL_ICON));
94    }
95  
96    public void testRegisterButton() throws Exception {
97      final MyAction action1 = new MyAction("blah");
98      final MyAction action2 = new MyAction("blah");
99  
100     final JButton button = new JButton();
101     button.setAction(action1);
102     action1.registerButton(button);
103 
104     // Its a little sick to abuse the property listener when there's no
105     // corresponding property.
106     action1.firePropertyChange("setAction", null, action2);
107     assertSame(action2, button.getAction());
108 
109     action1.firePropertyChange("whatever", null, action1);
110     assertSame(action2, button.getAction());
111 
112     action1.registerButton(button);
113     assertSame(action2, button.getAction());
114   }
115 
116   public void testRelevantToSelection() throws Exception {
117     final MyAction action1 = new MyAction("blah");
118     final MyAction action2 = new MyAction("x");
119 
120     assertFalse(action1.isRelevantToSelection());
121     assertFalse(action2.isRelevantToSelection());
122 
123     action1.setRelevantToSelection(true);
124     assertTrue(action1.isRelevantToSelection());
125     assertFalse(action2.isRelevantToSelection());
126 
127     action1.setRelevantToSelection(false);
128     assertFalse(action1.isRelevantToSelection());
129     assertFalse(action2.isRelevantToSelection());
130   }
131 
132 }