aboutsummaryrefslogtreecommitdiffstats
path: root/sdkmanager/libs/sdklib/tests/src
diff options
context:
space:
mode:
Diffstat (limited to 'sdkmanager/libs/sdklib/tests/src')
-rwxr-xr-xsdkmanager/libs/sdklib/tests/src/com/android/sdklib/LayoutlibVersionTest.java51
-rwxr-xr-xsdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTest.java47
-rwxr-xr-xsdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTestCase.java193
3 files changed, 291 insertions, 0 deletions
diff --git a/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/LayoutlibVersionTest.java b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/LayoutlibVersionTest.java
new file mode 100755
index 0000000..0c197aa
--- /dev/null
+++ b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/LayoutlibVersionTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2011 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.sdklib;
+
+import com.android.sdklib.SdkManager.LayoutlibVersion;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for {@link LayoutlibVersion}.
+ */
+public class LayoutlibVersionTest extends TestCase {
+
+ public void testLayoutlibVersion_create() {
+ LayoutlibVersion lv = new LayoutlibVersion(1, 2);
+ assertEquals(1, lv.getApi());
+ assertEquals(2, lv.getRevision());
+ }
+
+ public void testLayoutlibVersion_compare() {
+ assertTrue(new LayoutlibVersion(1, 1).compareTo(new LayoutlibVersion(1, 1)) == 0);
+ assertTrue(new LayoutlibVersion(1, 2).compareTo(new LayoutlibVersion(1, 1)) > 0);
+ assertTrue(new LayoutlibVersion(1, 1).compareTo(new LayoutlibVersion(1, 2)) < 0);
+ assertTrue(new LayoutlibVersion(2, 2).compareTo(new LayoutlibVersion(1, 3)) > 0);
+
+ // the lack of an API (== 0) naturally sorts as the lowest value possible.
+ assertTrue(new LayoutlibVersion(0, 1).compareTo(new LayoutlibVersion(0, 2)) < 0);
+ assertTrue(new LayoutlibVersion(0, 1).compareTo(new LayoutlibVersion(1, 2)) < 0);
+ assertTrue(new LayoutlibVersion(0, 3).compareTo(new LayoutlibVersion(1, 2)) < 0);
+ assertTrue(new LayoutlibVersion(1, 2).compareTo(new LayoutlibVersion(0, 3)) > 0);
+
+ // if we lack the revision number, we don't use it in comparison
+ assertTrue(new LayoutlibVersion(2, 0).compareTo(new LayoutlibVersion(2, 2)) == 0);
+ assertTrue(new LayoutlibVersion(2, 2).compareTo(new LayoutlibVersion(2, 0)) == 0);
+ }
+
+}
diff --git a/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTest.java b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTest.java
new file mode 100755
index 0000000..bf8600c
--- /dev/null
+++ b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTest.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.sdklib;
+
+
+import com.android.sdklib.SdkManager.LayoutlibVersion;
+
+public class SdkManagerTest extends SdkManagerTestCase {
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testSdkManager_LayoutlibVersion() {
+ SdkManager sdkman = getSdkManager();
+ IAndroidTarget t = sdkman.getTargets()[0];
+
+ assertTrue(t instanceof PlatformTarget);
+
+ LayoutlibVersion lv = ((PlatformTarget) t).getLayoutlibVersion();
+ assertNotNull(lv);
+ assertEquals(5, lv.getApi());
+ assertEquals(2, lv.getRevision());
+
+ assertSame(lv, sdkman.getMaxLayoutlibVersion());
+ }
+}
diff --git a/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTestCase.java b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTestCase.java
new file mode 100755
index 0000000..e4feacf
--- /dev/null
+++ b/sdkmanager/libs/sdklib/tests/src/com/android/sdklib/SdkManagerTestCase.java
@@ -0,0 +1,193 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.sdklib;
+
+
+import com.android.prefs.AndroidLocation;
+import com.android.prefs.AndroidLocation.AndroidLocationException;
+import com.android.sdklib.ISdkLog;
+import com.android.sdklib.SdkConstants;
+import com.android.sdklib.SdkManager;
+import com.android.sdklib.internal.avd.AvdManager;
+import com.android.sdklib.mock.MockLog;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case that allocates a temporary SDK, a temporary AVD base folder
+ * with an SdkManager and an AvdManager that points to them.
+ */
+public abstract class SdkManagerTestCase extends TestCase {
+
+ private File mFakeSdk;
+ private MockLog mLog;
+ private SdkManager mSdkManager;
+ private TmpAvdManager mAvdManager;
+
+ /** Returns the {@link MockLog} for this test case. */
+ public MockLog getLog() {
+ return mLog;
+ }
+
+ /** Returns the {@link SdkManager} for this test case. */
+ public SdkManager getSdkManager() {
+ return mSdkManager;
+ }
+
+ /** Returns the {@link AvdManager} for this test case. */
+ public TmpAvdManager getAvdManager() {
+ return mAvdManager;
+ }
+
+ /**
+ * Sets up a {@link MockLog}, a fake SDK in a temporary directory
+ * and an AVD Manager pointing to an initially-empty AVD directory.
+ */
+ @Override
+ public void setUp() throws Exception {
+ mLog = new MockLog();
+ mFakeSdk = makeFakeSdk();
+ mSdkManager = SdkManager.createManager(mFakeSdk.getAbsolutePath(), mLog);
+ assertNotNull("SdkManager location was invalid", mSdkManager);
+
+ mAvdManager = new TmpAvdManager(mSdkManager, mLog);
+ }
+
+ /**
+ * Removes the temporary SDK and AVD directories.
+ */
+ @Override
+ public void tearDown() throws Exception {
+ deleteDir(mFakeSdk);
+ }
+
+ /**
+ * An {@link AvdManager} that uses a temporary directory
+ * located <em>inside</em> the SDK directory for testing.
+ * The AVD list should be initially empty.
+ */
+ protected static class TmpAvdManager extends AvdManager {
+
+ /*
+ * Implementation detail:
+ * - When the super.AvdManager constructor is invoked, it will invoke
+ * the buildAvdFilesList() to fill the initial AVD list, which will in
+ * turn call getBaseAvdFolder().
+ * - That's why mTmpAvdRoot is initialized in getAvdRoot() rather than
+ * in the constructor, since we can't initialize fields before the super()
+ * call.
+ */
+
+ /**
+ * AVD Root, initialized "lazily" when the AVD root is first requested.
+ */
+ private File mTmpAvdRoot;
+
+ public TmpAvdManager(SdkManager sdkManager, ISdkLog log) throws AndroidLocationException {
+ super(sdkManager, log);
+ }
+
+ @Override
+ public String getBaseAvdFolder() throws AndroidLocationException {
+ if (mTmpAvdRoot == null) {
+ mTmpAvdRoot = new File(getSdkManager().getLocation(), "tmp_avds");
+ mTmpAvdRoot.mkdirs();
+ }
+ return mTmpAvdRoot.getAbsolutePath();
+ }
+ }
+
+ /**
+ * Build enough of a skeleton SDK to make the tests pass.
+ * <p/>
+ * Ideally this wouldn't touch the file system but the current
+ * structure of the SdkManager and AvdManager makes this difficult.
+ *
+ * @return Path to the temporary SDK root
+ * @throws IOException
+ */
+ private File makeFakeSdk() throws IOException {
+
+ // First we create a temp file to "reserve" the temp directory name we want to use.
+ File tmpFile = File.createTempFile(
+ this.getClass().getSimpleName() + '_' + this.getName(), null);
+ // Then erase the file and make the directory
+ tmpFile.delete();
+ tmpFile.mkdirs();
+
+ AndroidLocation.resetFolder();
+ System.setProperty("user.home", tmpFile.getAbsolutePath());
+ File addonsDir = new File(tmpFile, SdkConstants.FD_ADDONS);
+ addonsDir.mkdir();
+ File toolsLibEmuDir = new File(tmpFile, SdkConstants.OS_SDK_TOOLS_LIB_FOLDER + "emulator");
+ toolsLibEmuDir.mkdirs();
+ new File(toolsLibEmuDir, "snapshots.img").createNewFile();
+ File platformsDir = new File(tmpFile, SdkConstants.FD_PLATFORMS);
+
+ // Creating a fake target here on down
+ File targetDir = new File(platformsDir, "v0_0");
+ targetDir.mkdirs();
+ new File(targetDir, SdkConstants.FN_FRAMEWORK_LIBRARY).createNewFile();
+ new File(targetDir, SdkConstants.FN_FRAMEWORK_AIDL).createNewFile();
+
+ File sourceProp = new File(targetDir, SdkConstants.FN_SOURCE_PROP);
+ sourceProp.createNewFile();
+ FileWriter out = new FileWriter(sourceProp);
+ out.write("Layoutlib.Api=5\n");
+ out.write("Layoutlib.Revision=2\n");
+ out.close();
+
+ File buildProp = new File(targetDir, SdkConstants.FN_BUILD_PROP);
+ out = new FileWriter(buildProp);
+ out.write(SdkManager.PROP_VERSION_RELEASE + "=0.0\n");
+ out.write(SdkManager.PROP_VERSION_SDK + "=0\n");
+ out.write(SdkManager.PROP_VERSION_CODENAME + "=REL\n");
+ out.close();
+
+ File imagesDir = new File(targetDir, "images");
+ imagesDir.mkdirs();
+
+ new File(imagesDir, "userdata.img").createNewFile();
+ File skinsDir = new File(targetDir, "skins");
+ File hvgaDir = new File(skinsDir, "HVGA");
+ hvgaDir.mkdirs();
+ return tmpFile;
+ }
+
+ /**
+ * Recursive delete directory. Mostly for fake SDKs.
+ *
+ * @param root directory to delete
+ */
+ private void deleteDir(File root) {
+ if (root.exists()) {
+ for (File file : root.listFiles()) {
+ if (file.isDirectory()) {
+ deleteDir(file);
+ } else {
+ file.delete();
+ }
+ }
+ root.delete();
+ }
+ }
+
+}