1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.io.IOException ; 23 import java.util.List ; 24 import junit.framework.*; 25 import org.netbeans.api.editor.mimelookup.MimeLookup; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.actions.CutAction; 28 import org.openide.actions.FindAction; 29 import org.openide.actions.NewAction; 30 import org.openide.actions.RenameAction; 31 import org.openide.actions.ReplaceAction; 32 import org.openide.util.Lookup; 33 import org.openide.util.Lookup.Template; 34 import org.openide.util.LookupEvent; 35 import org.openide.util.LookupListener; 36 37 42 public class Depr_MimeLookupPopupItemsChangeTest extends NbTestCase { 43 44 private static final int WAIT_TIME = 5000; 45 private String fsstruct []; 46 47 public Depr_MimeLookupPopupItemsChangeTest(java.lang.String testName) { 48 super(testName); 49 } 50 51 protected void setUp() throws java.lang.Exception { 52 fsstruct = new String [] { 53 "Editors/Popup/org-openide-actions-CutAction.instance", "Editors/Popup/org-openide-actions-CopyAction.instance", "Editors/Popup/org-openide-actions-PasteAction.instance", "Editors/text/x-java/Popup/org-openide-actions-DeleteAction.instance", "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance", "Editors/text/x-java/text/xml/Popup/org-openide-actions-PrintAction.instance", "Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", }; 61 62 EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object [] {}, 63 getClass().getClassLoader()); 64 65 } 66 67 68 public void testDynamicChangeInPopupFolders() throws IOException { 69 final int resultChangedCount[] = new int[1]; 70 resultChangedCount[0] = 0; 71 72 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"). childLookup("text/html"); Lookup.Result result = lookup.lookup(new Template(PopupActions.class)); 75 result.allInstances(); LookupListener listener = new LookupListener(){ 77 public void resultChanged(LookupEvent ev){ 78 resultChangedCount[0]++; 79 } 80 }; 81 result.addLookupListener(listener); 82 PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class); 83 assertTrue("PopupActions should be found", actions != null); 84 List popupActions = actions.getPopupActions(); 85 int size = popupActions.size(); 86 assertTrue("Number of PopupActions found:"+size+" and should be:"+fsstruct.length, size == fsstruct.length); 87 88 TestUtilities.deleteFile(getWorkDir(), 90 "Editors/text/x-java/Popup/org-openide-actions-RenameAction.instance"); 91 checkPopupItemPresence(lookup, RenameAction.class, false); 92 93 assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1); 95 resultChangedCount[0] = 0; 96 97 TestUtilities.deleteFile(getWorkDir(), 99 "Editors/Popup/org-openide-actions-CutAction.instance"); 100 101 checkPopupItemPresence(lookup, CutAction.class, false); 102 103 assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1); 105 resultChangedCount[0] = 0; 106 107 TestUtilities.createFile(getWorkDir(), 109 "Editors/Popup/org-openide-actions-FindAction.instance"); 111 checkPopupItemPresence(lookup, FindAction.class, true); 112 113 assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1); 115 resultChangedCount[0] = 0; 116 117 TestUtilities.createFile(getWorkDir(), 119 "Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-ReplaceAction.instance"); 121 checkPopupItemPresence(lookup, ReplaceAction.class, true); 122 123 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"); checkPopupItemPresence(lookup, ReplaceAction.class, false); 127 checkPopupItemPresence(lookup, FindAction.class, true); 128 129 lookup = MimeLookup.getMimeLookup("text/html"); checkPopupItemPresence(lookup, ReplaceAction.class, false); 132 TestUtilities.createFile(getWorkDir(), 134 "Editors/text/html/Popup/org-openide-actions-ReplaceAction.instance"); 136 checkPopupItemPresence(lookup, ReplaceAction.class, true); 137 138 } 139 140 private void checkPopupItemPresence(final MimeLookup lookup, final Class checkedClazz, final boolean shouldBePresent){ 141 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 142 public Object getValue(){ 143 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 144 if (pa == null){ 145 return Boolean.FALSE; 146 } 147 boolean bool = false; 148 List items = pa.getPopupActions(); 149 for (int i=0; i<items.size(); i++){ 150 Object obj = items.get(i); 151 if (checkedClazz == obj.getClass()){ 152 bool = true; 153 break; 154 } 155 } 156 if (!shouldBePresent){ 157 bool = !bool; 158 } 159 return Boolean.valueOf(bool); 160 } 161 }, Boolean.TRUE); 162 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 163 assertTrue("PopupActions should be found", pa != null); 164 boolean bool = false; 165 List items = pa.getPopupActions(); 166 for (int i=0; i<items.size(); i++){ 167 Object obj = items.get(i); 168 if (checkedClazz == obj.getClass()){ 169 bool = true; 170 break; 171 } 172 } 173 if (shouldBePresent){ 174 assertTrue("Class: "+checkedClazz+" should be present in lookup", bool); 175 }else{ 176 assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool); 177 } 178 } 179 180 181 } 182 | Popular Tags |