1 19 20 package org.openide.util; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import javax.swing.AbstractAction ; 31 import javax.swing.AbstractButton ; 32 import javax.swing.Action ; 33 import javax.swing.JButton ; 34 import javax.swing.JMenuItem ; 35 import javax.swing.JPopupMenu ; 36 import javax.swing.KeyStroke ; 37 import junit.framework.TestCase; 38 import org.netbeans.junit.MockServices; 39 import org.netbeans.modules.openide.util.AWTBridge; 40 import org.openide.util.actions.Presenter; 41 import org.openide.util.lookup.Lookups; 42 43 46 public class UtilitiesTest extends TestCase { 47 48 public UtilitiesTest (String testName) { 49 super (testName); 50 } 51 52 private String originalOsName; 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 Utilities.resetOperatingSystem (); 57 originalOsName = System.getProperty("os.name"); 58 } 59 60 protected void tearDown() throws Exception { 61 System.setProperty("os.name", originalOsName); 62 super.tearDown(); 63 } 64 65 public void testGetOperatingSystemWinNT () { 66 System.setProperty ("os.name", "Windows NT"); 67 assertEquals ("Windows NT recognized as OS_WINNT", Utilities.OS_WINNT, Utilities.getOperatingSystem ()); 69 } 70 71 public void testGetOperatingSystemFreebsd () { 72 System.setProperty ("os.name", "FreeBSD"); 73 assertEquals ("System.getProperty (os.name) returns FreeBSD", "FreeBSD", System.getProperty ("os.name")); 74 assertEquals ("System.getProperty (os.name) returns freebsd", "freebsd", System.getProperty ("os.name").toLowerCase (Locale.US)); 75 assertEquals ("FreeBSD recognized as OS_FREEBSD", Utilities.OS_FREEBSD, Utilities.getOperatingSystem ()); 76 } 77 78 public void testGetOperatingSystemFreeBSDLowerCase () { 79 System.setProperty ("os.name", "freebsd"); 80 assertEquals ("FreeBSD recognized as OS_FREEBSD", Utilities.OS_FREEBSD, Utilities.getOperatingSystem ()); 81 } 82 83 public void testGetUnknownOperatingSystem () { 84 System.setProperty ("os.name", "Unknown"); 85 assertEquals ("Windows NT recognized as Unknown", Utilities.OS_OTHER, Utilities.getOperatingSystem ()); 86 } 87 88 public void testWhatIsWinXP () { 89 System.setProperty ("os.name", "Windows XP"); 90 assertTrue ("Windows XP isWindows", Utilities.isWindows ()); 91 assertFalse ("Windows XP not isUnix", Utilities.isUnix ()); 92 } 93 94 public void testWhatIsLinux () { 95 System.setProperty ("os.name", "Linux"); 96 assertFalse ("Linux not isWindows", Utilities.isWindows ()); 97 assertTrue ("Linux isUnix", Utilities.isUnix ()); 98 } 99 100 public void testWhatIsMac () { 101 System.setProperty ("os.name", "Mac OS X"); 102 assertFalse ("Mac not isWindows", Utilities.isWindows ()); 103 assertTrue ("Mac isMac", Utilities.isMac ()); 104 } 105 106 public void testWhatIsFreeBSD () { 107 System.setProperty ("os.name", "freebsd"); 108 assertFalse ("freebsd is not isWindows", Utilities.isWindows ()); 109 assertTrue ("freebsd isUnix", Utilities.isUnix ()); 110 } 111 112 127 128 public void testSpecialKeyworksOn14AsWell15 () throws Exception { 129 KeyStroke ks = Utilities.stringToKey("C-CONTEXT_MENU"); 130 assertNotNull ("key stroke created", ks); 131 KeyStroke alt = ks.getKeyStroke(ks.getKeyCode(), KeyEvent.ALT_MASK); 132 String s = Utilities.keyToString(alt); 133 assertEquals ("Correctly converted", "A-CONTEXT_MENU", s); 134 } 135 136 public void testSpecialKeyworksOn14AsWell15WithoutModificators () throws Exception { 137 KeyStroke ks = Utilities.stringToKey("CONTEXT_MENU"); 138 assertNotNull ("key stroke created", ks); 139 String s = Utilities.keyToString(ks); 140 assertEquals ("Correctly converted", "CONTEXT_MENU", s); 141 } 142 143 public void testActionsToPopupWithLookup() throws Exception { 144 MockServices.setServices(AwtBridgeImpl.class); 145 final List <String > commands = new ArrayList <String >(); 146 class BasicAction extends AbstractAction { 147 public BasicAction(String name) { 148 super(name); 149 } 150 public void actionPerformed(ActionEvent e) { 151 commands.add((String ) getValue(Action.NAME)); 152 } 153 } 154 class ContextAction extends BasicAction implements ContextAwareAction { 155 public ContextAction(String name) { 156 super(name); 157 } 158 public Action createContextAwareInstance(final Lookup actionContext) { 159 return new AbstractAction () { 160 public void actionPerformed(ActionEvent e) { 161 commands.add(ContextAction.this.getValue(Action.NAME) + "/" + actionContext.lookup(String .class)); 162 } 163 }; 164 } 165 } 166 class SpecialMenuAction extends BasicAction implements Presenter.Popup { 167 public SpecialMenuAction(String name) { 168 super(name); 169 } 170 public JMenuItem getPopupPresenter() { 171 JMenuItem item = new JMenuItem ((String ) getValue(Action.NAME)); 172 item.addActionListener(new ActionListener () { 173 public void actionPerformed(ActionEvent e) { 174 commands.add(((String ) getValue(Action.NAME)) + "/popup"); 175 } 176 }); 177 return item; 178 } 179 } 180 Action duplicated = new BasicAction("duplicated"); 181 Action [] actions = new Action [] { 182 null, 183 null, 184 new BasicAction("first"), 185 duplicated, 186 null, 187 null, 188 new BasicAction("second"), 189 duplicated, 190 null, 191 new ContextAction("context"), 192 new SpecialMenuAction("presenter"), 193 null, 194 new BasicAction("top"), 195 new BasicAction("HIDDEN"), 196 null, 197 new BasicAction("bottom"), 198 null, 199 null, 200 }; 201 Lookup l = Lookups.singleton("thing"); 202 JPopupMenu menu = Utilities.actionsToPopup(actions, l); 203 for (Component element : menu.getComponents()) { if (element instanceof AbstractButton ) { 205 ((AbstractButton ) element).doClick(); 206 } else { 207 commands.add(null); 208 } 209 } 210 String [] expectedCommands = new String [] { 211 "first", 213 "duplicated", 214 null, "second", 216 null, 218 "context/thing", "presenter/popup", null, 221 "top", 222 null, 225 "bottom", 226 }; 228 assertEquals("correct generated menu", Arrays.asList(expectedCommands), commands); 229 } 230 231 423 424 public static final class AwtBridgeImpl extends AWTBridge { 425 public JPopupMenu createEmptyPopup() { 426 return new JPopupMenu (); 427 } 428 public JMenuItem createMenuPresenter(Action action) { 429 return new JMenuItem (action); 430 } 431 public JMenuItem createPopupPresenter(Action action) { 432 return new JMenuItem (action); 433 } 434 public Component createToolbarPresenter(Action action) { 435 return new JButton (action); 436 } 437 public Component [] convertComponents(Component comp) { 438 if (comp instanceof JMenuItem && "HIDDEN".equals(((JMenuItem ) comp).getText())) { 439 return new Component [0]; 440 } else { 441 return new Component [] {comp}; 442 } 443 } 444 } 445 446 } 447 | Popular Tags |