UI test for repository added

This commit is contained in:
2020-04-22 15:17:53 +01:00
parent b9a66c3a79
commit c1f61eee3b
12 changed files with 151 additions and 22 deletions

View File

@@ -72,4 +72,5 @@ dependencies {
implementation "androidx.preference:preference-ktx:1.1.0" implementation "androidx.preference:preference-ktx:1.1.0"
implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.squareup.picasso:picasso:2.71828'
androidTestImplementation 'androidx.test:rules:1.3.0-alpha05'
} }

View File

@@ -1,13 +1,11 @@
package com.example.h_mal.candyspace package com.example.h_mal.candyspace
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.junit.Assert.*
/** /**
* Instrumented test, which will execute on an Android device. * Instrumented test, which will execute on an Android device.
* *

View File

@@ -0,0 +1,135 @@
package com.example.h_mal.candyspace.ui.main
import android.view.View
import android.view.ViewGroup
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import com.example.h_mal.candyspace.R
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.*
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@LargeTest
@RunWith(AndroidJUnit4::class)
class AppUITest {
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(MainActivity::class.java)
@Test
fun launchApp_SuccessfulLaunch() {
onView(allOf(withId(R.id.action_bar), isDisplayed())).check(matches(withText("Candy Space")))
onView(allOf(withId(R.id.submit), isDisplayed())).check(matches(isDisplayed()))
onView(allOf(withId(R.id.search_bar), isDisplayed())).check(matches(isDisplayed()))
onView(allOf(withId(R.id.recycler_view), isDisplayed())).check(matches(isDisplayed()))
}
@Test
fun RunSearch_SuccessfulReturn() {
onView(allOf(withId(R.id.submit), isDisplayed())).check(matches(isDisplayed()))
onView(allOf(withId(R.id.search_bar), isDisplayed())).perform(replaceText("kenny"),
closeSoftKeyboard())
onView(allOf(withId(R.id.submit), isDisplayed())).perform(click())
//wait for load
waitFor(2000)
onView(
allOf(
childAtPosition(
allOf(
withId(R.id.recycler_view),
childAtPosition(
withId(R.id.main),
1
)
),
0
)
)
).perform(click())
onView(allOf(withId(R.id.action_bar), isDisplayed())).check(matches(withText("User")))
onView(allOf(withId(R.id.username), isDisplayed())).check(matches(isDisplayed()))
}
@Test
fun backButtonPressed_SuccessfulResponse() {
onView(allOf(withId(R.id.submit), isDisplayed())).check(matches(isDisplayed()))
onView(allOf(withId(R.id.search_bar), isDisplayed())).perform(replaceText("kenny"),
closeSoftKeyboard())
onView(allOf(withId(R.id.submit), isDisplayed())).perform(click())
//wait for load
waitFor(2000)
onView(
allOf(
childAtPosition(
allOf(
withId(R.id.recycler_view),
childAtPosition(
withId(R.id.main),
1
)
),
0
)
)
).perform(click())
onView(allOf(withId(R.id.action_bar), isDisplayed())).check(matches(withText("User")))
onView(childAtPosition(withId(R.id.action_bar_container), 0)).perform(click())
onView(allOf(withId(R.id.action_bar), isDisplayed())).check(matches(withText("Candy Space")))
}
private fun childAtPosition(
parentMatcher: Matcher<View>, position: Int
): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}
public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
private fun waitFor(delay: Long): ViewAction? {
return object : ViewAction {
override fun getConstraints(): Matcher<View> {
return isRoot()
}
override fun getDescription(): String {
return "wait for " + delay + "milliseconds"
}
override fun perform(uiController: UiController, view: View?) {
uiController.loopMainThreadForAtLeast(delay)
}
}
}
}

View File

@@ -7,6 +7,7 @@ import java.io.IOException
abstract class ResponseUnwrap { abstract class ResponseUnwrap {
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun<T: Any> responseUnwrap(call: suspend () -> Response<T>) : T{ suspend fun<T: Any> responseUnwrap(call: suspend () -> Response<T>) : T{
val response = call.invoke() val response = call.invoke()

View File

@@ -1,7 +1,5 @@
package com.example.h_mal.candyspace.data.api.model package com.example.h_mal.candyspace.data.api.model
import com.example.h_mal.candyspace.data.api.model.User
data class ApiResponse( data class ApiResponse(
val items : List<User>?, val items : List<User>?,
val has_more : Boolean?, val has_more : Boolean?,

View File

@@ -1,8 +1,8 @@
package com.example.h_mal.candyspace.data.repositories package com.example.h_mal.candyspace.data.repositories
import com.example.h_mal.candyspace.data.api.ApiClass import com.example.h_mal.candyspace.data.api.ApiClass
import com.example.h_mal.candyspace.data.api.model.ApiResponse
import com.example.h_mal.candyspace.data.api.ResponseUnwrap import com.example.h_mal.candyspace.data.api.ResponseUnwrap
import com.example.h_mal.candyspace.data.api.model.ApiResponse
class Repository( class Repository(
private val api: ApiClass private val api: ApiClass

View File

@@ -1,8 +1,8 @@
package com.example.h_mal.candyspace.ui.main package com.example.h_mal.candyspace.ui.main
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import com.example.h_mal.candyspace.R import com.example.h_mal.candyspace.R
import com.example.h_mal.candyspace.ui.home.MainFragment import com.example.h_mal.candyspace.ui.home.MainFragment
@@ -34,8 +34,8 @@ class MainActivity : AppCompatActivity(), KodeinAware, CompletionListener {
setContentView(R.layout.main_activity) setContentView(R.layout.main_activity)
//setup home button for back navigation //setup home button for back navigation
supportActionBar?.setDisplayHomeAsUpEnabled(true); supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true); supportActionBar?.setDisplayShowHomeEnabled(true)
//retrieve viewmodel from viewmodel factory //retrieve viewmodel from viewmodel factory
viewModel = ViewModelProvider(this, factory).get(MainViewModel::class.java) viewModel = ViewModelProvider(this, factory).get(MainViewModel::class.java)

View File

@@ -1,10 +1,11 @@
package com.example.h_mal.candyspace.ui.user package com.example.h_mal.candyspace.ui.user
import android.os.Bundle import android.os.Bundle
import android.view.* import android.view.LayoutInflater
import androidx.fragment.app.Fragment import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.example.h_mal.candyspace.R import com.example.h_mal.candyspace.R
import com.example.h_mal.candyspace.databinding.FragmentUserProfileBinding import com.example.h_mal.candyspace.databinding.FragmentUserProfileBinding
import com.example.h_mal.candyspace.ui.main.MainActivity.Companion.viewModel import com.example.h_mal.candyspace.ui.main.MainActivity.Companion.viewModel

View File

@@ -1,6 +1,5 @@
package com.example.h_mal.candyspace.utils package com.example.h_mal.candyspace.utils
import com.squareup.picasso.Picasso
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*

View File

@@ -1,9 +1,8 @@
package com.example.h_mal.candyspace package com.example.h_mal.candyspace
import org.junit.Assert.assertEquals
import org.junit.Test import org.junit.Test
import org.junit.Assert.*
/** /**
* Example local unit test, which will execute on the development machine (host). * Example local unit test, which will execute on the development machine (host).
* *

View File

@@ -5,9 +5,9 @@ import com.example.h_mal.candyspace.data.api.model.ApiResponse
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.MediaType import okhttp3.MediaType
import okhttp3.ResponseBody import okhttp3.ResponseBody
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before import org.junit.Before
import org.junit.Assert.*
import org.junit.Test import org.junit.Test
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito

View File

@@ -1,14 +1,12 @@
package com.example.h_mal.candyspace.ui.main package com.example.h_mal.candyspace.ui.main
import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.example.h_mal.candyspace.data.api.ApiClass
import com.example.h_mal.candyspace.data.api.model.ApiResponse import com.example.h_mal.candyspace.data.api.model.ApiResponse
import com.example.h_mal.candyspace.data.api.model.User import com.example.h_mal.candyspace.data.api.model.User
import com.example.h_mal.candyspace.data.repositories.Repository import com.example.h_mal.candyspace.data.repositories.Repository
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Assert.*
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.TestRule import org.junit.rules.TestRule
@@ -16,7 +14,6 @@ import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.Mockito.mock import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import retrofit2.Response
import java.io.IOException import java.io.IOException
class MainViewModelTest { class MainViewModelTest {