1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may obtain a 6 * copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.ide.eclipse.tests; 17 18 import com.android.ide.common.sdk.LoadStatus; 19 import com.android.ide.eclipse.adt.AdtPlugin; 20 import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; 21 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetParser; 22 import com.android.ide.eclipse.adt.internal.sdk.Sdk; 23 import com.android.sdklib.IAndroidTarget; 24 import com.android.testutils.SdkTestCase; 25 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.NullProgressMonitor; 28 29 /** 30 * A test case which uses the SDK loaded by the ADT plugin. 31 */ 32 public abstract class SdkLoadingTestCase extends SdkTestCase { 33 34 private Sdk mSdk; 35 36 protected SdkLoadingTestCase() { 37 } 38 39 /** 40 * Retrieve the {@link Sdk} under test. 41 */ 42 protected Sdk getSdk() { 43 if (mSdk == null) { 44 mSdk = loadSdk(); 45 assertNotNull(mSdk); 46 validateSdk(mSdk); 47 } 48 return mSdk; 49 } 50 51 /** 52 * Gets the current SDK from ADT, waiting if necessary. 53 */ 54 private Sdk loadSdk() { 55 AdtPlugin adt = AdtPlugin.getDefault(); 56 57 // We'll never get an AdtPlugin object when running this with the 58 // non-Eclipse jUnit test runner. 59 if (adt == null) { 60 return null; 61 } 62 63 // We'll never break out of the SDK load-wait-loop if the AdtPlugin doesn't 64 // actually have a valid SDK location because it won't have started an async load: 65 String sdkLocation = AdtPrefs.getPrefs().getOsSdkFolder(); 66 assertTrue("No valid SDK installation is set; for tests you typically need to set the" 67 + " environment variable ADT_TEST_SDK_PATH to point to an SDK folder", 68 sdkLocation != null && sdkLocation.length() > 0); 69 70 Object sdkLock = Sdk.getLock(); 71 LoadStatus loadStatus = LoadStatus.LOADING; 72 // wait for ADT to load the SDK on a separate thread 73 // loop max of 600 times * 200 ms = 2 minutes 74 final int maxWait = 600; 75 for (int i=0; i < maxWait && loadStatus == LoadStatus.LOADING; i++) { 76 try { 77 Thread.sleep(200); 78 } 79 catch (InterruptedException e) { 80 // ignore 81 } 82 synchronized (sdkLock) { 83 loadStatus = adt.getSdkLoadStatus(); 84 } 85 } 86 Sdk sdk = null; 87 synchronized (sdkLock) { 88 assertEquals(LoadStatus.LOADED, loadStatus); 89 sdk = Sdk.getCurrent(); 90 } 91 assertNotNull(sdk); 92 return sdk; 93 } 94 95 protected boolean validateSdk(IAndroidTarget target) { 96 return true; 97 } 98 99 /** 100 * Checks that the provided sdk contains one or more valid targets. 101 * @param sdk the {@link Sdk} to validate. 102 */ 103 private void validateSdk(Sdk sdk) { 104 assertTrue("sdk has no targets", sdk.getTargets().length > 0); 105 for (IAndroidTarget target : sdk.getTargets()) { 106 if (!validateSdk(target)) { 107 continue; 108 } 109 if (false) { // This takes forEVER 110 IStatus status = new AndroidTargetParser(target).run(new NullProgressMonitor()); 111 if (status.getCode() != IStatus.OK) { 112 fail("Failed to parse targets data"); 113 } 114 } 115 } 116 } 117 } 118