MountServiceTestspublic class MountServiceTests extends android.test.AndroidTestCase
Fields Summary |
---|
private static final String | TAG | private static final long | MAX_WAIT_TIME | private static final long | WAIT_TIME_INCR | private static final String | OBB_MOUNT_PREFIX |
Methods Summary |
---|
private static void | assertStartsWith(java.lang.String message, java.lang.String prefix, java.lang.String actual)
if (!actual.startsWith(prefix)) {
throw new ComparisonFailure(message, prefix, actual);
}
| private java.lang.String | checkMountedPath(android.os.storage.StorageManager sm, java.io.File file)
final String mountPath = sm.getMountedObbPath(file.getPath());
assertStartsWith("Path should be in " + OBB_MOUNT_PREFIX,
OBB_MOUNT_PREFIX,
mountPath);
return mountPath;
| private void | copyRawToFile(int rawResId, java.io.File outFile)
Resources res = mContext.getResources();
InputStream is = null;
try {
is = res.openRawResource(rawResId);
} catch (NotFoundException e) {
fail("Failed to load resource with id: " + rawResId);
}
FileUtils.setPermissions(outFile.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
| FileUtils.S_IRWXO, -1, -1);
assertTrue(FileUtils.copyToFile(is, outFile));
FileUtils.setPermissions(outFile.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
| FileUtils.S_IRWXO, -1, -1);
| private java.io.File | getFilePath(java.lang.String name)
final File filesDir = mContext.getFilesDir();
final File outFile = new File(filesDir, name);
return outFile;
| private android.os.storage.StorageManager | getStorageManager()
return (StorageManager) getContext().getSystemService(Context.STORAGE_SERVICE);
| private void | mountObb(android.os.storage.StorageManager sm, int resource, java.io.File file, int expectedState)
copyRawToFile(resource, file);
final ObbObserver observer = new ObbObserver();
assertTrue("mountObb call on " + file.getPath() + " should succeed",
sm.mountObb(file.getPath(), null, observer));
assertTrue("Mount should have completed",
observer.waitForCompletion());
if (expectedState == OnObbStateChangeListener.MOUNTED) {
assertTrue("OBB should be mounted", sm.isObbMounted(file.getPath()));
}
assertEquals("Actual file and resolved file should be the same",
file.getPath(), observer.getPath());
assertEquals(expectedState, observer.getState());
| private com.android.server.MountServiceTests$ObbObserver | mountObbWithoutWait(android.os.storage.StorageManager sm, int resource, java.io.File file)
copyRawToFile(resource, file);
final ObbObserver observer = new ObbObserver();
assertTrue("mountObb call on " + file.getPath() + " should succeed", sm.mountObb(file
.getPath(), null, observer));
return observer;
| protected void | setUp()
super.setUp();
| protected void | tearDown()
super.tearDown();
| public void | testAttemptMountNonObb()
StorageManager sm = getStorageManager();
final File outFile = getFilePath("test1_nosig.obb");
mountObb(sm, R.raw.test1_nosig, outFile, OnObbStateChangeListener.ERROR_INTERNAL);
assertFalse("OBB should not be mounted",
sm.isObbMounted(outFile.getPath()));
assertNull("OBB's mounted path should be null",
sm.getMountedObbPath(outFile.getPath()));
| public void | testAttemptMountObbWrongPackage()
StorageManager sm = getStorageManager();
final File outFile = getFilePath("test1_wrongpackage.obb");
mountObb(sm, R.raw.test1_wrongpackage, outFile,
OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
assertFalse("OBB should not be mounted",
sm.isObbMounted(outFile.getPath()));
assertNull("OBB's mounted path should be null",
sm.getMountedObbPath(outFile.getPath()));
| public void | testBuildObbPath()
final int userId = 10;
// Paths outside external storage should remain untouched
assertEquals("/storage/random/foo",
buildObbPath("/storage/random/foo", userId, true));
assertEquals("/storage/random/foo",
buildObbPath("/storage/random/foo", userId, false));
// Paths on user-specific emulated storage
assertEquals("/mnt/shell/emulated/10/foo",
buildObbPath("/storage/emulated_legacy/foo", userId, true));
assertEquals("/storage/emulated/10/foo",
buildObbPath("/storage/emulated_legacy/foo", userId, false));
assertEquals("/mnt/shell/emulated/10/foo",
buildObbPath("/storage/emulated/10/foo", userId, true));
assertEquals("/storage/emulated/10/foo",
buildObbPath("/storage/emulated/10/foo", userId, false));
// Paths on shared OBB emulated storage
assertEquals("/mnt/shell/emulated/obb/foo",
buildObbPath("/storage/emulated_legacy/Android/obb/foo", userId, true));
assertEquals("/storage/emulated/0/Android/obb/foo",
buildObbPath("/storage/emulated_legacy/Android/obb/foo", userId, false));
assertEquals("/mnt/shell/emulated/obb/foo",
buildObbPath("/storage/emulated/10/Android/obb/foo", userId, true));
assertEquals("/storage/emulated/0/Android/obb/foo",
buildObbPath("/storage/emulated/10/Android/obb/foo", userId, false));
| public void | testMountAndUnmountObbNormal()
StorageManager sm = getStorageManager();
final File outFile = getFilePath("test1.obb");
mountObb(sm, R.raw.test1, outFile, OnObbStateChangeListener.MOUNTED);
mountObb(sm, R.raw.test1, outFile, OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
final String mountPath = checkMountedPath(sm, outFile);
final File mountDir = new File(mountPath);
assertTrue("OBB mounted path should be a directory",
mountDir.isDirectory());
unmountObb(sm, outFile, OnObbStateChangeListener.UNMOUNTED);
| public void | testMountAndUnmountTwoObbs()
StorageManager sm = getStorageManager();
final File file1 = getFilePath("test1.obb");
final File file2 = getFilePath("test2.obb");
ObbObserver oo1 = mountObbWithoutWait(sm, R.raw.test1, file1);
ObbObserver oo2 = mountObbWithoutWait(sm, R.raw.test1, file2);
Log.d(TAG, "Waiting for OBB #1 to complete mount");
waitForObbActionCompletion(sm, file1, oo1, OnObbStateChangeListener.MOUNTED, false);
Log.d(TAG, "Waiting for OBB #2 to complete mount");
waitForObbActionCompletion(sm, file2, oo2, OnObbStateChangeListener.MOUNTED, false);
final String mountPath1 = checkMountedPath(sm, file1);
final File mountDir1 = new File(mountPath1);
assertTrue("OBB mounted path should be a directory", mountDir1.isDirectory());
final String mountPath2 = checkMountedPath(sm, file2);
final File mountDir2 = new File(mountPath2);
assertTrue("OBB mounted path should be a directory", mountDir2.isDirectory());
unmountObb(sm, file1, OnObbStateChangeListener.UNMOUNTED);
unmountObb(sm, file2, OnObbStateChangeListener.UNMOUNTED);
| private void | unmountObb(android.os.storage.StorageManager sm, java.io.File file, int expectedState)
final ObbObserver observer = new ObbObserver();
assertTrue("unmountObb call on test1.obb should succeed",
sm.unmountObb(file.getPath(),
false, observer));
assertTrue("Unmount should have completed",
observer.waitForCompletion());
assertEquals(expectedState, observer.getState());
if (expectedState == OnObbStateChangeListener.UNMOUNTED) {
assertFalse("OBB should not be mounted", sm.isObbMounted(file.getPath()));
}
| private void | waitForObbActionCompletion(android.os.storage.StorageManager sm, java.io.File file, com.android.server.MountServiceTests$ObbObserver observer, int expectedState, boolean checkPath)
assertTrue("Mount should have completed", observer.waitForCompletion());
assertTrue("OBB should be mounted", sm.isObbMounted(file.getPath()));
if (checkPath) {
assertEquals("Actual file and resolved file should be the same", file.getPath(),
observer.getPath());
}
assertEquals(expectedState, observer.getState());
|
|