Skip to content

Commit

Permalink
Add version of testActivity enabling launching via Intent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaran Flaath committed Apr 28, 2021
1 parent 8103eb1 commit 41914bc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.agens.darjeeling

import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import no.agens.darjeeling.android.DarjeelingUtils.eventuallyActivityLaunched
import no.agens.darjeeling.android.ext.button
import no.agens.darjeeling.android.ext.textView
Expand Down Expand Up @@ -30,4 +32,19 @@ class TestActivityExampleTest {
}
}
}

@Test
fun testActivityWithIntent() {

val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
intent.putExtra("message", "Hello, Test!")

testActivity<MainActivity>(intent) {

onActivity {
button(R.id.buttonHello).performClick()
assertEquals("Hello, Test!", textView(R.id.textDemo).text.toString())
}
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/no/agens/darjeeling/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}

val message = intent?.getStringExtra("message")

buttonHello.setOnClickListener {
textDemo.text = "Activity says hello."
textDemo.text = if (message.isNullOrEmpty()) "Activity says hello." else message
}
}

Expand Down
2 changes: 1 addition & 1 deletion darjeeling-android-testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
minSdkVersion(21)
targetSdkVersion(30)
versionCode = gitInfo.count
versionName = "0.90.b4"
versionName = "0.90"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.agens.darjeeling.android

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
Expand All @@ -9,6 +10,15 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.intent.Intents
import kotlin.reflect.KClass

fun <T : Activity> testActivity(intent: Intent, perform: ActivityScenario<T>.() -> Unit) {
Intents.init()
try {
ActivityScenario.launch<T>(intent).use { it.perform() }
} finally {
Intents.release()
}
}

fun <T : Activity> testActivity(activityClass: KClass<T>, perform: ActivityScenario<T>.() -> Unit) {

Intents.init()
Expand Down
12 changes: 12 additions & 0 deletions docs/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ testActivity(MainActivity::class) {
}
```

You can also test your activity using an `Intent`, for passing in extras etc:
```
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
intent.putExtra("message", "Hello, Test!")
testActivity<MainActivity>(intent) {
onActivity { activity ->
// TODO perform operations on the activity and assertions here
}
}
```

Further documentation on what you can do with the ActivityScenario can
be found [here](https://developer.android.com/reference/androidx/test/core/app/ActivityScenario).
[This blog post on testing with ActivityScenario](https://medium.com/google-developer-experts/stepping-into-activity-tests-with-activityscenarios-5db98d5311e6) can also be helpful for getting into activity testing.
Expand Down

0 comments on commit 41914bc

Please sign in to comment.