Utils { @SuppressWarnings("BooleanMethodIsAlwaysInverted") static boolean hasBackgroundLocationPermission(final Context context) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { return hasPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION); } else { return true; } } catch (Throwable t) { return false; } }    }
@Test public void testHasBackgroundLocationPermission() { Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.P); boolean actualWhenSdkIsP = Utils.hasBackgroundLocationPermission(application); assertTrue("hasBackgroundLocationPermission must return true when sdk int is less than Q", actualWhenSdkIsP); Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.Q); shadowApplication.denyPermissions(Manifest.permission.ACCESS_BACKGROUND_LOCATION); boolean actualWhenPermissionDenied = Utils.hasBackgroundLocationPermission(application); assertFalse("hasBackgroundLocationPermission must return false when permission " + "is denied and sdk int is greater than P", actualWhenPermissionDenied); }