Commit 7a12fc43 authored by Habib Kazemi's avatar Habib Kazemi Committed by Geoffrey Métais

Refactor tests

android.arch.core:core-testing dependency added so we have tools to test
arch components.
Utils that common between test and AndroidTest moved to
test-common.
Dao tests all extend DbTest so initDb and closeDb handled there.
Dao tests have new naming convention.
Signed-off-by: default avatarGeoffrey Métais <geoffrey.metais@gmail.com>
parent e68ab515
......@@ -20,86 +20,113 @@
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
import org.videolan.vlc.util.getValue
import java.org.videolan.vlc.util.TestUtil
@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)
}
class BrowserFavDaoTest: DbTest() {
@After fun closeDb() {
database.close()
}
@Test fun insertTwoNetworkAndOneLocal_GetAllShouldReturnThreeFav() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFav = TestUtil.createLocalFavs(1)[0]
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
db.browserFavDao().insert(fakeLocalFav)
@Test fun getAllBrowserFavs() {
val browsersFavs = getValue(browserFavDao.getAll())
assertThat(browsersFavs.size, equalTo(3))
/*===========================================================*/
assertThat(browsersFavs, hasItem(netwrokFav1))
assertThat(browsersFavs, hasItem(netwrokFav2))
assertThat(browsersFavs, hasItem(localFav1))
val browsersFavs = getValue(db.browserFavDao().getAll())
assertThat(browsersFavs.size, `is`(3))
assertThat(browsersFavs, hasItem(fakeNetworkFavs[0]))
assertThat(browsersFavs, hasItem(fakeNetworkFavs[1]))
assertThat(browsersFavs, hasItem(fakeLocalFav))
}
@Test fun getAllNetworkFavs() {
val networkFavs = getValue(browserFavDao.getAllNetwrokFavs())
@Test fun insertTwoNetworkAndOneLocal_GetNetworkFavsShouldReturnTwoFav() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFavs = TestUtil.createLocalFavs(1)[0]
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
db.browserFavDao().insert(fakeLocalFavs)
/*===========================================================*/
val networkFavs = getValue(db.browserFavDao().getAllNetwrokFavs())
assertThat(networkFavs.size, equalTo(2))
assertThat(networkFavs, hasItem(netwrokFav1))
assertThat(networkFavs, hasItem(netwrokFav2))
assertThat(networkFavs, hasItem(fakeNetworkFavs[0]))
assertThat(networkFavs, hasItem(fakeNetworkFavs[1]))
}
@Test fun insertTwoNetworkAndTwoLocal_GetLocalFavsShouldReturnTwoFav() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFavs = TestUtil.createLocalFavs(2)
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
fakeLocalFavs.forEach { db.browserFavDao().insert(it)}
/*===========================================================*/
val localFavs = getValue(db.browserFavDao().getAllLocalFavs())
assertThat(localFavs.size, `is`(2))
assertThat(localFavs, hasItem(localFavs[0]))
assertThat(localFavs, hasItem(localFavs[1]))
}
@Test fun insertTwoNetworkAndTwoLocal_GetFavByUriShouldReturnOneFav() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFavs = TestUtil.createLocalFavs(2)
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
fakeLocalFavs.forEach { db.browserFavDao().insert(it)}
/*===========================================================*/
val fav = db.browserFavDao().get(fakeNetworkFavs[0].uri)
@Test fun getAllLocalFavs() {
val localFavs = getValue(browserFavDao.getAllLocalFavs())
assertThat(fav.size, `is`(1))
assertThat(fav[0], `is`(fakeNetworkFavs[0]))
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 insertTwoNetworkAndTwoLocal_DeleteOne() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFavs = TestUtil.createLocalFavs(2)
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
fakeLocalFavs.forEach { db.browserFavDao().insert(it)}
/*===========================================================*/
db.browserFavDao().delete(fakeNetworkFavs[0].uri)
val favs = getValue(db.browserFavDao().getAll())
assertThat(favs.size, `is`(3))
assertThat(favs, not(hasItem(fakeNetworkFavs[0])))
}
@Test fun deleteBrowserFav() {
browserFavDao.delete(netwrokFav1.uri)
val browsers = getValue(browserFavDao.getAll())
assertThat(browsers.size, equalTo(2))
@Test fun insertTwoNetworkAndTwoLocal_DeleteAll() {
val fakeNetworkFavs = TestUtil.crateNetworkFavs(2)
val fakeLocalFavs = TestUtil.createLocalFavs(2)
fakeNetworkFavs.forEach { db.browserFavDao().insert(it) }
fakeLocalFavs.forEach { db.browserFavDao().insert(it)}
/*===========================================================*/
assertThat(browsers, not(hasItem(netwrokFav1)))
assertThat(browsers, hasItem(netwrokFav2))
fakeLocalFavs.forEach { db.browserFavDao().delete(it.uri) }
fakeNetworkFavs.forEach { db.browserFavDao().delete(it.uri) }
val favs = getValue(db.browserFavDao().getAll())
browserFavDao.delete(netwrokFav2.uri)
browserFavDao.delete(localFav1.uri)
val browserFavsAfterDeleteAll = getValue(browserFavDao.getAll())
assertThat(browserFavsAfterDeleteAll.size, equalTo(0))
assertThat(favs.size, `is`(0))
}
}
\ No newline at end of file
/*******************************************************************************
* DbTest.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.core.executor.testing.CountingTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import org.junit.After
import org.junit.Before
import org.junit.Rule
import java.util.concurrent.TimeUnit
abstract class DbTest {
@Rule
@JvmField
val countingTaskExecutorRule = CountingTaskExecutorRule()
private lateinit var _db: MediaDatabase
val db: MediaDatabase
get() = _db
@Before
fun initDb() {
_db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getTargetContext(),
MediaDatabase::class.java)
.build()
}
@After
fun closeDb() {
countingTaskExecutorRule.drainTasks(10, TimeUnit.SECONDS)
_db.close()
}
}
......@@ -20,75 +20,77 @@
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
import java.org.videolan.vlc.util.TestUtil
@RunWith(AndroidJUnit4::class)
class ExternalSubDaoTest {
class ExternalSubDaoTest: DbTest() {
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)
}
@Test fun insertTwoSubtitleForEachOfTwoMedias_GetShouldReturnTwoForEachOne() {
val foo = "foo.mkv"
val bar = "bar.mkv"
@After fun closeDb() {
database.close()
}
val fakeFooSubtitles = TestUtil.createExternalSubsForMedia(foo, 2)
val fakeBarSubtitles = TestUtil.createExternalSubsForMedia(bar, 2)
fakeFooSubtitles.forEach {
db.externalSubDao().insert(it)
}
fakeBarSubtitles.forEach {
db.externalSubDao().insert(it)
}
@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))
val fooSubtitles = db.externalSubDao().get(foo)
assertThat(fooSubtitles.size, equalTo(2))
assertThat(fooSubtitles, hasItem(fakeFooSubtitles[0]))
assertThat(fooSubtitles, hasItem(fakeFooSubtitles[1]))
val barSubtitles = db.externalSubDao().get(bar)
assertThat(barSubtitles.size, equalTo(2))
assertThat(barSubtitles, hasItem(fakeBarSubtitles[0]))
assertThat(barSubtitles, hasItem(fakeBarSubtitles[1]))
}
@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())
@Test fun InsertTwoSubtitleForOneMedia_DeleteOne() {
val foo = "foo.mkv"
val fakeFooSubtitles = TestUtil.createExternalSubsForMedia(foo, 2)
fakeFooSubtitles.forEach {
db.externalSubDao().insert(it)
}
/*===========================================================*/
db.externalSubDao().delete(fakeFooSubtitles[0])
var fooSubtitles = db.externalSubDao().get(foo)
assertThat(fooSubtitles, not(hasItem(fakeFooSubtitles[0])))
assertThat(fooSubtitles, hasItem(fakeFooSubtitles[1]))
}
@Test fun InsertTwoSubtitleForOneMedia_DeleteAll() {
val foo = "foo.mkv"
val fakeFooSubtitles = TestUtil.createExternalSubsForMedia(foo, 2)
fakeFooSubtitles.forEach {
db.externalSubDao().insert(it)
}
/*===========================================================*/
db.externalSubDao().delete(fakeFooSubtitles[0])
db.externalSubDao().delete(fakeFooSubtitles[1])
var fooSubtitles = db.externalSubDao().get(foo)
assertThat(fooSubtitles.size, `is`(0))
}
}
\ No newline at end of file
......@@ -20,56 +20,26 @@
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
import java.org.videolan.vlc.util.TestUtil
@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()
class SlaveDaoTest: DbTest() {
slaveDao.insert(slave1eng)
slaveDao.insert(slave1fa)
slaveDao.insert(slave2)
}
@After fun closeDb() {
database.close()
}
@Test fun insertTwoSubtitleSlave_GetShouldReturnJustLatOne() {
val fakeSlaves = TestUtil.createSubtitleSlavesForMedia("foo", 2)
fakeSlaves.forEach {
db.slaveDao().insert(it)
}
@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))
val slaves = db.slaveDao().get(fakeSlaves[0].mediaPath)
assertThat(slaves.size, equalTo(1))
assertThat(slaves, hasItem(fakeSlaves[1]))
}
}
......@@ -177,10 +177,10 @@ android {
res.srcDirs = ['flavors/debug/res']
}
sourceSets.test {
java.srcDirs = ['test']
java.srcDirs = ['test', 'test-common']
}
sourceSets.androidTest {
java.srcDirs = ['androidTest']
java.srcDirs = ['androidTest', 'test-common']
assets.srcDirs += files("$projectDir/assets/schemas".toString())
}
}
......@@ -223,6 +223,9 @@ dependencies {
androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
testImplementation "junit:junit:$rootProject.ext.junitVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.ext.roomVersion"
testImplementation "android.arch.core:core-testing:$rootProject.ext.archVersion"
androidTestImplementation "android.arch.core:core-testing:$rootProject.ext.archVersion"
}
static def buildTime() {
......
/*******************************************************************************
* TestUtil.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 java.org.videolan.vlc.util
import android.net.Uri
import org.videolan.libvlc.Media
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
object TestUtil {
private const val fakeUri: String = "https://www.videolan.org/fake_"
private const val fakeSubUri: String = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/subs/"
private const val fakeMediaUri: String = "/storage/emulated/0/Android/data/org.videolan.vlc.debug/files/medias/"
fun createLocalFav(uri: Uri, title: String, iconUrl: String?): BrowserFav {
return BrowserFav(uri, Constants.TYPE_LOCAL_FAV, title, iconUrl)
}
fun createLocalFavs(count: Int): List<BrowserFav> {
return (0 until count).map {
createLocalFav(Uri.parse(fakeUri + "local" + it),
"foo" + 1,
null)
}
}
fun createNetworkFav(uri: Uri, title: String, iconUrl: String?): BrowserFav {
return BrowserFav(uri, Constants.TYPE_NETWORK_FAV, title, iconUrl)
}
fun crateNetworkFavs(count: Int): List<BrowserFav> {
return (0 until count).map {
createNetworkFav(
Uri.parse(fakeUri + "network" + it),
"foo" + 1,
null)
}
}
fun createExternalSub(uri: String, mediaName: String): ExternalSub {
return ExternalSub(uri, mediaName)
}
fun createExternalSubsForMedia(mediaName: String, count: Int): List<ExternalSub> {
return (0 until count).map {
ExternalSub("$fakeSubUri$mediaName$it", mediaName)
}
}
fun createSubtitleSlave(mediaPath: String, uri: String): Slave {
return Slave(mediaPath, Media.Slave.Type.Subtitle, 2, uri)
}
fun createSubtitleSlavesForMedia(mediaName: String, count:Int): List<Slave> {
return (0 until count).map {
createSubtitleSlave( "$fakeMediaUri$mediaName", "$fakeSubUri$mediaName$it.srt" )
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment