PercentageBasedRemainingStorageRequirementRule implements StorageRequirementRule { @SuppressLint("UsableSpace") @Override public boolean hasViolatedRule(File storageDirectory, FileSize downloadFileSize) { long storageCapacityInBytes = storageCapacityReader.storageCapacityInBytes(storageDirectory.getPath()); long minimumStorageRequiredInBytes = (long) (storageCapacityInBytes * percentageOfStorageRemaining); long usableStorageInBytes = storageDirectory.getUsableSpace(); long remainingStorageAfterDownloadInBytes = usableStorageInBytes - downloadFileSize.remainingSize(); Logger.v("Storage capacity in bytes: ", storageCapacityInBytes); Logger.v("Usable storage in bytes: ", usableStorageInBytes); Logger.v("Minimum required storage in bytes: ", minimumStorageRequiredInBytes); return remainingStorageAfterDownloadInBytes < minimumStorageRequiredInBytes; } PercentageBasedRemainingStorageRequirementRule(StorageCapacityReader storageCapacityReader,
                                                   @FloatRange(from = 0.0, to = 0.5) float percentageOfStorageRemaining); @SuppressLint("UsableSpace") @Override boolean hasViolatedRule(File storageDirectory,
                                   FileSize downloadFileSize);  }
@Test public void doesNotViolateRule_whenRemainingFileSizeIsLessThanRestriction() { given(fileSize.remainingSize()).willReturn(REMAINING_ONE_HUNDRED_MB_IN_BYTES); boolean hasViolatedRule = storageRequirementRule.hasViolatedRule(file, fileSize); assertThat(hasViolatedRule).isFalse(); }
@Test public void violatesRule_whenRemainingFileSizeIsGreaterThanRestriction() { given(fileSize.remainingSize()).willReturn(REMAINING_OVER_ONE_HUNDRED_MB_IN_BYTES); boolean hasViolatedRule = storageRequirementRule.hasViolatedRule(file, fileSize); assertThat(hasViolatedRule).isTrue(); }