1
0
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:
XiangRongLin 2020-12-19 16:28:10 +01:00
parent 8fceffd6fd
commit 122e80fae9
4 changed files with 106 additions and 154 deletions

View File

@ -32,14 +32,9 @@ import org.schabi.newpipe.util.FilePickerActivityHelper;
import org.schabi.newpipe.util.ZipHelper; import org.schabi.newpipe.util.ZipHelper;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipFile;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; 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 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()); final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle(R.string.import_settings); alert.setTitle(R.string.import_settings);

View File

@ -57,7 +57,7 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
return success return success
} }
fun containSettings(filePath: String): Boolean { fun extractSettings(filePath: String): Boolean {
return ZipHelper return ZipHelper
.extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings") .extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings")
} }

View File

@ -1,187 +1,144 @@
package org.schabi.newpipe.settings package org.schabi.newpipe.settings
import android.content.SharedPreferences import android.content.SharedPreferences
import org.junit.Assert
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Assume
import org.junit.Before import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.junit.runners.Suite
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
import org.mockito.Mockito.anyString import org.mockito.Mockito.anyString
import org.mockito.Mockito.anyBoolean import org.mockito.Mockito.anyBoolean
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.atLeastOnce 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.*
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(MockitoJUnitRunner::class)
@Suite.SuiteClasses(ExportTest::class, ImportTest::class)
class ContentSettingsManagerTest { class ContentSettingsManagerTest {
@RunWith(MockitoJUnitRunner::class) companion object {
class ExportTest { private val classloader = ContentSettingsManager::class.java.classLoader!!
}
companion object { private lateinit var fileLocator: NewPipeFileLocator
private lateinit var fileLocator: NewPipeFileLocator
private lateinit var newpipeDb: File
private lateinit var newpipeSettings: File
@JvmStatic @Before
@BeforeClass fun setupFileLocator() {
fun setupFiles() { fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
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!!) @Test
newpipeSettings = File(settingsPath!!) fun `The settings must be exported successfully in the correct format`() {
val db = File(classloader.getResource("settings/newpipe.db")!!.file)
val newpipeSettings = File.createTempFile("newpipe_", "")
`when`(fileLocator.db).thenReturn(db)
`when`(fileLocator.settings).thenReturn(newpipeSettings)
fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly()) val expectedPreferences = mapOf("such pref" to "much wow")
`when`(fileLocator.db).thenReturn(newpipeDb) val sharedPreferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
`when`(fileLocator.settings).thenReturn(newpipeSettings) `when`(sharedPreferences.all).thenReturn(expectedPreferences)
val output = File.createTempFile("newpipe_", "")
ContentSettingsManager(fileLocator).exportDatabase(sharedPreferences, output.absolutePath)
val zipFile = ZipFile(output)
val entries = zipFile.entries().toList()
assertEquals(2, entries.size)
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual ->
db.inputStream().use { expected ->
assertEquals(expected.reader().readText(), actual.reader().readText())
} }
} }
private lateinit var preferences: SharedPreferences zipFile.getInputStream(entries.first { it.name == "newpipe.settings" }).use { actual ->
val actualPreferences = ObjectInputStream(actual).readObject()
@Before assertEquals(expectedPreferences, actualPreferences)
fun setupMocks() {
preferences = Mockito.mock(SharedPreferences::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 manager = ContentSettingsManager(fileLocator)
val output = File.createTempFile("newpipe_", "")
manager.exportDatabase(preferences, output.absolutePath)
val zipFile = ZipFile(output.absoluteFile)
val entries = zipFile.entries().toList()
assertEquals(2, entries.size)
zipFile.getInputStream(entries.first { it.name == "newpipe.db" }).use { actual ->
newpipeDb.inputStream().use { expected ->
assertEquals(expected.reader().readText(), actual.reader().readText())
}
}
zipFile.getInputStream(entries.first { it.name == "newpipe.settings" }).use { actual ->
val actualPreferences = ObjectInputStream(actual).readObject()
assertEquals(expectedPreferences, actualPreferences)
}
} }
} }
@RunWith(MockitoJUnitRunner::class) @Test
class ImportTest { fun `The database must be extracted from the zip file`() {
val db = File.createTempFile("newpipe_", "")
companion object { val dbJournal = File.createTempFile("newpipe_", "")
private lateinit var fileLocator: NewPipeFileLocator val dbWal = File.createTempFile("newpipe_", "")
private lateinit var zip: File val dbShm = File.createTempFile("newpipe_", "")
private lateinit var emptyZip: File `when`(fileLocator.db).thenReturn(db)
private lateinit var db: File `when`(fileLocator.dbJournal).thenReturn(dbJournal)
private lateinit var dbJournal: File `when`(fileLocator.dbShm).thenReturn(dbShm)
private lateinit var dbWal: File `when`(fileLocator.dbWal).thenReturn(dbWal)
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())
`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 success = ContentSettingsManager(fileLocator).extractDb(zip.path)
assertTrue(success)
assertFalse(dbJournal.exists())
assertFalse(dbWal.exists())
assertFalse(dbShm.exists())
assertTrue("database file size is zero", Files.size(db.toPath()) > 0)
}
@Test
fun `Extracting the database from an empty zip must not work`() {
val success = ContentSettingsManager(fileLocator).extractDb(emptyZip.path)
assertFalse(success)
assertTrue(dbJournal.exists())
assertTrue(dbWal.exists())
assertTrue(dbShm.exists())
assertEquals(0, Files.size(db.toPath()))
}
@Test
fun `Contain setting must return true, if a settings file exists in the zip`() {
val contains = ContentSettingsManager(fileLocator).containSettings(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)
assertFalse(contains)
}
@Test
fun `Preferences must be set from the settings file`() {
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)
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
}
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
val success = ContentSettingsManager(fileLocator).extractDb(zip.path)
assertTrue(success)
assertFalse(dbJournal.exists())
assertFalse(dbWal.exists())
assertFalse(dbShm.exists())
assertTrue("database file size is zero", Files.size(db.toPath()) > 0)
} }
@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)
assertTrue(dbJournal.exists())
assertTrue(dbWal.exists())
assertTrue(dbShm.exists())
assertEquals(0, Files.size(db.toPath()))
}
@Test
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 `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)
ContentSettingsManager(fileLocator).loadSharedPreferences(preferences)
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
verify(editor, atLeastOnce()).putString(anyString(), anyString())
verify(editor, atLeastOnce()).putInt(anyString(), anyInt())
}
} }