Skip to content

Commit

Permalink
impl: 244: sample for Shopify#244
Browse files Browse the repository at this point in the history
  • Loading branch information
oradkovsky committed Sep 25, 2021
1 parent 8ad705e commit 0fab088
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ android {
buildFeatures {
viewBinding true
}

kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
Expand All @@ -63,4 +67,5 @@ dependencies {
androidTestImplementation "androidx.test:runner:${versions.androidx.test.runner}"
androidTestImplementation "org.mockito:mockito-android:${versions.mockitoAndroid}"
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:${versions.mockitokotlin}"
androidTestImplementation "androidx.test:core-ktx:${versions.androidx.test.coreKtx}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.shopify.testify.sample

import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import com.shopify.testify.sample.clients.MockClientData
import com.shopify.testify.sample.clients.details.ClientDetailsActivity
import com.shopify.testify.sample.clients.index.ClientListActivity
import org.hamcrest.CoreMatchers.endsWith
import org.junit.Test

/**
* Intention behind this setup is to share typical use cases utilizing ActivityScenario:
* https://developer.android.com/reference/androidx/test/core/app/ActivityScenario
*/
class ActivityScenarioExampleTest {
@Test
fun activityScenarioNoIntentUseCase() {
launchActivity<ClientListActivity>().use {
onView(ViewMatchers.withClassName(endsWith("RecyclerView")))
.perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(THE_CLIENT_POSITION, scrollTo()))
.check(matches(hasDescendant(withText(THE_CLIENT.name))))
}
}

@Test
fun activityScenarioIntentUseCase() {
launch<ClientDetailsActivity>(
ClientDetailsActivity.createClientDetailsActivityIntent(
InstrumentationRegistry.getInstrumentation().targetContext,
THE_CLIENT
)
).use {
onView(withId(R.id.address)).check(matches(withText(R.string.mock_address)))
}
}

companion object {
const val THE_CLIENT_POSITION = 13
val THE_CLIENT = MockClientData.CLIENTS[THE_CLIENT_POSITION]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.shopify.testify.sample.clients.details

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -60,10 +61,13 @@ class ClientDetailsActivity : AppCompatActivity() {
private const val EXTRA_CLIENT_ITEM = "client_item"

fun Activity.startClientDetailsActivity(item: MockClientData.Client) {
val intent = Intent(this, ClientDetailsActivity::class.java)
intent.putExtra(EXTRA_CLIENT_ITEM, item)
this.startActivity(intent)
startActivity(createClientDetailsActivityIntent(this, item))
}

fun createClientDetailsActivityIntent(context: Context, item: MockClientData.Client): Intent {
return Intent(context, ClientDetailsActivity::class.java).apply {
putExtra(EXTRA_CLIENT_ITEM, item)
}
}
}
}
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ buildscript {
ext {
versions = [
'androidGradlePlugin': '4.2.0', // https://developer.android.com/studio/releases/gradle-plugin.html
'androidx' : [
'androidx': [
'constraintLayout': '2.0.0-beta4', // https://developer.android.com/jetpack/androidx/releases
'appCompat' : '1.2.0', // https://developer.android.com/jetpack/androidx/releases
'coreKtx' : '1.1.0', // https://developer.android.com/jetpack/androidx/releases
'test' : [
'core' : '2.1.0', // https://developer.android.com/jetpack/androidx/releases
'coreKtx' : '1.4.0', // https://mvnrepository.com/artifact/androidx.test/core-ktx
'espresso': '3.3.0', // https://developer.android.com/jetpack/androidx/releases
'junit' : '1.1.2', // https://developer.android.com/jetpack/androidx/releases
'rules' : '1.3.0', // https://developer.android.com/jetpack/androidx/releases
'runner' : '1.3.0', // https://developer.android.com/jetpack/androidx/releases
]
],
'fragment' : '1.3.6', // https://developer.android.com/jetpack/androidx/releases/fragment
],
'colormath' : '1.4.1', // https://github.com/ajalt/colormath/releases
'dokka' : '1.4.32', // https://github.com/Kotlin/dokka/releases
Expand Down

0 comments on commit 0fab088

Please sign in to comment.