VibrationHelper { public static boolean hasVibrationPermission(Context context) { return (context.getPackageManager().checkPermission(Manifest.permission.VIBRATE, context.getPackageName()) == PackageManager.PERMISSION_GRANTED); }  @SuppressLint("MissingPermission") static void vibrate(Context context, int duration); static boolean hasVibrationPermission(Context context);  }
@Test public void hasVibratePermission_returnsTrueIfVibratePermissionInManifest() { PackageManager packageManager = mock(PackageManager.class); when(packageManager.checkPermission(eq(Manifest.permission.VIBRATE), anyString())) .thenReturn(PackageManager.PERMISSION_GRANTED); Context context = mock(Context.class); when(context.getPackageManager()).thenReturn(packageManager); assertTrue(VibrationHelper.hasVibrationPermission(context)); }
@Test public void hasVibratePermission_returnsFalseIfVibratePermissionNotInManifest() { PackageManager packageManager = mock(PackageManager.class); when(packageManager.checkPermission(eq(Manifest.permission.VIBRATE), anyString())) .thenReturn(PackageManager.PERMISSION_DENIED); Context context = mock(Context.class); when(context.getPackageManager()).thenReturn(packageManager); when(context.getPackageName()).thenReturn("com.braintreepayments.cardform.test"); assertFalse(VibrationHelper.hasVibrationPermission(context)); }