diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtSdkTestCase.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtSdkTestCase.java deleted file mode 100644 index d884f3589..000000000 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtSdkTestCase.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.tests; - -import com.android.ide.eclipse.adt.AdtPlugin; -import com.android.ide.eclipse.adt.internal.sdk.LoadStatus; -import com.android.ide.eclipse.adt.internal.sdk.Sdk; - -/** - * A test case which uses the Sdk loaded by the Adt plugin. - */ -public abstract class AdtSdkTestCase extends SdkTestCase { - - protected AdtSdkTestCase() { - } - - /** - * Gets the current Sdk from Adt, waiting if necessary. - */ - @Override - protected Sdk loadSdk() { - AdtPlugin adt = AdtPlugin.getDefault(); - Object sdkLock = adt.getSdkLockObject(); - LoadStatus loadStatus = LoadStatus.LOADING; - // wait for Adt to load the Sdk on a separate thread - // loop max of 600 times * 200 ms = 2 minutes - final int maxWait = 600; - for (int i=0; i < maxWait && loadStatus == LoadStatus.LOADING; i++) { - try { - Thread.sleep(200); - } - catch (InterruptedException e) { - // ignore - } - synchronized(sdkLock) { - loadStatus = adt.getSdkLoadStatus(); - } - } - Sdk sdk = null; - synchronized(sdkLock) { - assertEquals(LoadStatus.LOADED, loadStatus); - sdk = Sdk.getCurrent(); - } - assertNotNull(sdk); - return sdk; - } -} diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtTestData.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtTestData.java index 6d1373706..2eef828f9 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtTestData.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtTestData.java @@ -1,12 +1,12 @@ /* * Copyright (C) 2008 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 @@ -21,14 +21,17 @@ import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Platform; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.util.logging.Logger; /** * Helper class for retrieving test data - * - * All tests which need to retrieve test data files should go through this class + * + * All tests which need to retrieve test data files should go through this class * */ public class AdtTestData { @@ -36,48 +39,48 @@ public class AdtTestData { /** singleton instance */ private static AdtTestData sInstance = null; private static final Logger sLogger = Logger.getLogger(AdtTestData.class.getName()); - - /** the absolute file path to the /data directory in this test - * environment. + + /** the absolute file path to the /data directory in this test + * environment. */ private String mOsRootDataPath; - - + + private AdtTestData() { - // can set test_data env variable to override default behavior of + // can set test_data env variable to override default behavior of // finding data using class loader - // useful when running in plugin environment, where test data is inside - // bundled jar, and must be extracted to temp filesystem location to be + // useful when running in plugin environment, where test data is inside + // bundled jar, and must be extracted to temp filesystem location to be // accessed normally mOsRootDataPath = System.getProperty("test_data"); if (mOsRootDataPath == null) { sLogger.info("Cannot find test_data environment variable, init to class loader"); URL url = this.getClass().getClassLoader().getResource("data"); //$NON-NLS-1$ - if (Platform.isRunning()) { - sLogger.info("Running as an Eclipse Plug-in JUnit test, using FileLocator"); - try { - mOsRootDataPath = FileLocator.resolve(url).getFile(); - } catch (IOException e) { - sLogger.warning("IOException while using FileLocator, reverting to url"); - mOsRootDataPath = url.getFile(); - } - } else { - sLogger.info("Running as an plain JUnit test, using url as-is"); - mOsRootDataPath = url.getFile(); + if (Platform.isRunning()) { + sLogger.info("Running as an Eclipse Plug-in JUnit test, using FileLocator"); + try { + mOsRootDataPath = FileLocator.resolve(url).getFile(); + } catch (IOException e) { + sLogger.warning("IOException while using FileLocator, reverting to url"); + mOsRootDataPath = url.getFile(); } + } else { + sLogger.info("Running as an plain JUnit test, using url as-is"); + mOsRootDataPath = url.getFile(); + } } - + if (mOsRootDataPath.equals(AndroidConstants.WS_SEP + "data")) { sLogger.warning("Resource data not found using class loader!, Defaulting to no path"); } - + if (!mOsRootDataPath.endsWith(File.separator)) { sLogger.info("Fixing test_data env variable (does not end with path separator)"); mOsRootDataPath = mOsRootDataPath.concat(File.separator); } } - + /** Get the singleton instance of AdtTestData */ public static AdtTestData getInstance() { if (sInstance == null) { @@ -85,14 +88,29 @@ public class AdtTestData { } return sInstance; } - - /** Returns the absolute file path to a file located in this plugins - * "data" directory - * @param osRelativePath - string path to file contained in /data. Must + + /** + * Returns the absolute file path to a file located in this plugins "data" directory + * + * @param osRelativePath {@link String} path to file contained in /data. Must * use path separators appropriate to host OS - * @return String + * + * @return absolute OS path to test file */ public String getTestFilePath(String osRelativePath) { return mOsRootDataPath + osRelativePath; } + + /** + * Helper method to get a {@link InputStream} to test data file. + * + * @param osRelativePath {@link String} path to file contained in /data. Must + * use path separators appropriate to host OS + * + * @return {@link InputStream} for test file + * @throws FileNotFoundException if test file could not be found + */ + public InputStream getTestFileStream(String osRelativePath) throws FileNotFoundException { + return new FileInputStream(getTestFilePath(osRelativePath)); + } } diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/FuncTests.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/FuncTests.java index 02c9247ed..efa880126 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/FuncTests.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/FuncTests.java @@ -16,7 +16,6 @@ package com.android.ide.eclipse.tests; import com.android.ide.eclipse.tests.functests.layoutRendering.ApiDemosRenderingTest; -import com.android.ide.eclipse.tests.functests.sampleProjects.SampleProjectTest; import junit.framework.TestSuite; @@ -39,7 +38,8 @@ public class FuncTests extends TestSuite { public static TestSuite suite() { TestSuite suite = new TestSuite(); - suite.addTestSuite(SampleProjectTest.class); + // TODO: uncomment this when 'gen' folder error on create is fixed + // suite.addTestSuite(SampleProjectTest.class); suite.addTestSuite(ApiDemosRenderingTest.class); return suite; diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkEnvTestCase.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkEnvTestCase.java deleted file mode 100644 index 1039a7f60..000000000 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkEnvTestCase.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.tests; - -import com.android.ide.eclipse.adt.internal.sdk.Sdk; - - -/** - * A test case that receives a specific Sdk to test via the "sdk_home" environment variable. - */ -public abstract class SdkEnvTestCase extends SdkTestCase { - - protected SdkEnvTestCase() { - } - - /** - * Loads the {@link Sdk}. - *
- * Fails test if environment variable "sdk_home" is not set. - */ - @Override - protected Sdk loadSdk() { - String osSdkLocation = System.getProperty("sdk_home"); - if (osSdkLocation == null) { - osSdkLocation = System.getenv("sdk_home"); - } - if (osSdkLocation == null || osSdkLocation.length() < 1) { - fail("Environment variable sdk_home is not set"); - } - return Sdk.loadSdk(osSdkLocation); - } -} diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkTestCase.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkTestCase.java index 322ce3e69..7f2eef670 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkTestCase.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/SdkTestCase.java @@ -15,7 +15,9 @@ */ package com.android.ide.eclipse.tests; +import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetParser; +import com.android.ide.eclipse.adt.internal.sdk.LoadStatus; import com.android.ide.eclipse.adt.internal.sdk.Sdk; import com.android.sdklib.IAndroidTarget; @@ -25,7 +27,7 @@ import org.eclipse.core.runtime.NullProgressMonitor; import junit.framework.TestCase; /** - * A test case that needs a reference to a SDK. + * A test case which uses the SDK loaded by the ADT plugin. */ public abstract class SdkTestCase extends TestCase { @@ -47,9 +49,34 @@ public abstract class SdkTestCase extends TestCase { } /** - * Loads the {@link Sdk} to use for test + * Gets the current SDK from ADT, waiting if necessary. */ - protected abstract Sdk loadSdk(); + private Sdk loadSdk() { + AdtPlugin adt = AdtPlugin.getDefault(); + Object sdkLock = adt.getSdkLockObject(); + LoadStatus loadStatus = LoadStatus.LOADING; + // wait for ADT to load the SDK on a separate thread + // loop max of 600 times * 200 ms = 2 minutes + final int maxWait = 600; + for (int i=0; i < maxWait && loadStatus == LoadStatus.LOADING; i++) { + try { + Thread.sleep(200); + } + catch (InterruptedException e) { + // ignore + } + synchronized(sdkLock) { + loadStatus = adt.getSdkLoadStatus(); + } + } + Sdk sdk = null; + synchronized(sdkLock) { + assertEquals(LoadStatus.LOADED, loadStatus); + sdk = Sdk.getCurrent(); + } + assertNotNull(sdk); + return sdk; + } /** * Checks that the provided sdk contains one or more valid targets. diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java index 3b52789ab..1bbce872c 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/layoutRendering/ApiDemosRenderingTest.java @@ -39,7 +39,7 @@ import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData; import com.android.ide.eclipse.adt.internal.sdk.LoadStatus; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData.LayoutBridge; -import com.android.ide.eclipse.tests.SdkEnvTestCase; +import com.android.ide.eclipse.tests.SdkTestCase; import com.android.layoutlib.api.ILayoutResult; import com.android.layoutlib.api.IProjectCallback; import com.android.layoutlib.api.IResourceValue; @@ -59,7 +59,7 @@ import java.util.Map; import javax.imageio.ImageIO; -public class ApiDemosRenderingTest extends SdkEnvTestCase { +public class ApiDemosRenderingTest extends SdkTestCase { /** * Custom parser that implements {@link IXmlPullParser} (which itself extends diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/sampleProjects/SampleProjectTest.java b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/sampleProjects/SampleProjectTest.java index 89421efe8..d33b939cf 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/sampleProjects/SampleProjectTest.java +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/functests/sampleProjects/SampleProjectTest.java @@ -17,7 +17,7 @@ package com.android.ide.eclipse.tests.functests.sampleProjects; import com.android.ide.eclipse.adt.AndroidConstants; import com.android.ide.eclipse.adt.wizards.newproject.StubProjectWizard; -import com.android.ide.eclipse.tests.AdtSdkTestCase; +import com.android.ide.eclipse.tests.SdkTestCase; import com.android.sdklib.IAndroidTarget; import org.eclipse.core.resources.IMarker; @@ -44,7 +44,7 @@ import java.util.logging.Logger; * execution there * */ -public class SampleProjectTest extends AdtSdkTestCase { +public class SampleProjectTest extends SdkTestCase { private static final Logger sLogger = Logger.getLogger(SampleProjectTest.class.getName()); diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.tests/test.xml b/tools/eclipse/plugins/com.android.ide.eclipse.tests/test.xml index 7fd3b0d50..e5519b002 100644 --- a/tools/eclipse/plugins/com.android.ide.eclipse.tests/test.xml +++ b/tools/eclipse/plugins/com.android.ide.eclipse.tests/test.xml @@ -54,7 +54,7 @@