FileDocCategorySizeDatePackage
BroadcastTest.javaAPI DocAndroid 5.1 API21521Thu Mar 12 22:22:12 GMT 2015android.app.activity

BroadcastTest

public class BroadcastTest extends ActivityTestsBase

Fields Summary
public static final int
BROADCAST_TIMEOUT
public static final String
BROADCAST_REGISTERED
public static final String
BROADCAST_LOCAL
public static final String
BROADCAST_LOCAL_GRANTED
public static final String
BROADCAST_LOCAL_DENIED
public static final String
BROADCAST_REMOTE
public static final String
BROADCAST_REMOTE_GRANTED
public static final String
BROADCAST_REMOTE_DENIED
public static final String
BROADCAST_ALL
public static final String
BROADCAST_MULTI
public static final String
BROADCAST_ABORT
public static final String
BROADCAST_STICKY1
public static final String
BROADCAST_STICKY2
public static final String
BROADCAST_FAIL_REGISTER
public static final String
BROADCAST_FAIL_BIND
public static final String
RECEIVER_REG
public static final String
RECEIVER_LOCAL
public static final String
RECEIVER_REMOTE
public static final String
RECEIVER_ABORT
public static final String
RECEIVER_RESULTS
public static final String
DATA_1
public static final String
DATA_2
public static final int
GOT_RECEIVE_TRANSACTION
public static final int
ERROR_TRANSACTION
private String[]
mExpectedReceivers
private int
mNextReceiver
private String[]
mExpectedData
private boolean[]
mReceivedData
boolean
mReceiverRegistered
private android.os.Binder
mCallTarget
private android.content.BroadcastReceiver
mReceiver
Constructors Summary
Methods Summary
private voidfinishTest()


       
        if (mReceiverRegistered) {
            addIntermediate("before-unregister");
            unregisterMyReceiver();
        }
        finishTiming(true);
        finishGood();
    
public voidfinishWithResult(int resultCode, android.content.Intent data)

        unregisterMyReceiver();
        super.finishWithResult(resultCode, data);
    
public final voidgotReceive(java.lang.String name, android.content.Intent intent)

        synchronized (this) {

            //System.out.println("Got receive: " + name);
            //System.out.println(mNextReceiver + " in " + mExpectedReceivers);
            //new RuntimeException("stack").printStackTrace();

            addIntermediate(name);

            if (mExpectedData != null) {
                int n = mExpectedData.length;
                int i;
                boolean prev = false;
                for (i = 0; i < n; i++) {
                    if (mExpectedData[i].equals(intent.getStringExtra("test"))) {
                        if (mReceivedData[i]) {
                            prev = true;
                            continue;
                        }
                        mReceivedData[i] = true;
                        break;
                    }
                }
                if (i >= n) {
                    if (prev) {
                        finishBad("Receive got data too many times: "
                                + intent.getStringExtra("test"));
                    } else {
                        finishBad("Receive got unexpected data: "
                                + intent.getStringExtra("test"));
                    }
                    new RuntimeException("stack").printStackTrace();
                    return;
                }
            }

            if (mNextReceiver >= mExpectedReceivers.length) {
                finishBad("Got too many onReceiveIntent() calls!");
//                System.out.println("Too many intents received: now at "
//                        + mNextReceiver + ", expect list: "
//                        + Arrays.toString(mExpectedReceivers));
                fail("Got too many onReceiveIntent() calls!");
            } else if (!mExpectedReceivers[mNextReceiver].equals(name)) {
                finishBad("Receive out of order: got " + name
                        + " but expected "
                        + mExpectedReceivers[mNextReceiver]);
                fail("Receive out of order: got " + name
                        + " but expected "
                        + mExpectedReceivers[mNextReceiver]);
            } else {
                mNextReceiver++;
                if (mNextReceiver == mExpectedReceivers.length) {
                    finishTest();
                }
            }
        }
    
public android.content.IntentmakeBroadcastIntent(java.lang.String action)

        Intent intent = new Intent(action, null);
        intent.putExtra("caller", mCallTarget);
        return intent;
    
public voidonRegisteredReceiver(android.content.Intent intent)

        gotReceive(RECEIVER_REG, intent);
    
public voidonTimeout()

        String msg = "Timeout";
        if (mExpectedReceivers != null && mNextReceiver < mExpectedReceivers.length) {
            msg = msg + " waiting for " + mExpectedReceivers[mNextReceiver];
        }
        finishBad(msg);
    
public voidregisterMyReceiver(android.content.IntentFilter filter, java.lang.String permission)

        mReceiverRegistered = true;
        //System.out.println("Registering: " + mReceiver);
        getContext().registerReceiver(mReceiver, filter, permission, null);
    
public voidsetExpectedData(java.lang.String[] data)

        mExpectedData = data;
        mReceivedData = new boolean[data.length];
    
public voidsetExpectedReceivers(java.lang.String[] receivers)


        
        mExpectedReceivers = receivers;
        mNextReceiver = 0;
    
public voidtestAbort()

        runLaunchpad(LaunchpadActivity.BROADCAST_ABORT);
    
public voidtestAll()

        runLaunchpad(LaunchpadActivity.BROADCAST_ALL);
    
public voidtestClearSticky()

        Intent intent = new Intent(LaunchpadActivity.BROADCAST_STICKY1, null);
        intent.putExtra("test", LaunchpadActivity.DATA_1);
        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());

        ActivityManagerNative.getDefault().unbroadcastIntent(
                null, new Intent(LaunchpadActivity.BROADCAST_STICKY1, null),
                UserHandle.myUserId());
        addIntermediate("finished-unbroadcast");

        IntentFilter filter = new IntentFilter(LaunchpadActivity.BROADCAST_STICKY1);
        Intent sticky = getContext().registerReceiver(null, filter);
        assertNull("Sticky not found", sticky);
    
public voidtestLocal()

        runLaunchpad(LaunchpadActivity.BROADCAST_LOCAL);
    
public voidtestLocalBroadcastPermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_LOCAL),
                PERMISSION_DENIED, finish, null, Activity.RESULT_CANCELED,
                null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestLocalBroadcastPermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_LOCAL});
        getContext().sendBroadcast(
                makeBroadcastIntent(BROADCAST_LOCAL),
                PERMISSION_GRANTED);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestLocalReceivePermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_LOCAL_DENIED),
                null, finish, null, Activity.RESULT_CANCELED,
                null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestLocalReceivePermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_LOCAL});
        getContext().sendBroadcast(makeBroadcastIntent(BROADCAST_LOCAL_GRANTED));
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestLocalUnregisterTwice()

        registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), null);
        unregisterMyReceiverNoCheck();
        try {
            unregisterMyReceiverNoCheck();
            fail("No exception thrown on second unregister");
        } catch (IllegalArgumentException e) {
            Log.i("foo", "Unregister exception", e);
        }
    
public voidtestMulti()

        runLaunchpad(LaunchpadActivity.BROADCAST_MULTI);
    
public voidtestReceive2Sticky()

        Intent intent = new Intent(LaunchpadActivity.BROADCAST_STICKY1, null);
        intent.putExtra("test", LaunchpadActivity.DATA_1);
        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());
        intent = new Intent(LaunchpadActivity.BROADCAST_STICKY2, null);
        intent.putExtra("test", LaunchpadActivity.DATA_2);
        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());

        runLaunchpad(LaunchpadActivity.BROADCAST_STICKY2);
    
public voidtestReceiveSticky()

        Intent intent = new Intent(LaunchpadActivity.BROADCAST_STICKY1, null);
        intent.putExtra("test", LaunchpadActivity.DATA_1);
        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());

        runLaunchpad(LaunchpadActivity.BROADCAST_STICKY1);
    
public voidtestReceiverCanNotBind()

        setExpectedReceivers(new String[]{RECEIVER_LOCAL});
        getContext().sendBroadcast(makeBroadcastIntent(BROADCAST_FAIL_BIND));
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestReceiverCanNotRegister()

        setExpectedReceivers(new String[]{RECEIVER_LOCAL});
        getContext().sendBroadcast(makeBroadcastIntent(BROADCAST_FAIL_REGISTER));
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRegistered()


    // Mark flaky until http://b/issue?id=1191607 is resolved.
    
         
        runLaunchpad(LaunchpadActivity.BROADCAST_REGISTERED);
    
public voidtestRegisteredBroadcastPermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});
        registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), null);
        addIntermediate("after-register");

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_REGISTERED),
                PERMISSION_DENIED, finish, null, Activity.RESULT_CANCELED,
                null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRegisteredBroadcastPermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_REG});
        registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), null);
        addIntermediate("after-register");
        getContext().sendBroadcast(
                makeBroadcastIntent(BROADCAST_REGISTERED),
                PERMISSION_GRANTED);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRegisteredReceivePermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});
        registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), PERMISSION_DENIED);
        addIntermediate("after-register");

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_REGISTERED),
                null, finish, null, Activity.RESULT_CANCELED, null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRegisteredReceivePermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_REG});
        registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), PERMISSION_GRANTED);
        addIntermediate("after-register");
        getContext().sendBroadcast(makeBroadcastIntent(BROADCAST_REGISTERED));
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRemote()

        runLaunchpad(LaunchpadActivity.BROADCAST_REMOTE);
    
public voidtestRemoteBroadcastPermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_REMOTE),
                PERMISSION_DENIED, finish, null, Activity.RESULT_CANCELED,
                null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRemoteBroadcastPermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_REMOTE});
        getContext().sendBroadcast(
                makeBroadcastIntent(BROADCAST_REMOTE),
                PERMISSION_GRANTED);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRemoteReceivePermissionDenied()

        setExpectedReceivers(new String[]{RECEIVER_RESULTS});

        BroadcastReceiver finish = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                gotReceive(RECEIVER_RESULTS, intent);
            }
        };

        getContext().sendOrderedBroadcast(
                makeBroadcastIntent(BROADCAST_REMOTE_DENIED),
                null, finish, null, Activity.RESULT_CANCELED,
                null, null);
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestRemoteReceivePermissionGranted()

        setExpectedReceivers(new String[]{RECEIVER_REMOTE});
        getContext().sendBroadcast(makeBroadcastIntent(BROADCAST_REMOTE_GRANTED));
        waitForResultOrThrow(BROADCAST_TIMEOUT);
    
public voidtestReplaceSticky()

        Intent intent = new Intent(LaunchpadActivity.BROADCAST_STICKY1, null);
        intent.putExtra("test", LaunchpadActivity.DATA_1);
        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());
        intent.putExtra("test", LaunchpadActivity.DATA_2);

        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());
        addIntermediate("finished-broadcast");

        IntentFilter filter = new IntentFilter(LaunchpadActivity.BROADCAST_STICKY1);
        Intent sticky = getContext().registerReceiver(null, filter);
        assertNotNull("Sticky not found", sticky);
        assertEquals(LaunchpadActivity.DATA_2, sticky.getStringExtra("test"));
    
public voidtestResult()

        TestBroadcastReceiver broadcastReceiver = new TestBroadcastReceiver();

        synchronized (this) {
            Bundle map = new Bundle();
            map.putString("foo", "you");
            map.putString("remove", "me");
            getContext().sendOrderedBroadcast(
                    new Intent("com.android.frameworks.coretests.activity.BROADCAST_RESULT"),
                    null, broadcastReceiver, null, 1, "foo", map);
            while (!broadcastReceiver.mHaveResult) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }

            //System.out.println("Code: " + mResultCode + ", data: " + mResultData);
            //System.out.println("Extras: " + mResultExtras);

            assertEquals("Incorrect code: " + broadcastReceiver.getResultCode(),
                    3, broadcastReceiver.getResultCode());

            assertEquals("bar", broadcastReceiver.getResultData());

            Bundle resultExtras = broadcastReceiver.getResultExtras(false);
            assertEquals("them", resultExtras.getString("bar"));
            assertEquals("you", resultExtras.getString("foo"));
            assertNull(resultExtras.getString("remove"));
        }
    
public voidtestSetSticky()

        Intent intent = new Intent(LaunchpadActivity.BROADCAST_STICKY1, null);
        intent.putExtra("test", LaunchpadActivity.DATA_1);
        ActivityManagerNative.getDefault().unbroadcastIntent(null, intent,
                UserHandle.myUserId());

        ActivityManagerNative.broadcastStickyIntent(intent, null, UserHandle.myUserId());
        addIntermediate("finished-broadcast");

        IntentFilter filter = new IntentFilter(LaunchpadActivity.BROADCAST_STICKY1);
        Intent sticky = getContext().registerReceiver(null, filter);
        assertNotNull("Sticky not found", sticky);
        assertEquals(LaunchpadActivity.DATA_1, sticky.getStringExtra("test"));
    
public voidunregisterMyReceiver()

        if (mReceiverRegistered) {
            unregisterMyReceiverNoCheck();
        }
    
public voidunregisterMyReceiverNoCheck()

        mReceiverRegistered = false;
        //System.out.println("Unregistering: " + mReceiver);
        getContext().unregisterReceiver(mReceiver);