KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > ActionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Nokia. Portions Copyright 2004 Nokia. All Rights Reserved.
17  */

18
19 package org.openide.awt;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Observable JavaDoc;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.AbstractButton JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import javax.swing.KeyStroke JavaDoc;
38 import javax.swing.text.Keymap JavaDoc;
39 import org.netbeans.junit.MockServices;
40 import org.netbeans.junit.NbTestCase;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.Lookup;
43 import org.openide.util.actions.SystemAction;
44
45 /**
46  * Tests for the Actions class.
47  * @author David Strupl
48  */

49 public class ActionsTest extends NbTestCase {
50     
51     // colors of the testing images in this order:
52
// (test recognizes the icon by the white/black colors in specified positions :-)))
53
// testIcon.gif
54
// testIcon_rollover.gif
55
// testIcon_pressed.gif
56
// testIcon_disabled.gif
57
private static int[][] RESULT_COLORS_00 = {
58         {255, 255, 255},
59         {0, 0, 0},
60         {255, 255, 255},
61         {0, 0, 0},
62         {255, 255, 255},
63         {0, 0, 0},
64         {255, 255, 255},
65         {0, 0, 0},
66     };
67     private static int[][] RESULT_COLORS_01 = {
68         {255, 255, 255},
69         {255, 255, 255},
70         {0, 0, 0},
71         {0, 0, 0},
72         {255, 255, 255},
73         {255, 255, 255},
74         {0, 0, 0},
75         {0, 0, 0},
76     };
77     private static int[][] RESULT_COLORS_11 = {
78         {255, 255, 255},
79         {255, 255, 255},
80         {255, 255, 255},
81         {255, 255, 255},
82         {0, 0, 0},
83         {0, 0, 0},
84         {0, 0, 0},
85         {0, 0, 0},
86     };
87     
88     
89     public ActionsTest(String JavaDoc name) {
90         super(name);
91     }
92     
93     protected void setUp() {
94         MockServices.setServices(new Class JavaDoc[] {TestKeymap.class, TestConnector.class});
95         assertNotNull("Keymap has to be in lookup", Lookup.getDefault().lookup(Keymap JavaDoc.class));
96     }
97     
98     /**
99      * Test whether pressed, rollover and disabled icons
100      * work for javax.swing.Action.
101      */

102     public void testIconsAction() throws Exception JavaDoc {
103         JButton JavaDoc jb = new JButton JavaDoc();
104         Actions.connect(jb, new TestAction());
105         
106         Icon JavaDoc icon = jb.getIcon();
107         assertNotNull(icon);
108         checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon");
109         
110         Icon JavaDoc rolloverIcon = jb.getRolloverIcon();
111         assertNotNull(rolloverIcon);
112         checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon");
113         
114         Icon JavaDoc pressedIcon = jb.getPressedIcon();
115         assertNotNull(pressedIcon);
116         checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon");
117         
118         Icon JavaDoc disabledIcon = jb.getDisabledIcon();
119         assertNotNull(disabledIcon);
120         checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon");
121     }
122     
123     /**
124      * Test whether pressed, rollover and disabled icons
125      * work for SystemAction.
126      */

127     public void testIconsSystemAction() throws Exception JavaDoc {
128         SystemAction saInstance = SystemAction.get(TestSystemAction.class);
129         
130         JButton JavaDoc jb = new JButton JavaDoc();
131         Actions.connect(jb, saInstance);
132         
133         Icon JavaDoc icon = jb.getIcon();
134         assertNotNull(icon);
135         checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon");
136         
137         Icon JavaDoc rolloverIcon = jb.getRolloverIcon();
138         assertNotNull(rolloverIcon);
139         checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon");
140         
141         Icon JavaDoc pressedIcon = jb.getPressedIcon();
142         assertNotNull(pressedIcon);
143         checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon");
144         
145         Icon JavaDoc disabledIcon = jb.getDisabledIcon();
146         assertNotNull(disabledIcon);
147         checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon");
148     }
149     
150     /**
151      * Test whether pressed, rollover and disabled 24x24 icons
152      * work for javax.swing.Action.
153      */

154     public void testIconsAction24() throws Exception JavaDoc {
155         JButton JavaDoc jb = new JButton JavaDoc();
156         jb.putClientProperty("PreferredIconSize",new Integer JavaDoc(24));
157         Actions.connect(jb, new TestAction());
158         
159         Icon JavaDoc icon = jb.getIcon();
160         assertNotNull(icon);
161         checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon");
162         
163         Icon JavaDoc rolloverIcon = jb.getRolloverIcon();
164         assertNotNull(rolloverIcon);
165         checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon");
166         
167         Icon JavaDoc pressedIcon = jb.getPressedIcon();
168         assertNotNull(pressedIcon);
169         checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon");
170         
171         Icon JavaDoc disabledIcon = jb.getDisabledIcon();
172         assertNotNull(disabledIcon);
173         checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon");
174     }
175     
176     /**
177      * #47527
178      * Tests if "noIconInMenu" really will NOT push the icon from the action
179      * to the menu item.
180      */

181     public void testNoIconInMenu() throws Exception JavaDoc {
182         JMenuItem JavaDoc item = new JMenuItem JavaDoc();
183         item.setIcon(null);
184         Actions.connect(item, new TestNoMenuIconAction(), false);
185         assertNull(item.getIcon());
186     }
187     
188     /**
189      * Test whether pressed, rollover and disabled 24x24 icons
190      * work for SystemAction.
191      */

192     public void testIconsSystemAction24() throws Exception JavaDoc {
193         SystemAction saInstance = SystemAction.get(TestSystemAction.class);
194         
195         JButton JavaDoc jb = new JButton JavaDoc();
196         jb.putClientProperty("PreferredIconSize",new Integer JavaDoc(24));
197         Actions.connect(jb, saInstance);
198         
199         Icon JavaDoc icon = jb.getIcon();
200         assertNotNull(icon);
201         checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon");
202         
203         Icon JavaDoc rolloverIcon = jb.getRolloverIcon();
204         assertNotNull(rolloverIcon);
205         checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon");
206         
207         Icon JavaDoc pressedIcon = jb.getPressedIcon();
208         assertNotNull(pressedIcon);
209         checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon");
210         
211         Icon JavaDoc disabledIcon = jb.getDisabledIcon();
212         assertNotNull(disabledIcon);
213         checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon");
214     }
215     
216     /**
217      * tests if the accelerator for JMenuItem is reset when the global KeyMap changes.
218      * Has to work even when the menu is not visible (when visible is handled by Actions.Bridge listeners)
219      * when not visible handled by the tested Actions.setMenuActionConnection() - only for menu items.
220      * #39508
221      */

222     public void testActionRemoval_Issue39508() throws Exception JavaDoc {
223         // prepare
224
Keymap JavaDoc map = (Keymap JavaDoc)Lookup.getDefault().lookup(Keymap JavaDoc.class);
225         map.removeBindings();
226         Action JavaDoc action = new ActionsTest.TestAction();
227         KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke("ctrl alt 7");
228         assertNotNull(stroke);
229         //test start
230
JMenuItem JavaDoc menu = new JMenuItem JavaDoc();
231         assertNull(menu.getAccelerator());
232         Actions.connect(menu, action, false);
233         assertEquals(1, ((Observable JavaDoc)map).countObservers());
234         assertNull(menu.getAccelerator());
235         map.addActionForKeyStroke(stroke, action);
236         assertNotNull(action.getValue(Action.ACCELERATOR_KEY));
237         assertNotNull(menu.getAccelerator());
238         map.removeKeyStrokeBinding(stroke);
239         assertNull(action.getValue(Action.ACCELERATOR_KEY));
240         assertNull(menu.getAccelerator());
241         Reference JavaDoc ref = new WeakReference JavaDoc(action);
242         menu = null;
243         action = null;
244         assertGC("action can dissappear", ref);
245     }
246     
247     /**
248      * Tests if changes in accelerator key or name of the action does not change the tooltip
249      * of the button if the action has a custom tooltip. See first part of #57974.
250      */

251     public void testTooltipsArePersistent() throws Exception JavaDoc {
252         Action JavaDoc action = new ActionsTest.TestActionWithTooltip();
253         JButton JavaDoc button = new JButton JavaDoc();
254         
255         Actions.connect(button, action);
256         
257         JFrame JavaDoc f = new JFrame JavaDoc();
258         
259         f.getContentPane().add(button);
260         f.setVisible(true);
261         
262         assertTrue(button.getToolTipText().equals(TestActionWithTooltip.TOOLTIP));
263         
264         action.putValue(Action.NAME, "new-name");
265         
266         assertTrue(button.getToolTipText().equals(TestActionWithTooltip.TOOLTIP));
267         
268         action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('a'));
269         
270         assertTrue(button.getToolTipText().indexOf(TestActionWithTooltip.TOOLTIP) != (-1));
271         
272         f.setVisible(false);
273     }
274     
275     /**
276      * Tests if the tooltip is made out of the NAME if there is not tooltip set for an action.
277      * See also #57974.
278      */

279     public void testTooltipsIsBuiltFromNameIfNoTooltip() throws Exception JavaDoc {
280         Action JavaDoc action = new ActionsTest.TestAction();
281         JButton JavaDoc button = new JButton JavaDoc();
282         
283         Actions.connect(button, action);
284         
285         JFrame JavaDoc f = new JFrame JavaDoc();
286         
287         f.getContentPane().add(button);
288         f.setVisible(true);
289         
290         assertTrue(button.getToolTipText().equals("test"));
291         
292         action.putValue(Action.NAME, "new-name");
293         
294         assertTrue(button.getToolTipText().equals("new-name"));
295         
296         action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('a'));
297         
298         assertTrue(button.getToolTipText().indexOf("new-name") != (-1));
299         
300         f.setVisible(false);
301     }
302     
303     /**
304      * Tests if the accelerator key is shown in the button's tooltip for actions with
305      * custom tooltips.
306      */

307     public void testTooltipsContainAccelerator() throws Exception JavaDoc {
308         Action JavaDoc action = new ActionsTest.TestActionWithTooltip();
309         JButton JavaDoc button = new JButton JavaDoc();
310         
311         Actions.connect(button, action);
312         
313         JFrame JavaDoc f = new JFrame JavaDoc();
314         
315         f.getContentPane().add(button);
316         f.setVisible(true);
317         
318         assertTrue(button.getToolTipText().equals(TestActionWithTooltip.TOOLTIP));
319         
320         action.putValue(Action.NAME, "new-name");
321         
322         assertTrue(button.getToolTipText().equals(TestActionWithTooltip.TOOLTIP));
323         
324         action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK));
325         
326         assertTrue(button.getToolTipText().indexOf("Ctrl+C") != (-1));
327         
328         action.putValue(Action.SHORT_DESCRIPTION, null);
329         
330         assertTrue(button.getToolTipText().indexOf("Ctrl+C") != (-1));
331         
332         f.setVisible(false);
333     }
334
335     /**
336      * Tests whether the ButtonActionConnector is being called. The testing
337      * implementation is set to "active" only for this test - so the other
338      * tests should retain the behaviour like running without the
339      * ButtonActionConnector.
340      */

341     public void testButtonActionConnector() throws Exception JavaDoc {
342         TestConnector tc = Lookup.getDefault().lookup(TestConnector.class);
343         tc.setActive(true);
344         Action JavaDoc action = new ActionsTest.TestAction();
345         JButton JavaDoc button = new JButton JavaDoc();
346         Actions.connect(button, action);
347         assertEquals(1, tc.getConnectCalled());
348         JMenuItem JavaDoc jmi = new JMenuItem JavaDoc();
349         Actions.connect(jmi, action, false);
350         assertEquals(3, tc.getConnectCalled());
351         tc.setActive(false);
352     }
353     
354     
355     protected boolean runInEQ() {
356         return true;
357     }
358     
359     private void checkIfLoadedCorrectIcon(Icon JavaDoc icon, Component JavaDoc c, int rowToCheck, String JavaDoc nameOfIcon) {
360         checkIfIconOk(icon, c, 0, 0, RESULT_COLORS_00[rowToCheck], nameOfIcon);
361         checkIfIconOk(icon, c, 0, 1, RESULT_COLORS_01[rowToCheck], nameOfIcon);
362         checkIfIconOk(icon, c, 1, 1, RESULT_COLORS_11[rowToCheck], nameOfIcon);
363     }
364     
365     /**
366      * Checks colors on coordinates X,Y of the icon and compares them
367      * to expectedResult.
368      */

369     private void checkIfIconOk(Icon JavaDoc icon, Component JavaDoc c, int pixelX, int pixelY, int[] expectedResult, String JavaDoc nameOfIcon) {
370         BufferedImage JavaDoc bufImg = new BufferedImage JavaDoc(16, 16, BufferedImage.TYPE_INT_RGB);
371         icon.paintIcon(c, bufImg.getGraphics(), 0, 0);
372         int[] res = bufImg.getData().getPixel(pixelX, pixelY, (int[])null);
373         log("Icon height is " + icon.getIconHeight());
374         log("Icon width is " + icon.getIconWidth());
375         for (int i = 0; i < res.length; i++) {
376             // Huh, Ugly hack. the sparc returns a fuzzy values +/- 1 unit e.g. 254 for Black instead of 255 as other OSs do
377
// this hack doesn't broken the functionality which should testing
378
assertTrue(nameOfIcon + ": Color of the ["+pixelX+","+pixelY+"] pixel is " + res[i] + ", expected was " + expectedResult[i], Math.abs(res[i] - expectedResult[i]) < 10);
379         }
380     }
381     
382     private static final class TestSystemAction extends SystemAction {
383         
384         public void actionPerformed(ActionEvent JavaDoc e) {
385         }
386         
387         public HelpCtx getHelpCtx() {
388             return null;
389         }
390         
391         public String JavaDoc getName() {
392             return "TestSystemAction";
393         }
394         
395         protected String JavaDoc iconResource() {
396             return "org/openide/awt/data/testIcon.gif";
397         }
398         
399     }
400     
401     private static final class TestAction extends AbstractAction JavaDoc {
402         
403         public TestAction() {
404             putValue("iconBase", "org/openide/awt/data/testIcon.gif");
405             putValue(NAME, "test");
406         }
407         
408         public void actionPerformed(ActionEvent JavaDoc e) {
409         }
410         
411     }
412     
413     private static final class TestNoMenuIconAction extends AbstractAction JavaDoc {
414         
415         public TestNoMenuIconAction() {
416             putValue("iconBase", "org/openide/awt/data/testIcon.gif");
417             putValue("noIconInMenu", Boolean.TRUE);
418         }
419         
420         public void actionPerformed(ActionEvent JavaDoc e) {
421         }
422         
423     }
424     
425     private static final class TestActionWithTooltip extends AbstractAction JavaDoc {
426         
427         private static String JavaDoc TOOLTIP = "tooltip";
428         
429         public TestActionWithTooltip() {
430             putValue(NAME, "name");
431             putValue(SHORT_DESCRIPTION, TOOLTIP);
432         }
433         
434         public void actionPerformed(ActionEvent JavaDoc e) {
435         }
436         
437     }
438     
439     public static final class TestKeymap extends Observable JavaDoc implements Keymap JavaDoc {
440         
441         private Map JavaDoc map = new HashMap JavaDoc();
442         private Action JavaDoc defAct;
443         
444         public void addActionForKeyStroke(KeyStroke JavaDoc key, Action JavaDoc act) {
445             map.put(key, act);
446             act.putValue(Action.ACCELERATOR_KEY, key);
447             setChanged();
448             notifyObservers();
449         }
450         
451         public Action JavaDoc getAction(KeyStroke JavaDoc key) {
452             return (Action JavaDoc)map.get(key);
453         }
454         
455         public Action JavaDoc[] getBoundActions() {
456             return new Action JavaDoc[0];
457         }
458         
459         public KeyStroke JavaDoc[] getBoundKeyStrokes() {
460             return new KeyStroke JavaDoc[0];
461         }
462         
463         public Action JavaDoc getDefaultAction() {
464             return defAct;
465         }
466         
467         public KeyStroke JavaDoc[] getKeyStrokesForAction(Action JavaDoc a) {
468             return new KeyStroke JavaDoc[0];
469         }
470         
471         public String JavaDoc getName() {
472             return "testKeymap";
473         }
474         
475         public Keymap JavaDoc getResolveParent() {
476             return null;
477         }
478         
479         public boolean isLocallyDefined(KeyStroke JavaDoc key) {
480             return true;
481         }
482         
483         public void removeBindings() {
484             map.clear();
485         }
486         
487         public void removeKeyStrokeBinding(KeyStroke JavaDoc keys) {
488             Action JavaDoc act = (Action JavaDoc)map.remove(keys);
489             if (act != null) {
490                 act.putValue(Action.ACCELERATOR_KEY, null);
491             }
492             setChanged();
493             notifyObservers();
494         }
495         
496         public void setDefaultAction(Action JavaDoc a) {
497             defAct = a;
498         }
499         
500         public void setResolveParent(Keymap JavaDoc parent) {
501             // ignore
502
}
503         
504     }
505     
506     public static final class TestConnector implements Actions.ButtonActionConnector {
507         
508         private int called = 0;
509         private boolean active = false;
510         
511         public TestConnector() {}
512         
513         public boolean connect(AbstractButton JavaDoc button, Action JavaDoc action) {
514             if (!active) {
515                 return false;
516             }
517             called +=1;
518             return true;
519         }
520
521         public boolean connect(JMenuItem JavaDoc item, Action JavaDoc action, boolean popup) {
522             if (!active) {
523                 return false;
524             }
525             called += 2;
526             return true;
527         }
528         
529         public int getConnectCalled() {
530             return called;
531         }
532         public void setActive(boolean a) {
533             called = 0;
534             active = a;
535         }
536     }
537     
538 }
539
Popular Tags