mirror of
https://github.com/nextcloud/talk-android
synced 2025-03-06 22:29:09 +00:00
DoNotDisturbUtils: refactor to avoid mocking static stuff
Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
parent
de86324d4e
commit
fd4b7080cc
@ -20,22 +20,33 @@
|
||||
|
||||
package com.nextcloud.talk.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.media.AudioManager
|
||||
import android.os.Build
|
||||
import android.os.Vibrator
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import com.nextcloud.talk.application.NextcloudTalkApplication
|
||||
|
||||
object DoNotDisturbUtils {
|
||||
fun shouldPlaySound(): Boolean {
|
||||
val context = NextcloudTalkApplication.sharedApplication?.applicationContext
|
||||
|
||||
private var buildVersion = Build.VERSION.SDK_INT
|
||||
|
||||
@VisibleForTesting
|
||||
fun setTestingBuildVersion(version: Int) {
|
||||
buildVersion = version
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@JvmOverloads
|
||||
fun shouldPlaySound(context: Context? = NextcloudTalkApplication.sharedApplication?.applicationContext): Boolean {
|
||||
|
||||
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
|
||||
var shouldPlaySound = true
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (buildVersion >= Build.VERSION_CODES.M) {
|
||||
if (notificationManager.currentInterruptionFilter != NotificationManager.INTERRUPTION_FILTER_ALL) {
|
||||
shouldPlaySound = false
|
||||
}
|
||||
@ -50,16 +61,18 @@ object DoNotDisturbUtils {
|
||||
return shouldPlaySound
|
||||
}
|
||||
|
||||
fun hasVibrator(): Boolean {
|
||||
val context = NextcloudTalkApplication.sharedApplication?.applicationContext
|
||||
private fun hasVibrator(context: Context?): Boolean {
|
||||
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
||||
return vibrator.hasVibrator()
|
||||
}
|
||||
|
||||
fun shouldVibrate(vibrate: Boolean): Boolean {
|
||||
@JvmOverloads
|
||||
fun shouldVibrate(
|
||||
context: Context? = NextcloudTalkApplication.sharedApplication?.applicationContext,
|
||||
vibrate: Boolean
|
||||
): Boolean {
|
||||
|
||||
if (hasVibrator()) {
|
||||
val context = NextcloudTalkApplication.sharedApplication?.applicationContext
|
||||
if (hasVibrator(context)) {
|
||||
val audioManager = context?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
|
||||
return if (vibrate) {
|
||||
|
@ -26,36 +26,20 @@ import android.media.AudioManager;
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
|
||||
import com.nextcloud.talk.application.NextcloudTalkApplication;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(NextcloudTalkApplication.class)
|
||||
public class DoNotDisturbUtilsTest {
|
||||
|
||||
@Mock
|
||||
private Context context;
|
||||
|
||||
@Mock
|
||||
private NextcloudTalkApplication application;
|
||||
|
||||
@Mock
|
||||
private NotificationManager notificationManager;
|
||||
|
||||
@ -68,46 +52,30 @@ public class DoNotDisturbUtilsTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
mockStatic(NextcloudTalkApplication.Companion.class);
|
||||
NextcloudTalkApplication.Companion companionMock = PowerMockito.mock(NextcloudTalkApplication.Companion.class);
|
||||
Whitebox.setInternalState(NextcloudTalkApplication.class,"Companion",companionMock);
|
||||
PowerMockito.when(NextcloudTalkApplication.Companion.getSharedApplication()).thenReturn(application);
|
||||
when(application.getApplicationContext()).thenReturn(context);
|
||||
when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager);
|
||||
when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager);
|
||||
when(context.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(vibrator);
|
||||
}
|
||||
|
||||
private static void setFinalStatic(Field field, Object newValue) throws Exception {
|
||||
field.setAccessible(true);
|
||||
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
||||
|
||||
field.set(null, newValue);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldPlaySound_givenAndroidMAndInterruptionFilterNone_assertReturnsFalse()
|
||||
throws Exception {
|
||||
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.M);
|
||||
public void shouldPlaySound_givenAndroidMAndInterruptionFilterNone_assertReturnsFalse() {
|
||||
DoNotDisturbUtils.INSTANCE.setTestingBuildVersion(Build.VERSION_CODES.M);
|
||||
|
||||
when(notificationManager.getCurrentInterruptionFilter()).thenReturn(NotificationManager.INTERRUPTION_FILTER_NONE);
|
||||
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
|
||||
|
||||
assertFalse("shouldPlaySound incorrectly returned true",
|
||||
DoNotDisturbUtils.INSTANCE.shouldPlaySound());
|
||||
DoNotDisturbUtils.INSTANCE.shouldPlaySound(context));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPlaySound_givenRingerModeNotNormal_assertReturnsFalse() throws Exception {
|
||||
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.LOLLIPOP);
|
||||
DoNotDisturbUtils.INSTANCE.setTestingBuildVersion(Build.VERSION_CODES.LOLLIPOP);
|
||||
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
|
||||
|
||||
assertFalse("shouldPlaySound incorrectly returned true",
|
||||
DoNotDisturbUtils.INSTANCE.shouldPlaySound());
|
||||
DoNotDisturbUtils.INSTANCE.shouldPlaySound(context));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -115,7 +83,7 @@ public class DoNotDisturbUtilsTest {
|
||||
when(vibrator.hasVibrator()).thenReturn(false);
|
||||
|
||||
assertFalse("shouldVibrate returned true despite no vibrator",
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(true));
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -125,7 +93,7 @@ public class DoNotDisturbUtilsTest {
|
||||
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
|
||||
|
||||
assertTrue("shouldVibrate incorrectly returned false",
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(true));
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -135,6 +103,6 @@ public class DoNotDisturbUtilsTest {
|
||||
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_SILENT);
|
||||
|
||||
assertFalse("shouldVibrate returned true despite ringer mode set to silent",
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(true));
|
||||
DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user