diff --git a/app/build.gradle b/app/build.gradle index 59c76f8..7e44417 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/androidTest/java/com/example/h_mal/myapplication/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/h_mal/myapplication/ExampleInstrumentedTest.kt deleted file mode 100644 index 33816e0..0000000 --- a/app/src/androidTest/java/com/example/h_mal/myapplication/ExampleInstrumentedTest.kt +++ /dev/null @@ -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) - } - - -} diff --git a/app/src/androidTest/java/com/example/h_mal/myapplication/ui/MainActivityTest.kt b/app/src/androidTest/java/com/example/h_mal/myapplication/ui/MainActivityTest.kt index b361118..f6e165b 100644 --- a/app/src/androidTest/java/com/example/h_mal/myapplication/ui/MainActivityTest.kt +++ b/app/src/androidTest/java/com/example/h_mal/myapplication/ui/MainActivityTest.kt @@ -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() + 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 = mutableListOf(item1,item2).toList() + + val long = runBlocking { repoDao.saveAllRepos(list) } + + assertEquals(long.size, 2) + } } diff --git a/app/src/androidTest/java/com/example/h_mal/myapplication/MainActivityTest.kt b/app/src/androidTest/java/com/example/h_mal/myapplication/ui/UiTest.kt similarity index 56% rename from app/src/androidTest/java/com/example/h_mal/myapplication/MainActivityTest.kt rename to app/src/androidTest/java/com/example/h_mal/myapplication/ui/UiTest.kt index ffe12c7..ec3fd92 100644 --- a/app/src/androidTest/java/com/example/h_mal/myapplication/MainActivityTest.kt +++ b/app/src/androidTest/java/com/example/h_mal/myapplication/ui/UiTest.kt @@ -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( diff --git a/app/src/main/java/com/example/h_mal/myapplication/db/RepoDao.kt b/app/src/main/java/com/example/h_mal/myapplication/db/RepoDao.kt index c385995..e2af147 100644 --- a/app/src/main/java/com/example/h_mal/myapplication/db/RepoDao.kt +++ b/app/src/main/java/com/example/h_mal/myapplication/db/RepoDao.kt @@ -11,7 +11,7 @@ import com.example.h_mal.myapplication.model.Repo interface RepoDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun saveAllRepos(repos : List) + fun saveAllRepos(repos : List) : List @Query("SELECT * FROM Repo") fun getRepos() : LiveData>