| Index: editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/ast/DartElementLocatorTest.java
|
| diff --git a/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/ast/DartElementLocatorTest.java b/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/ast/DartElementLocatorTest.java
|
| index 2b707590b1e113492652b99961095e7ac6ba2f95..dd47cb1a926b260e609a92048713b5fb0e8627c3 100644
|
| --- a/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/ast/DartElementLocatorTest.java
|
| +++ b/editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/ast/DartElementLocatorTest.java
|
| @@ -13,6 +13,10 @@
|
| */
|
| package com.google.dart.tools.core.utilities.ast;
|
|
|
| +import static com.google.dart.tools.core.test.util.MoneyProjectUtilities.getMoneyCompilationUnit;
|
| +
|
| +import com.google.common.base.Joiner;
|
| +import com.google.common.collect.Lists;
|
| import com.google.dart.compiler.DartCompilationError;
|
| import com.google.dart.compiler.ast.DartUnit;
|
| import com.google.dart.tools.core.model.CompilationUnit;
|
| @@ -20,13 +24,180 @@ import com.google.dart.tools.core.model.DartElement;
|
| import com.google.dart.tools.core.model.DartModelException;
|
| import com.google.dart.tools.core.utilities.compiler.DartCompilerUtilities;
|
|
|
| -import static com.google.dart.tools.core.test.util.MoneyProjectUtilities.getMoneyCompilationUnit;
|
| -
|
| import junit.framework.TestCase;
|
|
|
| +import org.eclipse.swt.graphics.Point;
|
| +import org.eclipse.swt.graphics.Rectangle;
|
| +import org.eclipse.swt.widgets.Display;
|
| +import org.eclipse.swt.widgets.Shell;
|
| +import org.eclipse.ui.IViewPart;
|
| +import org.eclipse.ui.IViewReference;
|
| +import org.eclipse.ui.IWorkbenchPage;
|
| +import org.eclipse.ui.IWorkbenchWindow;
|
| +import org.eclipse.ui.PlatformUI;
|
| +
|
| import java.util.ArrayList;
|
| +import java.util.List;
|
|
|
| public class DartElementLocatorTest extends TestCase {
|
| + private static boolean m_firstDesignerTest = true;
|
| +
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + //
|
| + // setUp/tearDown
|
| + //
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + @Override
|
| + protected void setUp() throws Exception {
|
| + super.setUp();
|
| + configureFirstTime();
|
| + }
|
| +
|
| + private void configureFirstTime() {
|
| + configureEclipseWindowLocation();
|
| + if (!m_firstDesignerTest) {
|
| + return;
|
| + }
|
| + m_firstDesignerTest = false;
|
| + // hide views
|
| + closeAllViews();
|
| + }
|
| +
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + //
|
| + // Tests
|
| + //
|
| + ////////////////////////////////////////////////////////////////////////////
|
| +
|
| + public void test_VariableElement_parameter_inClassMethod() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "print(x) {}",
|
| + "class A {",
|
| + " foo(a, bb, ccc) {",
|
| + " print(bb);",
|
| + " }",
|
| + "}"}, "bb);", "bb, ", 2);
|
| + }
|
| +
|
| + public void test_VariableElement_parameter_inTopMethod() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "print(x) {}",
|
| + "foo(a, bb, ccc) {",
|
| + " print(bb);",
|
| + "}"}, "bb);", "bb, ", 2);
|
| + }
|
| +
|
| + public void test_VariableElement_localVariable() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "print(x) {}",
|
| + "foo() {",
|
| + " var aaa = 1;",
|
| + " print(aaa);",
|
| + "}"}, "aaa);", "aaa = 1", 3);
|
| + }
|
| +
|
| + public void test_FieldElement_topLevel() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "print(x) {}",
|
| + "var aaa = 1;",
|
| + "foo() {",
|
| + " print(aaa);",
|
| + "}"}, "aaa);", "aaa = 1", 3);
|
| + }
|
| +
|
| + public void test_FieldElement_classMember() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "print(x) {}",
|
| + "class A {",
|
| + " var bbb = 1;",
|
| + "}",
|
| + "foo() {",
|
| + " A a = new A();",
|
| + " print(a.bbb);",
|
| + "}"}, "bbb);", "bbb = 1", 3);
|
| + }
|
| +
|
| + public void test_type_typeName() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "class A {}",
|
| + "foo() {",
|
| + " A a = null;",
|
| + "}"}, "A a =", "A {}", 1);
|
| + }
|
| +
|
| + public void test_type_newExpression() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "class A {",
|
| + " A() {}",
|
| + "}",
|
| + "foo() {",
|
| + " A a = new A();",
|
| + "}"}, "A();", "A() {}", 1);
|
| + }
|
| +
|
| + public void test_methodInvocation() throws Exception {
|
| + testElementLocator(new String[]{
|
| + "// filler filler filler filler filler filler filler filler filler filler filler",
|
| + "foo() {}",
|
| + "bar() {",
|
| + " foo();",
|
| + "}"}, "foo();", "foo() {}", 3);
|
| + }
|
| +
|
| + private void testElementLocator(String[] sourceLines,
|
| + String posMarker,
|
| + String expectedMarker,
|
| + int expectedLen) throws Exception {
|
| + TestProject testProject = new TestProject("Test");
|
| + try {
|
| + CompilationUnit unit =
|
| + testProject.setUnitContent("Test.dart", Joiner.on("\n").join(sourceLines));
|
| + assertLocation(unit, posMarker, expectedMarker, expectedLen);
|
| + } finally {
|
| + testProject.dispose();
|
| + }
|
| + }
|
| +
|
| + private static void assertLocation(CompilationUnit unit,
|
| + String posMarker,
|
| + String expectedMarker,
|
| + int expectedLen) throws Exception {
|
| + String source = unit.getSource();
|
| + // prepare DartUnit
|
| + DartUnit dartUnit;
|
| + {
|
| + List<DartCompilationError> errors = Lists.newArrayList();
|
| + dartUnit = DartCompilerUtilities.resolveUnit(unit, errors);
|
| + // we don't want errors
|
| + if (!errors.isEmpty()) {
|
| + fail("Parse/resolve errors: " + errors);
|
| + }
|
| + }
|
| + // prepare position to search on
|
| + int pos = source.indexOf(posMarker);
|
| + assertTrue("Unable to find position marker '" + posMarker + "'", pos > 0);
|
| + // use Locator
|
| + DartElementLocator locator = new DartElementLocator(unit, pos, true);
|
| + DartElement result = locator.searchWithin(dartUnit);
|
| + // verify
|
| + if (expectedMarker != null) {
|
| + assertNotNull(result);
|
| + int expectedPos = source.indexOf(expectedMarker);
|
| + assertTrue("Unable to find expected marker '" + expectedMarker + "'", expectedPos > 0);
|
| + assertEquals(expectedPos, locator.getCandidateRegion().getOffset());
|
| + assertEquals(expectedLen, locator.getCandidateRegion().getLength());
|
| + } else {
|
| + assertNull(result);
|
| + }
|
| + }
|
| +
|
| public void test_DartElementLocator_searchWithin_declaration_with() throws Exception {
|
| CompilationUnit unit = getMoneyCompilationUnit("simple_money.dart");
|
| assertLocation(true, unit, 418, true);
|
| @@ -42,7 +213,9 @@ public class DartElementLocatorTest extends TestCase {
|
| assertLocation(true, unit, 394, false);
|
| }
|
|
|
| - private void assertLocation(boolean expectElement, CompilationUnit unit, int offset,
|
| + private void assertLocation(boolean expectElement,
|
| + CompilationUnit unit,
|
| + int offset,
|
| boolean includeDeclarations) throws DartModelException {
|
| DartUnit ast = DartCompilerUtilities.resolveUnit(unit, new ArrayList<DartCompilationError>());
|
| DartElementLocator locator = new DartElementLocator(unit, offset, includeDeclarations);
|
| @@ -53,4 +226,78 @@ public class DartElementLocatorTest extends TestCase {
|
| assertNull(result);
|
| }
|
| }
|
| +
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + //
|
| + // UI utils
|
| + //
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + /**
|
| + * Waits given number of milliseconds and runs events loop every 1 millisecond. At least one
|
| + * events loop will be executed.
|
| + */
|
| + protected static void waitEventLoop(int time) {
|
| + waitEventLoop(time, 0);
|
| + }
|
| +
|
| + /**
|
| + * Waits given number of milliseconds and runs events loop every <code>sleepMillis</code>
|
| + * milliseconds. At least one events loop will be executed.
|
| + */
|
| + public static void waitEventLoop(int time, long sleepMillis) {
|
| + long start = System.currentTimeMillis();
|
| + do {
|
| + try {
|
| + Thread.sleep(sleepMillis);
|
| + } catch (Throwable e) {
|
| + }
|
| + while (Display.getCurrent().readAndDispatch()) {
|
| + // do nothing
|
| + }
|
| + } while (System.currentTimeMillis() - start < time);
|
| + }
|
| +
|
| + ////////////////////////////////////////////////////////////////////////////
|
| + //
|
| + // Workbench
|
| + //
|
| + ////////////////////////////////////////////////////////////////////////////
|
| +
|
| + /**
|
| + * Ensures that Eclipse main window is in top-right corner of screen.
|
| + */
|
| + private void configureEclipseWindowLocation() {
|
| + int x = 450;
|
| + int y = 0;
|
| + Shell shell = getActiveWorkbenchWindow().getShell();
|
| + Point shellLocation = shell.getLocation();
|
| + if (shellLocation.x != x || shellLocation.y != y) {
|
| + Rectangle clientArea = Display.getDefault().getClientArea();
|
| + shell.setBounds(x, y, clientArea.width - x, clientArea.height - 350);
|
| + waitEventLoop(100, 10);
|
| + }
|
| + }
|
| +
|
| + private static IWorkbenchWindow getActiveWorkbenchWindow() {
|
| + return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
| + }
|
| +
|
| + /**
|
| + * Closes all {@link IViewPart}s.
|
| + */
|
| + public static void closeAllViews() {
|
| + // run event loop to allow any async's be executed
|
| + while (Display.getCurrent().readAndDispatch()) {
|
| + // do nothing
|
| + }
|
| + // do close
|
| + IWorkbenchPage activePage = getActiveWorkbenchWindow().getActivePage();
|
| + IViewReference[] viewReferences = activePage.getViewReferences();
|
| + if (viewReferences.length != 0) {
|
| + for (IViewReference viewReference : viewReferences) {
|
| + activePage.hideView(viewReference);
|
| + }
|
| + waitEventLoop(100);
|
| + }
|
| + }
|
| }
|
|
|