Commit 5fc48e37 authored by Habib Kazemi's avatar Habib Kazemi Committed by Geoffrey Métais

Migrate database to Room

Database migrated to Room Android Room Persistence Library.
Also, Android testing dependencies added to build.gradle.
I also wrote Tests for Dao and repository
Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent e7c04c2b
......@@ -27,7 +27,10 @@ ext {
appCompatVersion = '27.1.1'
constraintLayoutVersion = '1.1.2'
archVersion = '1.1.1'
roomVersion = '1.1.1'
compileSdkVersion = 26
junitVersion = '4.12'
espressoVersion = '3.0.1'
minSdkVersion = 14
targetSdkVersion = 26
versionName = '3.0.13'
......
/*******************************************************************************
* BrowserFavDaoTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.database
import android.arch.persistence.room.Room
import android.net.Uri
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.videolan.vlc.database.models.BrowserFav
import org.videolan.vlc.util.Constants
@RunWith(AndroidJUnit4::class)
class BrowserFavDaoTest {
private lateinit var database: MediaDatabase
private lateinit var browserFavDao: BrowserFavDao
val netwrokFav1: BrowserFav = BrowserFav(Uri.parse("upnp://http://[fe80::61a1:a5a4:c66:bc5d]:2869/u"), Constants.TYPE_NETWORK_FAV, "Test1", null)
val netwrokFav2: BrowserFav = BrowserFav(Uri.parse("upnp://http://[fe80::61a1:a5a4:c66:bc6d]:2869/u"), Constants.TYPE_NETWORK_FAV, "Test2", null)
val localFav1: BrowserFav = BrowserFav(Uri.parse("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.mkv"), Constants.TYPE_LOCAL_FAV, "Test3", null)
@Before fun createDao() {
val context = InstrumentationRegistry.getTargetContext()
database = Room.inMemoryDatabaseBuilder(context, MediaDatabase::class.java).build()
browserFavDao = database.browserFavDao()
browserFavDao.insert(netwrokFav1)
browserFavDao.insert(netwrokFav2)
browserFavDao.insert(localFav1)
}
@After fun closeDb() {
database.close()
}
@Test fun getAllBrowserFavs() {
val browsersFavs = browserFavDao.getAll()
assertThat(browsersFavs.size, equalTo(3))
assertThat(browsersFavs, hasItem(netwrokFav1))
assertThat(browsersFavs, hasItem(netwrokFav2))
assertThat(browsersFavs, hasItem(localFav1))
}
@Test fun getAllNetworkFavs() {
val networkFavs = browserFavDao.getAllNetwrokFavs()
assertThat(networkFavs.size, equalTo(2))
assertThat(networkFavs, hasItem(netwrokFav1))
assertThat(networkFavs, hasItem(netwrokFav2))
}
@Test fun getAllLocalFavs() {
val localFavs = browserFavDao.getAllLocalFavs()
assertThat(localFavs.size, equalTo(1))
assertThat(localFavs, hasItem(localFav1))
}
@Test fun getBrowserFav() {
val browser = browserFavDao.get(netwrokFav1.uri)
assertThat(browser.size, equalTo(1))
assertThat(browser[0], equalTo(netwrokFav1))
}
@Test fun deleteBrowserFav() {
browserFavDao.delete(netwrokFav1.uri)
val browsers = browserFavDao.getAll()
assertThat(browsers.size, equalTo(2))
assertThat(browsers, not(hasItem(netwrokFav1)))
assertThat(browsers, hasItem(netwrokFav2))
browserFavDao.delete(netwrokFav2.uri)
browserFavDao.delete(localFav1.uri)
val browserFavsAfterDeleteAll = browserFavDao.getAll()
assertThat(browserFavsAfterDeleteAll.size, equalTo(0))
}
}
\ No newline at end of file
/*******************************************************************************
* ConvertersTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.database
import android.net.Uri
import android.support.test.runner.AndroidJUnit4
import junit.framework.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ConvertersTest {
private val str = "upnp://http://[fe80::61a1:a5a4:c66:bc5d]:2869/u"
private val uri = Uri.parse(str)
@Test fun uriToString() {
assertEquals(str, Converters().uriToString(uri))
}
@Test fun stringToUri() {
assertEquals(uri, Converters().stringToUri(str))
}
}
\ No newline at end of file
/*******************************************************************************
* ExternalSubDaoTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.database
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.hamcrest.Matchers.*
import org.junit.After
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.videolan.vlc.database.models.ExternalSub
@RunWith(AndroidJUnit4::class)
class ExternalSubDaoTest {
private lateinit var database: MediaDatabase
private lateinit var externalSubDao: ExternalSubDao
private val file1 = "file1.mkv"
private val file2 = "file2.mkv"
private val file1Sub1 = ExternalSub("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.eng.srt", file1)
private val file1Sub2 = ExternalSub("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.fa.srt", file1)
private val file1Sub3 = ExternalSub("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.fr.srt", file1)
private val file2Sub1 = ExternalSub("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file2.eng.srt", file2)
@Before fun createDao() {
val context = InstrumentationRegistry.getTargetContext()
database = Room.inMemoryDatabaseBuilder(context, MediaDatabase::class.java).build()
externalSubDao = database.externalSubDao()
externalSubDao.insert(file1Sub1)
externalSubDao.insert(file1Sub2)
externalSubDao.insert(file1Sub3)
externalSubDao.insert(file2Sub1)
}
@After fun closeDb() {
database.close()
}
@Test fun getExternalSubtitles() {
val file1Subtitles = externalSubDao.get(file1)
assertThat(file1Subtitles.size, equalTo(3))
assertThat(file1Subtitles, hasItem(file1Sub1))
assertThat(file1Subtitles, hasItem(file1Sub2))
assertThat(file1Subtitles, hasItem(file1Sub3))
val file2Subtitles = externalSubDao.get(file2)
assertThat(file2Subtitles.size, equalTo(1))
assertThat(file2Subtitles, hasItem(file2Sub1))
}
@Test fun deleteSubtitle(){
externalSubDao.delete(file1Sub1)
var file1Subtitles = externalSubDao.get(file1)
assertThat(file1Subtitles, not(hasItem(file1Sub1)))
assertThat(file1Subtitles, hasItem(file1Sub2))
assertThat(file1Subtitles, hasItem(file1Sub3))
externalSubDao.delete(file1Sub2)
file1Subtitles = externalSubDao.get(file1)
assertThat(file1Subtitles, not(hasItem(file2)))
assertThat(file1Subtitles, hasItem(file1Sub3))
externalSubDao.delete(file1Sub3)
file1Subtitles = externalSubDao.get(file1)
assertThat(file1Subtitles, not(hasItem(file1Sub3)))
assertThat(file1Subtitles, empty())
externalSubDao.delete(file2Sub1)
val file2Subtitles = externalSubDao.get(file2)
assertThat(file2Subtitles, empty())
}
}
\ No newline at end of file
/*******************************************************************************
* MigrationTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.database
import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory
import android.arch.persistence.room.Room
import android.arch.persistence.room.testing.MigrationTestHelper
import android.net.Uri
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.videolan.libvlc.Media
import org.videolan.vlc.database.helpers.*
import org.videolan.vlc.database.models.BrowserFav
import org.videolan.vlc.database.models.ExternalSub
import org.videolan.vlc.database.models.Slave
import org.videolan.vlc.util.Constants
@RunWith(AndroidJUnit4::class)
class MigrationTest {
// Slave
private val slaveMedia1Path = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.mkv"
private val slaveMedia1UriFa = "file:///storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.fa.srt"
// External Sub
private val exSubMedia1Name = "file1.mkv"
private val exSubMedisubsFolder = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/"
private val exSubfile1Sub1 = "${exSubMedisubsFolder}file1.eng.srt"
// Favs
private val favUri = Uri.parse("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.mkv")
private val favTitle = "test1"
private val TEST_DB_NAME = "test-db"
@get:Rule
val migrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
MediaDatabase::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory())
@Test fun migrate26To27() {
val sqliteTestDbOpenHelper = SqliteTestDbOpenHelper(InstrumentationRegistry.getTargetContext(), TEST_DB_NAME, 26)
createSlavesTable(sqliteTestDbOpenHelper)
createExternalSubsTable(sqliteTestDbOpenHelper)
createNetworkFavsTable(sqliteTestDbOpenHelper)
// Insert Data before migration
saveSlave(slaveMedia1Path, Media.Slave.Type.Subtitle, 2, slaveMedia1UriFa, sqliteTestDbOpenHelper)
saveExSubtitle(exSubfile1Sub1, exSubMedia1Name, sqliteTestDbOpenHelper)
saveNetworkFavItem(favUri, favTitle, null, sqliteTestDbOpenHelper)
migrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 27, true, migration_26_27)
val migratedDb = getMigratedRoomDatabase()
val slave: Slave = migratedDb.slaveDao().get(slaveMedia1Path)[0]
val exSub: ExternalSub = migratedDb.externalSubDao().get(exSubMedia1Name)[0]
val fav: BrowserFav = migratedDb.browserFavDao().get(favUri)[0]
assertEquals(slave.mediaPath, slaveMedia1Path)
assertEquals(slave.type, Media.Slave.Type.Subtitle)
assertEquals(slave.priority, 2)
assertEquals(slave.uri, slaveMedia1UriFa)
assertEquals(exSub.uri, exSubfile1Sub1)
assertEquals(exSub.mediaName, exSubMedia1Name)
assertEquals(fav.uri, favUri)
assertEquals(fav.title, favTitle)
assertEquals(fav.iconUrl, null)
assertEquals(fav.type, Constants.TYPE_NETWORK_FAV)
clearDatabase(sqliteTestDbOpenHelper)
}
fun getMigratedRoomDatabase(): MediaDatabase {
val database: MediaDatabase = Room.databaseBuilder(
InstrumentationRegistry.getTargetContext(),
MediaDatabase::class.java, TEST_DB_NAME )
.addMigrations(migration_26_27)
.build()
// close the database and release any stream resources when the test finishes
migrationTestHelper.closeWhenFinished(database);
return database
}
}
\ No newline at end of file
/*******************************************************************************
* SlaveDaoTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.database
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.videolan.libvlc.Media
import org.videolan.vlc.database.models.Slave
@RunWith(AndroidJUnit4::class)
class SlaveDaoTest {
private lateinit var database: MediaDatabase
private lateinit var slaveDao: SlaveDao
val media1Path = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.mkv"
val media1UriEng = "file:///storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.eng.srt"
val media1UriFa = "file:///storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.fa.srt"
val media2Path = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file2.mkv"
val media2UriEng = "file:///storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file2.eng.srt"
private val slave1eng = Slave(media1Path, Media.Slave.Type.Subtitle, 0, media1UriEng)
private val slave1fa = Slave(media1Path, Media.Slave.Type.Subtitle, 0, media1UriFa)
private val slave2 = Slave(media2Path, Media.Slave.Type.Subtitle, 1, media2UriEng )
@Before fun createDao() {
val context = InstrumentationRegistry.getTargetContext()
database = Room.inMemoryDatabaseBuilder(context, MediaDatabase::class.java).build()
slaveDao = database.slaveDao()
slaveDao.insert(slave1eng)
slaveDao.insert(slave1fa)
slaveDao.insert(slave2)
}
@After fun closeDb() {
database.close()
}
@Test fun getSlaves() {
val s1 = slaveDao.get(media1Path)
assertThat(s1.size, equalTo(1))
assertThat(s1, not(hasItem(slave1eng)))
assertThat(s1, hasItem(slave1fa))
val s2 = slaveDao.get(media2Path)
assertThat(s2.size, equalTo(1))
assertThat(s2, hasItem(slave2))
}
}
/*******************************************************************************
* SqliteDatabaseTestHelper
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*/
package org.videolan.vlc.database.helpers
import android.content.ContentValues
import android.net.Uri
import android.text.TextUtils
private val NETWORK_FAV_TABLE_NAME = "fav_table"
private val NETWORK_FAV_URI = "uri"
private val NETWORK_FAV_TITLE = "title"
private val NETWORK_FAV_ICON_URL = "icon_url"
private val EXTERNAL_SUBTITLES_TABLE_NAME = "external_subtitles_table"
private val EXTERNAL_SUBTITLES_MEDIA_NAME = "media_name"
private val EXTERNAL_SUBTITLES_URI = "uri"
private val SLAVES_TABLE_NAME = "SLAVES_table"
private val SLAVES_MEDIA_PATH = "slave_media_mrl"
private val SLAVES_TYPE = "slave_type"
private val SLAVES_PRIORITY = "slave_priority"
private val SLAVES_URI = "slave_uri"
fun createSlavesTable(helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
db.execSQL("CREATE TABLE IF NOT EXISTS $SLAVES_TABLE_NAME ( slave_media_mrl TEXT PRIMARY KEY NOT NULL, slave_type INTEGER NOT NULL, slave_priority INTEGER, slave_uri TEXT NOT NULL);")
db.close()
}
fun createExternalSubsTable(helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
db.execSQL("CREATE TABLE IF NOT EXISTS $EXTERNAL_SUBTITLES_TABLE_NAME ( uri TEXT PRIMARY KEY NOT NULL, media_name TEXT NOT NULL);")
db.close()
}
fun createNetworkFavsTable(helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
db.execSQL("CREATE TABLE IF NOT EXISTS $NETWORK_FAV_TABLE_NAME ( uri TEXT PRIMARY KEY NOT NULL, title TEXT NOT NULL, icon_url TEXT);")
db.close()
}
fun saveSlave(mediaPath: String, type: Int, priority: Int, uriString: String, helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
val values = ContentValues()
values.put(SLAVES_MEDIA_PATH, mediaPath)
values.put(SLAVES_TYPE, type)
values.put(SLAVES_PRIORITY, priority)
values.put(SLAVES_URI, uriString)
db.replace(SLAVES_TABLE_NAME, null, values)
db.close()
}
fun saveExSubtitle(path: String, mediaName: String, helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
if (TextUtils.isEmpty(path) || TextUtils.isEmpty(mediaName))
return
val values = ContentValues()
values.put(EXTERNAL_SUBTITLES_URI, path)
values.put(EXTERNAL_SUBTITLES_MEDIA_NAME, mediaName)
db.replace(EXTERNAL_SUBTITLES_TABLE_NAME, null, values)
db.close()
}
fun saveNetworkFavItem(uri: Uri, title: String, iconUrl: String?, helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
val values = ContentValues()
values.put(NETWORK_FAV_URI, uri.toString())
values.put(NETWORK_FAV_TITLE, Uri.encode(title))
values.put(NETWORK_FAV_ICON_URL, Uri.encode(iconUrl))
db.replace(NETWORK_FAV_TABLE_NAME, null, values)
db.close()
}
fun clearDatabase(helper: SqliteTestDbOpenHelper) {
val db = helper.writableDatabase
db.execSQL("DROP TABLE IF EXISTS $EXTERNAL_SUBTITLES_TABLE_NAME")
db.execSQL("DROP TABLE IF EXISTS $SLAVES_TABLE_NAME")
db.execSQL("DROP TABLE IF EXISTS $NETWORK_FAV_TABLE_NAME")
db.close()
}
/*******************************************************************************
* SqliteTestDbOpenHelper
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*/
package org.videolan.vlc.database.helpers
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class SqliteTestDbOpenHelper(context: Context, databaseName: String, version: Int) : SQLiteOpenHelper(context, databaseName, null, version) {
override fun onCreate(db: SQLiteDatabase) {
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// Not required as at version 1
}
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// Not required as at version 1
}
}
/*******************************************************************************
* BrowserFavRepositoryTest.kt
* ****************************************************************************
* Copyright © 2018 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
******************************************************************************/
package org.videolan.vlc.repository
import android.arch.persistence.room.Room
import android.net.Uri
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import junit.framework.Assert.assertNull
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.runBlocking
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.videolan.vlc.database.MediaDatabase
import org.videolan.vlc.util.runBackground
@RunWith(AndroidJUnit4::class)
class BrowserFavRepositoryTest {
private lateinit var database: MediaDatabase
private lateinit var browserFavRepository: BrowserFavRepository
private val uri = Uri.parse("/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/file1.mkv")
@Before
fun onCreateRepository() {
val context = InstrumentationRegistry.getTargetContext()
database = Room.inMemoryDatabaseBuilder(context, MediaDatabase::class.java).build()
browserFavRepository = BrowserFavRepository(context, mediaDatabase = database)
}
@Test fun addBrowserFaveItem() {
val title = "test1"
runBlocking {
browserFavRepository.addLocalFavItem(uri, title, null).join()
val exists = browserFavRepository.browserFavExists(uri)
assertThat(exists, equalTo(true))
}
val browserFavs = browserFavRepository.getAllBrowserFavs()
assertThat(browserFavs[0].uri, equalTo(uri))
assertThat(browserFavs[0].title, equalTo(title))
assertNull(browserFavs[0].artworkURL)
}
@Test fun deleteBrowserFav() = runBlocking {
val title = "test1"
browserFavRepository.addLocalFavItem(uri, title, null).join()
browserFavRepository.deleteBrowserFav(uri)
val favs = browserFavRepository.getAllBrowserFavs()
assertThat(favs.size, equalTo(0))
}
}
\ No newline at end of file
/*********************************************************