Move import export manager to separate folder
This commit is contained in:
parent
5e7ad6ffd1
commit
ea18b4ea1f
3 changed files with 19 additions and 16 deletions
|
@ -24,6 +24,7 @@ import androidx.preference.PreferenceManager;
|
||||||
import org.schabi.newpipe.NewPipeDatabase;
|
import org.schabi.newpipe.NewPipeDatabase;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.error.ErrorUtil;
|
import org.schabi.newpipe.error.ErrorUtil;
|
||||||
|
import org.schabi.newpipe.settings.export.ImportExportManager;
|
||||||
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
|
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard;
|
||||||
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
import org.schabi.newpipe.streams.io.StoredFileHelper;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
|
@ -42,7 +43,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
|
||||||
|
|
||||||
private final SimpleDateFormat exportDateFormat =
|
private final SimpleDateFormat exportDateFormat =
|
||||||
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
|
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
|
||||||
private ContentSettingsManager manager;
|
private ImportExportManager manager;
|
||||||
private String importExportDataPathKey;
|
private String importExportDataPathKey;
|
||||||
private final ActivityResultLauncher<Intent> requestImportPathLauncher =
|
private final ActivityResultLauncher<Intent> requestImportPathLauncher =
|
||||||
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
|
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
|
||||||
|
@ -57,7 +58,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {
|
||||||
@Nullable final String rootKey) {
|
@Nullable final String rootKey) {
|
||||||
final File homeDir = ContextCompat.getDataDir(requireContext());
|
final File homeDir = ContextCompat.getDataDir(requireContext());
|
||||||
Objects.requireNonNull(homeDir);
|
Objects.requireNonNull(homeDir);
|
||||||
manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir));
|
manager = new ImportExportManager(new NewPipeFileLocator(homeDir));
|
||||||
manager.deleteSettingsFile();
|
manager.deleteSettingsFile();
|
||||||
|
|
||||||
importExportDataPathKey = getString(R.string.import_export_data_path);
|
importExportDataPathKey = getString(R.string.import_export_data_path);
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package org.schabi.newpipe.settings
|
package org.schabi.newpipe.settings.export
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import org.schabi.newpipe.MainActivity.DEBUG
|
import org.schabi.newpipe.MainActivity.DEBUG
|
||||||
|
import org.schabi.newpipe.settings.NewPipeFileLocator
|
||||||
import org.schabi.newpipe.streams.io.SharpOutputStream
|
import org.schabi.newpipe.streams.io.SharpOutputStream
|
||||||
import org.schabi.newpipe.streams.io.StoredFileHelper
|
import org.schabi.newpipe.streams.io.StoredFileHelper
|
||||||
import org.schabi.newpipe.util.ZipHelper
|
import org.schabi.newpipe.util.ZipHelper
|
||||||
|
@ -11,7 +12,7 @@ import java.io.ObjectInputStream
|
||||||
import java.io.ObjectOutputStream
|
import java.io.ObjectOutputStream
|
||||||
import java.util.zip.ZipOutputStream
|
import java.util.zip.ZipOutputStream
|
||||||
|
|
||||||
class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
|
class ImportExportManager(private val fileLocator: NewPipeFileLocator) {
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "ContentSetManager"
|
const val TAG = "ContentSetManager"
|
||||||
}
|
}
|
|
@ -17,6 +17,7 @@ import org.mockito.Mockito.verify
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
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.export.ImportExportManager
|
||||||
import org.schabi.newpipe.streams.io.StoredFileHelper
|
import org.schabi.newpipe.streams.io.StoredFileHelper
|
||||||
import us.shandian.giga.io.FileStream
|
import us.shandian.giga.io.FileStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -25,10 +26,10 @@ import java.nio.file.Files
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner::class)
|
@RunWith(MockitoJUnitRunner::class)
|
||||||
class ContentSettingsManagerTest {
|
class ImportExportManagerTest {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val classloader = ContentSettingsManager::class.java.classLoader!!
|
private val classloader = ImportExportManager::class.java.classLoader!!
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var fileLocator: NewPipeFileLocator
|
private lateinit var fileLocator: NewPipeFileLocator
|
||||||
|
@ -54,7 +55,7 @@ class ContentSettingsManagerTest {
|
||||||
|
|
||||||
val output = File.createTempFile("newpipe_", "")
|
val output = File.createTempFile("newpipe_", "")
|
||||||
`when`(storedFileHelper.stream).thenReturn(FileStream(output))
|
`when`(storedFileHelper.stream).thenReturn(FileStream(output))
|
||||||
ContentSettingsManager(fileLocator).exportDatabase(sharedPreferences, storedFileHelper)
|
ImportExportManager(fileLocator).exportDatabase(sharedPreferences, storedFileHelper)
|
||||||
|
|
||||||
val zipFile = ZipFile(output)
|
val zipFile = ZipFile(output)
|
||||||
val entries = zipFile.entries().toList()
|
val entries = zipFile.entries().toList()
|
||||||
|
@ -77,7 +78,7 @@ class ContentSettingsManagerTest {
|
||||||
val settings = File.createTempFile("newpipe_", "")
|
val settings = File.createTempFile("newpipe_", "")
|
||||||
`when`(fileLocator.settings).thenReturn(settings)
|
`when`(fileLocator.settings).thenReturn(settings)
|
||||||
|
|
||||||
ContentSettingsManager(fileLocator).deleteSettingsFile()
|
ImportExportManager(fileLocator).deleteSettingsFile()
|
||||||
|
|
||||||
assertFalse(settings.exists())
|
assertFalse(settings.exists())
|
||||||
}
|
}
|
||||||
|
@ -87,7 +88,7 @@ class ContentSettingsManagerTest {
|
||||||
val settings = File("non_existent")
|
val settings = File("non_existent")
|
||||||
`when`(fileLocator.settings).thenReturn(settings)
|
`when`(fileLocator.settings).thenReturn(settings)
|
||||||
|
|
||||||
ContentSettingsManager(fileLocator).deleteSettingsFile()
|
ImportExportManager(fileLocator).deleteSettingsFile()
|
||||||
|
|
||||||
assertFalse(settings.exists())
|
assertFalse(settings.exists())
|
||||||
}
|
}
|
||||||
|
@ -98,7 +99,7 @@ class ContentSettingsManagerTest {
|
||||||
Assume.assumeTrue(dir.delete())
|
Assume.assumeTrue(dir.delete())
|
||||||
`when`(fileLocator.dbDir).thenReturn(dir)
|
`when`(fileLocator.dbDir).thenReturn(dir)
|
||||||
|
|
||||||
ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
|
ImportExportManager(fileLocator).ensureDbDirectoryExists()
|
||||||
assertTrue(dir.exists())
|
assertTrue(dir.exists())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ class ContentSettingsManagerTest {
|
||||||
val dir = Files.createTempDirectory("newpipe_").toFile()
|
val dir = Files.createTempDirectory("newpipe_").toFile()
|
||||||
`when`(fileLocator.dbDir).thenReturn(dir)
|
`when`(fileLocator.dbDir).thenReturn(dir)
|
||||||
|
|
||||||
ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
|
ImportExportManager(fileLocator).ensureDbDirectoryExists()
|
||||||
assertTrue(dir.exists())
|
assertTrue(dir.exists())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +125,7 @@ class ContentSettingsManagerTest {
|
||||||
|
|
||||||
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
||||||
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
|
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
|
||||||
val success = ContentSettingsManager(fileLocator).extractDb(storedFileHelper)
|
val success = ImportExportManager(fileLocator).extractDb(storedFileHelper)
|
||||||
|
|
||||||
assertTrue(success)
|
assertTrue(success)
|
||||||
assertFalse(dbJournal.exists())
|
assertFalse(dbJournal.exists())
|
||||||
|
@ -143,7 +144,7 @@ class ContentSettingsManagerTest {
|
||||||
|
|
||||||
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
||||||
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
|
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
|
||||||
val success = ContentSettingsManager(fileLocator).extractDb(storedFileHelper)
|
val success = ImportExportManager(fileLocator).extractDb(storedFileHelper)
|
||||||
|
|
||||||
assertFalse(success)
|
assertFalse(success)
|
||||||
assertTrue(dbJournal.exists())
|
assertTrue(dbJournal.exists())
|
||||||
|
@ -159,7 +160,7 @@ class ContentSettingsManagerTest {
|
||||||
|
|
||||||
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
|
||||||
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
|
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
|
||||||
val contains = ContentSettingsManager(fileLocator).extractSettings(storedFileHelper)
|
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)
|
||||||
|
|
||||||
assertTrue(contains)
|
assertTrue(contains)
|
||||||
}
|
}
|
||||||
|
@ -171,7 +172,7 @@ class ContentSettingsManagerTest {
|
||||||
|
|
||||||
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
|
||||||
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
|
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
|
||||||
val contains = ContentSettingsManager(fileLocator).extractSettings(storedFileHelper)
|
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)
|
||||||
|
|
||||||
assertFalse(contains)
|
assertFalse(contains)
|
||||||
}
|
}
|
||||||
|
@ -185,7 +186,7 @@ class ContentSettingsManagerTest {
|
||||||
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)
|
ImportExportManager(fileLocator).loadSharedPreferences(preferences)
|
||||||
|
|
||||||
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
|
verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
|
||||||
verify(editor, atLeastOnce()).putString(anyString(), anyString())
|
verify(editor, atLeastOnce()).putString(anyString(), anyString())
|
Loading…
Reference in a new issue