Search in sources :

Example 1 with SafeIntent

use of mozilla.components.utils.SafeIntent in project focus-android by mozilla-mobile.

the class SessionTest method testCustomTabConfiguration.

@Test
public void testCustomTabConfiguration() {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(Color.GREEN);
    final Intent rawIntent = builder.build().intent;
    rawIntent.putExtra(CustomTabConfig.EXTRA_CUSTOM_TAB_ID, UUID.randomUUID().toString());
    final SafeIntent intent = new SafeIntent(rawIntent);
    final CustomTabConfig customTabConfig = CustomTabConfig.parseCustomTabIntent(RuntimeEnvironment.application, intent);
    final Session session = new Session(TEST_URL, customTabConfig);
    assertTrue(session.isCustomTab());
    assertNotNull(session.getCustomTabConfig());
    assertNotNull(session.getCustomTabConfig().toolbarColor);
    assertEquals(Color.GREEN, (int) session.getCustomTabConfig().toolbarColor);
}
Also used : CustomTabsIntent(android.support.customtabs.CustomTabsIntent) CustomTabConfig(org.mozilla.focus.customtabs.CustomTabConfig) SafeIntent(mozilla.components.utils.SafeIntent) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Intent(android.content.Intent) SafeIntent(mozilla.components.utils.SafeIntent) Test(org.junit.Test)

Example 2 with SafeIntent

use of mozilla.components.utils.SafeIntent in project focus-android by mozilla-mobile.

the class CustomTabConfigTest method isCustomTabIntent.

@Test
public void isCustomTabIntent() throws Exception {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent customTabsIntent = builder.build();
    assertTrue(CustomTabConfig.isCustomTabIntent(new SafeIntent(customTabsIntent.intent)));
}
Also used : CustomTabsIntent(android.support.customtabs.CustomTabsIntent) SafeIntent(mozilla.components.utils.SafeIntent) Test(org.junit.Test)

Example 3 with SafeIntent

use of mozilla.components.utils.SafeIntent in project focus-android by mozilla-mobile.

the class CustomTabConfigTest method malformedExtras.

@Test
public void malformedExtras() throws Exception {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.putExtra("garbage", new UnparcelableParcel());
    // We write the extras into a parcel so that we can check what happens when unparcelling fails
    final Parcel parcel = Parcel.obtain();
    final Bundle extras = customTabsIntent.intent.getExtras();
    extras.writeToParcel(parcel, 0);
    extras.clear();
    // Bundle is lazy and doesn't unparcel when calling readBundle()
    parcel.setDataPosition(0);
    final Bundle injectedBundle = parcel.readBundle();
    parcel.recycle();
    // We aren't usually allowed to overwrite an intent's extras:
    final Field extrasField = Intent.class.getDeclaredField("mExtras");
    extrasField.setAccessible(true);
    extrasField.set(customTabsIntent.intent, injectedBundle);
    extrasField.setAccessible(false);
    // And we can't access any extras now because unparcelling fails:
    assertFalse(CustomTabConfig.isCustomTabIntent(new SafeIntent(customTabsIntent.intent)));
    customTabsIntent.intent.putExtra(CustomTabConfig.EXTRA_CUSTOM_TAB_ID, UUID.randomUUID().toString());
    // Ensure we don't crash regardless
    final CustomTabConfig c = CustomTabConfig.parseCustomTabIntent(RuntimeEnvironment.application, new SafeIntent(customTabsIntent.intent));
    // And we don't have any data:
    assertNull(c.actionButtonConfig);
}
Also used : Field(java.lang.reflect.Field) CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Parcel(android.os.Parcel) Bundle(android.os.Bundle) SafeIntent(mozilla.components.utils.SafeIntent) Test(org.junit.Test)

Example 4 with SafeIntent

use of mozilla.components.utils.SafeIntent in project focus-android by mozilla-mobile.

the class CustomTabConfigTest method closeButton.

// Tests that a small icon is correctly processed
@Test
public void closeButton() throws Exception {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    {
        final Bitmap bitmap = Bitmap.createBitmap(new int[] { Color.RED }, 1, 1, Bitmap.Config.ARGB_8888);
        builder.setCloseButtonIcon(bitmap);
    }
    final CustomTabsIntent customTabsIntent = builder.build();
    final Bitmap bitmap = CustomTabConfig.getCloseButtonIcon(RuntimeEnvironment.application, new SafeIntent(customTabsIntent.intent));
    // An arbitrary icon
    assertEquals(1, bitmap.getWidth());
    assertEquals(1, bitmap.getHeight());
    assertEquals(Color.RED, bitmap.getPixel(0, 0));
}
Also used : CustomTabsIntent(android.support.customtabs.CustomTabsIntent) Bitmap(android.graphics.Bitmap) SafeIntent(mozilla.components.utils.SafeIntent) Test(org.junit.Test)

Example 5 with SafeIntent

use of mozilla.components.utils.SafeIntent in project focus-android by mozilla-mobile.

the class CustomTabConfigTest method menuTest.

@Test
public void menuTest() throws Exception {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final PendingIntent pendingIntent = PendingIntent.getActivity(null, 0, null, 0);
    builder.addMenuItem("menuitem1", pendingIntent);
    builder.addMenuItem("menuitem2", pendingIntent);
    // We can only handle menu items with an actual PendingIntent, other ones should be ignored:
    builder.addMenuItem("menuitemIGNORED", null);
    final CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.putExtra(CustomTabConfig.EXTRA_CUSTOM_TAB_ID, UUID.randomUUID().toString());
    final CustomTabConfig config = CustomTabConfig.parseCustomTabIntent(RuntimeEnvironment.application, new SafeIntent(customTabsIntent.intent));
    assertEquals("Menu should contain 2 items", 2, config.menuItems.size());
    final String s = config.menuItems.get(0).name;
    assertEquals("Unexpected menu item", "menuitem1", config.menuItems.get(0).name);
    assertEquals("Unexpected menu item", "menuitem2", config.menuItems.get(1).name);
}
Also used : CustomTabsIntent(android.support.customtabs.CustomTabsIntent) SafeIntent(mozilla.components.utils.SafeIntent) PendingIntent(android.app.PendingIntent) Test(org.junit.Test)

Aggregations

SafeIntent (mozilla.components.utils.SafeIntent)23 CustomTabsIntent (android.support.customtabs.CustomTabsIntent)20 Test (org.junit.Test)19 Intent (android.content.Intent)10 Bitmap (android.graphics.Bitmap)5 PendingIntent (android.app.PendingIntent)3 Bundle (android.os.Bundle)3 CustomTabConfig (org.mozilla.focus.customtabs.CustomTabConfig)2 Parcel (android.os.Parcel)1 RequiresApi (android.support.annotation.RequiresApi)1 Field (java.lang.reflect.Field)1