1 19 20 package org.netbeans.modules.editor; 21 22 import java.net.URL ; 23 import javax.swing.JEditorPane ; 24 import javax.swing.JPanel ; 25 import javax.swing.text.Document ; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.LocalFileSystem; 29 import org.openide.loaders.DataObject; 30 import org.openide.nodes.Node; 31 import org.openide.util.Lookup; 32 import org.openide.util.lookup.Lookups; 33 34 38 public class NbEditorToolBarTest extends NbTestCase { 39 40 public NbEditorToolBarTest(String testName) { 41 super(testName); 42 } 43 44 public boolean runInEQ() { 45 return true; 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 51 clearWorkDir(); 52 53 EditorTestLookup.setLookup( 54 new URL [] { 55 EditorTestConstants.EDITOR_LAYER_URL, 56 }, 57 new Object [] {}, 58 getClass().getClassLoader() 59 ); 60 } 61 62 66 public void testActionContextAncestorsLookupProviderIsPreferred() throws Exception { 67 JPanel parent1 = new LookupPanel(Lookups.singleton(new Foo() { })); 68 JPanel parent2 = new LookupPanel(Lookups.singleton(new Bar() { })); 69 parent1.add(parent2); 70 JEditorPane editor = new JEditorPane (); 71 editor.setEditorKit(new NbEditorKit()); 72 parent2.add(editor); 73 DataObject docDataObject = createDataObject(); 74 assertNotNull(docDataObject); 75 editor.getDocument().putProperty(Document.StreamDescriptionProperty, docDataObject); 76 77 NbEditorToolBar toolbar = new NbEditorToolBar(editor); 78 Lookup actionContext = toolbar.createActionContext(editor); 79 assertNotNull(actionContext.lookup(Bar.class)); 80 assertNotNull(actionContext.lookup(Node.class)); 81 assertNull(actionContext.lookup(Foo.class)); 82 } 83 84 89 public void testActionContextFallbackToDataObject() throws Exception { 90 JPanel parent = new JPanel (); 91 JEditorPane editor = new JEditorPane (); 92 editor.setEditorKit(new NbEditorKit()); 93 parent.add(editor); 94 DataObject docDataObject = createDataObject(); 95 assertNotNull(docDataObject); 96 editor.getDocument().putProperty(Document.StreamDescriptionProperty, docDataObject); 97 98 NbEditorToolBar toolbar = new NbEditorToolBar(editor); 99 Lookup actionContext = toolbar.createActionContext(editor); 100 assertNotNull(actionContext.lookup(Node.class)); 101 assertNull(actionContext.lookup(Foo.class)); 102 } 103 104 109 public void testActionContextNullWhenNoDataObject() { 110 JPanel parent = new JPanel (); 111 JEditorPane editor = new JEditorPane (); 112 editor.setEditorKit(new NbEditorKit()); 113 parent.add(editor); 114 115 NbEditorToolBar toolbar = new NbEditorToolBar(editor); 116 Lookup actionContext = toolbar.createActionContext(editor); 117 assertNull(actionContext); 118 } 119 120 126 public void testActionContextLookupContainsNodeOnlyOnce() throws Exception { 127 DataObject docDataObject = createDataObject(); 128 assertNotNull(docDataObject); 129 JPanel parent = new LookupPanel(Lookups.fixed(new Object [] { new Bar() { }, docDataObject.getNodeDelegate().getLookup() })); 130 JEditorPane editor = new JEditorPane (); 131 editor.setEditorKit(new NbEditorKit()); 132 parent.add(editor); 133 editor.getDocument().putProperty(Document.StreamDescriptionProperty, docDataObject); 134 135 NbEditorToolBar toolbar = new NbEditorToolBar(editor); 136 Lookup actionContext = toolbar.createActionContext(editor); 137 assertNotNull(actionContext.lookup(Bar.class)); 138 assertNotNull(actionContext.lookup(Node.class)); 139 assertEquals(1, actionContext.lookup(new Lookup.Template(Node.class)).allInstances().size()); 140 } 141 142 private DataObject createDataObject() throws Exception { 143 getWorkDir().mkdirs(); 144 LocalFileSystem lfs = new LocalFileSystem(); 145 lfs.setRootDirectory(getWorkDir()); 146 FileObject fo = lfs.getRoot().createData("file", "txt"); 147 return DataObject.find(fo); 148 } 149 150 private static final class LookupPanel extends JPanel implements Lookup.Provider { 151 152 private final Lookup lookup; 153 154 public LookupPanel(Lookup lookup) { 155 this.lookup = lookup; 156 } 157 158 public Lookup getLookup() { 159 return lookup; 160 } 161 } 162 163 private static interface Foo { 164 } 165 166 private static interface Bar { 167 } 168 } 169 | Popular Tags |