DoNotDisturbUtilsTest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.nextcloud.talk.utils;
  2. import android.app.NotificationManager;
  3. import android.content.Context;
  4. import android.media.AudioManager;
  5. import android.os.Build;
  6. import android.os.Vibrator;
  7. import com.nextcloud.talk.application.NextcloudTalkApplication;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.mockito.Mock;
  12. import org.mockito.MockitoAnnotations;
  13. import org.powermock.api.mockito.PowerMockito;
  14. import org.powermock.core.classloader.annotations.PrepareForTest;
  15. import org.powermock.modules.junit4.PowerMockRunner;
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.Modifier;
  18. import static org.junit.Assert.*;
  19. import static org.mockito.Mockito.when;
  20. import static org.powermock.api.mockito.PowerMockito.mockStatic;
  21. @RunWith(PowerMockRunner.class)
  22. @PrepareForTest(NextcloudTalkApplication.class)
  23. public class DoNotDisturbUtilsTest {
  24. @Mock
  25. private Context context;
  26. @Mock
  27. private NextcloudTalkApplication application;
  28. @Mock
  29. private NotificationManager notificationManager;
  30. @Mock
  31. private AudioManager audioManager;
  32. @Mock
  33. private Vibrator vibrator;
  34. @Before
  35. public void setUp() {
  36. MockitoAnnotations.initMocks(this);
  37. mockStatic(NextcloudTalkApplication.class);
  38. PowerMockito.when(NextcloudTalkApplication.getSharedApplication()).thenReturn(application);
  39. when(application.getApplicationContext()).thenReturn(context);
  40. when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager);
  41. when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager);
  42. when(context.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(vibrator);
  43. }
  44. private static void setFinalStatic(Field field, Object newValue) throws Exception {
  45. field.setAccessible(true);
  46. Field modifiersField = Field.class.getDeclaredField("modifiers");
  47. modifiersField.setAccessible(true);
  48. modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  49. field.set(null, newValue);
  50. }
  51. @Test
  52. public void shouldPlaySound_givenAndroidMAndInterruptionFilterNone_assertReturnsFalse()
  53. throws Exception {
  54. setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.M);
  55. when(notificationManager.getCurrentInterruptionFilter()).thenReturn(NotificationManager.INTERRUPTION_FILTER_NONE);
  56. when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
  57. assertFalse("shouldPlaySound incorrectly returned true",
  58. DoNotDisturbUtils.shouldPlaySound());
  59. }
  60. @Test
  61. public void shouldPlaySound_givenRingerModeNotNormal_assertReturnsFalse() throws Exception {
  62. setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.LOLLIPOP);
  63. when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
  64. assertFalse("shouldPlaySound incorrectly returned true",
  65. DoNotDisturbUtils.shouldPlaySound());
  66. }
  67. @Test
  68. public void shouldVibrate_givenNoVibrator_assertReturnsFalse() {
  69. when(vibrator.hasVibrator()).thenReturn(false);
  70. assertFalse("shouldVibrate returned true despite no vibrator",
  71. DoNotDisturbUtils.shouldVibrate(true));
  72. }
  73. @Test
  74. public void shouldVibrate_givenVibratorAndRingerModeNormal_assertReturnsTrue() {
  75. when(vibrator.hasVibrator()).thenReturn(true);
  76. when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
  77. assertTrue("shouldVibrate incorrectly returned false",
  78. DoNotDisturbUtils.shouldVibrate(true));
  79. }
  80. @Test
  81. public void shouldVibrate_givenVibratorAndRingerModeSilent_assertReturnsFalse() {
  82. when(vibrator.hasVibrator()).thenReturn(true);
  83. when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_SILENT);
  84. assertFalse("shouldVibrate returned true despite ringer mode set to silent",
  85. DoNotDisturbUtils.shouldVibrate(true));
  86. }
  87. }