Skip to content

Commit

Permalink
Move import export manager to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Mar 30, 2024
1 parent 5e7ad6f commit ea18b4e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
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.StoredFileHelper;
import org.schabi.newpipe.util.NavigationHelper;
Expand All @@ -42,7 +43,7 @@ public class BackupRestoreSettingsFragment extends BasePreferenceFragment {

private final SimpleDateFormat exportDateFormat =
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
private ContentSettingsManager manager;
private ImportExportManager manager;
private String importExportDataPathKey;
private final ActivityResultLauncher<Intent> requestImportPathLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
Expand All @@ -57,7 +58,7 @@ public void onCreatePreferences(@Nullable final Bundle savedInstanceState,
@Nullable final String rootKey) {
final File homeDir = ContextCompat.getDataDir(requireContext());
Objects.requireNonNull(homeDir);
manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir));
manager = new ImportExportManager(new NewPipeFileLocator(homeDir));
manager.deleteSettingsFile();

importExportDataPathKey = getString(R.string.import_export_data_path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.schabi.newpipe.settings
package org.schabi.newpipe.settings.export

import android.content.SharedPreferences
import android.util.Log
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.StoredFileHelper
import org.schabi.newpipe.util.ZipHelper
Expand All @@ -11,7 +12,7 @@ import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.zip.ZipOutputStream

class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
class ImportExportManager(private val fileLocator: NewPipeFileLocator) {
companion object {
const val TAG = "ContentSetManager"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.Mockito.withSettings
import org.mockito.junit.MockitoJUnitRunner
import org.schabi.newpipe.settings.export.ImportExportManager
import org.schabi.newpipe.streams.io.StoredFileHelper
import us.shandian.giga.io.FileStream
import java.io.File
Expand All @@ -25,10 +26,10 @@ import java.nio.file.Files
import java.util.zip.ZipFile

@RunWith(MockitoJUnitRunner::class)
class ContentSettingsManagerTest {
class ImportExportManagerTest {

companion object {
private val classloader = ContentSettingsManager::class.java.classLoader!!
private val classloader = ImportExportManager::class.java.classLoader!!
}

private lateinit var fileLocator: NewPipeFileLocator
Expand All @@ -54,7 +55,7 @@ class ContentSettingsManagerTest {

val output = File.createTempFile("newpipe_", "")
`when`(storedFileHelper.stream).thenReturn(FileStream(output))
ContentSettingsManager(fileLocator).exportDatabase(sharedPreferences, storedFileHelper)
ImportExportManager(fileLocator).exportDatabase(sharedPreferences, storedFileHelper)

val zipFile = ZipFile(output)
val entries = zipFile.entries().toList()
Expand All @@ -77,7 +78,7 @@ class ContentSettingsManagerTest {
val settings = File.createTempFile("newpipe_", "")
`when`(fileLocator.settings).thenReturn(settings)

ContentSettingsManager(fileLocator).deleteSettingsFile()
ImportExportManager(fileLocator).deleteSettingsFile()

assertFalse(settings.exists())
}
Expand All @@ -87,7 +88,7 @@ class ContentSettingsManagerTest {
val settings = File("non_existent")
`when`(fileLocator.settings).thenReturn(settings)

ContentSettingsManager(fileLocator).deleteSettingsFile()
ImportExportManager(fileLocator).deleteSettingsFile()

assertFalse(settings.exists())
}
Expand All @@ -98,7 +99,7 @@ class ContentSettingsManagerTest {
Assume.assumeTrue(dir.delete())
`when`(fileLocator.dbDir).thenReturn(dir)

ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
ImportExportManager(fileLocator).ensureDbDirectoryExists()
assertTrue(dir.exists())
}

Expand All @@ -107,7 +108,7 @@ class ContentSettingsManagerTest {
val dir = Files.createTempDirectory("newpipe_").toFile()
`when`(fileLocator.dbDir).thenReturn(dir)

ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
ImportExportManager(fileLocator).ensureDbDirectoryExists()
assertTrue(dir.exists())
}

Expand All @@ -124,7 +125,7 @@ class ContentSettingsManagerTest {

val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
val success = ContentSettingsManager(fileLocator).extractDb(storedFileHelper)
val success = ImportExportManager(fileLocator).extractDb(storedFileHelper)

assertTrue(success)
assertFalse(dbJournal.exists())
Expand All @@ -143,7 +144,7 @@ class ContentSettingsManagerTest {

val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
val success = ContentSettingsManager(fileLocator).extractDb(storedFileHelper)
val success = ImportExportManager(fileLocator).extractDb(storedFileHelper)

assertFalse(success)
assertTrue(dbJournal.exists())
Expand All @@ -159,7 +160,7 @@ class ContentSettingsManagerTest {

val zip = File(classloader.getResource("settings/newpipe.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(zip))
val contains = ContentSettingsManager(fileLocator).extractSettings(storedFileHelper)
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)

assertTrue(contains)
}
Expand All @@ -171,7 +172,7 @@ class ContentSettingsManagerTest {

val emptyZip = File(classloader.getResource("settings/empty.zip")?.file!!)
`when`(storedFileHelper.stream).thenReturn(FileStream(emptyZip))
val contains = ContentSettingsManager(fileLocator).extractSettings(storedFileHelper)
val contains = ImportExportManager(fileLocator).extractSettings(storedFileHelper)

assertFalse(contains)
}
Expand All @@ -185,7 +186,7 @@ class ContentSettingsManagerTest {
val editor = Mockito.mock(SharedPreferences.Editor::class.java)
`when`(preferences.edit()).thenReturn(editor)

ContentSettingsManager(fileLocator).loadSharedPreferences(preferences)
ImportExportManager(fileLocator).loadSharedPreferences(preferences)

verify(editor, atLeastOnce()).putBoolean(anyString(), anyBoolean())
verify(editor, atLeastOnce()).putString(anyString(), anyString())
Expand Down

0 comments on commit ea18b4e

Please sign in to comment.