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.api.editor.mimelookup.MimePath; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.actions.CutAction; 29 import org.openide.actions.FindAction; 30 import org.openide.actions.NewAction; 31 import org.openide.actions.RenameAction; 32 import org.openide.actions.ReplaceAction; 33 import org.openide.util.Lookup; 34 import org.openide.util.Lookup.Template; 35 import org.openide.util.LookupEvent; 36 import org.openide.util.LookupListener; 37 38 42 public class MimeLookupPopupItemsChangeTest extends NbTestCase { 43 44 private static final int WAIT_TIME = 5000; 45 private String fsstruct []; 46 47 public 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 MimePath mp = MimePath.parse("text/x-java/text/xml/text/html"); 73 Lookup lookup = MimeLookup.getLookup(mp); 74 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 mp = MimePath.get(MimePath.get("text/x-java"), "text/xml"); 126 lookup = MimeLookup.getLookup(mp); 127 checkPopupItemPresence(lookup, ReplaceAction.class, false); 128 checkPopupItemPresence(lookup, FindAction.class, true); 129 130 lookup = MimeLookup.getMimeLookup("text/html"); checkPopupItemPresence(lookup, ReplaceAction.class, false); 133 TestUtilities.createFile(getWorkDir(), 135 "Editors/text/html/Popup/org-openide-actions-ReplaceAction.instance"); 137 checkPopupItemPresence(lookup, ReplaceAction.class, true); 138 139 } 140 141 private void checkPopupItemPresence(final Lookup lookup, final Class checkedClazz, final boolean shouldBePresent){ 142 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 143 public Object getValue(){ 144 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 145 if (pa == null){ 146 return Boolean.FALSE; 147 } 148 boolean bool = false; 149 List items = pa.getPopupActions(); 150 for (int i=0; i<items.size(); i++){ 151 Object obj = items.get(i); 152 if (checkedClazz == obj.getClass()){ 153 bool = true; 154 break; 155 } 156 } 157 if (!shouldBePresent){ 158 bool = !bool; 159 } 160 return Boolean.valueOf(bool); 161 } 162 }, Boolean.TRUE); 163 PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class); 164 assertTrue("PopupActions should be found", pa != null); 165 boolean bool = false; 166 List items = pa.getPopupActions(); 167 for (int i=0; i<items.size(); i++){ 168 Object obj = items.get(i); 169 if (checkedClazz == obj.getClass()){ 170 bool = true; 171 break; 172 } 173 } 174 if (shouldBePresent){ 175 assertTrue("Class: "+checkedClazz+" should be present in lookup", bool); 176 }else{ 177 assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool); 178 } 179 } 180 181 182 } 183 | Popular Tags |