NotificationBadgeDecorator extends HaloNotificationDecorator { @Override public NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle) { String badge = bundle.getString("badge"); if (!TextUtils.isEmpty(badge)) { try { int badgeNumber = Integer.parseInt(badge); builder.setNumber(badgeNumber); } catch (NumberFormatException e) { Halog.e(getClass(), "The badge field on the notification is not a number"); } } return chain(builder, bundle); } NotificationBadgeDecorator(HaloNotificationDecorator decorator); @Override NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle);  }
@Test public void badgeTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().putString("badge", "1").build(); when(getBuilder().setNumber(any(Integer.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; Assert.assertEquals(1, invocation.getArguments()[0]); return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void emptyBadgeTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().build(); when(getBuilder().setNumber(any(Integer.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; Assert.assertEquals(1, invocation.getArguments()[0]); return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertFalse(marker[0]); }
@Test public void notNumericBadgeTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().putString("badge", "aaa").build(); when(getBuilder().setNumber(any(Integer.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]); }