mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-04 03:06:41 -04:00
Merge pull request #1498 from kiwix/feature/macgills/#1496-storage-calc-crash
Feature/macgills/#1496 storage calc crash
This commit is contained in:
commit
52805818dd
@ -1,7 +0,0 @@
|
||||
package org.kiwix.kiwixmobile
|
||||
|
||||
import android.os.Build
|
||||
|
||||
object KiwixBuildConfig {
|
||||
val SDK_INT = Build.VERSION.SDK_INT
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
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 org.kiwix.kiwixmobile.KiwixBuildConfig
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
@ -25,7 +21,7 @@ import javax.inject.Inject
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* 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 =
|
||||
Bytes(availableBytes(file)).humanReadable
|
||||
@ -33,16 +29,9 @@ class StorageCalculator @Inject constructor(private val storageManager: StorageM
|
||||
fun calculateTotalSpace(file: File): String =
|
||||
Bytes(totalBytes(file)).humanReadable
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
fun availableBytes(file: File) =
|
||||
if (file.exists()) {
|
||||
if (KiwixBuildConfig.SDK_INT >= VERSION_CODES.O)
|
||||
storageManager.getAllocatableBytes(storageManager.getUuidForPath(file))
|
||||
else
|
||||
file.freeSpace
|
||||
} else {
|
||||
0L
|
||||
}
|
||||
if (file.exists()) file.freeSpace
|
||||
else 0L
|
||||
|
||||
private fun totalBytes(file: File) = if (file.exists()) file.totalSpace else 0L
|
||||
}
|
||||
|
@ -18,29 +18,19 @@ package org.kiwix.kiwixmobile.settings
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import android.os.storage.StorageManager
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kiwix.kiwixmobile.KiwixBuildConfig
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
|
||||
internal class StorageCalculatorTest {
|
||||
|
||||
private val storageManager: StorageManager = mockk()
|
||||
private val storageCalculator = StorageCalculator(storageManager)
|
||||
private val storageCalculator = StorageCalculator()
|
||||
private val file: File = mockk()
|
||||
|
||||
init {
|
||||
mockkObject(KiwixBuildConfig)
|
||||
}
|
||||
|
||||
@Test
|
||||
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")
|
||||
@ -59,16 +49,6 @@ internal class StorageCalculatorTest {
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user