• 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.eclipse.adt.AdtPlugin;
19 import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetParser;
20 import com.android.ide.eclipse.adt.internal.sdk.LoadStatus;
21 import com.android.ide.eclipse.adt.internal.sdk.Sdk;
22 import com.android.sdklib.IAndroidTarget;
23 
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 
27 import junit.framework.TestCase;
28 
29 /**
30  * A test case which uses the SDK loaded by the ADT plugin.
31  */
32 public abstract class SdkTestCase extends TestCase {
33 
34     private Sdk mSdk;
35 
SdkTestCase()36     protected SdkTestCase() {
37     }
38 
39     /**
40      * Retrieve the {@link Sdk} under test.
41      */
getSdk()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      */
loadSdk()54     private Sdk loadSdk() {
55         AdtPlugin adt = AdtPlugin.getDefault();
56         Object sdkLock = adt.getSdkLockObject();
57         LoadStatus loadStatus = LoadStatus.LOADING;
58         // wait for ADT to load the SDK on a separate thread
59         // loop max of 600 times * 200 ms =  2 minutes
60         final int maxWait = 600;
61         for (int i=0; i < maxWait && loadStatus == LoadStatus.LOADING; i++) {
62             try {
63                 Thread.sleep(200);
64             }
65             catch (InterruptedException e) {
66                 // ignore
67             }
68             synchronized(sdkLock) {
69                 loadStatus = adt.getSdkLoadStatus();
70             }
71         }
72         Sdk sdk = null;
73         synchronized(sdkLock) {
74             assertEquals(LoadStatus.LOADED, loadStatus);
75             sdk = Sdk.getCurrent();
76         }
77         assertNotNull(sdk);
78         return sdk;
79     }
80 
81     /**
82      * Checks that the provided sdk contains one or more valid targets.
83      * @param sdk the {@link Sdk} to validate.
84      */
validateSdk(Sdk sdk)85     private void validateSdk(Sdk sdk) {
86         assertTrue("sdk has no targets", sdk.getTargets().length > 0);
87         for (IAndroidTarget target : sdk.getTargets()) {
88             IStatus status = new AndroidTargetParser(target).run(new NullProgressMonitor());
89             if (status.getCode() != IStatus.OK) {
90                 fail("Failed to parse targets data");
91             }
92         }
93     }
94 }
95