FileDocCategorySizeDatePackage
ServiceTest.javaAPI DocAndroid 1.5 API17099Wed May 06 22:42:02 BST 2009com.android.unit_tests.activity

ServiceTest

public class ServiceTest extends ActivityTestsBase

Fields Summary
public static final String
SERVICE_LOCAL
public static final String
SERVICE_LOCAL_GRANTED
public static final String
SERVICE_LOCAL_DENIED
public static final String
REPORT_OBJ_NAME
public static final int
STARTED_CODE
public static final int
DESTROYED_CODE
public static final int
SET_REPORTER_CODE
public static final int
UNBIND_CODE
public static final int
REBIND_CODE
public static final int
STATE_START_1
public static final int
STATE_START_2
public static final int
STATE_UNBIND
public static final int
STATE_DESTROY
public static final int
STATE_REBIND
public static final int
STATE_UNBIND_ONLY
public int
mStartState
public android.os.IBinder
mStartReceiver
Constructors Summary
Methods Summary
voidbindAutoExpectResult(android.content.Intent service)

        TestConnection conn = new TestConnection(false, true);
        boolean success = false;
        try {
            conn.setMonitor(true);
            mStartState = STATE_START_1;
            getContext().bindService(
                    service, conn, Context.BIND_AUTO_CREATE);
            waitForResultOrThrow(5 * 1000, "connection to start and receive service");
            success = true;
        } finally {
            if (!success) {
                try {
                    getContext().unbindService(conn);
                } catch (Exception e) {
                    // eat
                }
            }
        }
        mStartState = STATE_UNBIND;
        getContext().unbindService(conn);
        waitForResultOrThrow(5 * 1000, "disconnecting from service");
    
voidbindExpectNoPermission(android.content.Intent service)

        TestConnection conn = new TestConnection(false, false);
        try {
            getContext().bindService(service, conn, Context.BIND_AUTO_CREATE);
            fail("Expected security exception when binding " + service);
        } catch (SecurityException e) {
            // expected
        } finally {
            getContext().unbindService(conn);
        }
    
voidbindExpectResult(android.content.Intent service)

        TestConnection conn = new TestConnection(true, false);
        TestConnection conn2 = new TestConnection(false, false);
        boolean success = false;
        try {
            // Expect to see the TestConnection connected.
            mStartState = STATE_START_1;
            getContext().bindService(service, conn, 0);
            getContext().startService(service);
            waitForResultOrThrow(5 * 1000, "existing connection to receive service");

            // Expect to see the second TestConnection connected.
            getContext().bindService(service, conn2, 0);
            waitForResultOrThrow(5 * 1000, "new connection to receive service");

            getContext().unbindService(conn2);
            success = true;
        } finally {
            if (!success) {
                try {
                    getContext().stopService(service);
                    getContext().unbindService(conn);
                    getContext().unbindService(conn2);
                } catch (Exception e) {
                    // eat
                }
            }
        }

        // Expect to see the TestConnection disconnected.
        mStartState = STATE_DESTROY;
        getContext().stopService(service);
        waitForResultOrThrow(5 * 1000, "existing connection to lose service");

        getContext().unbindService(conn);
        
        conn = new TestConnection(true, true);
        success = false;
        try {
            // Expect to see the TestConnection connected.
            conn.setMonitor(true);
            mStartState = STATE_START_1;
            getContext().bindService(service, conn, 0);
            getContext().startService(service);
            waitForResultOrThrow(5 * 1000, "existing connection to receive service");

            success = true;
        } finally {
            if (!success) {
                try {
                    getContext().stopService(service);
                    getContext().unbindService(conn);
                } catch (Exception e) {
                    // eat
                }
            }
        }

        // Expect to see the service unbind and then destroyed.
        conn.setMonitor(false);
        mStartState = STATE_UNBIND;
        getContext().stopService(service);
        waitForResultOrThrow(5 * 1000, "existing connection to lose service");

        getContext().unbindService(conn);
        
        conn = new TestConnection(true, true);
        success = false;
        try {
            // Expect to see the TestConnection connected.
            conn.setMonitor(true);
            mStartState = STATE_START_1;
            getContext().bindService(service, conn, 0);
            getContext().startService(service);
            waitForResultOrThrow(5 * 1000, "existing connection to receive service");

            success = true;
        } finally {
            if (!success) {
                try {
                    getContext().stopService(service);
                    getContext().unbindService(conn);
                } catch (Exception e) {
                    // eat
                }
            }
        }

        // Expect to see the service unbind but not destroyed.
        conn.setMonitor(false);
        mStartState = STATE_UNBIND_ONLY;
        getContext().unbindService(conn);
        waitForResultOrThrow(5 * 1000, "existing connection to unbind service");
        
        // Expect to see the service rebound.
        mStartState = STATE_REBIND;
        getContext().bindService(service, conn, 0);
        waitForResultOrThrow(5 * 1000, "existing connection to rebind service");
        
        // Expect to see the service unbind and then destroyed.
        mStartState = STATE_UNBIND;
        getContext().stopService(service);
        waitForResultOrThrow(5 * 1000, "existing connection to lose service");

        getContext().unbindService(conn);
    
voidstartExpectNoPermission(android.content.Intent service)

        try {
            getContext().startService(service);
            fail("Expected security exception when starting " + service);
        } catch (SecurityException e) {
            // expected
        }
    
voidstartExpectResult(android.content.Intent service)

        startExpectResult(service, new Bundle());
    
voidstartExpectResult(android.content.Intent service, android.os.Bundle bundle)

        bundle.putIBinder(REPORT_OBJ_NAME, mStartReceiver);
        boolean success = false;
        try {
            //Log.i("foo", "STATE_START_1");
            mStartState = STATE_START_1;
            getContext().startService(new Intent(service).putExtras(bundle));
            waitForResultOrThrow(5 * 1000, "service to start first time");
            //Log.i("foo", "STATE_START_2");
            mStartState = STATE_START_2;
            getContext().startService(new Intent(service).putExtras(bundle));
            waitForResultOrThrow(5 * 1000, "service to start second time");
            success = true;
        } finally {
            if (!success) {
                try {
                    getContext().stopService(service);
                } catch (Exception e) {
                    // eat
                }
            }
        }
        //Log.i("foo", "STATE_DESTROY");
        mStartState = STATE_DESTROY;
        getContext().stopService(service);
        waitForResultOrThrow(5 * 1000, "service to be destroyed");
    
public voidtestLocalBindAction()

        bindExpectResult(new Intent(SERVICE_LOCAL));
    
public voidtestLocalBindActionPermissionDenied()

        bindExpectNoPermission(new Intent(SERVICE_LOCAL_DENIED));
    
public voidtestLocalBindActionPermissionGranted()

        bindExpectResult(new Intent(SERVICE_LOCAL_GRANTED));
    
public voidtestLocalBindAutoAction()

        bindAutoExpectResult(new Intent(SERVICE_LOCAL));
    
public voidtestLocalBindAutoActionPermissionGranted()

        bindAutoExpectResult(new Intent(SERVICE_LOCAL_GRANTED));
    
public voidtestLocalBindAutoClass()

        bindAutoExpectResult(new Intent(getContext(), LocalService.class));
    
public voidtestLocalBindAutoClassPermissionGranted()

        bindAutoExpectResult(new Intent(getContext(), LocalGrantedService.class));
    
public voidtestLocalBindClass()

        bindExpectResult(new Intent(getContext(), LocalService.class));
    
public voidtestLocalBindClassPermissionDenied()

        bindExpectNoPermission(new Intent(getContext(), LocalDeniedService.class));
    
public voidtestLocalBindClassPermissionGranted()

        bindExpectResult(new Intent(getContext(), LocalGrantedService.class));
    
public voidtestLocalStartAction()

        startExpectResult(new Intent(SERVICE_LOCAL));
    
public voidtestLocalStartActionPermissionDenied()

        startExpectNoPermission(new Intent(SERVICE_LOCAL_DENIED));
    
public voidtestLocalStartActionPermissionGranted()

        startExpectResult(new Intent(SERVICE_LOCAL_GRANTED));
    
public voidtestLocalStartClass()

        startExpectResult(new Intent(getContext(), LocalService.class));
    
public voidtestLocalStartClassPermissionDenied()

        startExpectNoPermission(new Intent(getContext(), LocalDeniedService.class));
    
public voidtestLocalStartClassPermissionGranted()

        startExpectResult(new Intent(getContext(), LocalGrantedService.class));
    
public voidtestLocalUnbindTwice()

        EmptyConnection conn = new EmptyConnection();
        getContext().bindService(
                new Intent(SERVICE_LOCAL_GRANTED), conn, 0);
        getContext().unbindService(conn);
        try {
            getContext().unbindService(conn);
            fail("No exception thrown on second unbind");
        } catch (IllegalArgumentException e) {
            //Log.i("foo", "Unbind exception", e);
        }