| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012, the Dart project authors. |
| 3 * |
| 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * |
| 7 * http://www.eclipse.org/legal/epl-v10.html |
| 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 package editor; |
| 15 |
| 16 import com.google.common.base.Joiner; |
| 17 import com.google.dart.tools.core.model.CompilationUnit; |
| 18 import com.google.dart.tools.core.test.util.TestProject; |
| 19 import com.google.dart.tools.ui.internal.text.editor.DartEditor; |
| 20 import com.google.dart.ui.test.driver.Operation; |
| 21 import com.google.dart.ui.test.driver.OperationExecutor; |
| 22 import com.google.dart.ui.test.util.UiContext; |
| 23 |
| 24 import junit.framework.TestCase; |
| 25 |
| 26 import org.eclipse.core.resources.IFile; |
| 27 import org.eclipse.jface.action.IAction; |
| 28 import org.eclipse.jface.text.source.ISourceViewer; |
| 29 import org.eclipse.swt.custom.StyledText; |
| 30 import org.eclipse.ui.IActionBars; |
| 31 import org.eclipse.ui.PlatformUI; |
| 32 import org.eclipse.ui.ide.IDE; |
| 33 import org.eclipse.ui.texteditor.AbstractTextEditor; |
| 34 |
| 35 import static org.fest.assertions.Assertions.assertThat; |
| 36 |
| 37 import java.lang.reflect.Method; |
| 38 import java.util.concurrent.TimeUnit; |
| 39 |
| 40 /** |
| 41 * Base for editor tab tests. |
| 42 */ |
| 43 public class AbstractDartEditorTest extends TestCase { |
| 44 protected final static String EOL = System.getProperty("line.separator", "\n")
; |
| 45 |
| 46 /** |
| 47 * Opens given {@link CompilationUnit} in the {@link DartEditor}. |
| 48 */ |
| 49 public static DartEditor openEditor(IFile file) throws Exception { |
| 50 return (DartEditor) IDE.openEditor( |
| 51 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), |
| 52 file); |
| 53 } |
| 54 |
| 55 protected static String makeSource(String... lines) { |
| 56 return Joiner.on(EOL).join(lines); |
| 57 } |
| 58 |
| 59 protected TestProject testProject; |
| 60 private IFile testFile; |
| 61 |
| 62 protected DartEditor testEditor; |
| 63 protected ISourceViewer sourceViewer; |
| 64 protected StyledText textWidget; |
| 65 |
| 66 private final OperationExecutor operationExecutor = new OperationExecutor(); |
| 67 |
| 68 /** |
| 69 * Asserts that the unit <code>test.dart</code> has the given content. |
| 70 */ |
| 71 public void assertTestUnitContent(String... lines) throws Exception { |
| 72 assertEquals(makeSource(lines), getTestUnitContent()); |
| 73 } |
| 74 |
| 75 /** |
| 76 * @return the content of the unit <code>test.dart</code>. |
| 77 */ |
| 78 public String getTestUnitContent() throws Exception { |
| 79 return testProject.getFileString("test.dart"); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Opens {@link DartEditor} for unit <code>test.dart</code> with given content
. |
| 84 */ |
| 85 public void openTestEditor(String... lines) throws Exception { |
| 86 testFile = testProject.setFileContent("test.dart", makeSource(lines)); |
| 87 testEditor = openEditor(testFile); |
| 88 // prepare ISourceViewer and StyledText |
| 89 { |
| 90 Method method = AbstractTextEditor.class.getDeclaredMethod("getSourceViewe
r"); |
| 91 method.setAccessible(true); |
| 92 sourceViewer = (ISourceViewer) method.invoke(testEditor); |
| 93 } |
| 94 textWidget = sourceViewer.getTextWidget(); |
| 95 } |
| 96 |
| 97 /** |
| 98 * Schedules the {@link Operation} execution. |
| 99 */ |
| 100 protected final void addOperation(Operation operation) { |
| 101 operationExecutor.addOperation(operation); |
| 102 } |
| 103 |
| 104 /** |
| 105 * @return the {@link IAction} with given definition ID, not <code>null</code>
. |
| 106 */ |
| 107 protected final IAction getEditorAction(String id) { |
| 108 IActionBars actionBars = testEditor.getEditorSite().getActionBars(); |
| 109 IAction action = actionBars.getGlobalActionHandler(id); |
| 110 assertNotNull("Can not find action " + id, action); |
| 111 return action; |
| 112 } |
| 113 |
| 114 /** |
| 115 * Runs the scheduled {@link Operation}s, waits for the given time at most. |
| 116 */ |
| 117 protected final void runUiOperations(long waitFor, TimeUnit unit) throws Excep
tion { |
| 118 operationExecutor.runUiOperations(waitFor, unit); |
| 119 } |
| 120 |
| 121 /** |
| 122 * Places caret into the position of the given pattern. |
| 123 */ |
| 124 protected final void selectOffset(String pattern) throws Exception { |
| 125 int position = findOffset(pattern); |
| 126 testEditor.selectAndReveal(position, 0); |
| 127 } |
| 128 |
| 129 /** |
| 130 * Selects and reveals the range associated with the given pattern. |
| 131 */ |
| 132 protected final void selectRange(String pattern) throws Exception { |
| 133 int position = findOffset(pattern); |
| 134 testEditor.selectAndReveal(position, pattern.length()); |
| 135 } |
| 136 |
| 137 @Override |
| 138 protected void setUp() throws Exception { |
| 139 super.setUp(); |
| 140 testProject = new TestProject(); |
| 141 } |
| 142 |
| 143 @Override |
| 144 protected void tearDown() throws Exception { |
| 145 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAl
lEditors(false); |
| 146 UiContext.runEventLoopOnce(); |
| 147 testProject.dispose(); |
| 148 super.tearDown(); |
| 149 } |
| 150 |
| 151 /** |
| 152 * @return the offset of the given pattern in the editor source. Fails in patt
ern was not found. |
| 153 */ |
| 154 private int findOffset(String pattern) { |
| 155 int position = sourceViewer.getDocument().get().indexOf(pattern); |
| 156 assertThat(position).isNotEqualTo(-1); |
| 157 return position; |
| 158 } |
| 159 } |
| OLD | NEW |