Merge change I2c1b68ce into eclair
* changes:
ADT: Refactor GLE1 and GLE2 classes in their own package.
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/TestGroovy.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/TestGroovy.java
new file mode 100755
index 0000000..8a9744e
--- /dev/null
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/TestGroovy.java
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Eclipse Public License, Version 1.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/org/documents/epl-v10.php
+ *
+ * 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 com.android.ide.eclipse.adt.internal.sdk;
+
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.eclipse.swt.graphics.Rectangle;
+
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import groovy.lang.GroovyClassLoader;
+import junit.framework.TestCase;
+
+/**
+ * Tests we can invoke a groovy script that implements a given interface.
+ */
+public class TestGroovy extends TestCase {
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /**
+ * This is the interface that we want our Groovy script to implement.
+ */
+ public static interface AdtTestInterface {
+
+ /** Method that returns a boolean. */
+ public boolean acceptDrag(String xmlName);
+ /** Method that returns an SWT Rectangle. */
+ public Rectangle acceptDrop(String xmlName);
+
+ /** Method that returns some Groovy object (opaque for us). */
+ public Object returnGroovyObject();
+ /** Method that accepts the Groovy object back. */
+ public boolean testGroovyObject(Object o);
+ }
+
+ /**
+ * Loads a groovy script that defines one class that implements the {@link AdtTestInterface}.
+ *
+ * @param filename The name of the script to load, that must be located in this Java package.
+ * @return A non-null instance of the groovy class on success.
+ * @throws CompilationFailedException if the groovy script failed to compile (e.g. syntax
+ * errors or it doesn't completely implement the interface.)
+ * @throws ClassCastException if the groovy script does not implement our interface.
+ * @throws FileNotFoundException if the groovy script does not exist.
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public AdtTestInterface loadScript(String filename)
+ throws CompilationFailedException, ClassCastException,
+ InstantiationException, IllegalAccessException,
+ FileNotFoundException {
+ // Get the input source from the sources or the JAR.
+ InputStream myGroovyStream = getClass().getResourceAsStream(filename);
+
+ // The stream is null if the file does not exists.
+ if (myGroovyStream == null) {
+ throw new FileNotFoundException(filename);
+ }
+
+ // Create a groovy class from it. Can fail to compile.
+ ClassLoader cl = getClass().getClassLoader();
+ GroovyClassLoader gcl = new GroovyClassLoader(cl);
+ Class gClass = gcl.parseClass(myGroovyStream, filename);
+
+ // Get an instance. This might throw ClassCastException.
+ return (AdtTestInterface) gClass.newInstance();
+ }
+
+ /**
+ * Tests that a {@link FileNotFoundException} is thrown if when trying
+ * to load a missing script.
+ */
+ public void testMissingScript() throws Exception {
+ try {
+ AdtTestInterface instance = loadScript("not_an_existing_script.groovy");
+ } catch (FileNotFoundException e) {
+ assertEquals("not_an_existing_script.groovy", e.getMessage());
+ return; // succeed
+ }
+
+ fail("Script failed to throw an exception on missing groovy file.");
+ }
+
+ /**
+ * Tests that a {@link ClassCastException} is thrown if the script does not
+ * implement our interface.
+ */
+ public void testInvalidInterface() throws Exception {
+ try {
+ AdtTestInterface instance = loadScript("invalid_interface.groovy");
+ } catch(ClassCastException e) {
+ // This has to fail because the script does not implement our interface
+ // The message explains why but we're not harcoding the message in the test.
+ assertNotNull(e.getMessage());
+ return; // succeed
+ }
+
+ fail("Script failed to throw a ClassCastException.");
+ }
+
+ /**
+ * Tests that a {@link CompilationFailedException} is thrown if the script
+ * is not valid.
+ */
+ public void testCompilationError() throws Exception {
+ try {
+ AdtTestInterface instance = loadScript("compile_error.groovy");
+ } catch (CompilationFailedException e) {
+ // This script does not compile, the message explains why but we're not
+ // harcoding the message in the test.
+ assertNotNull(e.getMessage());
+ return; // succeed
+ }
+
+ fail("Script failed to throw a compilation error.");
+ }
+
+ /**
+ * Tests a valid script scenario with only some basic methods
+ */
+ public void testSimpleMethods() throws Exception {
+ AdtTestInterface instance = loadScript("simple_test.groovy");
+
+ assertTrue(instance.acceptDrag("LinearLayout"));
+ assertFalse(instance.acceptDrag("RelativeLayout"));
+ assertNull(instance.acceptDrop("none"));
+
+ Rectangle r = instance.acceptDrop("LinearLayout");
+ assertNotNull(r);
+ assertEquals(new Rectangle(1, 2, 3, 4), r);
+ }
+
+ /**
+ * Tests a valid script scenario with some methods providing some callbacks.
+ */
+ public void testCallback() throws Exception {
+ AdtTestInterface instance = loadScript("simple_test.groovy");
+
+ // The groovy method returns an object. We should treat it as an opaque object
+ // which purpose is just to give it back to groovy later.
+ Object o = instance.returnGroovyObject();
+ assertNotNull(o);
+
+ // Let the groovy script get back the object and play with it
+ assertTrue(instance.testGroovyObject(o));
+ }
+
+
+}
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/compile_error.groovy b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/compile_error.groovy
new file mode 100755
index 0000000..b78bc3f
--- /dev/null
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/compile_error.groovy
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Eclipse Public License, Version 1.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/org/documents/epl-v10.php
+ *
+ * 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.
+ */
+
+import com.android.ide.eclipse.adt.internal.sdk.TestGroovy.AdtTestInterface;
+
+import org.eclipse.swt.graphics.Rectangle;
+
+class CompileError implements AdtTestInterface {
+
+ public boolean acceptDrag(String xmlName) {
+ // missing return value (implicit in Groovy so not really an error)
+ }
+
+ // invalid syntax
+ public Rectangle accept(])
+
+ // missing method: public Rectangle acceptDrop(String xmlName)
+}
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/invalid_interface.groovy b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/invalid_interface.groovy
new file mode 100755
index 0000000..e6ba873
--- /dev/null
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/invalid_interface.groovy
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Eclipse Public License, Version 1.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/org/documents/epl-v10.php
+ *
+ * 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.
+ */
+
+class InvalidInterface implements Runnable {
+
+ public void run() {
+ // pass
+ }
+}
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/simple_test.groovy b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/simple_test.groovy
new file mode 100755
index 0000000..175c229
--- /dev/null
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/sdk/simple_test.groovy
@@ -0,0 +1,84 @@
+import org.eclipse.swt.graphics.Rectangle;
+
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Eclipse Public License, Version 1.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/org/documents/epl-v10.php
+ *
+ * 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.
+ */
+
+import com.android.ide.eclipse.adt.internal.sdk.TestGroovy.AdtTestInterface;
+
+import org.eclipse.swt.graphics.Rectangle;
+
+class AdtGroovyTest implements AdtTestInterface {
+
+ /** Returns a true if the argument is LinearLayout. */
+ public boolean acceptDrag(String xmlName) {
+ if (xmlName == "LinearLayout") {
+ return true;
+ }
+
+ return false;
+ }
+
+ /** Returns a new SWT Rectangle if LinearLayout or null. */
+ public Rectangle acceptDrop(String xmlName) {
+ if (xmlName == "LinearLayout") {
+ return new Rectangle(1, 2, 3, 4);
+ }
+
+ return null;
+ }
+
+ /** Always throw an assert. */
+ public void testAssert() {
+ assert true == false
+ }
+
+ /**
+ * Returns some Groovy object, in this case a map with some info and a closure.
+ * The caller will return this object to testGroovyObject.
+ */
+ public Object returnGroovyObject() {
+
+ return [
+ TheInstance: this,
+ SomeRect: new Rectangle(1, 2, 3, 4),
+ SomeClosure: { int x, int y -> x + y }
+ ]
+ }
+
+ /** Returns true if the object is the same as the one created by returnGroovyObject. */
+ public boolean testGroovyObject(Object o) {
+ // Input argument should be a map
+ assert o.getClass() == LinkedHashMap
+
+ // We expected these keys
+ assert o.containsKey("TheInstance")
+ assert o.containsKey("SomeRect")
+ assert o.containsKey("SomeClosure")
+
+ // Check the values
+ assert o.TheInstance.is(this) // check identity, not equality
+ assert o.SomeRect == new Rectangle(1, 2, 3, 4)
+ assert o.SomeClosure != null
+
+ // Execute the closure
+ assert o.SomeClosure(42, 3) == 45
+
+ // Everything succeeded
+ return true
+ }
+
+}
+