1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.net.URL ; 23 import java.util.List ; 24 import javax.swing.JSeparator ; 25 import org.netbeans.api.editor.mimelookup.MimeLookup; 26 import org.netbeans.api.editor.mimelookup.MimePath; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.actions.CopyAction; 29 import org.openide.actions.CutAction; 30 import org.openide.actions.DeleteAction; 31 import org.openide.actions.PasteAction; 32 import org.openide.actions.ReplaceAction; 33 import org.openide.actions.FindAction; 34 import org.openide.actions.NewAction; 35 import org.openide.util.Lookup; 36 37 41 public class MimeLookupInheritanceTest extends NbTestCase { 42 43 private static final int WAIT_TIME = 2000; 44 45 public MimeLookupInheritanceTest(java.lang.String testName) { 46 super(testName); 47 } 48 49 protected void setUp() throws java.lang.Exception { 50 EditorTestLookup.setLookup( 52 new URL [] { 53 getClass().getResource("test-layer.xml") 54 }, 55 getWorkDir(), 56 new Object [] {}, 57 getClass().getClassLoader(), 58 null 59 ); 60 } 61 62 private void testPopupItems(Lookup lookup, Class [] layerObjects){ 63 PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class); 64 assertTrue("PopupActions should be found", actions != null); 65 if (actions != null){ 66 List popupActions = actions.getPopupActions(); 67 int popupSize = popupActions.size(); 68 assertTrue("popupActions count is not the same as etalon action count" + 69 "Expecting:"+layerObjects.length+" Found:"+popupSize, 70 popupSize == layerObjects.length); 71 72 for (int i = 0; i<layerObjects.length; i++){ 73 Object obj = popupActions.get(i); 74 assertTrue("Incorrect sorting or item is missing in the popup menu." + 75 "Expecting:"+layerObjects[i]+" Found:"+obj.getClass(), 76 layerObjects[i].isAssignableFrom(obj.getClass())); 77 } 78 } 79 } 80 81 82 public void testBaseLevelPopups(){ 83 MimeLookup lookup = MimeLookup.getMimeLookup(""); Class layerObjects[] = {CutAction.class, CopyAction.class, PasteAction.class}; 85 testPopupItems(lookup, layerObjects); 86 } 87 88 89 public void testMimeLevelPopups(){ 90 MimePath mp = MimePath.parse("text/x-java/text/html"); Lookup lookup = MimeLookup.getLookup(mp); 92 Class layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class, 93 NewAction.class, PasteAction.class}; 94 testPopupItems(lookup, layerObjects); 95 } 96 97 98 public void testMimeLevelPopupsWithStringAndSeparator(){ 99 MimePath mp = MimePath.parse("text/x-java/text/html/text/xml"); Lookup lookup = MimeLookup.getLookup(mp); 101 Class layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class, 102 NewAction.class, PasteAction.class, ReplaceAction.class, JSeparator .class, String .class}; 103 testPopupItems(lookup, layerObjects); 104 } 105 106 109 public void testHidding(){ 110 Lookup lookup = MimeLookup.getLookup(MimePath.get("text/xml")); 111 checkLookupObject(lookup, CopyAction.class, true); 112 checkLookupObject(lookup, ReplaceAction.class, true); 113 checkLookupObject(lookup, PasteAction.class, false); 114 lookup = MimeLookup.getLookup(MimePath.get("text/x-ant+xml")); 115 checkLookupObject(lookup, CutAction.class, true); 116 checkLookupObject(lookup, CopyAction.class, false); 117 checkLookupObject(lookup, PasteAction.class, true); 118 checkLookupObject(lookup, ReplaceAction.class, false); 119 } 120 121 124 public void test61245(){ 125 MimeLookup lookup = MimeLookup.getMimeLookup("application/xml"); 126 checkLookupObject(lookup, FindAction.class, true); 127 lookup = MimeLookup.getMimeLookup("application/xhtml+xml"); 128 checkLookupObject(lookup, CutAction.class, true); 129 checkLookupObject(lookup, FindAction.class, false); 130 checkLookupObject(lookup, ReplaceAction.class, true); 131 } 132 133 public void testAntXmlPopup(){ 134 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); Class layerObjects[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class}; 136 testPopupItems(lookup, layerObjects); 137 lookup = MimeLookup.getMimeLookup("text/x-ant+xml"); Class layerObjects2[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class, FindAction.class}; 139 testPopupItems(lookup, layerObjects2); 140 } 141 142 151 protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 152 int time = (int) maxMiliSeconds / 100; 153 while (time > 0) { 154 Object resolvedValue = resolver.getValue(); 155 if (requiredValue == null && resolvedValue == null){ 156 return true; 157 } 158 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 159 return true; 160 } 161 try { 162 Thread.currentThread().sleep(100); 163 } catch (InterruptedException ex) { 164 time=0; 165 } 166 time--; 167 } 168 return false; 169 } 170 171 174 public static interface ValueResolver{ 175 176 Object getValue(); 177 } 178 179 180 private void checkLookupObject(final Lookup lookup, final Class clazz, final boolean shouldBePresent){ 181 waitMaxMilisForValue(WAIT_TIME, new ValueResolver(){ 182 public Object getValue(){ 183 Object obj = lookup.lookup(clazz); 184 boolean bool = (shouldBePresent) ? obj != null : obj == null; 185 return Boolean.valueOf(bool); 186 } 187 }, Boolean.TRUE); 188 Object obj = lookup.lookup(clazz); 189 if (shouldBePresent){ 190 assertTrue("Object should be present in the lookup",obj!=null); 191 } else { 192 assertTrue("Object should NOT be present in the lookup",obj==null); 193 } 194 } 195 196 197 } 198 | Popular Tags |