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.junit.NbTestCase; 27 import org.openide.actions.CopyAction; 28 import org.openide.actions.CutAction; 29 import org.openide.actions.DeleteAction; 30 import org.openide.actions.PasteAction; 31 import org.openide.actions.ReplaceAction; 32 import org.openide.actions.FindAction; 33 import org.openide.actions.NewAction; 34 35 36 41 public class Depr_MimeLookupInheritanceTest extends NbTestCase { 42 43 private static final int WAIT_TIME = 2000; 44 45 public Depr_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(MimeLookup 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 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/html"); Class layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class, 92 NewAction.class, PasteAction.class}; 93 testPopupItems(lookup, layerObjects); 94 } 95 96 97 public void testMimeLevelPopupsWithStringAndSeparator(){ 98 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/html").childLookup("text/xml"); Class layerObjects[] = {CutAction.class, DeleteAction.class, CopyAction.class, 100 NewAction.class, PasteAction.class, ReplaceAction.class, JSeparator .class, String .class}; 101 testPopupItems(lookup, layerObjects); 102 } 103 104 107 public void testHidding(){ 108 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); 109 checkLookupObject(lookup, CopyAction.class, true); 110 checkLookupObject(lookup, ReplaceAction.class, true); 111 checkLookupObject(lookup, PasteAction.class, false); 112 lookup = MimeLookup.getMimeLookup("text/x-ant+xml"); 113 checkLookupObject(lookup, CutAction.class, true); 114 checkLookupObject(lookup, CopyAction.class, false); 115 checkLookupObject(lookup, PasteAction.class, true); 116 checkLookupObject(lookup, ReplaceAction.class, false); 117 } 118 119 122 public void test61245(){ 123 MimeLookup lookup = MimeLookup.getMimeLookup("application/xml"); 124 checkLookupObject(lookup, FindAction.class, true); 125 lookup = MimeLookup.getMimeLookup("application/xhtml+xml"); 126 checkLookupObject(lookup, CutAction.class, true); 127 checkLookupObject(lookup, FindAction.class, false); 128 checkLookupObject(lookup, ReplaceAction.class, true); 129 } 130 131 public void testAntXmlPopup(){ 132 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); Class layerObjects[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class}; 134 testPopupItems(lookup, layerObjects); 135 lookup = MimeLookup.getMimeLookup("text/x-ant+xml"); Class layerObjects2[] = {CutAction.class, CopyAction.class, PasteAction.class, ReplaceAction.class, FindAction.class}; 137 testPopupItems(lookup, layerObjects2); 138 } 139 140 149 protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 150 int time = (int) maxMiliSeconds / 100; 151 while (time > 0) { 152 Object resolvedValue = resolver.getValue(); 153 if (requiredValue == null && resolvedValue == null){ 154 return true; 155 } 156 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 157 return true; 158 } 159 try { 160 Thread.currentThread().sleep(100); 161 } catch (InterruptedException ex) { 162 time=0; 163 } 164 time--; 165 } 166 return false; 167 } 168 169 172 public static interface ValueResolver{ 173 174 Object getValue(); 175 } 176 177 178 private void checkLookupObject(final MimeLookup lookup, final Class clazz, final boolean shouldBePresent){ 179 waitMaxMilisForValue(WAIT_TIME, new ValueResolver(){ 180 public Object getValue(){ 181 Object obj = lookup.lookup(clazz); 182 boolean bool = (shouldBePresent) ? obj != null : obj == null; 183 return Boolean.valueOf(bool); 184 } 185 }, Boolean.TRUE); 186 Object obj = lookup.lookup(clazz); 187 if (shouldBePresent){ 188 assertTrue("Object should be present in the lookup",obj!=null); 189 } else { 190 assertTrue("Object should NOT be present in the lookup",obj==null); 191 } 192 } 193 194 195 } 196 | Popular Tags |