1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Introduce NewPipeFileLocator class

This commit is contained in:
XiangRongLin 2020-12-19 15:16:22 +01:00
parent f778c48923
commit 8fceffd6fd
2 changed files with 9 additions and 14 deletions

View File

@ -46,9 +46,7 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
} }
fun extractDb( fun extractDb(filePath: String): Boolean {
filePath: String,
): Boolean {
val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db") val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db")
if (success) { if (success) {
fileLocator.dbJournal.delete() fileLocator.dbJournal.delete()
@ -59,16 +57,12 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
return success return success
} }
fun containSettings( fun containSettings(filePath: String): Boolean {
filePath: String,
): Boolean {
return ZipHelper return ZipHelper
.extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings") .extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings")
} }
fun loadSharedPreferences( fun loadSharedPreferences(preferences: SharedPreferences) {
preferences: SharedPreferences,
) {
try { try {
val preferenceEditor = preferences.edit() val preferenceEditor = preferences.edit()

View File

@ -19,14 +19,14 @@ import org.mockito.Mockito.atLeastOnce
import org.mockito.Mockito.verify import org.mockito.Mockito.verify
import org.mockito.Mockito.withSettings import org.mockito.Mockito.withSettings
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.schabi.newpipe.settings.ContentSettingsManagerTest.ExportTest import org.schabi.newpipe.settings.ContentSettingsManagerTest.*
import java.io.File import java.io.File
import java.io.ObjectInputStream import java.io.ObjectInputStream
import java.nio.file.Files import java.nio.file.Files
import java.util.zip.ZipFile import java.util.zip.ZipFile
@RunWith(Suite::class) @RunWith(Suite::class)
@Suite.SuiteClasses(ExportTest::class) @Suite.SuiteClasses(ExportTest::class, ImportTest::class)
class ContentSettingsManagerTest { class ContentSettingsManagerTest {
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
@ -73,23 +73,24 @@ class ContentSettingsManagerTest {
val zipFile = ZipFile(output.absoluteFile) val zipFile = ZipFile(output.absoluteFile)
val entries = zipFile.entries().toList() val entries = zipFile.entries().toList()
Assert.assertEquals(2, entries.size) assertEquals(2, entries.size)
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual -> zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual ->
newpipeDb.inputStream().use { expected -> newpipeDb.inputStream().use { expected ->
Assert.assertEquals(expected.reader().readText(), actual.reader().readText()) assertEquals(expected.reader().readText(), actual.reader().readText())
} }
} }
zipFile.getInputStream(entries.first { it.name == "newpipe.settings" }).use { actual -> zipFile.getInputStream(entries.first { it.name == "newpipe.settings" }).use { actual ->
val actualPreferences = ObjectInputStream(actual).readObject() val actualPreferences = ObjectInputStream(actual).readObject()
Assert.assertEquals(expectedPreferences, actualPreferences) assertEquals(expectedPreferences, actualPreferences)
} }
} }
} }
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
class ImportTest { class ImportTest {
companion object { companion object {
private lateinit var fileLocator: NewPipeFileLocator private lateinit var fileLocator: NewPipeFileLocator
private lateinit var zip: File private lateinit var zip: File