Added
Link Here
|
1 |
/* |
2 |
* PaletteItemTest.java |
3 |
* JUnit based test |
4 |
* |
5 |
* Created on August 10, 2005, 3:33 PM |
6 |
*/ |
7 |
|
8 |
package org.netbeans.spi.palette; |
9 |
import java.beans.BeanInfo; |
10 |
import java.beans.PropertyChangeEvent; |
11 |
import java.beans.PropertyChangeListener; |
12 |
import java.io.IOException; |
13 |
import java.io.OutputStreamWriter; |
14 |
import org.netbeans.junit.NbTestCase; |
15 |
import org.netbeans.modules.palette.Category; |
16 |
import org.netbeans.modules.palette.Item; |
17 |
import org.openide.filesystems.FileLock; |
18 |
import org.openide.filesystems.FileObject; |
19 |
import org.openide.filesystems.FileUtil; |
20 |
import org.openide.filesystems.Repository; |
21 |
import org.openide.loaders.DataObject; |
22 |
import org.openide.nodes.Node; |
23 |
import org.openide.text.ActiveEditorDrop; |
24 |
import org.openide.util.Lookup; |
25 |
import org.openide.util.NbBundle; |
26 |
import org.openide.util.Utilities; |
27 |
|
28 |
/** |
29 |
* |
30 |
* @author Libor Kotouc |
31 |
*/ |
32 |
public class PaletteItemTest extends NbTestCase { |
33 |
|
34 |
private static final String PALETTE_ROOT = "FooPalette"; |
35 |
private static final String ITEMS_FOLDER = PALETTE_ROOT + "/FooCategory"; |
36 |
private static final String ITEM_FILE = "FooItem.xml"; |
37 |
private static final String CLASS_NAME = "org.netbeans.spi.palette.FooItem"; |
38 |
private static final String BODY = "<fooTag att=''></fooTag>"; |
39 |
private static final String ICON16 = "org/netbeans/spi/palette/FooItem16.gif"; |
40 |
private static final String ICON32 = "org/netbeans/spi/palette/FooItem32.gif"; |
41 |
private static final String BUNDLE_NAME = "org.netbeans.spi.palette.Bundle"; |
42 |
private static final String NAME_KEY = "NAME_foo-FooItem"; |
43 |
private static final String TOOLTIP_KEY = "HINT_foo-FooItem"; |
44 |
|
45 |
|
46 |
private FileObject itemsFolder; |
47 |
private FileObject itemFile; |
48 |
|
49 |
private Lookup selectedNode; |
50 |
|
51 |
public PaletteItemTest(String testName) { |
52 |
super(testName); |
53 |
} |
54 |
|
55 |
protected void setUp() throws Exception { |
56 |
itemsFolder = createItemsFolder(); |
57 |
assertNotNull(itemsFolder); |
58 |
} |
59 |
|
60 |
protected void tearDown() throws Exception { |
61 |
FileObject[] ch = itemsFolder.getChildren(); |
62 |
for (int i = 0; i < ch.length; i++) |
63 |
ch[i].delete(); |
64 |
FileObject paletteRoot = itemsFolder.getParent(); |
65 |
itemsFolder.delete(); |
66 |
paletteRoot.delete(); |
67 |
} |
68 |
|
69 |
public void testReadItemWithActiveEditorDrop() throws Exception { |
70 |
itemFile = createItemFileWithActiveEditorDrop(); |
71 |
assertNotNull(itemFile); |
72 |
|
73 |
Node itemNode = DataObject.find(itemFile).getNodeDelegate(); |
74 |
|
75 |
assertEquals("Item loaded with a wrong display name.", NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY), itemNode.getDisplayName()); |
76 |
assertEquals("Item loaded with a wrong description.", NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY), itemNode.getShortDescription()); |
77 |
assertNotNull("Item loaded with no small icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_16x16)); |
78 |
assertNotNull("Item loaded with no big icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_32x32)); |
79 |
|
80 |
Object o = itemNode.getLookup().lookup(ActiveEditorDrop.class); |
81 |
assertNotNull("Item does not contain ActiveEditorDrop implementation in its lookup.", o); |
82 |
} |
83 |
|
84 |
public void testReadItemWithBody() throws Exception { |
85 |
itemFile = createItemFileWithBody(); |
86 |
assertNotNull(itemFile); |
87 |
|
88 |
Node itemNode = DataObject.find(itemFile).getNodeDelegate(); |
89 |
|
90 |
assertEquals("Item loaded with a wrong display name.", NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY), itemNode.getDisplayName()); |
91 |
assertEquals("Item loaded with a wrong description.", NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY), itemNode.getShortDescription()); |
92 |
assertNotNull("Item loaded with no small icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_16x16)); |
93 |
assertNotNull("Item loaded with no big icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_32x32)); |
94 |
|
95 |
Object o = itemNode.getLookup().lookup(String.class); |
96 |
assertNotNull("Item does not contain body string in its lookup.", o); |
97 |
assertEquals("Item loaded with a wrong body string.", BODY, o); |
98 |
} |
99 |
|
100 |
public void testFindItem() throws Exception { |
101 |
itemFile = createItemFileWithActiveEditorDrop(); |
102 |
assertNotNull(itemFile); |
103 |
|
104 |
//create palette |
105 |
PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions()); |
106 |
|
107 |
//find node with ActiveEditorDrop impl in its lookup |
108 |
Node root = (Node)pc.getRoot().lookup(Node.class); |
109 |
Node[] cats = root.getChildren().getNodes(true); |
110 |
Node foundNode = null; |
111 |
Node foundCat = null; |
112 |
assertEquals("Too many categories", 1, cats.length); |
113 |
Node[] items = cats[0].getChildren().getNodes(true); |
114 |
assertEquals("Too many items", 1, items.length); |
115 |
|
116 |
assertTrue("Item not found.", |
117 |
DataObject.find(itemFile).getNodeDelegate().getLookup().lookup(ActiveEditorDrop.class) == |
118 |
items[0].getLookup().lookup(ActiveEditorDrop.class) |
119 |
); |
120 |
|
121 |
} |
122 |
|
123 |
public void testSelectItem() throws Exception { |
124 |
itemFile = createItemFileWithActiveEditorDrop(); |
125 |
assertNotNull(itemFile); |
126 |
|
127 |
//create palette |
128 |
PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions()); |
129 |
pc.addPropertyChangeListener(new PaletteListener(pc)); |
130 |
|
131 |
//simulate node selection |
132 |
Category modelCat = pc.getModel().getCategories()[0]; |
133 |
Item modelItem = modelCat.getItems()[0]; |
134 |
pc.getModel().setSelectedItem(modelCat.getLookup(), modelItem.getLookup()); |
135 |
|
136 |
assertNotNull("Selected item does not contain ActiveEditorDrop implementation", |
137 |
selectedNode.lookup(ActiveEditorDrop.class)); |
138 |
} |
139 |
|
140 |
|
141 |
//---------------------------------- helpers ------------------------------------------------------------------ |
142 |
|
143 |
private class PaletteListener implements PropertyChangeListener { |
144 |
PaletteController pc; |
145 |
PaletteListener(PaletteController pc) { this.pc = pc; } |
146 |
public void propertyChange(PropertyChangeEvent evt) { |
147 |
assertEquals("Property " + PaletteController.PROP_SELECTED_ITEM + " was expected.", |
148 |
PaletteController.PROP_SELECTED_ITEM, evt.getPropertyName()); |
149 |
|
150 |
selectedNode = pc.getSelectedItem(); |
151 |
} |
152 |
}; |
153 |
|
154 |
|
155 |
private FileObject createItemsFolder() throws IOException { |
156 |
FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); |
157 |
FileObject fooCategory = FileUtil.createFolder(root, ITEMS_FOLDER); |
158 |
return fooCategory; |
159 |
} |
160 |
|
161 |
private FileObject createItemFileWithActiveEditorDrop() throws Exception { |
162 |
FileObject fo = itemsFolder.createData(ITEM_FILE); |
163 |
FileLock lock = fo.lock(); |
164 |
try { |
165 |
OutputStreamWriter writer = new OutputStreamWriter(fo.getOutputStream(lock), "UTF-8"); |
166 |
try { |
167 |
writer.write("<?xml version='1.0' encoding='UTF-8'?>"); |
168 |
writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>"); |
169 |
writer.write("<editor_palette_item version='1.0'>"); |
170 |
writer.write("<class name='" + CLASS_NAME + "' />"); |
171 |
writer.write("<icon16 urlvalue='" + ICON16 + "' />"); |
172 |
writer.write("<icon32 urlvalue='" + ICON32 + "' />"); |
173 |
writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />"); |
174 |
writer.write("</editor_palette_item>"); |
175 |
} finally { |
176 |
writer.close(); |
177 |
} |
178 |
} finally { |
179 |
lock.releaseLock(); |
180 |
} |
181 |
return fo; |
182 |
} |
183 |
|
184 |
private FileObject createItemFileWithBody() throws Exception { |
185 |
FileObject fo = itemsFolder.createData(ITEM_FILE); |
186 |
FileLock lock = fo.lock(); |
187 |
try { |
188 |
OutputStreamWriter writer = new OutputStreamWriter(fo.getOutputStream(lock), "UTF-8"); |
189 |
try { |
190 |
writer.write("<?xml version='1.0' encoding='UTF-8'?>"); |
191 |
writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>"); |
192 |
writer.write("<editor_palette_item version='1.0'>"); |
193 |
writer.write("<body><![CDATA[" + BODY + "]]></body>"); |
194 |
writer.write("<icon16 urlvalue='" + ICON16 + "' />"); |
195 |
writer.write("<icon32 urlvalue='" + ICON32 + "' />"); |
196 |
writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />"); |
197 |
writer.write("</editor_palette_item>"); |
198 |
} finally { |
199 |
writer.close(); |
200 |
} |
201 |
} finally { |
202 |
lock.releaseLock(); |
203 |
} |
204 |
return fo; |
205 |
} |
206 |
|
207 |
} |