Skip to content

Instantly share code, notes, and snippets.

Avatar

Oleksandr Melnykov makovkastar

View GitHub Profile
View ContentLoadingSwipeRefreshLayout.kt
import android.content.Context
import android.os.SystemClock
import android.support.v4.widget.SwipeRefreshLayout
import android.util.AttributeSet
/**
* ContentLoadingSwipeRefreshLayout implements a SwipeRefreshLayout that waits a minimum time to
* dismiss the refreshing indicator before showing. Once visible, the refreshing indicator will be
* visible for a minimum amount of time to avoid "flashes" in the UI when an event could take
* a largely variable time to complete (from none, to a user perceivable amount).
View gist:6a251596f1041a8412cb5782c710b7d9
class TaskDiffCallback : DiffUtil.ItemCallback<Task>() {
override fun areItemsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem?.id == newItem?.id
}
override fun areContentsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem == newItem
}
}
View SingleFragmentActivity.kt
class SingleFragmentActivity : AppCompatActivity() {
fun setFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.add(android.R.id.content, fragment)
.commit()
}
}
View LoginFragmentTest.kt
@Test
fun navigateToForgotPasswordPage_StartsBrowser() {
intending(not(isInternal())).respondWith(
Instrumentation.ActivityResult(Activity.RESULT_OK, null))
navigateToForgotPasswordPage.call()
intended(allOf(hasAction(Intent.ACTION_VIEW),
hasData(Urls.FORGOT_PASSWORD)))
}
View LoginFragmentTest.kt
@Test
fun clickForgotPasswordButton_CallsViewModel() {
onView(withId(R.id.label_forgot_password)).perform(click())
verify(viewModel).onForgotPasswordLabelClicked()
verifyNoMoreInteractions(viewModel)
}
@Test
fun clickSignInButton_CallsViewModel() {
View LoginFragmentTest.kt
@Test
fun isSignInButtonEnabledBinding() {
isSignInButtonEnabled.set(true)
onView(withId(R.id.button_sign_in)).check(matches(isEnabled()))
}
@Test
fun isProgressBarVisibleBinding() {
isProgressBarVisible.set(true)
onView(withId(R.id.progress_overlay)).check(matches(isDisplayed()))
View LoginFragmentTest.kt
@Test
fun emailAndPasswordAreEmptyInitially() {
onView(withId(R.id.edit_text_email)).check(matches(withText("")))
onView(withId(R.id.edit_text_password)).check(matches(withText("")))
}
View LoginFragmentTest.kt
@MediumTest
@RunWith(AndroidJUnit4::class)
class LoginFragmentTest {
@Rule
@JvmField
val activityRule = IntentsTestRule(SingleFragmentActivity::class.java)
@Rule
@JvmField
View build.gradle
android {
...
defaultConfig {
...
testInstrumentationRunner "com.melnykov.mvvmtesting.testutil.AndroidTestRunner"
}
}
View AndroidTestRunner.kt
class AndroidTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader, className: String, context: Context): Application {
// The DexOpener library allows to make Kotlin classes, properties and functions
// open during tests in order to mock then with Mockito.
DexOpener.install(this)
return super.newApplication(cl, AndroidTestApplication::class.java.name, context)
}
}