FileDocCategorySizeDatePackage
MountServiceTests.javaAPI DocAndroid 5.1 API11524Thu Mar 12 22:22:42 GMT 2015com.android.server

MountServiceTests

public 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
Constructors Summary
Methods Summary
private static voidassertStartsWith(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.StringcheckMountedPath(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 voidcopyRawToFile(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.FilegetFilePath(java.lang.String name)

        final File filesDir = mContext.getFilesDir();
        final File outFile = new File(filesDir, name);
        return outFile;
    
private android.os.storage.StorageManagergetStorageManager()

        return (StorageManager) getContext().getSystemService(Context.STORAGE_SERVICE);
    
private voidmountObb(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$ObbObservermountObbWithoutWait(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 voidsetUp()


    
         
        super.setUp();
    
protected voidtearDown()

        super.tearDown();
    
public voidtestAttemptMountNonObb()

        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 voidtestAttemptMountObbWrongPackage()

        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 voidtestBuildObbPath()

        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 voidtestMountAndUnmountObbNormal()

        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 voidtestMountAndUnmountTwoObbs()

        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 voidunmountObb(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 voidwaitForObbActionCompletion(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());