|
@@ -3,9 +3,6 @@ package eu.kanade.tachiyomi.data.backup
|
|
|
import android.app.Application
|
|
|
import android.content.Context
|
|
|
import android.os.Build
|
|
|
-import com.github.salomonbrys.kotson.fromJson
|
|
|
-import com.google.gson.JsonArray
|
|
|
-import com.google.gson.JsonObject
|
|
|
import eu.kanade.tachiyomi.BuildConfig
|
|
|
import eu.kanade.tachiyomi.CustomRobolectricGradleTestRunner
|
|
|
import eu.kanade.tachiyomi.data.backup.legacy.LegacyBackupManager
|
|
@@ -17,12 +14,16 @@ import eu.kanade.tachiyomi.data.database.models.Chapter
|
|
|
import eu.kanade.tachiyomi.data.database.models.ChapterImpl
|
|
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
|
|
import eu.kanade.tachiyomi.data.database.models.MangaImpl
|
|
|
+import eu.kanade.tachiyomi.data.database.models.Track
|
|
|
import eu.kanade.tachiyomi.data.database.models.TrackImpl
|
|
|
import eu.kanade.tachiyomi.source.SourceManager
|
|
|
import eu.kanade.tachiyomi.source.online.HttpSource
|
|
|
import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
|
|
|
import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
+import kotlinx.serialization.decodeFromString
|
|
|
+import kotlinx.serialization.encodeToString
|
|
|
+import kotlinx.serialization.json.buildJsonObject
|
|
|
import org.assertj.core.api.Assertions.assertThat
|
|
|
import org.junit.Before
|
|
|
import org.junit.Test
|
|
@@ -47,16 +48,10 @@ import uy.kohesive.injekt.api.addSingleton
|
|
|
@RunWith(CustomRobolectricGradleTestRunner::class)
|
|
|
class BackupTest {
|
|
|
// Create root object
|
|
|
- var root = JsonObject()
|
|
|
+ var root = Backup()
|
|
|
|
|
|
// Create information object
|
|
|
- var information = JsonObject()
|
|
|
-
|
|
|
- // Create manga array
|
|
|
- var mangaEntries = JsonArray()
|
|
|
-
|
|
|
- // Create category array
|
|
|
- var categoryEntries = JsonArray()
|
|
|
+ var information = buildJsonObject {}
|
|
|
|
|
|
lateinit var app: Application
|
|
|
lateinit var context: Context
|
|
@@ -83,11 +78,6 @@ class BackupTest {
|
|
|
|
|
|
source = mock(HttpSource::class.java)
|
|
|
`when`(legacyBackupManager.sourceManager.get(anyLong())).thenReturn(source)
|
|
|
-
|
|
|
- root.add(Backup.MANGAS, mangaEntries)
|
|
|
- root.add(Backup.CATEGORIES, categoryEntries)
|
|
|
-
|
|
|
- clearJson()
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -95,11 +85,8 @@ class BackupTest {
|
|
|
*/
|
|
|
@Test
|
|
|
fun testRestoreEmptyCategory() {
|
|
|
- // Create backup of empty database
|
|
|
- legacyBackupManager.backupCategories(categoryEntries)
|
|
|
-
|
|
|
// Restore Json
|
|
|
- legacyBackupManager.restoreCategories(categoryEntries)
|
|
|
+ legacyBackupManager.restoreCategories(root.categories ?: emptyList())
|
|
|
|
|
|
// Check if empty
|
|
|
val dbCats = db.getCategories().executeAsBlocking()
|
|
@@ -115,7 +102,7 @@ class BackupTest {
|
|
|
val category = addSingleCategory("category")
|
|
|
|
|
|
// Restore Json
|
|
|
- legacyBackupManager.restoreCategories(categoryEntries)
|
|
|
+ legacyBackupManager.restoreCategories(root.categories ?: emptyList())
|
|
|
|
|
|
// Check if successful
|
|
|
val dbCats = legacyBackupManager.databaseHelper.getCategories().executeAsBlocking()
|
|
@@ -139,7 +126,7 @@ class BackupTest {
|
|
|
db.insertCategory(category).executeAsBlocking()
|
|
|
|
|
|
// Restore Json
|
|
|
- legacyBackupManager.restoreCategories(categoryEntries)
|
|
|
+ legacyBackupManager.restoreCategories(root.categories ?: emptyList())
|
|
|
|
|
|
// Check if successful
|
|
|
val dbCats = legacyBackupManager.databaseHelper.getCategories().executeAsBlocking()
|
|
@@ -167,9 +154,6 @@ class BackupTest {
|
|
|
assertThat(favoriteManga[0].readingModeType).isEqualTo(ReadingModeType.VERTICAL.flagValue)
|
|
|
assertThat(favoriteManga[0].orientationType).isEqualTo(OrientationType.PORTRAIT.flagValue)
|
|
|
|
|
|
- // Update json with all options enabled
|
|
|
- mangaEntries.add(legacyBackupManager.backupMangaObject(manga, 1))
|
|
|
-
|
|
|
// Change manga in database to default values
|
|
|
val dbManga = getSingleManga("One Piece")
|
|
|
dbManga.id = manga.id
|
|
@@ -198,9 +182,9 @@ class BackupTest {
|
|
|
|
|
|
// Restore Json
|
|
|
// Create JSON from manga to test parser
|
|
|
- val json = legacyBackupManager.parser.toJsonTree(manga)
|
|
|
+ val json = legacyBackupManager.parser.encodeToString(manga)
|
|
|
// Restore JSON from manga to test parser
|
|
|
- val jsonManga = legacyBackupManager.parser.fromJson<MangaImpl>(json)
|
|
|
+ val jsonManga = legacyBackupManager.parser.decodeFromString<Manga>(json)
|
|
|
|
|
|
// Restore manga with fetch observable
|
|
|
val networkManga = getSingleManga("One Piece")
|
|
@@ -237,8 +221,8 @@ class BackupTest {
|
|
|
}
|
|
|
|
|
|
// Check parser
|
|
|
- val chaptersJson = legacyBackupManager.parser.toJsonTree(chapters)
|
|
|
- val restoredChapters = legacyBackupManager.parser.fromJson<List<ChapterImpl>>(chaptersJson)
|
|
|
+ val chaptersJson = legacyBackupManager.parser.encodeToString(chapters)
|
|
|
+ val restoredChapters = legacyBackupManager.parser.decodeFromString<List<Chapter>>(chaptersJson)
|
|
|
|
|
|
// Fetch chapters from upstream
|
|
|
// Create list
|
|
@@ -275,8 +259,8 @@ class BackupTest {
|
|
|
historyList.add(historyJson)
|
|
|
|
|
|
// Check parser
|
|
|
- val historyListJson = legacyBackupManager.parser.toJsonTree(historyList)
|
|
|
- val history = legacyBackupManager.parser.fromJson<List<DHistory>>(historyListJson)
|
|
|
+ val historyListJson = legacyBackupManager.parser.encodeToString(historyList)
|
|
|
+ val history = legacyBackupManager.parser.decodeFromString<List<DHistory>>(historyListJson)
|
|
|
|
|
|
// Restore categories
|
|
|
legacyBackupManager.restoreHistoryForManga(history)
|
|
@@ -314,8 +298,8 @@ class BackupTest {
|
|
|
// Check parser and restore already in database
|
|
|
var trackList = listOf(track)
|
|
|
// Check parser
|
|
|
- var trackListJson = legacyBackupManager.parser.toJsonTree(trackList)
|
|
|
- var trackListRestore = legacyBackupManager.parser.fromJson<List<TrackImpl>>(trackListJson)
|
|
|
+ var trackListJson = legacyBackupManager.parser.encodeToString(trackList)
|
|
|
+ var trackListRestore = legacyBackupManager.parser.decodeFromString<List<Track>>(trackListJson)
|
|
|
legacyBackupManager.restoreTrackForManga(manga, trackListRestore)
|
|
|
|
|
|
// Assert if restore works.
|
|
@@ -337,8 +321,8 @@ class BackupTest {
|
|
|
trackList = listOf(track2)
|
|
|
|
|
|
// Check parser
|
|
|
- trackListJson = legacyBackupManager.parser.toJsonTree(trackList)
|
|
|
- trackListRestore = legacyBackupManager.parser.fromJson<List<TrackImpl>>(trackListJson)
|
|
|
+ trackListJson = legacyBackupManager.parser.encodeToString(trackList)
|
|
|
+ trackListRestore = legacyBackupManager.parser.decodeFromString<List<Track>>(trackListJson)
|
|
|
legacyBackupManager.restoreTrackForManga(manga2, trackListRestore)
|
|
|
|
|
|
// Assert if restore works.
|
|
@@ -348,16 +332,13 @@ class BackupTest {
|
|
|
}
|
|
|
|
|
|
private fun clearJson() {
|
|
|
- root = JsonObject()
|
|
|
- information = JsonObject()
|
|
|
- mangaEntries = JsonArray()
|
|
|
- categoryEntries = JsonArray()
|
|
|
+ root = Backup()
|
|
|
+ information = buildJsonObject {}
|
|
|
}
|
|
|
|
|
|
private fun addSingleCategory(name: String): Category {
|
|
|
val category = Category.create(name)
|
|
|
- val catJson = legacyBackupManager.parser.toJsonTree(category)
|
|
|
- categoryEntries.add(catJson)
|
|
|
+ root.categories = listOf(category)
|
|
|
return category
|
|
|
}
|
|
|
|