Added
Link Here
|
1 |
package org.eclipse.ui.tests.multieditor; |
2 |
|
3 |
import junit.framework.TestSuite; |
4 |
|
5 |
import org.eclipse.core.resources.IFile; |
6 |
import org.eclipse.core.resources.IProject; |
7 |
import org.eclipse.core.resources.IWorkspace; |
8 |
import org.eclipse.core.resources.ResourcesPlugin; |
9 |
import org.eclipse.core.runtime.CoreException; |
10 |
import org.eclipse.core.runtime.ILog; |
11 |
import org.eclipse.core.runtime.ILogListener; |
12 |
import org.eclipse.core.runtime.IStatus; |
13 |
import org.eclipse.jface.action.IContributionItem; |
14 |
import org.eclipse.jface.action.ToolBarContributionItem; |
15 |
import org.eclipse.jface.action.ToolBarManager; |
16 |
import org.eclipse.swt.widgets.Display; |
17 |
import org.eclipse.swt.widgets.ToolBar; |
18 |
import org.eclipse.swt.widgets.ToolItem; |
19 |
import org.eclipse.ui.IActionBars2; |
20 |
import org.eclipse.ui.IEditorInput; |
21 |
import org.eclipse.ui.IEditorPart; |
22 |
import org.eclipse.ui.IEditorRegistry; |
23 |
import org.eclipse.ui.IViewPart; |
24 |
import org.eclipse.ui.IWorkbenchPage; |
25 |
import org.eclipse.ui.IWorkbenchWindow; |
26 |
import org.eclipse.ui.PartInitException; |
27 |
import org.eclipse.ui.internal.WorkbenchPage; |
28 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
29 |
import org.eclipse.ui.part.FileEditorInput; |
30 |
import org.eclipse.ui.part.MultiEditor; |
31 |
import org.eclipse.ui.part.MultiEditorInput; |
32 |
import org.eclipse.ui.tests.util.CallHistory; |
33 |
import org.eclipse.ui.tests.util.UITestCase; |
34 |
|
35 |
public class MultiEditorTest extends UITestCase { |
36 |
private static final String TEST04_PROPERTIES = "test04.properties"; |
37 |
|
38 |
private static final String TESTEDITOR_COOLBAR = "org.eclipse.ui.tests.multieditor.TestEditor"; |
39 |
|
40 |
private static final String TEST03_ETEST = "test03.etest"; |
41 |
|
42 |
private static final String BUILD_XML = "build.xml"; |
43 |
|
44 |
private static final String EDITOR_FAIL_LOG = "Unable to create editor ID "; |
45 |
|
46 |
public static final String TILED_EDITOR_ID = "org.eclipse.ui.tests.multieditor.TiledEditor"; |
47 |
|
48 |
private static final String TEST01_TXT = "test01.txt"; |
49 |
|
50 |
private static final String TEST02_TXT = "test02.txt"; |
51 |
|
52 |
private NPEListener fNpeListener; |
53 |
|
54 |
public static TestSuite suite() { |
55 |
return new TestSuite(MultiEditorTest.class); |
56 |
} |
57 |
|
58 |
public MultiEditorTest(String tc) { |
59 |
super(tc); |
60 |
} |
61 |
|
62 |
public static class NPEListener implements ILogListener { |
63 |
public boolean noNPE = true; |
64 |
|
65 |
public void logging(IStatus status, String plugin) { |
66 |
String msg = status.getMessage(); |
67 |
if (msg != null |
68 |
&& msg.indexOf(MultiEditorTest.EDITOR_FAIL_LOG |
69 |
+ MultiEditorTest.TILED_EDITOR_ID) >= 0) { |
70 |
noNPE = false; |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
public void testOpenBasicEditor() throws Throwable { |
76 |
final String[] simpleFiles = { TEST01_TXT, TEST02_TXT }; |
77 |
|
78 |
setupNpe(); |
79 |
IWorkbenchWindow window = fWorkbench.getActiveWorkbenchWindow(); |
80 |
|
81 |
// Create a blurb file. |
82 |
IWorkbenchPage page = window.getActivePage(); |
83 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
84 |
IProject testProject = workspace.getRoot().getProject( |
85 |
"TiledEditorProject"); |
86 |
if (!testProject.exists()) { |
87 |
testProject.create(null); |
88 |
} |
89 |
testProject.open(null); |
90 |
|
91 |
MultiEditorInput input = generateEditorInput(simpleFiles, testProject); |
92 |
|
93 |
openAndValidateEditor(page, input); |
94 |
// listItems((WorkbenchPage) page); |
95 |
} |
96 |
|
97 |
public void testOpenBuildFile() throws Throwable { |
98 |
final String[] simpleFiles = { TEST01_TXT, TEST02_TXT, |
99 |
TEST04_PROPERTIES, BUILD_XML, TEST03_ETEST }; |
100 |
|
101 |
setupNpe(); |
102 |
IWorkbenchWindow window = fWorkbench.getActiveWorkbenchWindow(); |
103 |
|
104 |
WorkbenchPage page = (WorkbenchPage) window.getActivePage(); |
105 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
106 |
IProject testProject = workspace.getRoot().getProject( |
107 |
"TiledEditorProject"); |
108 |
if (!testProject.exists()) { |
109 |
testProject.create(null); |
110 |
} |
111 |
testProject.open(null); |
112 |
|
113 |
MultiEditorInput input = generateEditorInput(simpleFiles, testProject); |
114 |
|
115 |
IEditorPart editor = openAndValidateEditor(page, input); |
116 |
assertTrue(editor instanceof MultiEditor); |
117 |
MultiEditor multiEditor = (MultiEditor) editor; |
118 |
chewUpEvents(); |
119 |
|
120 |
IEditorPart activeEditor = multiEditor.getActiveEditor(); |
121 |
//checkView(page); |
122 |
listItems(page); |
123 |
// getInfo(page); |
124 |
|
125 |
IContributionItem contribution = ((IActionBars2) page.getActionBars()) |
126 |
.getCoolBarManager().find(TESTEDITOR_COOLBAR); |
127 |
assertNotNull(contribution); |
128 |
System.err.println("contribution " + contribution.getClass().getName()); |
129 |
|
130 |
assertFalse("We open in the default text editor", contribution |
131 |
.isVisible()); |
132 |
assertTrue("And we're good to go", contribution.isEnabled()); |
133 |
|
134 |
IEditorPart[] innerEditors = multiEditor.getInnerEditors(); |
135 |
innerEditors[innerEditors.length - 1].setFocus(); |
136 |
|
137 |
chewUpEvents(); |
138 |
// listHistory(((TiledEditor) multiEditor).callHistory); |
139 |
listItems(page); |
140 |
// getInfo(page); |
141 |
|
142 |
activeEditor = multiEditor.getActiveEditor(); |
143 |
|
144 |
assertNotNull(contribution); |
145 |
assertTrue("We are in the test editor", contribution.isVisible()); |
146 |
assertTrue("And we're good to go", contribution.isEnabled()); |
147 |
|
148 |
// extra test |
149 |
innerEditors[innerEditors.length - 2].setFocus(); |
150 |
chewUpEvents(); |
151 |
|
152 |
listItems(page); |
153 |
// getInfo(page); |
154 |
|
155 |
innerEditors[innerEditors.length - 3].setFocus(); |
156 |
chewUpEvents(); |
157 |
|
158 |
listItems(page); |
159 |
// getInfo(page); |
160 |
} |
161 |
|
162 |
private void checkView(WorkbenchPage page) { |
163 |
IViewPart[] views = page.getViews(); |
164 |
for (int i=0; i<views.length; ++i) { |
165 |
System.err.println("view: " + views[i].getViewSite().getId() |
166 |
+ "/" + views[i].getViewSite().getRegisteredName()); |
167 |
} |
168 |
} |
169 |
|
170 |
private void listHistory(CallHistory history) { |
171 |
history.printToConsole(); |
172 |
} |
173 |
|
174 |
private void listItems(WorkbenchPage page) { |
175 |
checkView(page); |
176 |
IContributionItem[] items = ((IActionBars2) page.getActionBars()) |
177 |
.getCoolBarManager().getItems(); |
178 |
System.err.println("Length: " + items.length); |
179 |
for (int i = 0; i < items.length; ++i) { |
180 |
System.err.println("" + items[i].isEnabled() + ":" |
181 |
+ items[i].isVisible() + " " + items[i].getId() + "\n\t" |
182 |
+ items[i].getClass().getName()); |
183 |
if (items[i] instanceof ToolBarContributionItem) { |
184 |
displayItem(items[i]); |
185 |
} |
186 |
} |
187 |
System.err.println("----"); |
188 |
} |
189 |
|
190 |
private void getInfo(WorkbenchPage page) { |
191 |
IContributionItem[] items = ((IActionBars2) page.getActionBars()) |
192 |
.getCoolBarManager().getItems(); |
193 |
for (int i = 0; i < items.length; ++i) { |
194 |
IContributionItem contributionItem = items[i]; |
195 |
if (contributionItem.getId().equals( |
196 |
"org.eclipse.ui.edit.text.actionSet.presentation") |
197 |
|| contributionItem.getId().equals( |
198 |
"org.eclipse.ui.tests.multieditor.actionSet")) { |
199 |
System.err.println("----\n" + contributionItem.getId()); |
200 |
displayItem(contributionItem); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
private void displayItem(IContributionItem contributionItem) { |
206 |
ToolBarManager toolBarManager = (ToolBarManager) ((ToolBarContributionItem) contributionItem) |
207 |
.getToolBarManager(); |
208 |
ToolBar bar = toolBarManager.getControl(); |
209 |
if (bar == null) { |
210 |
System.err.println("\tInfo-items: -1"); |
211 |
} else { |
212 |
System.err.println("\tInfo-items: " + bar.getItemCount() + "/" |
213 |
+ bar.getEnabled() + "/" + bar.isEnabled() + "/" |
214 |
+ bar.isVisible()); |
215 |
ToolItem[] tools = bar.getItems(); |
216 |
for (int t = 0; t < tools.length; ++t) { |
217 |
System.err.println("\t\titem: " + tools[t].getEnabled() + " " |
218 |
+ tools[t].getToolTipText()); |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
/** |
224 |
* After an internal action, see if there are any outstanding SWT events. |
225 |
*/ |
226 |
private void chewUpEvents() throws InterruptedException { |
227 |
Thread.sleep(250); |
228 |
Display display = Display.getCurrent(); |
229 |
while (display.readAndDispatch()) |
230 |
; |
231 |
Thread.sleep(5000); |
232 |
} |
233 |
|
234 |
private IEditorPart openAndValidateEditor(IWorkbenchPage page, |
235 |
MultiEditorInput input) throws PartInitException { |
236 |
IEditorPart editorPart = page.openEditor(input, |
237 |
MultiEditorTest.TILED_EDITOR_ID); |
238 |
// System.err.println("Editor " + editorPart.getClass().getName()); |
239 |
assertNotNull(editorPart); |
240 |
// 3.1.0 only |
241 |
// assertFalse("Blew up", editorPart instanceof ErrorEditorPart); |
242 |
|
243 |
assertTrue("Unable to create " + MultiEditorTest.TILED_EDITOR_ID, |
244 |
fNpeListener.noNPE); |
245 |
return editorPart; |
246 |
} |
247 |
|
248 |
private void setupNpe() { |
249 |
final ILog log = WorkbenchPlugin.getDefault().getLog(); |
250 |
fNpeListener = new NPEListener(); |
251 |
log.addLogListener(fNpeListener); |
252 |
} |
253 |
|
254 |
private MultiEditorInput generateEditorInput(String[] simpleFiles, |
255 |
IProject testProject) throws CoreException { |
256 |
String[] ids = new String[simpleFiles.length]; |
257 |
IEditorInput[] inputs = new IEditorInput[simpleFiles.length]; |
258 |
IEditorRegistry registry = fWorkbench.getEditorRegistry(); |
259 |
|
260 |
for (int f = 0; f < simpleFiles.length; ++f) { |
261 |
IFile f1 = testProject.getFile(simpleFiles[f]); |
262 |
if (!f1.exists()) { |
263 |
f1.create(getClass().getResourceAsStream(simpleFiles[f]), true, |
264 |
null); |
265 |
} |
266 |
ids[f] = registry.getDefaultEditor(f1.getName()).getId(); |
267 |
inputs[f] = new FileEditorInput(f1); |
268 |
} |
269 |
|
270 |
MultiEditorInput input = new MultiEditorInput(ids, inputs); |
271 |
return input; |
272 |
} |
273 |
|
274 |
protected void doTearDown() throws Exception { |
275 |
fWorkbench.getActiveWorkbenchWindow().getActivePage().closeAllEditors( |
276 |
false); |
277 |
super.doTearDown(); |
278 |
} |
279 |
|
280 |
} |