Search in sources :

Example 31 with LocalBroadcastManager

use of android.support.v4.content.LocalBroadcastManager in project Bolts-Android by BoltsFramework.

the class AppLinkTest method testAppLinkNavInEventBroadcast.

public void testAppLinkNavInEventBroadcast() throws Exception {
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    Bundle appLinkData = new Bundle();
    appLinkData.putString("target_url", "http://www.example2.com");
    Bundle appLinkRefererData = new Bundle();
    appLinkRefererData.putString("url", "referer://");
    appLinkRefererData.putString("app_name", "Referrer App");
    appLinkRefererData.putString("package", "com.bolts.referrer");
    appLinkData.putBundle("referer_app_link", appLinkRefererData);
    Bundle applinkExtras = new Bundle();
    applinkExtras.putString("token", "a_token");
    appLinkData.putBundle("extras", applinkExtras);
    i.putExtra("al_applink_data", appLinkData);
    final CountDownLatch lock = new CountDownLatch(1);
    final String[] receivedStrings = new String[7];
    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getInstrumentation().getTargetContext());
    manager.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String eventName = intent.getStringExtra("event_name");
            Bundle eventArgs = intent.getBundleExtra("event_args");
            receivedStrings[0] = eventName;
            receivedStrings[1] = eventArgs.getString("targetURL");
            receivedStrings[2] = eventArgs.getString("inputURL");
            receivedStrings[3] = eventArgs.getString("refererURL");
            receivedStrings[4] = eventArgs.getString("refererAppName");
            receivedStrings[5] = eventArgs.getString("extras/token");
            receivedStrings[6] = eventArgs.getString("sourceApplication");
            lock.countDown();
        }
    }, new IntentFilter("com.parse.bolts.measurement_event"));
    Uri targetUrl = AppLinks.getTargetUrlFromInboundIntent(getInstrumentation().getTargetContext(), i);
    lock.await(2000, TimeUnit.MILLISECONDS);
    assertEquals("al_nav_in", receivedStrings[0]);
    assertEquals("http://www.example2.com", receivedStrings[1]);
    assertEquals("http://www.example.com", receivedStrings[2]);
    assertEquals("referer://", receivedStrings[3]);
    assertEquals("Referrer App", receivedStrings[4]);
    assertEquals("a_token", receivedStrings[5]);
    assertEquals("com.bolts.referrer", receivedStrings[6]);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Bundle(android.os.Bundle) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch) BroadcastReceiver(android.content.BroadcastReceiver) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) Uri(android.net.Uri)

Example 32 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 33 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 34 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 35 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)

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