KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > SystemActionTest


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 Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.actions;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.awt.image.ImageObserver JavaDoc;
27 import java.awt.image.PixelGrabber JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import org.netbeans.junit.Log;
32 import org.netbeans.junit.NbTestCase;
33 import org.openide.util.HelpCtx;
34
35 /** Test general aspects of system actions.
36  * Currently, just the icon.
37  * @author Jesse Glick
38  */

39 public class SystemActionTest extends NbTestCase {
40
41     public SystemActionTest(String JavaDoc name) {
42         super(name);
43     }
44
45     @Override JavaDoc
46     protected Level JavaDoc logLevel() {
47         return Level.OFF;
48     }
49
50     /** Test that iconResource really works.
51      * @see "#26887"
52      */

53     public void testIcons() throws Exception JavaDoc {
54         Image JavaDoc i = Toolkit.getDefaultToolkit().getImage(SystemActionTest.class.getResource("data/someicon.gif"));
55         int h = imageHash("Control icon", i, 16, 16);
56         CharSequence JavaDoc log = Log.enable("org.openide.util", Level.WARNING);
57         SystemAction a = SystemAction.get(SystemAction1.class);
58         assertEquals("Absolute slash-initial iconResource works (though deprecated)", h, imageHash("icon1", icon2Image(a.getIcon()), 16, 16));
59         assertTrue(log.toString(), log.toString().contains("Initial slashes in Utilities.loadImage deprecated"));
60         a = SystemAction.get(SystemAction2.class);
61         assertEquals("Absolute no-slash-initial iconResource works", h, imageHash("icon2", icon2Image(a.getIcon()), 16, 16));
62         a = SystemAction.get(SystemAction3.class);
63         assertEquals("Relative iconResource works (though deprecated)", h, imageHash("icon3", icon2Image(a.getIcon()), 16, 16));
64         assertTrue(log.toString(), log.toString().contains("Deprecated relative path"));
65         a = SystemAction.get(SystemAction4.class);
66         a.getIcon();
67         assertTrue(log.toString(), log.toString().contains("No such icon"));
68     }
69     
70     private static abstract class TestSystemAction extends SystemAction {
71         public void actionPerformed(ActionEvent JavaDoc e) {}
72         public HelpCtx getHelpCtx() {
73             return HelpCtx.DEFAULT_HELP;
74         }
75         public String JavaDoc getName() {
76             return getClass().getName();
77         }
78     }
79     public static final class SystemAction1 extends TestSystemAction {
80         protected String JavaDoc iconResource() {
81             return "/org/openide/util/actions/data/someicon.gif";
82         }
83     }
84     public static final class SystemAction2 extends TestSystemAction {
85         protected String JavaDoc iconResource() {
86             return "org/openide/util/actions/data/someicon.gif";
87         }
88     }
89     public static final class SystemAction3 extends TestSystemAction {
90         protected String JavaDoc iconResource() {
91             return "data/someicon.gif";
92         }
93     }
94     public static final class SystemAction4 extends TestSystemAction {
95         protected String JavaDoc iconResource() {
96             return "no/such/icon.gif";
97         }
98     }
99     
100     private static Image JavaDoc icon2Image(Icon JavaDoc ico) {
101         int w = ico.getIconWidth();
102         int h = ico.getIconHeight();
103         BufferedImage JavaDoc img = new BufferedImage JavaDoc(w, h, BufferedImage.TYPE_INT_ARGB);
104         ico.paintIcon(new JButton JavaDoc(), img.getGraphics(), 0, 0);
105         return img;
106     }
107     
108     // Copied from SystemFileSystemTest:
109
private static int imageHash(String JavaDoc name, Image JavaDoc img, int w, int h) throws InterruptedException JavaDoc {
110         int[] pixels = new int[w * h];
111         PixelGrabber JavaDoc pix = new PixelGrabber JavaDoc(img, 0, 0, w, h, pixels, 0, w);
112         pix.grabPixels();
113         assertEquals(0, pix.getStatus() & ImageObserver.ABORT);
114         if (false) {
115             // Debugging.
116
System.out.println("Pixels of " + name + ":");
117             for (int y = 0; y < h; y++) {
118                 for (int x = 0; x < w; x++) {
119                     if (x == 0) {
120                         System.out.print('\t');
121                     } else {
122                         System.out.print(' ');
123                     }
124                     int p = pixels[y * w + x];
125                     String JavaDoc hex = Integer.toHexString(p);
126                     while (hex.length() < 8) {
127                         hex = "0" + hex;
128                     }
129                     System.out.print(hex);
130                     if (x == w - 1) {
131                         System.out.print('\n');
132                     }
133                 }
134             }
135         }
136         int hash = 0;
137         for (int i = 0; i < pixels.length; i++) {
138             hash += 172881;
139             int p = pixels[i];
140             if ((p & 0xff000000) == 0) {
141                 // Transparent; normalize.
142
p = 0;
143             }
144             hash ^= p;
145         }
146         return hash;
147     }
148     
149 }
150
Popular Tags