NotificationSoundDecorator extends HaloNotificationDecorator { @Override public NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle) { String sound = bundle.getString("sound"); if (!TextUtils.isEmpty(sound)) { Uri soundUri = null; if ("default".equalsIgnoreCase(sound)) { soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } else { int soundId = mContext.getResources().getIdentifier(sound, "raw", mContext.getPackageName()); if (soundId != 0) { soundUri = HaloUtils.getUriFromResource(mContext, soundId); } } if (soundUri != null) { builder.setSound(soundUri); } } return chain(builder, bundle); } NotificationSoundDecorator(Context context, HaloNotificationDecorator decorator); @Override NotificationCompat.Builder decorate(@NonNull NotificationCompat.Builder builder, @NonNull Bundle bundle);  }
@Test public void defaultSoundTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().putString("sound", "default").build(); when(getBuilder().setSound(any(Uri.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; Assert.assertEquals(Uri.parse("content: return null; } }); getDecorator().decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void soundTest() { final boolean[] marker = new boolean[1]; final String ringtone = "adara"; Context context = mock(Context.class); Resources res = mock(Resources.class); NotificationSoundDecorator decorator = new NotificationSoundDecorator(context, null); when(context.getResources()).thenReturn(res); when(res.getIdentifier(ringtone, "raw", context.getPackageName())).thenReturn(1); when(res.getResourcePackageName(1)).thenReturn("com.mobgen.halo.android.sdk"); when(res.getResourceTypeName(1)).thenReturn("raw"); when(res.getResourceEntryName(1)).thenReturn(ringtone); Bundle bundle = BundleTestUtils.builder().putString("sound", ringtone).build(); when(getBuilder().setSound(any(Uri.class))).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { marker[0] = true; Assert.assertEquals(Uri.parse("android.resource: return null; } }); decorator.decorate(getBuilder(), bundle); Assert.assertTrue(marker[0]); }
@Test public void emptySoundTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().build(); when(getBuilder().setSound(any(Uri.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]); }
@Test public void invalidSoundTest() { final boolean[] marker = new boolean[1]; Bundle bundle = BundleTestUtils.builder().putString("sound", "fakeSound").build(); when(getBuilder().setSound(any(Uri.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]); }