diff options
author | Kenny Root <kroot@google.com> | 2010-08-05 08:40:00 -0700 |
---|---|---|
committer | Kenny Root <kroot@google.com> | 2010-08-05 08:40:09 -0700 |
commit | d7b421b4de68d803252a5d9b3cb0106731217ad7 (patch) | |
tree | 86343e28f9e8708cae4efdfd048520d7fe94636d /core | |
parent | cde66df44240cfe5a7bec12ac52464c3bf26c14f (diff) | |
download | frameworks_base-d7b421b4de68d803252a5d9b3cb0106731217ad7.zip frameworks_base-d7b421b4de68d803252a5d9b3cb0106731217ad7.tar.gz frameworks_base-d7b421b4de68d803252a5d9b3cb0106731217ad7.tar.bz2 |
Fix up PackageManager tests
PackageManagerTests was trying to mount and unmount the media without
waiting for the correct state to arrive. It was proceeding with any
state transition.
Some tests had their own volume mounting and unmounting code that was
flawed, so switch it to the more robust mountMedia() and unmountMedia()
implementations.
Change-Id: If464927bb09dd15cb040c6cce195a6ad8452de95
Diffstat (limited to 'core')
-rwxr-xr-x | core/tests/coretests/src/android/content/pm/PackageManagerTests.java | 111 | ||||
-rw-r--r-- | core/tests/coretests/src/android/os/storage/StorageListener.java | 21 |
2 files changed, 79 insertions, 53 deletions
diff --git a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java index 7c28516..cbf8c87 100755 --- a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java +++ b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java @@ -69,7 +69,7 @@ public class PackageManagerTests extends AndroidTestCase { @Override protected void setUp() throws Exception { super.setUp(); - mOrigState = getMediaState(); + mOrigState = checkMediaState(Environment.MEDIA_MOUNTED); if (!mountMedia()) { Log.i(TAG, "sdcard not mounted? Some of these tests might fail"); } @@ -78,12 +78,12 @@ public class PackageManagerTests extends AndroidTestCase { @Override protected void tearDown() throws Exception { // Restore media state. - boolean newState = getMediaState(); + boolean newState = checkMediaState(Environment.MEDIA_MOUNTED); if (newState != mOrigState) { if (mOrigState) { - getMs().mountVolume(Environment.getExternalStorageDirectory().getPath()); + mountMedia(); } else { - getMs().unmountVolume(Environment.getExternalStorageDirectory().getPath(), true); + unmountMedia(); } } super.tearDown(); @@ -576,6 +576,7 @@ public class PackageManagerTests extends AndroidTestCase { @LargeTest public void testInstallSdcard() { + mountMedia(); sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, true); } @@ -913,41 +914,62 @@ public class PackageManagerTests extends AndroidTestCase { return null; } - boolean getMediaState() { + boolean checkMediaState(String desired) { try { - String mPath = Environment.getExternalStorageDirectory().getPath(); - String state = getMs().getVolumeState(mPath); - return Environment.MEDIA_MOUNTED.equals(state); + String mPath = Environment.getExternalStorageDirectory().getPath(); + String actual = getMs().getVolumeState(mPath); + if (desired.equals(actual)) { + return true; + } else { + return false; + } } catch (RemoteException e) { + Log.e(TAG, "Exception while checking media state", e); return false; } } boolean mountMedia() { - if (getMediaState()) { + if (checkMediaState(Environment.MEDIA_MOUNTED)) { return true; } + + final String path = Environment.getExternalStorageDirectory().toString(); + StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED); + StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); + sm.registerListener(observer); try { - String mPath = Environment.getExternalStorageDirectory().toString(); - int ret = getMs().mountVolume(mPath); - return ret == StorageResultCode.OperationSucceeded; - } catch (RemoteException e) { + // Wait on observer + synchronized (observer) { + int ret = getMs().mountVolume(path); + if (ret != StorageResultCode.OperationSucceeded) { + throw new Exception("Could not mount the media"); + } + long waitTime = 0; + while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) { + observer.wait(WAIT_TIME_INCR); + waitTime += WAIT_TIME_INCR; + } + if (!observer.isDone()) { + throw new Exception("Timed out waiting for unmount media notification"); + } + return true; + } + } catch (Exception e) { + Log.e(TAG, "Exception : " + e); return false; + } finally { + sm.unregisterListener(observer); } } private boolean unmountMedia() { - String path = Environment.getExternalStorageDirectory().getPath(); - try { - String state = getMs().getVolumeState(path); - if (Environment.MEDIA_UNMOUNTED.equals(state)) { - return true; - } - } catch (RemoteException e) { - failStr(e); + if (checkMediaState(Environment.MEDIA_UNMOUNTED)) { + return true; } - StorageListener observer = new StorageListener(); + final String path = Environment.getExternalStorageDirectory().getPath(); + StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED); StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); sm.registerListener(observer); try { @@ -976,7 +998,7 @@ public class PackageManagerTests extends AndroidTestCase { // Install pkg on sdcard InstallParams ip = sampleInstallFromRawResource(PackageManager.INSTALL_EXTERNAL, false); if (localLOGV) Log.i(TAG, "Installed pkg on sdcard"); - boolean origState = getMediaState(); + boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); boolean registeredReceiver = false; SdMountReceiver receiver = new SdMountReceiver(new String[]{ip.pkg.packageName}); try { @@ -1468,7 +1490,7 @@ public class PackageManagerTests extends AndroidTestCase { */ @LargeTest public void testInstallSdcardUnmount() { - boolean origState = getMediaState(); + boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); try { // Unmount sdcard assertTrue(unmountMedia()); @@ -1493,22 +1515,22 @@ public class PackageManagerTests extends AndroidTestCase { */ @LargeTest public void testInstallManifestSdcardUnmount() { - boolean origState = getMediaState(); - try { - // Unmount sdcard - assertTrue(unmountMedia()); - InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard); - installFromRawResource(ip, 0, true, false, -1, - PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); - } finally { - // Restore original media state - if (origState) { - mountMedia(); - } else { - unmountMedia(); - } - } - } + boolean origState = checkMediaState(Environment.MEDIA_MOUNTED); + try { + // Unmount sdcard + assertTrue(unmountMedia()); + InstallParams ip = new InstallParams("install.apk", R.raw.install_loc_sdcard); + installFromRawResource(ip, 0, true, false, -1, + PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY); + } finally { + // Restore original media state + if (origState) { + mountMedia(); + } else { + unmountMedia(); + } + } + } /*---------- Recommended install location tests ----*/ /* Precedence: FlagManifestExistingUser @@ -2269,7 +2291,7 @@ public class PackageManagerTests extends AndroidTestCase { @LargeTest public void testInstallOnSdPermissionsUnmount() { InstallParams ip = null; - boolean origMediaState = getMediaState(); + boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); try { // **: Upon installing a package, are its declared permissions published? int iFlags = PackageManager.INSTALL_INTERNAL; @@ -2300,8 +2322,10 @@ public class PackageManagerTests extends AndroidTestCase { */ @LargeTest public void testInstallSdcardStaleContainer() { - boolean origMediaState = getMediaState(); + boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); try { + // Mount media first + mountMedia(); String outFileName = "install.apk"; int rawResId = R.raw.install; PackageManager pm = mContext.getPackageManager(); @@ -2342,7 +2366,7 @@ public class PackageManagerTests extends AndroidTestCase { */ @LargeTest public void testInstallSdcardStaleContainerReinstall() { - boolean origMediaState = getMediaState(); + boolean origMediaState = checkMediaState(Environment.MEDIA_MOUNTED); try { // Mount media first mountMedia(); @@ -2375,7 +2399,6 @@ public class PackageManagerTests extends AndroidTestCase { } else { unmountMedia(); } - } } /* diff --git a/core/tests/coretests/src/android/os/storage/StorageListener.java b/core/tests/coretests/src/android/os/storage/StorageListener.java index d6dae22..6a26b88 100644 --- a/core/tests/coretests/src/android/os/storage/StorageListener.java +++ b/core/tests/coretests/src/android/os/storage/StorageListener.java @@ -21,21 +21,24 @@ import android.util.Log; public class StorageListener extends StorageEventListener { private static final boolean localLOGV = true; - public static final String TAG="StorageListener"; + public static final String TAG = "StorageListener"; - String oldState; - String newState; - String path; + private String mTargetState; private boolean doneFlag = false; + + public StorageListener(String targetState) { + mTargetState = targetState; + } + @Override public void onStorageStateChanged(String path, String oldState, String newState) { if (localLOGV) Log.i(TAG, "Storage state changed from " + oldState + " to " + newState); + synchronized (this) { - this.oldState = oldState; - this.newState = newState; - this.path = path; - doneFlag = true; - notifyAll(); + if (mTargetState.equals(newState)) { + doneFlag = true; + notifyAll(); + } } } |