Merge change I3e6a5048 into eclair

* changes:
  Fix ADT tests failures:  - Separate functional tests into ones which load their own Sdk, vs ones which need to use the same one as Adt.  - Make all base test classes abstract with protected constructors.
This commit is contained in:
Android (Google) Code Review
2009-10-09 16:45:44 -04:00
6 changed files with 199 additions and 94 deletions

View File

@@ -0,0 +1,59 @@
/*
* 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.
*/
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;
}
}

View File

@@ -1,84 +0,0 @@
/*
* 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
* 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.AndroidTargetParser;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.IAndroidTarget;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import junit.framework.TestCase;
/**
* Generic superclass for Eclipse Android functional test cases, that provides common facilities.
*/
public class FuncTestCase extends TestCase {
private String mOsSdkLocation;
protected Sdk mSdk;
/**
* Initializes test SDK
* <p/>
* Fails test if environment variable "sdk_home" is not set.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
mOsSdkLocation = System.getProperty("sdk_home");
if (mOsSdkLocation == null) {
mOsSdkLocation = System.getenv("sdk_home");
}
if (mOsSdkLocation == null || mOsSdkLocation.length() < 1) {
fail("Environment variable sdk_home is not set");
}
loadSdk(mOsSdkLocation);
}
/**
* Returns the absolute file system path of the Android SDK location to use for this test.
*/
protected String getOsSdkLocation() {
return mOsSdkLocation;
}
/**
* Returns the {@link Sdk} under test.
*/
protected Sdk getSdk() {
return mSdk;
}
/**
* Loads the {@link Sdk}.
*/
private void loadSdk(String sdkLocation) {
mSdk = Sdk.loadSdk(sdkLocation);
int n = mSdk.getTargets().length;
if (n > 0) {
for (IAndroidTarget target : mSdk.getTargets()) {
IStatus status = new AndroidTargetParser(target).run(new NullProgressMonitor());
if (status.getCode() != IStatus.OK) {
fail("Failed to parse targets data");
}
}
}
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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}.
* <p/>
* 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);
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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
* 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.AndroidTargetParser;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.sdklib.IAndroidTarget;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import junit.framework.TestCase;
/**
* A test case that needs a reference to a SDK.
*/
public abstract class SdkTestCase extends TestCase {
private Sdk mSdk;
protected SdkTestCase() {
}
/**
* Retrieve the {@link Sdk} under test.
*/
protected Sdk getSdk() {
if (mSdk == null) {
mSdk = loadSdk();
assertNotNull(mSdk);
validateSdk(mSdk);
}
return mSdk;
}
/**
* Loads the {@link Sdk} to use for test
*/
protected abstract Sdk loadSdk();
/**
* Checks that the provided sdk contains one or more valid targets.
* @param sdk the {@link Sdk} to validate.
*/
private void validateSdk(Sdk sdk) {
assertTrue("sdk has no targets", sdk.getTargets().length > 0);
for (IAndroidTarget target : sdk.getTargets()) {
IStatus status = new AndroidTargetParser(target).run(new NullProgressMonitor());
if (status.getCode() != IStatus.OK) {
fail("Failed to parse targets data");
}
}
}
}

View File

@@ -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.AndroidTargetData;
import com.android.ide.eclipse.adt.internal.sdk.LoadStatus; import com.android.ide.eclipse.adt.internal.sdk.LoadStatus;
import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData.LayoutBridge; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData.LayoutBridge;
import com.android.ide.eclipse.tests.FuncTestCase; import com.android.ide.eclipse.tests.SdkEnvTestCase;
import com.android.layoutlib.api.ILayoutResult; import com.android.layoutlib.api.ILayoutResult;
import com.android.layoutlib.api.IProjectCallback; import com.android.layoutlib.api.IProjectCallback;
import com.android.layoutlib.api.IResourceValue; import com.android.layoutlib.api.IResourceValue;
@@ -59,7 +59,7 @@ import java.util.Map;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public class ApiDemosRenderingTest extends FuncTestCase { public class ApiDemosRenderingTest extends SdkEnvTestCase {
/** /**
* Custom parser that implements {@link IXmlPullParser} (which itself extends * Custom parser that implements {@link IXmlPullParser} (which itself extends
@@ -121,7 +121,7 @@ public class ApiDemosRenderingTest extends FuncTestCase {
} }
private void findApiDemos() throws IOException, XmlPullParserException { private void findApiDemos() throws IOException, XmlPullParserException {
IAndroidTarget[] targets = mSdk.getTargets(); IAndroidTarget[] targets = getSdk().getTargets();
for (IAndroidTarget target : targets) { for (IAndroidTarget target : targets) {
String path = target.getPath(IAndroidTarget.SAMPLES); String path = target.getPath(IAndroidTarget.SAMPLES);
@@ -141,7 +141,7 @@ public class ApiDemosRenderingTest extends FuncTestCase {
} }
private void testSample(IAndroidTarget target, File sampleProject) throws IOException, XmlPullParserException { private void testSample(IAndroidTarget target, File sampleProject) throws IOException, XmlPullParserException {
AndroidTargetData data = mSdk.getTargetData(target); AndroidTargetData data = getSdk().getTargetData(target);
if (data == null) { if (data == null) {
fail("No AndroidData!"); fail("No AndroidData!");
} }

View File

@@ -16,12 +16,13 @@
package com.android.ide.eclipse.tests.functests.sampleProjects; package com.android.ide.eclipse.tests.functests.sampleProjects;
import com.android.ide.eclipse.adt.AndroidConstants; import com.android.ide.eclipse.adt.AndroidConstants;
import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
import com.android.ide.eclipse.adt.wizards.newproject.StubProjectWizard; import com.android.ide.eclipse.adt.wizards.newproject.StubProjectWizard;
import com.android.ide.eclipse.tests.FuncTestCase; import com.android.ide.eclipse.tests.AdtSdkTestCase;
import com.android.sdklib.IAndroidTarget; import com.android.sdklib.IAndroidTarget;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent; import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener; import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.IResourceDelta;
@@ -43,7 +44,7 @@ import java.util.logging.Logger;
* execution there * execution there
* *
*/ */
public class SampleProjectTest extends FuncTestCase { public class SampleProjectTest extends AdtSdkTestCase {
private static final Logger sLogger = Logger.getLogger(SampleProjectTest.class.getName()); private static final Logger sLogger = Logger.getLogger(SampleProjectTest.class.getName());
@@ -56,7 +57,7 @@ public class SampleProjectTest extends FuncTestCase {
public void testSamples() throws CoreException { public void testSamples() throws CoreException {
// TODO: For reporting purposes, it would be better if a separate test success or failure // TODO: For reporting purposes, it would be better if a separate test success or failure
// could be reported for each sample // could be reported for each sample
IAndroidTarget[] targets = mSdk.getTargets(); IAndroidTarget[] targets = getSdk().getTargets();
for (IAndroidTarget target : targets) { for (IAndroidTarget target : targets) {
doTestSamplesForTarget(target); doTestSamplesForTarget(target);
} }
@@ -145,8 +146,25 @@ public class SampleProjectTest extends FuncTestCase {
private void validateNoProblems(IProject iproject) throws CoreException { private void validateNoProblems(IProject iproject) throws CoreException {
waitForBuild(iproject); waitForBuild(iproject);
assertFalse(String.format("%s project has compile errors", iproject.getName()),
ProjectHelper.hasError(iproject, true)); boolean hasErrors = false;
StringBuilder failureBuilder = new StringBuilder(String.format("%s project has errors:",
iproject.getName()));
IMarker[] markers = iproject.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
if (markers != null && markers.length > 0) {
// the project has marker(s). even though they are "problem" we
// don't know their severity. so we loop on them and figure if they
// are warnings or errors
for (IMarker m : markers) {
int s = m.getAttribute(IMarker.SEVERITY, -1);
if (s == IMarker.SEVERITY_ERROR) {
hasErrors = true;
failureBuilder.append("\n");
failureBuilder.append(m.getAttribute(IMarker.MESSAGE, ""));
}
}
}
assertFalse(failureBuilder.toString(), hasErrors);
} }
/** /**