KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > NbEditorToolBarTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor;
21
22 import java.net.URL JavaDoc;
23 import javax.swing.JEditorPane JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.text.Document JavaDoc;
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 /**
35  *
36  * @author Andrei Badea
37  */

38 public class NbEditorToolBarTest extends NbTestCase {
39
40     public NbEditorToolBarTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public boolean runInEQ() {
45         return true;
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50
51         clearWorkDir();
52
53         EditorTestLookup.setLookup(
54             new URL JavaDoc[] {
55                 EditorTestConstants.EDITOR_LAYER_URL,
56             },
57             new Object JavaDoc[] {},
58             getClass().getClassLoader()
59         );
60     }
61
62     /**
63      * Tests that the action context for the context-aware toolbar actions
64      * is the first Lookup.Provider ancestor.
65      */

66     public void testActionContextAncestorsLookupProviderIsPreferred() throws Exception JavaDoc {
67         JPanel JavaDoc parent1 = new LookupPanel(Lookups.singleton(new Foo() { }));
68         JPanel JavaDoc parent2 = new LookupPanel(Lookups.singleton(new Bar() { }));
69         parent1.add(parent2);
70         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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     /**
85      * Tests that the action context for the context-aware toolbar actions
86      * is the DataObject corresponding to the current document if there is no
87      * Lookup.Provider ancestor.
88      */

89     public void testActionContextFallbackToDataObject() throws Exception JavaDoc {
90         JPanel JavaDoc parent = new JPanel JavaDoc();
91         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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     /**
105      * Tests that the action context for the context-aware toolbar actions
106      * is null if there is no Lookup.Provider ancestor and no DataObject
107      * corresponding to the current document.
108      */

109     public void testActionContextNullWhenNoDataObject() {
110         JPanel JavaDoc parent = new JPanel JavaDoc();
111         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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     /**
121      * Tests that the action context for the context-aware toolbar actions
122      * contains the node corresponding to the current document only once, even
123      * though the node is both contained in an ancestor Lookup.Provider and
124      * obtained as the node delegate of the DataObject of the current document.
125      */

126     public void testActionContextLookupContainsNodeOnlyOnce() throws Exception JavaDoc {
127         DataObject docDataObject = createDataObject();
128         assertNotNull(docDataObject);
129         JPanel JavaDoc parent = new LookupPanel(Lookups.fixed(new Object JavaDoc[] { new Bar() { }, docDataObject.getNodeDelegate().getLookup() }));
130         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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 JavaDoc {
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 JavaDoc 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