NotificationImageDecorator extends HaloNotificationDecorator { @Override public NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle) { if (bundle.get(IMAGE_KEY) != null) { try { PushImage pushImage = PushImage.deserialize(bundle.get(IMAGE_KEY).toString(), Halo.instance().framework().parser()); Bitmap bitmapNotification = getImageBitmap(pushImage.getUrl()); if (bitmapNotification != null) { String title = bundle.getString("title"); String message = bundle.getString("body"); RemoteViews remoteView, remoteViewExpanded; switch (pushImage.getLayout()) { case PushImage.DEFAULT: builder.setLargeIcon(bitmapNotification); break; case PushImage.EXPANDED: builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmapNotification)); break; case PushImage.LEFT: remoteView = getRemoteView(pushImage.getLayout(), bitmapNotification, title, message); builder.setCustomContentView(remoteView); break; case PushImage.RIGHT: remoteView = getRemoteView(pushImage.getLayout(), bitmapNotification, title, message); builder.setCustomContentView(remoteView); break; case PushImage.TOP: remoteView = getRemoteView(PushImage.LEFT, bitmapNotification, title, message); builder.setCustomContentView(remoteView); remoteViewExpanded = getRemoteView(pushImage.getLayout(), bitmapNotification, title, message); builder.setCustomBigContentView(remoteViewExpanded); break; case PushImage.BOTTOM: remoteView = getRemoteView(PushImage.RIGHT, bitmapNotification, title, message); builder.setCustomContentView(remoteView); remoteViewExpanded = getRemoteView(pushImage.getLayout(), bitmapNotification, title, message); builder.setCustomBigContentView(remoteViewExpanded); break; case PushImage.BACKGROUND: remoteView = getRemoteView(pushImage.getLayout(), bitmapNotification, title, message); builder.setCustomContentView(remoteView); builder.setCustomBigContentView(remoteView); break; } } } catch (HaloParsingException e) { Halog.v(NotificationImageDecorator.class, "Cannot parse the content"); } } return chain(builder, bundle); } NotificationImageDecorator(Context context, HaloNotificationDecorator decorator); @Override NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle); @VisibleForTesting void setIntent(Intent intent);  }
@Test public void thatCanHandleAImageOnTheNotificationWithDefaultType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "default"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setLargeIcon(any(Bitmap.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithExpandedType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "expanded"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setStyle(any(NotificationCompat.Style.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithBackgroundType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "background"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setCustomContentView(any(RemoteViews.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithLeftType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "left"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setCustomContentView(any(RemoteViews.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithRightType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "right"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setCustomContentView(any(RemoteViews.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithTopType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "top"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setCustomContentView(any(RemoteViews.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleAImageOnTheNotificationWithBottomType() throws IOException { final boolean[] marker = new boolean[1]; final String text = "This is my Text And Body"; final String image = givenAImageNotification(mHalo.context(), "bottom"); Bundle bundle = BundleTestUtils.builder().putString("body", text).putString("title", text).build(); when(bundle.get("image")).thenReturn(image); when(bundle.get("image").toString()).thenReturn(image); when(getBuilder().setCustomContentView(any(RemoteViews.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void thatCanHandleANullImage() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().build(); when(getBuilder().setContentTitle(any(String.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertFalse(marker[0]); }