Search in sources :

Example 26 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project robolectric by robolectric.

the class ShadowLocalBroadcastManagerTest method shouldSendBroadcastsWithDataScheme.

@Test
public void shouldSendBroadcastsWithDataScheme() throws Exception {
    LocalBroadcastManager instance = LocalBroadcastManager.getInstance(RuntimeEnvironment.application);
    final boolean[] called = new boolean[1];
    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            called[0] = true;
        }
    };
    IntentFilter intentFilter = new IntentFilter("com.foo");
    intentFilter.addDataScheme("http");
    instance.registerReceiver(receiver, intentFilter);
    instance.sendBroadcast(new Intent("com.foo", Uri.parse("ftp://robolectric.org")));
    assertFalse(called[0]);
    instance.sendBroadcast(new Intent("com.foo", Uri.parse("http://robolectric.org")));
    assertTrue(called[0]);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Test(org.junit.Test)

Example 27 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project robolectric by robolectric.

the class ShadowLocalBroadcastManagerTest method shouldUnregisterReceiver.

@Test
public void shouldUnregisterReceiver() throws Exception {
    LocalBroadcastManager instance = LocalBroadcastManager.getInstance(RuntimeEnvironment.application);
    final boolean[] called = new boolean[1];
    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            called[0] = true;
        }
    };
    instance.registerReceiver(receiver, new IntentFilter("com.foo"));
    instance.unregisterReceiver(receiver);
    instance.sendBroadcast(new Intent("com.foo"));
    assertFalse(called[0]);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Test(org.junit.Test)

Example 28 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project robolectric by robolectric.

the class ShadowLocalBroadcastManagerTest method testGetSentBroadcastIntents.

@Test
public void testGetSentBroadcastIntents() throws Exception {
    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(RuntimeEnvironment.application);
    ShadowLocalBroadcastManager shadowLocalBroadcastManager = shadowOf(broadcastManager);
    assertEquals(0, shadowLocalBroadcastManager.getSentBroadcastIntents().size());
    Intent broadcastIntent = new Intent("foo");
    broadcastManager.sendBroadcast(broadcastIntent);
    assertEquals(1, shadowLocalBroadcastManager.getSentBroadcastIntents().size());
    assertEquals(broadcastIntent, shadowLocalBroadcastManager.getSentBroadcastIntents().get(0));
}
Also used : Intent(android.content.Intent) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Test(org.junit.Test)

Example 29 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project robolectric by robolectric.

the class ShadowLocalBroadcastManagerTest method testGetBroadcastIntents.

@Test
public void testGetBroadcastIntents() throws Exception {
    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(RuntimeEnvironment.application);
    broadcastManager.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            transcript.add("got intent " + intent.getAction());
        }
    }, IntentFilter.create("foo", "application/blatz"));
    Intent intent1 = new Intent("foo").setType("application/blatz");
    broadcastManager.sendBroadcast(intent1);
    Intent intent2 = new Intent("bar").setType("application/blatz");
    broadcastManager.sendBroadcast(intent2);
    assertThat(transcript).containsExactly("got intent foo");
}
Also used : Context(android.content.Context) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Test(org.junit.Test)

Example 30 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project robolectric by robolectric.

the class ShadowLocalBroadcastManagerTest method testGetRegisteredBroadcastReceivers.

@Test
public void testGetRegisteredBroadcastReceivers() throws Exception {
    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(RuntimeEnvironment.application);
    ShadowLocalBroadcastManager shadowLocalBroadcastManager = shadowOf(broadcastManager);
    assertEquals(0, shadowLocalBroadcastManager.getRegisteredBroadcastReceivers().size());
    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
        }
    };
    IntentFilter filter = new IntentFilter("foo");
    broadcastManager.registerReceiver(receiver, filter);
    assertEquals(1, shadowLocalBroadcastManager.getRegisteredBroadcastReceivers().size());
    ShadowLocalBroadcastManager.Wrapper capturedWrapper = shadowLocalBroadcastManager.getRegisteredBroadcastReceivers().get(0);
    assertEquals(receiver, capturedWrapper.broadcastReceiver);
    assertEquals(filter, capturedWrapper.intentFilter);
    broadcastManager.unregisterReceiver(receiver);
    assertEquals(0, shadowLocalBroadcastManager.getRegisteredBroadcastReceivers().size());
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Test(org.junit.Test)

Aggregations

LocalBroadcastManager (android.support.v4.content.LocalBroadcastManager)36 IntentFilter (android.content.IntentFilter)15 Intent (android.content.Intent)11 Test (org.junit.Test)10 Context (android.content.Context)9 BroadcastReceiver (android.content.BroadcastReceiver)7 Bundle (android.os.Bundle)3 AccessToken (com.facebook.AccessToken)2 WaitForBroadcastReceiver (com.facebook.WaitForBroadcastReceiver)2 AppEventsLogger (com.facebook.appevents.AppEventsLogger)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Uri (android.net.Uri)1 Parcel (android.os.Parcel)1 Parcelable (android.os.Parcelable)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 RecipeList (com.se491.chef_ly.model.RecipeList)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1