1 19 20 package org.openide.util.actions; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.image.BufferedImage ; 26 import java.awt.image.ImageObserver ; 27 import java.awt.image.PixelGrabber ; 28 import java.util.logging.Level ; 29 import javax.swing.Icon ; 30 import javax.swing.JButton ; 31 import org.netbeans.junit.Log; 32 import org.netbeans.junit.NbTestCase; 33 import org.openide.util.HelpCtx; 34 35 39 public class SystemActionTest extends NbTestCase { 40 41 public SystemActionTest(String name) { 42 super(name); 43 } 44 45 @Override 46 protected Level logLevel() { 47 return Level.OFF; 48 } 49 50 53 public void testIcons() throws Exception { 54 Image i = Toolkit.getDefaultToolkit().getImage(SystemActionTest.class.getResource("data/someicon.gif")); 55 int h = imageHash("Control icon", i, 16, 16); 56 CharSequence 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 e) {} 72 public HelpCtx getHelpCtx() { 73 return HelpCtx.DEFAULT_HELP; 74 } 75 public String getName() { 76 return getClass().getName(); 77 } 78 } 79 public static final class SystemAction1 extends TestSystemAction { 80 protected String iconResource() { 81 return "/org/openide/util/actions/data/someicon.gif"; 82 } 83 } 84 public static final class SystemAction2 extends TestSystemAction { 85 protected String iconResource() { 86 return "org/openide/util/actions/data/someicon.gif"; 87 } 88 } 89 public static final class SystemAction3 extends TestSystemAction { 90 protected String iconResource() { 91 return "data/someicon.gif"; 92 } 93 } 94 public static final class SystemAction4 extends TestSystemAction { 95 protected String iconResource() { 96 return "no/such/icon.gif"; 97 } 98 } 99 100 private static Image icon2Image(Icon ico) { 101 int w = ico.getIconWidth(); 102 int h = ico.getIconHeight(); 103 BufferedImage img = new BufferedImage (w, h, BufferedImage.TYPE_INT_ARGB); 104 ico.paintIcon(new JButton (), img.getGraphics(), 0, 0); 105 return img; 106 } 107 108 private static int imageHash(String name, Image img, int w, int h) throws InterruptedException { 110 int[] pixels = new int[w * h]; 111 PixelGrabber pix = new PixelGrabber (img, 0, 0, w, h, pixels, 0, w); 112 pix.grabPixels(); 113 assertEquals(0, pix.getStatus() & ImageObserver.ABORT); 114 if (false) { 115 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 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 p = 0; 143 } 144 hash ^= p; 145 } 146 return hash; 147 } 148 149 } 150 | Popular Tags |