mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 08:30:44 +00:00
Remove subclasses from ContentSettingsManagerTest
ExportTest provides no value. ImportTest creates temporary files even if not needed.
This commit is contained in:
parent
8fceffd6fd
commit
122e80fae9
@ -32,14 +32,9 @@ import org.schabi.newpipe.util.FilePickerActivityHelper;
|
||||
import org.schabi.newpipe.util.ZipHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
||||
|
||||
@ -241,7 +236,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
||||
}
|
||||
|
||||
//If settings file exist, ask if it should be imported.
|
||||
if (manager.containSettings(filePath)) {
|
||||
if (manager.extractSettings(filePath)) {
|
||||
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||
alert.setTitle(R.string.import_settings);
|
||||
|
||||
|
@ -57,7 +57,7 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
|
||||
return success
|
||||
}
|
||||
|
||||
fun containSettings(filePath: String): Boolean {
|
||||
fun extractSettings(filePath: String): Boolean {
|
||||
return ZipHelper
|
||||
.extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings")
|
||||
}
|
||||
|
@ -1,82 +1,60 @@
|
||||
package org.schabi.newpipe.settings
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import org.junit.Assert
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume
|
||||
import org.junit.Before
|
||||
import org.junit.BeforeClass
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.Suite
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.Mockito.anyString
|
||||
import org.mockito.Mockito.anyBoolean
|
||||
import org.mockito.Mockito.anyInt
|
||||
import org.mockito.Mockito.atLeastOnce
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.Mockito.withSettings
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.schabi.newpipe.settings.ContentSettingsManagerTest.*
|
||||
import java.io.File
|
||||
import java.io.ObjectInputStream
|
||||
import java.nio.file.Files
|
||||
import java.util.zip.ZipFile
|
||||
|
||||
@RunWith(Suite::class)
|
||||
@Suite.SuiteClasses(ExportTest::class, ImportTest::class)
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ContentSettingsManagerTest {
|
||||
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ExportTest {
|
||||
|
||||
companion object {
|
||||
private val classloader = ContentSettingsManager::class.java.classLoader!!
|
||||
}
|
||||
|
||||
private lateinit var fileLocator: NewPipeFileLocator
|
||||
private lateinit var newpipeDb: File
|
||||
private lateinit var newpipeSettings: File
|
||||
|
||||
@JvmStatic
|
||||
@BeforeClass
|
||||
fun setupFiles() {
|
||||
val dbPath = ExportTest::class.java.classLoader?.getResource("settings/newpipe.db")?.file
|
||||
val settingsPath = ExportTest::class.java.classLoader?.getResource("settings/newpipe.settings")?.path
|
||||
Assume.assumeNotNull(dbPath)
|
||||
Assume.assumeNotNull(settingsPath)
|
||||
|
||||
newpipeDb = File(dbPath!!)
|
||||
newpipeSettings = File(settingsPath!!)
|
||||
|
||||
fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
|
||||
`when`(fileLocator.db).thenReturn(newpipeDb)
|
||||
`when`(fileLocator.settings).thenReturn(newpipeSettings)
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var preferences: SharedPreferences
|
||||
|
||||
@Before
|
||||
fun setupMocks() {
|
||||
preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
|
||||
fun setupFileLocator() {
|
||||
fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `The settings must be exported successfully in the correct format`() {
|
||||
val expectedPreferences = mapOf("such pref" to "much wow")
|
||||
`when`(preferences.all).thenReturn(expectedPreferences)
|
||||
val db = File(classloader.getResource("settings/newpipe.db")!!.file)
|
||||
val newpipeSettings = File.createTempFile("newpipe_", "")
|
||||
`when`(fileLocator.db).thenReturn(db)
|
||||
`when`(fileLocator.settings).thenReturn(newpipeSettings)
|
||||
|
||||
val manager = ContentSettingsManager(fileLocator)
|
||||
val expectedPreferences = mapOf("such pref" to "much wow")
|
||||
val sharedPreferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
|
||||
`when`(sharedPreferences.all).thenReturn(expectedPreferences)
|
||||
|
||||
val output = File.createTempFile("newpipe_", "")
|
||||
manager.exportDatabase(preferences, output.absolutePath)
|
||||
ContentSettingsManager(fileLocator).exportDatabase(sharedPreferences, output.absolutePath)
|
||||
|
||||
val zipFile = ZipFile(output.absoluteFile)
|
||||
val zipFile = ZipFile(output)
|
||||
val entries = zipFile.entries().toList()
|
||||
assertEquals(2, entries.size)
|
||||
|
||||
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual ->
|
||||
newpipeDb.inputStream().use { expected ->
|
||||
db.inputStream().use { expected ->
|
||||
assertEquals(expected.reader().readText(), actual.reader().readText())
|
||||
}
|
||||
}
|
||||
@ -86,52 +64,19 @@ class ContentSettingsManagerTest {
|
||||
assertEquals(expectedPreferences, actualPreferences)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ImportTest {
|
||||
|
||||
companion object {
|
||||
private lateinit var fileLocator: NewPipeFileLocator
|
||||
private lateinit var zip: File
|
||||
private lateinit var emptyZip: File
|
||||
private lateinit var db: File
|
||||
private lateinit var dbJournal: File
|
||||
private lateinit var dbWal: File
|
||||
private lateinit var dbShm: File
|
||||
private lateinit var settings: File
|
||||
|
||||
@JvmStatic
|
||||
@BeforeClass
|
||||
fun setupReadOnlyFiles() {
|
||||
val zipPath = ImportTest::class.java.classLoader?.getResource("settings/newpipe.zip")?.file
|
||||
val emptyZipPath = ImportTest::class.java.classLoader?.getResource("settings/empty.zip")?.file
|
||||
Assume.assumeNotNull(zipPath)
|
||||
Assume.assumeNotNull(emptyZipPath)
|
||||
|
||||
zip = File(zipPath!!)
|
||||
emptyZip = File(emptyZipPath!!)
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
fun setupWriteFiles() {
|
||||
db = File.createTempFile("newpipe_", "")
|
||||
dbJournal = File.createTempFile("newpipe_", "")
|
||||
dbWal = File.createTempFile("newpipe_", "")
|
||||
dbShm = File.createTempFile("newpipe_", "")
|
||||
settings = File.createTempFile("newpipe_", "")
|
||||
|
||||
fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
|
||||
@Test
|
||||
fun `The database must be extracted from the zip file`() {
|
||||
val db = File.createTempFile("newpipe_", "")
|
||||
val dbJournal = File.createTempFile("newpipe_", "")
|
||||
val dbWal = File.createTempFile("newpipe_", "")
|
||||
val dbShm = File.createTempFile("newpipe_", "")
|
||||
`when`(fileLocator.db).thenReturn(db)
|
||||
`when`(fileLocator.dbJournal).thenReturn(dbJournal)
|
||||
`when`(fileLocator.dbShm).thenReturn(dbShm)
|
||||
`when`(fileLocator.dbWal).thenReturn(dbWal)
|
||||
`when`(fileLocator.settings).thenReturn(settings)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `The database must be extracted from the zip file`() {
|
||||
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
||||
val success = ContentSettingsManager(fileLocator).extractDb(zip.path)
|
||||
|
||||
assertTrue(success)
|
||||
@ -143,6 +88,13 @@ class ContentSettingsManagerTest {
|
||||
|
||||
@Test
|
||||
fun `Extracting the database from an empty zip must not work`() {
|
||||
val db = File.createTempFile("newpipe_", "")
|
||||
val dbJournal = File.createTempFile("newpipe_", "")
|
||||
val dbWal = File.createTempFile("newpipe_", "")
|
||||
val dbShm = File.createTempFile("newpipe_", "")
|
||||
`when`(fileLocator.db).thenReturn(db)
|
||||
|
||||
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
||||
val success = ContentSettingsManager(fileLocator).extractDb(emptyZip.path)
|
||||
|
||||
assertFalse(success)
|
||||
@ -153,35 +105,40 @@ class ContentSettingsManagerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Contain setting must return true, if a settings file exists in the zip`() {
|
||||
val contains = ContentSettingsManager(fileLocator).containSettings(zip.path)
|
||||
fun `Contains setting must return true if a settings file exists in the zip`() {
|
||||
val settings = File.createTempFile("newpipe_", "")
|
||||
`when`(fileLocator.settings).thenReturn(settings)
|
||||
|
||||
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
||||
val contains = ContentSettingsManager(fileLocator).extractSettings(zip.path)
|
||||
|
||||
assertTrue(contains)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Contain setting must return false, if a no settings file exists in the zip`() {
|
||||
val contains = ContentSettingsManager(fileLocator).containSettings(emptyZip.path)
|
||||
fun `Contains setting must return false if a no settings file exists in the zip`() {
|
||||
val settings = File.createTempFile("newpipe_", "")
|
||||
`when`(fileLocator.settings).thenReturn(settings)
|
||||
|
||||
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
||||
val contains = ContentSettingsManager(fileLocator).extractSettings(emptyZip.path)
|
||||
|
||||
assertFalse(contains)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Preferences must be set from the settings file`() {
|
||||
val settings = File(classloader.getResource("settings/newpipe.settings")!!.path)
|
||||
`when`(fileLocator.settings).thenReturn(settings)
|
||||
|
||||
val preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
|
||||
val editor = Mockito.mock(SharedPreferences.Editor::class.java)
|
||||
`when`(preferences.edit()).thenReturn(editor)
|
||||
|
||||
|
||||
val manager = ContentSettingsManager(fileLocator)
|
||||
manager.containSettings(zip.path)
|
||||
manager.loadSharedPreferences(preferences)
|
||||
ContentSettingsManager(fileLocator).loadSharedPreferences(preferences)
|
||||
|
||||
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
|
||||
verify(editor, atLeastOnce()).putString(anyString(), anyString())
|
||||
verify(editor, atLeastOnce()).putInt(anyString(), anyInt())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user