| Index: editor/tools/plugins/com.google.dart.tools.ui_test/src-ui/editor/AbstractDartEditorTest.java
|
| diff --git a/editor/tools/plugins/com.google.dart.tools.ui_test/src-ui/editor/AbstractDartEditorTest.java b/editor/tools/plugins/com.google.dart.tools.ui_test/src-ui/editor/AbstractDartEditorTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4e02f993dc2b386443923712be2e1cc5c5300ac4
|
| --- /dev/null
|
| +++ b/editor/tools/plugins/com.google.dart.tools.ui_test/src-ui/editor/AbstractDartEditorTest.java
|
| @@ -0,0 +1,159 @@
|
| +/*
|
| + * Copyright (c) 2012, the Dart project authors.
|
| + *
|
| + * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
|
| + * in compliance with the License. You may obtain a copy of the License at
|
| + *
|
| + * http://www.eclipse.org/legal/epl-v10.html
|
| + *
|
| + * Unless required by applicable law or agreed to in writing, software distributed under the License
|
| + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
| + * or implied. See the License for the specific language governing permissions and limitations under
|
| + * the License.
|
| + */
|
| +package editor;
|
| +
|
| +import com.google.common.base.Joiner;
|
| +import com.google.dart.tools.core.model.CompilationUnit;
|
| +import com.google.dart.tools.core.test.util.TestProject;
|
| +import com.google.dart.tools.ui.internal.text.editor.DartEditor;
|
| +import com.google.dart.ui.test.driver.Operation;
|
| +import com.google.dart.ui.test.driver.OperationExecutor;
|
| +import com.google.dart.ui.test.util.UiContext;
|
| +
|
| +import junit.framework.TestCase;
|
| +
|
| +import org.eclipse.core.resources.IFile;
|
| +import org.eclipse.jface.action.IAction;
|
| +import org.eclipse.jface.text.source.ISourceViewer;
|
| +import org.eclipse.swt.custom.StyledText;
|
| +import org.eclipse.ui.IActionBars;
|
| +import org.eclipse.ui.PlatformUI;
|
| +import org.eclipse.ui.ide.IDE;
|
| +import org.eclipse.ui.texteditor.AbstractTextEditor;
|
| +
|
| +import static org.fest.assertions.Assertions.assertThat;
|
| +
|
| +import java.lang.reflect.Method;
|
| +import java.util.concurrent.TimeUnit;
|
| +
|
| +/**
|
| + * Base for editor tab tests.
|
| + */
|
| +public class AbstractDartEditorTest extends TestCase {
|
| + protected final static String EOL = System.getProperty("line.separator", "\n");
|
| +
|
| + /**
|
| + * Opens given {@link CompilationUnit} in the {@link DartEditor}.
|
| + */
|
| + public static DartEditor openEditor(IFile file) throws Exception {
|
| + return (DartEditor) IDE.openEditor(
|
| + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(),
|
| + file);
|
| + }
|
| +
|
| + protected static String makeSource(String... lines) {
|
| + return Joiner.on(EOL).join(lines);
|
| + }
|
| +
|
| + protected TestProject testProject;
|
| + private IFile testFile;
|
| +
|
| + protected DartEditor testEditor;
|
| + protected ISourceViewer sourceViewer;
|
| + protected StyledText textWidget;
|
| +
|
| + private final OperationExecutor operationExecutor = new OperationExecutor();
|
| +
|
| + /**
|
| + * Asserts that the unit <code>test.dart</code> has the given content.
|
| + */
|
| + public void assertTestUnitContent(String... lines) throws Exception {
|
| + assertEquals(makeSource(lines), getTestUnitContent());
|
| + }
|
| +
|
| + /**
|
| + * @return the content of the unit <code>test.dart</code>.
|
| + */
|
| + public String getTestUnitContent() throws Exception {
|
| + return testProject.getFileString("test.dart");
|
| + }
|
| +
|
| + /**
|
| + * Opens {@link DartEditor} for unit <code>test.dart</code> with given content.
|
| + */
|
| + public void openTestEditor(String... lines) throws Exception {
|
| + testFile = testProject.setFileContent("test.dart", makeSource(lines));
|
| + testEditor = openEditor(testFile);
|
| + // prepare ISourceViewer and StyledText
|
| + {
|
| + Method method = AbstractTextEditor.class.getDeclaredMethod("getSourceViewer");
|
| + method.setAccessible(true);
|
| + sourceViewer = (ISourceViewer) method.invoke(testEditor);
|
| + }
|
| + textWidget = sourceViewer.getTextWidget();
|
| + }
|
| +
|
| + /**
|
| + * Schedules the {@link Operation} execution.
|
| + */
|
| + protected final void addOperation(Operation operation) {
|
| + operationExecutor.addOperation(operation);
|
| + }
|
| +
|
| + /**
|
| + * @return the {@link IAction} with given definition ID, not <code>null</code>.
|
| + */
|
| + protected final IAction getEditorAction(String id) {
|
| + IActionBars actionBars = testEditor.getEditorSite().getActionBars();
|
| + IAction action = actionBars.getGlobalActionHandler(id);
|
| + assertNotNull("Can not find action " + id, action);
|
| + return action;
|
| + }
|
| +
|
| + /**
|
| + * Runs the scheduled {@link Operation}s, waits for the given time at most.
|
| + */
|
| + protected final void runUiOperations(long waitFor, TimeUnit unit) throws Exception {
|
| + operationExecutor.runUiOperations(waitFor, unit);
|
| + }
|
| +
|
| + /**
|
| + * Places caret into the position of the given pattern.
|
| + */
|
| + protected final void selectOffset(String pattern) throws Exception {
|
| + int position = findOffset(pattern);
|
| + testEditor.selectAndReveal(position, 0);
|
| + }
|
| +
|
| + /**
|
| + * Selects and reveals the range associated with the given pattern.
|
| + */
|
| + protected final void selectRange(String pattern) throws Exception {
|
| + int position = findOffset(pattern);
|
| + testEditor.selectAndReveal(position, pattern.length());
|
| + }
|
| +
|
| + @Override
|
| + protected void setUp() throws Exception {
|
| + super.setUp();
|
| + testProject = new TestProject();
|
| + }
|
| +
|
| + @Override
|
| + protected void tearDown() throws Exception {
|
| + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false);
|
| + UiContext.runEventLoopOnce();
|
| + testProject.dispose();
|
| + super.tearDown();
|
| + }
|
| +
|
| + /**
|
| + * @return the offset of the given pattern in the editor source. Fails in pattern was not found.
|
| + */
|
| + private int findOffset(String pattern) {
|
| + int position = sourceViewer.getDocument().get().indexOf(pattern);
|
| + assertThat(position).isNotEqualTo(-1);
|
| + return position;
|
| + }
|
| +}
|
|
|