• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 
28 import junit.framework.TestCase;
29 
30 /**
31  * A test case which uses the SDK loaded by the ADT plugin.
32  */
33 public abstract class SdkTestCase extends TestCase {
34 
35     private Sdk mSdk;
36 
SdkTestCase()37     protected SdkTestCase() {
38     }
39 
40     /**
41      * Retrieve the {@link Sdk} under test.
42      */
getSdk()43     protected Sdk getSdk() {
44         if (mSdk == null) {
45             mSdk = loadSdk();
46             assertNotNull(mSdk);
47             validateSdk(mSdk);
48         }
49         return mSdk;
50     }
51 
52     /**
53      * Gets the current SDK from ADT, waiting if necessary.
54      */
loadSdk()55     private Sdk loadSdk() {
56         AdtPlugin adt = AdtPlugin.getDefault();
57 
58         // We'll never get an AdtPlugin object when running this with the
59         // non-Eclipse jUnit test runner.
60         if (adt == null) {
61             return null;
62         }
63 
64         // We'll never break out of the SDK load-wait-loop if the AdtPlugin doesn't
65         // actually have a valid SDK location because it won't have started an async load:
66         String sdkLocation = AdtPrefs.getPrefs().getOsSdkFolder();
67         assertTrue("No valid SDK installation is set; for tests you typically need to set the"
68                 + " environment variable ADT_TEST_SDK_PATH to point to an SDK folder",
69                 sdkLocation != null && sdkLocation.length() > 0);
70 
71         Object sdkLock = Sdk.getLock();
72         LoadStatus loadStatus = LoadStatus.LOADING;
73         // wait for ADT to load the SDK on a separate thread
74         // loop max of 600 times * 200 ms =  2 minutes
75         final int maxWait = 600;
76         for (int i=0; i < maxWait && loadStatus == LoadStatus.LOADING; i++) {
77             try {
78                 Thread.sleep(200);
79             }
80             catch (InterruptedException e) {
81                 // ignore
82             }
83             synchronized (sdkLock) {
84                 loadStatus = adt.getSdkLoadStatus();
85             }
86         }
87         Sdk sdk = null;
88         synchronized (sdkLock) {
89             assertEquals(LoadStatus.LOADED, loadStatus);
90             sdk = Sdk.getCurrent();
91         }
92         assertNotNull(sdk);
93         return sdk;
94     }
95 
validateSdk(IAndroidTarget target)96     protected boolean validateSdk(IAndroidTarget target) {
97         return true;
98     }
99 
100     /**
101      * Checks that the provided sdk contains one or more valid targets.
102      * @param sdk the {@link Sdk} to validate.
103      */
validateSdk(Sdk sdk)104     private void validateSdk(Sdk sdk) {
105         assertTrue("sdk has no targets", sdk.getTargets().length > 0);
106         for (IAndroidTarget target : sdk.getTargets()) {
107             if (!validateSdk(target)) {
108                 continue;
109             }
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