|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.tests.multieditor; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.net.URL; |
| 15 |
import java.util.ArrayList; |
| 16 |
|
| 17 |
import org.eclipse.core.resources.IFile; |
| 18 |
import org.eclipse.core.resources.IProject; |
| 19 |
import org.eclipse.core.resources.IWorkspace; |
| 20 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 21 |
import org.eclipse.core.runtime.CoreException; |
| 22 |
import org.eclipse.core.runtime.FileLocator; |
| 23 |
import org.eclipse.core.runtime.ILog; |
| 24 |
import org.eclipse.core.runtime.ILogListener; |
| 25 |
import org.eclipse.core.runtime.IStatus; |
| 26 |
import org.eclipse.swt.SWT; |
| 27 |
import org.eclipse.swt.widgets.Event; |
| 28 |
import org.eclipse.ui.IEditorInput; |
| 29 |
import org.eclipse.ui.IEditorPart; |
| 30 |
import org.eclipse.ui.IEditorRegistry; |
| 31 |
import org.eclipse.ui.IPageLayout; |
| 32 |
import org.eclipse.ui.IViewPart; |
| 33 |
import org.eclipse.ui.IWorkbenchPage; |
| 34 |
import org.eclipse.ui.IWorkbenchPart; |
| 35 |
import org.eclipse.ui.IWorkbenchWindow; |
| 36 |
import org.eclipse.ui.internal.PartSite; |
| 37 |
import org.eclipse.ui.internal.WorkbenchPage; |
| 38 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
| 39 |
import org.eclipse.ui.part.FileEditorInput; |
| 40 |
import org.eclipse.ui.part.MultiEditorInput; |
| 41 |
import org.eclipse.ui.tests.TestPlugin; |
| 42 |
import org.eclipse.ui.tests.harness.util.UITestCase; |
| 43 |
|
| 44 |
public class AbstractMultiEditorTest extends UITestCase { |
| 45 |
|
| 46 |
private static final String PROJECT_NAME = "TiledEditorProject"; |
| 47 |
|
| 48 |
private static final String TILED_EDITOR_ID = "org.eclipse.ui.tests.multieditor.ConcreteAbstractMultiEditor"; |
| 49 |
|
| 50 |
// tiled editor test files |
| 51 |
private static final String DATA_FILES_DIR = "/data/org.eclipse.newMultiEditor/"; |
| 52 |
|
| 53 |
private static final String TEST01_TXT = "test01.txt"; |
| 54 |
|
| 55 |
private static final String TEST03_ETEST = "test03.etest"; |
| 56 |
|
| 57 |
private EditorErrorListener fErrorListener; |
| 58 |
|
| 59 |
public AbstractMultiEditorTest(String tc) { |
| 60 |
super(tc); |
| 61 |
} |
| 62 |
|
| 63 |
public void testBug317102() throws Throwable { |
| 64 |
final String[] simpleFiles = { TEST01_TXT, TEST03_ETEST }; |
| 65 |
|
| 66 |
IWorkbenchWindow window = openTestWindow(); |
| 67 |
WorkbenchPage page = (WorkbenchPage) window.getActivePage(); |
| 68 |
|
| 69 |
IProject testProject = findOrCreateProject(PROJECT_NAME); |
| 70 |
|
| 71 |
MultiEditorInput input = generateEditorInput(simpleFiles, testProject); |
| 72 |
|
| 73 |
IEditorPart editor = page.openEditor(input, |
| 74 |
AbstractMultiEditorTest.TILED_EDITOR_ID); |
| 75 |
|
| 76 |
// did we get a multieditor back? |
| 77 |
assertTrue(editor instanceof ConcreteAbstractMultiEditor); |
| 78 |
ConcreteAbstractMultiEditor multiEditor = (ConcreteAbstractMultiEditor) editor; |
| 79 |
|
| 80 |
IEditorPart[] innerEditors = multiEditor.getInnerEditors(); |
| 81 |
IEditorPart activeEditor = multiEditor.getActiveEditor(); |
| 82 |
assertEquals(activeEditor, innerEditors[0]); |
| 83 |
multiEditor.activateEditor(innerEditors[1]); |
| 84 |
|
| 85 |
// activate another view |
| 86 |
IViewPart view = page.showView(IPageLayout.ID_PROBLEM_VIEW); |
| 87 |
assertEquals(view, page.getActivePart()); |
| 88 |
|
| 89 |
fakeActivation(innerEditors[0]); |
| 90 |
} |
| 91 |
|
| 92 |
private void fakeActivation(IWorkbenchPart part) { |
| 93 |
try { |
| 94 |
setupErrorListener(); |
| 95 |
Event event = new Event(); |
| 96 |
event.type = SWT.Activate; |
| 97 |
((PartSite) part.getSite()).getPane().handleEvent(event); |
| 98 |
|
| 99 |
assertTrue("Nothing should have been logged", |
| 100 |
fErrorListener.messages.isEmpty()); |
| 101 |
} finally { |
| 102 |
removeErrorListener(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set up to catch any editor initialization exceptions. |
| 108 |
* |
| 109 |
*/ |
| 110 |
private void setupErrorListener() { |
| 111 |
final ILog log = WorkbenchPlugin.getDefault().getLog(); |
| 112 |
fErrorListener = new EditorErrorListener(); |
| 113 |
log.addLogListener(fErrorListener); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Remove the editor error listener. |
| 118 |
*/ |
| 119 |
private void removeErrorListener() { |
| 120 |
final ILog log = WorkbenchPlugin.getDefault().getLog(); |
| 121 |
if (fErrorListener != null) { |
| 122 |
log.removeLogListener(fErrorListener); |
| 123 |
fErrorListener = null; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Create the multi editor input in the given project. Creates the files in |
| 129 |
* the project from template files in the classpath if they don't already |
| 130 |
* exist. |
| 131 |
* |
| 132 |
* @param simpleFiles |
| 133 |
* the array of filenames to copy over |
| 134 |
* @param testProject |
| 135 |
* the project to create the files in |
| 136 |
* @return the editor input used to open the multieditor |
| 137 |
* @throws CoreException |
| 138 |
* @throws IOException |
| 139 |
*/ |
| 140 |
private MultiEditorInput generateEditorInput(String[] simpleFiles, |
| 141 |
IProject testProject) throws CoreException, IOException { |
| 142 |
String[] ids = new String[simpleFiles.length]; |
| 143 |
IEditorInput[] inputs = new IEditorInput[simpleFiles.length]; |
| 144 |
IEditorRegistry registry = fWorkbench.getEditorRegistry(); |
| 145 |
|
| 146 |
for (int f = 0; f < simpleFiles.length; ++f) { |
| 147 |
IFile f1 = createFile(testProject, simpleFiles[f]); |
| 148 |
ids[f] = registry.getDefaultEditor(f1.getName()).getId(); |
| 149 |
inputs[f] = new FileEditorInput(f1); |
| 150 |
} |
| 151 |
|
| 152 |
MultiEditorInput input = new MultiEditorInput(ids, inputs); |
| 153 |
return input; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Create the project to work in. If it already exists, just open it. |
| 158 |
* |
| 159 |
* @param projectName |
| 160 |
* the name of the project to create |
| 161 |
* @return the newly opened project |
| 162 |
* @throws CoreException |
| 163 |
*/ |
| 164 |
private static IProject findOrCreateProject(String projectName) |
| 165 |
throws CoreException { |
| 166 |
IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 167 |
IProject testProject = workspace.getRoot().getProject(projectName); |
| 168 |
if (!testProject.exists()) { |
| 169 |
testProject.create(null); |
| 170 |
} |
| 171 |
testProject.open(null); |
| 172 |
return testProject; |
| 173 |
} |
| 174 |
|
| 175 |
private static IFile createFile(IProject testProject, String simpleFile) |
| 176 |
throws CoreException, IOException { |
| 177 |
IFile file = testProject.getFile(simpleFile); |
| 178 |
if (!file.exists()) { |
| 179 |
URL url = FileLocator.toFileURL(TestPlugin.getDefault().getBundle() |
| 180 |
.getEntry(DATA_FILES_DIR + simpleFile)); |
| 181 |
file.create(url.openStream(), true, null); |
| 182 |
} |
| 183 |
return file; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Close any editors at the beginner of a test, so the test can be clean. |
| 188 |
*/ |
| 189 |
protected void doSetUp() throws Exception { |
| 190 |
super.doSetUp(); |
| 191 |
IWorkbenchPage page = fWorkbench.getActiveWorkbenchWindow() |
| 192 |
.getActivePage(); |
| 193 |
page.closeAllEditors(false); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Listens for the standard message that indicates the MultiEditor failed |
| 198 |
* ... usually caused by incorrect framework initialization that doesn't set |
| 199 |
* the innerChildren. |
| 200 |
* |
| 201 |
* @since 3.1 |
| 202 |
* |
| 203 |
*/ |
| 204 |
public static class EditorErrorListener implements ILogListener { |
| 205 |
|
| 206 |
public ArrayList messages = new ArrayList(); |
| 207 |
|
| 208 |
public void logging(IStatus status, String plugin) { |
| 209 |
String msg = status.getMessage(); |
| 210 |
Throwable ex = status.getException(); |
| 211 |
if (ex != null) { |
| 212 |
msg += ": " + ex.getMessage(); |
| 213 |
} |
| 214 |
messages.add(msg); |
| 215 |
} |
| 216 |
} |
| 217 |
} |