Merge pull request #1498 from kiwix/feature/macgills/#1496-storage-calc-crash

Feature/macgills/#1496 storage calc crash
This commit is contained in:
Seán Mac Gillicuddy 2019-09-23 09:38:02 +01:00 committed by GitHub
commit 52805818dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 42 deletions

View File

@ -1,7 +0,0 @@
package org.kiwix.kiwixmobile
import android.os.Build
object KiwixBuildConfig {
val SDK_INT = Build.VERSION.SDK_INT
}

View File

@ -1,10 +1,6 @@
package org.kiwix.kiwixmobile.settings package org.kiwix.kiwixmobile.settings
import android.annotation.SuppressLint
import android.os.Build.VERSION_CODES
import android.os.storage.StorageManager
import eu.mhutti1.utils.storage.Bytes import eu.mhutti1.utils.storage.Bytes
import org.kiwix.kiwixmobile.KiwixBuildConfig
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
@ -25,7 +21,7 @@ import javax.inject.Inject
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
class StorageCalculator @Inject constructor(private val storageManager: StorageManager) { class StorageCalculator @Inject constructor() {
fun calculateAvailableSpace(file: File): String = fun calculateAvailableSpace(file: File): String =
Bytes(availableBytes(file)).humanReadable Bytes(availableBytes(file)).humanReadable
@ -33,16 +29,9 @@ class StorageCalculator @Inject constructor(private val storageManager: StorageM
fun calculateTotalSpace(file: File): String = fun calculateTotalSpace(file: File): String =
Bytes(totalBytes(file)).humanReadable Bytes(totalBytes(file)).humanReadable
@SuppressLint("NewApi")
fun availableBytes(file: File) = fun availableBytes(file: File) =
if (file.exists()) { if (file.exists()) file.freeSpace
if (KiwixBuildConfig.SDK_INT >= VERSION_CODES.O) else 0L
storageManager.getAllocatableBytes(storageManager.getUuidForPath(file))
else
file.freeSpace
} else {
0L
}
private fun totalBytes(file: File) = if (file.exists()) file.totalSpace else 0L private fun totalBytes(file: File) = if (file.exists()) file.totalSpace else 0L
} }

View File

@ -18,29 +18,19 @@ package org.kiwix.kiwixmobile.settings
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import android.os.storage.StorageManager
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.mockkObject
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.KiwixBuildConfig
import java.io.File import java.io.File
import java.util.UUID
internal class StorageCalculatorTest { internal class StorageCalculatorTest {
private val storageManager: StorageManager = mockk() private val storageCalculator = StorageCalculator()
private val storageCalculator = StorageCalculator(storageManager)
private val file: File = mockk() private val file: File = mockk()
init {
mockkObject(KiwixBuildConfig)
}
@Test @Test
fun `calculate available space with existing file`() { fun `calculate available space with existing file`() {
every { KiwixBuildConfig.SDK_INT } returns 25
every { file.freeSpace } returns 1 every { file.freeSpace } returns 1
every { file.exists() } returns true every { file.exists() } returns true
assertThat(storageCalculator.calculateAvailableSpace(file)).isEqualTo("1 Bytes") assertThat(storageCalculator.calculateAvailableSpace(file)).isEqualTo("1 Bytes")
@ -59,16 +49,6 @@ internal class StorageCalculatorTest {
assertThat(storageCalculator.calculateTotalSpace(file)).isEqualTo("0 Bytes") 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 @Test
fun `available bytes of non existing file`() { fun `available bytes of non existing file`() {
every { file.exists() } returns false every { file.exists() } returns false