MVVM implementation

This commit is contained in:
2020-01-24 20:07:20 +00:00
parent 041b41710f
commit fdb98d5cf5
5 changed files with 90 additions and 43 deletions

View File

@@ -37,8 +37,10 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
//Retrofit and GSON
implementation 'com.squareup.retrofit2:retrofit:2.6.0'

View File

@@ -1,26 +0,0 @@
package com.example.h_mal.myapplication
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("com.example.h_mal.myapplication", appContext.packageName)
}
}

View File

@@ -2,13 +2,73 @@ package com.example.h_mal.myapplication.ui
import android.content.Context
import androidx.lifecycle.Observer
import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.LargeTest
import androidx.test.runner.AndroidJUnit4
import com.example.h_mal.myapplication.db.AppDatabase
import com.example.h_mal.myapplication.db.RepoDao
import com.example.h_mal.myapplication.model.Repo
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertSame
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
class ExampleInstrumentedTest {
private lateinit var repoDao: RepoDao
private lateinit var db: AppDatabase
private lateinit var context: Context
@Before
fun createDb() {
context = ApplicationProvider.getApplicationContext<Context>()
db = AppDatabase.invoke(context)
repoDao = db.getRepoDao()
}
@After
@Throws(IOException::class)
fun closeDb() {
db.clearAllTables()
}
@Test
@Throws(Exception::class)
fun addAndUpdateEntries() {
val item1 = Repo(
274562,
"yajl-objc",
"",
"",
"",
""
)
val item2 = Repo(
274563,
"simplerrd",
"",
"",
"",
""
)
val list: List<Repo> = mutableListOf(item1,item2).toList()
val long = runBlocking { repoDao.saveAllRepos(list) }
assertEquals(long.size, 2)
}
}

View File

@@ -1,35 +1,46 @@
package com.example.h_mal.myapplication
package com.example.h_mal.myapplication.ui
import android.support.test.espresso.Espresso
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.view.View
import android.view.ViewGroup
import com.example.h_mal.myapplication.ui.MainActivity
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import com.example.h_mal.myapplication.R
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anything
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
class UiTest {
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(MainActivity::class.java)
@Test
fun mainActivityTest() {
fun uiTest() {
val frameLayout = onData(anything())
.inAdapterView(
allOf(
withId(R.id.list_view),
childAtPosition(
withId(R.id.swipe_refresh),
0
)
)
)
.atPosition(0)
frameLayout.perform(click())
}
private fun childAtPosition(

View File

@@ -11,7 +11,7 @@ import com.example.h_mal.myapplication.model.Repo
interface RepoDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveAllRepos(repos : List<Repo>)
fun saveAllRepos(repos : List<Repo>) : List<Long>
@Query("SELECT * FROM Repo")
fun getRepos() : LiveData<List<Repo>>