From 930b0fbfe41ddd9c9f833a2a3bb345a8ad93580b Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Sep 2019 11:17:31 +0100 Subject: [PATCH] #1473 fix crash in storageCalculator --- .../kiwixmobile/settings/StorageCalculator.kt | 14 ++++++++----- .../settings/StorageCalculatorTest.kt | 21 ++++++++++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/settings/StorageCalculator.kt b/app/src/main/java/org/kiwix/kiwixmobile/settings/StorageCalculator.kt index b275e08af..3c5952af5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/settings/StorageCalculator.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/settings/StorageCalculator.kt @@ -35,10 +35,14 @@ class StorageCalculator @Inject constructor(private val storageManager: StorageM @SuppressLint("NewApi") fun availableBytes(file: File) = - if (KiwixBuildConfig.SDK_INT >= VERSION_CODES.O) - storageManager.getAllocatableBytes(storageManager.getUuidForPath(file)) - else - file.freeSpace + if (file.exists()) { + if (KiwixBuildConfig.SDK_INT >= VERSION_CODES.O) + storageManager.getAllocatableBytes(storageManager.getUuidForPath(file)) + else + file.freeSpace + } else { + 0L + } - private fun totalBytes(file: File) = file.totalSpace + private fun totalBytes(file: File) = if (file.exists()) file.totalSpace else 0L } diff --git a/app/src/test/java/org/kiwix/kiwixmobile/settings/StorageCalculatorTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/settings/StorageCalculatorTest.kt index fdff055dc..0c86241a8 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/settings/StorageCalculatorTest.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/settings/StorageCalculatorTest.kt @@ -39,24 +39,39 @@ internal class StorageCalculatorTest { } @Test - fun calculateAvailableSpace() { + fun `calculate available space with existing file`() { every { KiwixBuildConfig.SDK_INT } returns 25 every { file.freeSpace } returns 1 + every { file.exists() } returns true assertThat(storageCalculator.calculateAvailableSpace(file)).isEqualTo("1 Bytes") } @Test - fun calculateTotalSpace() { + fun `calculate total space of existing file`() { every { file.totalSpace } returns 1 + every { file.exists() } returns true assertThat(storageCalculator.calculateTotalSpace(file)).isEqualTo("1 Bytes") } @Test - fun availableBytes() { + fun `calculate total space of non existing file`() { + every { file.exists() } returns false + assertThat(storageCalculator.calculateTotalSpace(file)).isEqualTo("0 Bytes") + } + + @Test + fun `available bytes of existing file API 26`() { val uuid: UUID = mockk() every { KiwixBuildConfig.SDK_INT } returns 26 every { storageManager.getUuidForPath(file) } returns uuid every { storageManager.getAllocatableBytes(uuid) } returns 1 + every { file.exists() } returns true assertThat(storageCalculator.availableBytes(file)).isEqualTo(1L) } + + @Test + fun `available bytes of non existing file`() { + every { file.exists() } returns false + assertThat(storageCalculator.availableBytes(file)).isEqualTo(0L) + } }