1 19 20 package org.openide.awt; 21 22 import java.awt.event.ContainerEvent ; 23 import java.awt.event.ContainerListener ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.logging.Level ; 27 import javax.swing.JMenu ; 28 import org.netbeans.junit.Log; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.actions.OpenAction; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.loaders.*; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.Repository; 37 import org.openide.nodes.Node; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.actions.CallbackSystemAction; 40 41 45 public class MenuBarTest extends NbTestCase implements ContainerListener { 46 private DataFolder df; 47 private MenuBar mb; 48 49 private int add; 50 private int remove; 51 52 public MenuBarTest(String testName) { 53 super(testName); 54 } 55 56 protected Level logLevel() { 57 return Level.FINE; 58 } 59 60 protected void setUp() throws Exception { 61 FileObject fo = FileUtil.createFolder( 62 Repository.getDefault().getDefaultFileSystem().getRoot(), 63 "Folder" + getName() 64 ); 65 df = DataFolder.findFolder(fo); 66 mb = new MenuBar(df); 67 mb.waitFinished(); 68 } 69 70 protected void tearDown() throws Exception { 71 } 72 73 public void testAllInstances() throws Exception { 74 InstanceCookie[] ics = new InstanceCookie[] { 75 new IC(false), 76 new IC(true) 77 }; 78 MenuBar.allInstances(ics, new ArrayList <Object >()); 79 } 80 81 private static class IC implements InstanceCookie { 82 private boolean throwing; 83 IC(boolean throwing) { 84 this.throwing = throwing; 85 } 86 public String instanceName() { 87 return "dummy"; 88 } 89 90 public Class <?> instanceClass() throws IOException , ClassNotFoundException { 91 return Object .class; 92 } 93 94 public Object instanceCreate() throws IOException , ClassNotFoundException { 95 if (throwing) { 96 Exception e = new Exception ("original"); 97 throw (IOException ) new IOException ("inited").initCause(e); 98 } 99 return new Object (); 100 } 101 } 102 103 public void testHowManyRepaintsPerOneChangeAreThere() throws Exception { 104 mb.addContainerListener(this); 105 assertEquals("No children now", 0, mb.getComponentCount()); 106 107 class Atom implements FileSystem.AtomicAction { 108 FileObject m1, m2; 109 110 public void run() throws IOException { 111 m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1"); 112 m2 = FileUtil.createFolder(df.getPrimaryFile(), "m2"); 113 } 114 } 115 Atom atom = new Atom(); 116 df.getPrimaryFile().getFileSystem().runAtomicAction(atom); 117 mb.waitFinished(); 118 119 assertEquals("Two children there", 2, mb.getComponentCount()); 120 121 assertEquals("No removals", 0, remove); 122 assertEquals("Two additions", 2, add); 123 124 DataFolder f1 = DataFolder.findFolder(atom.m1); 125 InstanceDataObject.create(f1, "Kuk", OpenAction.class); 126 mb.waitFinished(); 127 128 assertEquals("Two children there", 2, mb.getComponentCount()); 129 Object o1 = mb.getComponent(0); 130 if (!(o1 instanceof JMenu )) { 131 fail("It has to be menu: " + o1); 132 } 133 JMenu m1 = (JMenu )o1; 134 m1.setSelected(true); 136 java.awt.Component [] content = m1.getPopupMenu().getComponents(); 137 assertEquals("Now it has one child", 1, content.length); 138 139 assertEquals("Still No removals in MenuBar", 0, remove); 140 assertEquals("Still Two additions in MenuBar", 2, add); 141 } 142 143 144 public void testMenusAreResolvedLazilyUntilTheyAreReallyNeeded() throws Exception { 145 mb.addContainerListener(this); 146 assertEquals("No children now", 0, mb.getComponentCount()); 147 148 class Atom implements FileSystem.AtomicAction { 149 FileObject m1, m2; 150 151 public void run() throws IOException { 152 m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1"); 153 DataFolder f1 = DataFolder.findFolder(m1); 154 InstanceDataObject.create(f1, "X", MyAction.class); 155 } 156 } 157 Atom atom = new Atom(); 158 df.getPrimaryFile().getFileSystem().runAtomicAction(atom); 159 mb.waitFinished(); 160 161 assertEquals("One submenu is there", 1, mb.getComponentCount()); 162 163 assertEquals("No removals", 0, remove); 164 assertEquals("One addition", 1, add); 165 166 Object o1 = mb.getComponent(0); 167 if (!(o1 instanceof JMenu )) { 168 fail("It has to be menu: " + o1); 169 } 170 JMenu m1 = (JMenu )o1; 171 172 assertEquals("We have the menu, but the content is still not computed", 0, MyAction.counter); 173 174 m1.setSelected(true); 176 java.awt.Component [] content = m1.getPopupMenu().getComponents(); 177 assertEquals("Now it has one child", 1, content.length); 178 179 assertEquals("Still No removals in MenuBar", 0, remove); 180 assertEquals("Still one addition in MenuBar", 1, add); 181 182 assertEquals("And now the action is created", 1, MyAction.counter); 183 } 184 185 public void testSurviveInvalidationOfAFolder() throws Exception { 186 CharSequence seq = Log.enable("", Level.ALL); 187 188 189 FileObject m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1"); 190 final DataFolder f1 = DataFolder.findFolder(m1); 191 192 mb.waitFinished(); 193 194 JMenu menu; 195 { 196 Object o1 = mb.getComponent(0); 197 if (!(o1 instanceof JMenu )) { 198 fail("It has to be menu: " + o1); 199 } 200 menu = (JMenu )o1; 201 assertEquals("simple name ", "m1", menu.getText()); 202 } 203 204 Node n = f1.getNodeDelegate(); 205 f1.setValid(false); 206 mb.waitFinished(); 207 208 n.setName("othername"); 209 210 mb.waitFinished(); 211 212 assertEquals("updated the folder is deleted now", f1.getName(), menu.getText()); 213 214 215 216 if (seq.toString().indexOf("fix your code") >= 0) { 217 fail("There were warnings about the use of invalid nodes: " + seq); 218 } 219 } 220 221 public void componentAdded(ContainerEvent e) { 222 add++; 223 } 224 225 public void componentRemoved(ContainerEvent e) { 226 remove++; 227 } 228 229 public static final class MyAction extends CallbackSystemAction { 230 public static int counter; 231 232 public MyAction() { 233 counter++; 234 } 235 236 public String getName() { 237 return "MyAction"; 238 } 239 240 public HelpCtx getHelpCtx() { 241 return HelpCtx.DEFAULT_HELP; 242 } 243 244 245 } 246 } 247 | Popular Tags |