From 25c925e6659e57f1cc8fef83be07f3917a108e3c Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Sat, 8 Sep 2012 22:02:21 -0700 Subject: Throw ALL the exceptions! (in the PackageManagerTests) Change all the test method headers to throw Exception so we don't have to try to catch anything during the tests. Catching an exception might mask an error during the tests. Also fix a lot of formatting errors that have built up over the years. This includes 3-space indentations instead of 4-space, missing spaces between braces and parens, too-long lines, wrapping when it isn't needed, etc. Change-Id: Ifccea5f0706b6f9235f37974a5f80bee76c654dd --- .../android/content/pm/PackageManagerTests.java | 891 ++++++++++++--------- 1 file changed, 493 insertions(+), 398 deletions(-) (limited to 'core/tests/coretests') diff --git a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java index 607150a..f46478c 100755 --- a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java +++ b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java @@ -55,19 +55,28 @@ import java.util.List; public class PackageManagerTests extends AndroidTestCase { private static final boolean localLOGV = true; - public static final String TAG="PackageManagerTests"; - public final long MAX_WAIT_TIME = 25*1000; - public final long WAIT_TIME_INCR = 5*1000; - private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec"; + + public static final String TAG = "PackageManagerTests"; + + public final long MAX_WAIT_TIME = 25 * 1000; + + public final long WAIT_TIME_INCR = 5 * 1000; + + private static final String SECURE_CONTAINERS_PREFIX = "/mnt/asec/"; + private static final int APP_INSTALL_AUTO = PackageHelper.APP_INSTALL_AUTO; + private static final int APP_INSTALL_DEVICE = PackageHelper.APP_INSTALL_INTERNAL; + private static final int APP_INSTALL_SDCARD = PackageHelper.APP_INSTALL_EXTERNAL; + private boolean mOrigState; void failStr(String errMsg) { - Log.w(TAG, "errMsg="+errMsg); + Log.w(TAG, "errMsg=" + errMsg); fail(errMsg); } + void failStr(Exception e) { failStr(e.getMessage()); } @@ -97,10 +106,11 @@ public class PackageManagerTests extends AndroidTestCase { private class PackageInstallObserver extends IPackageInstallObserver.Stub { public int returnCode; + private boolean doneFlag = false; public void packageInstalled(String packageName, int returnCode) { - synchronized(this) { + synchronized (this) { this.returnCode = returnCode; doneFlag = true; notifyAll(); @@ -114,10 +124,15 @@ public class PackageManagerTests extends AndroidTestCase { abstract class GenericReceiver extends BroadcastReceiver { private boolean doneFlag = false; + boolean received = false; + Intent intent; + IntentFilter filter; + abstract boolean notifyNow(Intent intent); + @Override public void onReceive(Context context, Intent intent) { if (notifyNow(intent)) { @@ -179,11 +194,11 @@ public class PackageManagerTests extends AndroidTestCase { mContext.registerReceiver(receiver, receiver.filter); try { // Wait on observer - synchronized(observer) { + synchronized (observer) { synchronized (receiver) { getPm().installPackage(packageURI, observer, flags, null); long waitTime = 0; - while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { + while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { try { observer.wait(WAIT_TIME_INCR); waitTime += WAIT_TIME_INCR; @@ -191,7 +206,7 @@ public class PackageManagerTests extends AndroidTestCase { Log.i(TAG, "Interrupted during sleep", e); } } - if(!observer.isDone()) { + if (!observer.isDone()) { fail("Timed out waiting for packageInstalled callback"); } @@ -214,7 +229,7 @@ public class PackageManagerTests extends AndroidTestCase { // Verify we received the broadcast waitTime = 0; - while((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME) ) { + while ((!receiver.isDone()) && (waitTime < MAX_WAIT_TIME)) { try { receiver.wait(WAIT_TIME_INCR); waitTime += WAIT_TIME_INCR; @@ -222,7 +237,7 @@ public class PackageManagerTests extends AndroidTestCase { Log.i(TAG, "Interrupted during sleep", e); } } - if(!receiver.isDone()) { + if (!receiver.isDone()) { fail("Timed out waiting for PACKAGE_ADDED notification"); } } @@ -236,10 +251,10 @@ public class PackageManagerTests extends AndroidTestCase { PackageInstallObserver observer = new PackageInstallObserver(); try { // Wait on observer - synchronized(observer) { + synchronized (observer) { getPm().installPackage(packageURI, observer, flags, null); long waitTime = 0; - while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) { + while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { try { observer.wait(WAIT_TIME_INCR); waitTime += WAIT_TIME_INCR; @@ -247,7 +262,7 @@ public class PackageManagerTests extends AndroidTestCase { Log.i(TAG, "Interrupted during sleep", e); } } - if(!observer.isDone()) { + if (!observer.isDone()) { fail("Timed out waiting for packageInstalled callback"); } assertEquals(expectedResult, observer.returnCode); @@ -280,39 +295,42 @@ public class PackageManagerTests extends AndroidTestCase { File sourceFile = new File(archiveFilePath); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); - PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); + PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, + metrics, 0); packageParser = null; return pkg; } + private boolean checkSd(long pkgLen) { String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { return false; } long sdSize = -1; - StatFs sdStats = new StatFs( - Environment.getExternalStorageDirectory().getPath()); - sdSize = (long)sdStats.getAvailableBlocks() * - (long)sdStats.getBlockSize(); + StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath()); + sdSize = (long) sdStats.getAvailableBlocks() * (long) sdStats.getBlockSize(); // TODO check for thresholds here return pkgLen <= sdSize; } + private boolean checkInt(long pkgLen) { StatFs intStats = new StatFs(Environment.getDataDirectory().getPath()); - long intSize = (long)intStats.getBlockCount() * - (long)intStats.getBlockSize(); - long iSize = (long)intStats.getAvailableBlocks() * - (long)intStats.getBlockSize(); + long intSize = (long) intStats.getBlockCount() * (long) intStats.getBlockSize(); + long iSize = (long) intStats.getAvailableBlocks() * (long) intStats.getBlockSize(); // TODO check for thresholds here? return pkgLen <= iSize; } + private static final int INSTALL_LOC_INT = 1; + private static final int INSTALL_LOC_SD = 2; + private static final int INSTALL_LOC_ERR = -1; + private int getInstallLoc(int flags, int expInstallLocation, long pkgLen) { // Flags explicitly over ride everything else. - if ((flags & PackageManager.INSTALL_EXTERNAL) != 0 ) { + if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { return INSTALL_LOC_SD; } else if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { return INSTALL_LOC_INT; @@ -320,7 +338,7 @@ public class PackageManagerTests extends AndroidTestCase { // Manifest option takes precedence next if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { if (checkSd(pkgLen)) { - return INSTALL_LOC_SD; + return INSTALL_LOC_SD; } if (checkInt(pkgLen)) { return INSTALL_LOC_INT; @@ -422,7 +440,7 @@ public class PackageManagerTests extends AndroidTestCase { } catch (IOException e) { fail("Can't read " + nativeLibDir.getPath()); } - } else if (rLoc == INSTALL_LOC_SD){ + } else if (rLoc == INSTALL_LOC_SD) { if ((flags & PackageManager.INSTALL_FORWARD_LOCK) != 0) { assertTrue("The application should be installed forward locked", (info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) != 0); @@ -451,8 +469,8 @@ public class PackageManagerTests extends AndroidTestCase { nativeLibSymLink.exists()); try { assertEquals(nativeLibSymLink.getPath() + " should be a symlink to " - + info.nativeLibraryDir, info.nativeLibraryDir, nativeLibSymLink - .getCanonicalPath()); + + info.nativeLibraryDir, info.nativeLibraryDir, + nativeLibSymLink.getCanonicalPath()); } catch (IOException e) { fail("Can't read " + nativeLibSymLink.getPath()); } @@ -475,30 +493,38 @@ public class PackageManagerTests extends AndroidTestCase { class InstallParams { Uri packageURI; + PackageParser.Package pkg; + InstallParams(String outFileName, int rawResId) { this.pkg = getParsedPackage(outFileName, rawResId); this.packageURI = Uri.fromFile(new File(pkg.mScanPath)); } + InstallParams(PackageParser.Package pkg) { this.packageURI = Uri.fromFile(new File(pkg.mScanPath)); this.pkg = pkg; } + long getApkSize() { File file = new File(pkg.mScanPath); return file.length(); } } - private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) { - return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, - false, -1, PackageInfo.INSTALL_LOCATION_UNSPECIFIED); + private InstallParams sampleInstallFromRawResource(int flags, boolean cleanUp) throws Exception { + return installFromRawResource("install.apk", R.raw.install, flags, cleanUp, false, -1, + PackageInfo.INSTALL_LOCATION_UNSPECIFIED); } static final String PERM_PACKAGE = "package"; + static final String PERM_DEFINED = "defined"; + static final String PERM_UNDEFINED = "undefined"; + static final String PERM_USED = "used"; + static final String PERM_NOTUSED = "notused"; private void assertPermissions(String[] cmds) { @@ -530,7 +556,7 @@ public class PackageManagerTests extends AndroidTestCase { assertEquals(pi.name, cmd); assertNotNull(pkgInfo); boolean found = false; - for (int j=0; j packages = getPm().getInstalledPackages(0); assertNotNull("installed packages cannot be null", packages); assertTrue("installed packages cannot be empty", packages.size() > 0); } - public void testGetUnInstalledPackages() { + public void testGetUnInstalledPackages() throws Exception { List packages = getPm().getInstalledPackages( PackageManager.GET_UNINSTALLED_PACKAGES); assertNotNull("installed packages cannot be null", packages); @@ -3187,10 +3283,9 @@ public class PackageManagerTests extends AndroidTestCase { } /** - * Test that getInstalledPackages returns all the data specified in - * flags. + * Test that getInstalledPackages returns all the data specified in flags. */ - public void testGetInstalledPackagesAll() { + public void testGetInstalledPackagesAll() throws Exception { int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS @@ -3226,7 +3321,7 @@ public class PackageManagerTests extends AndroidTestCase { * Test that getInstalledPackages returns all the data specified in * flags when the GET_UNINSTALLED_PACKAGES flag is set. */ - public void testGetUnInstalledPackagesAll() { + public void testGetUnInstalledPackagesAll() throws Exception { int flags = PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_INSTRUMENTATION -- cgit v1.1