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,82 +1,60 @@
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)
class ExportTest {
companion object { companion object {
private val classloader = ContentSettingsManager::class.java.classLoader!!
}
private lateinit var fileLocator: NewPipeFileLocator 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 @Before
fun setupMocks() { fun setupFileLocator() {
preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly()) fileLocator = Mockito.mock(NewPipeFileLocator::class.java, withSettings().stubOnly())
} }
@Test @Test
fun `The settings must be exported successfully in the correct format`() { fun `The settings must be exported successfully in the correct format`() {
val expectedPreferences = mapOf("such pref" to "much wow") val db = File(classloader.getResource("settings/newpipe.db")!!.file)
`when`(preferences.all).thenReturn(expectedPreferences) 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_", "") 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() val entries = zipFile.entries().toList()
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 -> db.inputStream().use { expected ->
assertEquals(expected.reader().readText(), actual.reader().readText()) assertEquals(expected.reader().readText(), actual.reader().readText())
} }
} }
@ -86,52 +64,19 @@ class ContentSettingsManagerTest {
assertEquals(expectedPreferences, actualPreferences) 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
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())
`when`(fileLocator.db).thenReturn(db) `when`(fileLocator.db).thenReturn(db)
`when`(fileLocator.dbJournal).thenReturn(dbJournal) `when`(fileLocator.dbJournal).thenReturn(dbJournal)
`when`(fileLocator.dbShm).thenReturn(dbShm) `when`(fileLocator.dbShm).thenReturn(dbShm)
`when`(fileLocator.dbWal).thenReturn(dbWal) `when`(fileLocator.dbWal).thenReturn(dbWal)
`when`(fileLocator.settings).thenReturn(settings)
}
@Test val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
fun `The database must be extracted from the zip file`() {
val success = ContentSettingsManager(fileLocator).extractDb(zip.path) val success = ContentSettingsManager(fileLocator).extractDb(zip.path)
assertTrue(success) assertTrue(success)
@ -143,6 +88,13 @@ class ContentSettingsManagerTest {
@Test @Test
fun `Extracting the database from an empty zip must not work`() { 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) val success = ContentSettingsManager(fileLocator).extractDb(emptyZip.path)
assertFalse(success) assertFalse(success)
@ -153,35 +105,40 @@ class ContentSettingsManagerTest {
} }
@Test @Test
fun `Contain setting must return true, if a settings file exists in the zip`() { fun `Contains setting must return true if a settings file exists in the zip`() {
val contains = ContentSettingsManager(fileLocator).containSettings(zip.path) 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) assertTrue(contains)
} }
@Test @Test
fun `Contain setting must return false, if a no settings file exists in the zip`() { fun `Contains setting must return false if a no settings file exists in the zip`() {
val contains = ContentSettingsManager(fileLocator).containSettings(emptyZip.path) 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) assertFalse(contains)
} }
@Test @Test
fun `Preferences must be set from the settings file`() { 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 preferences = Mockito.mock(SharedPreferences::class.java, withSettings().stubOnly())
val editor = Mockito.mock(SharedPreferences.Editor::class.java) val editor = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(preferences.edit()).thenReturn(editor) `when`(preferences.edit()).thenReturn(editor)
ContentSettingsManager(fileLocator).loadSharedPreferences(preferences)
val manager = ContentSettingsManager(fileLocator)
manager.containSettings(zip.path)
manager.loadSharedPreferences(preferences)
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean()) verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
verify(editor, atLeastOnce()).putString(anyString(), anyString())
verify(editor, atLeastOnce()).putInt(anyString(), anyInt())
} }
}
} }