Add test zips and extensive tests for ImportExportManager

Now all possible combinations of files in the zip (present or not) are checked at the same time
This commit is contained in:
Stypox 2024-03-30 18:42:11 +01:00
parent d8423499dc
commit 8e192acb63
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
18 changed files with 211 additions and 13 deletions

View file

@ -9,6 +9,7 @@ import com.grack.nanojson.JsonWriter
import org.schabi.newpipe.streams.io.SharpOutputStream
import org.schabi.newpipe.streams.io.StoredFileHelper
import org.schabi.newpipe.util.ZipHelper
import java.io.FileNotFoundException
import java.io.IOException
import java.io.ObjectOutputStream
import java.util.zip.ZipOutputStream
@ -110,10 +111,12 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
fun loadSerializedPrefs(zipFile: StoredFileHelper, preferences: SharedPreferences) {
ZipHelper.extractFileFromZip(zipFile, BackupFileLocator.FILE_NAME_SERIALIZED_PREFS) {
PreferencesObjectInputStream(it).use { input ->
val editor = preferences.edit()
editor.clear()
@Suppress("UNCHECKED_CAST")
val entries = input.readObject() as Map<String, *>
val editor = preferences.edit()
editor.clear()
for ((key, value) in entries) {
when (value) {
is Boolean -> editor.putBoolean(key, value)
@ -133,19 +136,24 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
Log.e(TAG, "Unable to loadSerializedPrefs")
}
}
}.let { fileExists ->
if (!fileExists) {
throw FileNotFoundException(BackupFileLocator.FILE_NAME_SERIALIZED_PREFS)
}
}
}
/**
* Remove all shared preferences from the app and load the preferences supplied to the manager.
*/
@Throws(JsonParserException::class)
@Throws(IOException::class, JsonParserException::class)
fun loadJsonPrefs(zipFile: StoredFileHelper, preferences: SharedPreferences) {
ZipHelper.extractFileFromZip(zipFile, BackupFileLocator.FILE_NAME_JSON_PREFS) {
val jsonObject = JsonParser.`object`().from(it)
val editor = preferences.edit()
editor.clear()
val jsonObject = JsonParser.`object`().from(it)
for ((key, value) in jsonObject) {
when (value) {
is Boolean -> editor.putBoolean(key, value)
@ -162,6 +170,10 @@ class ImportExportManager(private val fileLocator: BackupFileLocator) {
if (!editor.commit()) {
Log.e(TAG, "Unable to loadJsonPrefs")
}
}.let { fileExists ->
if (!fileExists) {
throw FileNotFoundException(BackupFileLocator.FILE_NAME_JSON_PREFS)
}
}
}
}