1 18 19 package org.openide.awt; 20 21 import java.awt.Component ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.KeyEvent ; 24 import java.awt.image.BufferedImage ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Observable ; 30 import javax.swing.AbstractAction ; 31 import javax.swing.AbstractButton ; 32 import javax.swing.Action ; 33 import javax.swing.Icon ; 34 import javax.swing.JButton ; 35 import javax.swing.JFrame ; 36 import javax.swing.JMenuItem ; 37 import javax.swing.KeyStroke ; 38 import javax.swing.text.Keymap ; 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 49 public class ActionsTest extends NbTestCase { 50 51 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 name) { 90 super(name); 91 } 92 93 protected void setUp() { 94 MockServices.setServices(new Class [] {TestKeymap.class, TestConnector.class}); 95 assertNotNull("Keymap has to be in lookup", Lookup.getDefault().lookup(Keymap .class)); 96 } 97 98 102 public void testIconsAction() throws Exception { 103 JButton jb = new JButton (); 104 Actions.connect(jb, new TestAction()); 105 106 Icon icon = jb.getIcon(); 107 assertNotNull(icon); 108 checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon"); 109 110 Icon rolloverIcon = jb.getRolloverIcon(); 111 assertNotNull(rolloverIcon); 112 checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon"); 113 114 Icon pressedIcon = jb.getPressedIcon(); 115 assertNotNull(pressedIcon); 116 checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon"); 117 118 Icon disabledIcon = jb.getDisabledIcon(); 119 assertNotNull(disabledIcon); 120 checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon"); 121 } 122 123 127 public void testIconsSystemAction() throws Exception { 128 SystemAction saInstance = SystemAction.get(TestSystemAction.class); 129 130 JButton jb = new JButton (); 131 Actions.connect(jb, saInstance); 132 133 Icon icon = jb.getIcon(); 134 assertNotNull(icon); 135 checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon"); 136 137 Icon rolloverIcon = jb.getRolloverIcon(); 138 assertNotNull(rolloverIcon); 139 checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon"); 140 141 Icon pressedIcon = jb.getPressedIcon(); 142 assertNotNull(pressedIcon); 143 checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon"); 144 145 Icon disabledIcon = jb.getDisabledIcon(); 146 assertNotNull(disabledIcon); 147 checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon"); 148 } 149 150 154 public void testIconsAction24() throws Exception { 155 JButton jb = new JButton (); 156 jb.putClientProperty("PreferredIconSize",new Integer (24)); 157 Actions.connect(jb, new TestAction()); 158 159 Icon icon = jb.getIcon(); 160 assertNotNull(icon); 161 checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon"); 162 163 Icon rolloverIcon = jb.getRolloverIcon(); 164 assertNotNull(rolloverIcon); 165 checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon"); 166 167 Icon pressedIcon = jb.getPressedIcon(); 168 assertNotNull(pressedIcon); 169 checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon"); 170 171 Icon disabledIcon = jb.getDisabledIcon(); 172 assertNotNull(disabledIcon); 173 checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon"); 174 } 175 176 181 public void testNoIconInMenu() throws Exception { 182 JMenuItem item = new JMenuItem (); 183 item.setIcon(null); 184 Actions.connect(item, new TestNoMenuIconAction(), false); 185 assertNull(item.getIcon()); 186 } 187 188 192 public void testIconsSystemAction24() throws Exception { 193 SystemAction saInstance = SystemAction.get(TestSystemAction.class); 194 195 JButton jb = new JButton (); 196 jb.putClientProperty("PreferredIconSize",new Integer (24)); 197 Actions.connect(jb, saInstance); 198 199 Icon icon = jb.getIcon(); 200 assertNotNull(icon); 201 checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon"); 202 203 Icon rolloverIcon = jb.getRolloverIcon(); 204 assertNotNull(rolloverIcon); 205 checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon"); 206 207 Icon pressedIcon = jb.getPressedIcon(); 208 assertNotNull(pressedIcon); 209 checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon"); 210 211 Icon disabledIcon = jb.getDisabledIcon(); 212 assertNotNull(disabledIcon); 213 checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon"); 214 } 215 216 222 public void testActionRemoval_Issue39508() throws Exception { 223 Keymap map = (Keymap )Lookup.getDefault().lookup(Keymap .class); 225 map.removeBindings(); 226 Action action = new ActionsTest.TestAction(); 227 KeyStroke stroke = KeyStroke.getKeyStroke("ctrl alt 7"); 228 assertNotNull(stroke); 229 JMenuItem menu = new JMenuItem (); 231 assertNull(menu.getAccelerator()); 232 Actions.connect(menu, action, false); 233 assertEquals(1, ((Observable )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 ref = new WeakReference (action); 242 menu = null; 243 action = null; 244 assertGC("action can dissappear", ref); 245 } 246 247 251 public void testTooltipsArePersistent() throws Exception { 252 Action action = new ActionsTest.TestActionWithTooltip(); 253 JButton button = new JButton (); 254 255 Actions.connect(button, action); 256 257 JFrame f = new JFrame (); 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 279 public void testTooltipsIsBuiltFromNameIfNoTooltip() throws Exception { 280 Action action = new ActionsTest.TestAction(); 281 JButton button = new JButton (); 282 283 Actions.connect(button, action); 284 285 JFrame f = new JFrame (); 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 307 public void testTooltipsContainAccelerator() throws Exception { 308 Action action = new ActionsTest.TestActionWithTooltip(); 309 JButton button = new JButton (); 310 311 Actions.connect(button, action); 312 313 JFrame f = new JFrame (); 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 341 public void testButtonActionConnector() throws Exception { 342 TestConnector tc = Lookup.getDefault().lookup(TestConnector.class); 343 tc.setActive(true); 344 Action action = new ActionsTest.TestAction(); 345 JButton button = new JButton (); 346 Actions.connect(button, action); 347 assertEquals(1, tc.getConnectCalled()); 348 JMenuItem jmi = new JMenuItem (); 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 icon, Component c, int rowToCheck, String 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 369 private void checkIfIconOk(Icon icon, Component c, int pixelX, int pixelY, int[] expectedResult, String nameOfIcon) { 370 BufferedImage bufImg = new BufferedImage (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 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 e) { 385 } 386 387 public HelpCtx getHelpCtx() { 388 return null; 389 } 390 391 public String getName() { 392 return "TestSystemAction"; 393 } 394 395 protected String iconResource() { 396 return "org/openide/awt/data/testIcon.gif"; 397 } 398 399 } 400 401 private static final class TestAction extends AbstractAction { 402 403 public TestAction() { 404 putValue("iconBase", "org/openide/awt/data/testIcon.gif"); 405 putValue(NAME, "test"); 406 } 407 408 public void actionPerformed(ActionEvent e) { 409 } 410 411 } 412 413 private static final class TestNoMenuIconAction extends AbstractAction { 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 e) { 421 } 422 423 } 424 425 private static final class TestActionWithTooltip extends AbstractAction { 426 427 private static String TOOLTIP = "tooltip"; 428 429 public TestActionWithTooltip() { 430 putValue(NAME, "name"); 431 putValue(SHORT_DESCRIPTION, TOOLTIP); 432 } 433 434 public void actionPerformed(ActionEvent e) { 435 } 436 437 } 438 439 public static final class TestKeymap extends Observable implements Keymap { 440 441 private Map map = new HashMap (); 442 private Action defAct; 443 444 public void addActionForKeyStroke(KeyStroke key, Action act) { 445 map.put(key, act); 446 act.putValue(Action.ACCELERATOR_KEY, key); 447 setChanged(); 448 notifyObservers(); 449 } 450 451 public Action getAction(KeyStroke key) { 452 return (Action )map.get(key); 453 } 454 455 public Action [] getBoundActions() { 456 return new Action [0]; 457 } 458 459 public KeyStroke [] getBoundKeyStrokes() { 460 return new KeyStroke [0]; 461 } 462 463 public Action getDefaultAction() { 464 return defAct; 465 } 466 467 public KeyStroke [] getKeyStrokesForAction(Action a) { 468 return new KeyStroke [0]; 469 } 470 471 public String getName() { 472 return "testKeymap"; 473 } 474 475 public Keymap getResolveParent() { 476 return null; 477 } 478 479 public boolean isLocallyDefined(KeyStroke key) { 480 return true; 481 } 482 483 public void removeBindings() { 484 map.clear(); 485 } 486 487 public void removeKeyStrokeBinding(KeyStroke keys) { 488 Action act = (Action )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 a) { 497 defAct = a; 498 } 499 500 public void setResolveParent(Keymap parent) { 501 } 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 button, Action action) { 514 if (!active) { 515 return false; 516 } 517 called +=1; 518 return true; 519 } 520 521 public boolean connect(JMenuItem item, Action 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 |