Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add first intrumentation test to demo #319

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ android {
versionCode = 1
versionName = "1.0"
vectorDrawables.useSupportLibrary = true

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
Expand Down Expand Up @@ -55,6 +57,7 @@ android {
sourceSets {
named("main") { java { srcDirs("src/main/kotlin") } }
named("test") { java { srcDirs("src/test/kotlin") } }
named("androidTest") { java { srcDirs("src/androidTest/kotlin") } }
named("debug") { java { srcDirs("src/debug/kotlin") } }
}

Expand All @@ -76,6 +79,15 @@ dependencies {
implementation("dev.hotwire:strada:1.0.0-beta3")

implementation(project(":turbo"))

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.ext:junit-ktx:1.1.5")
androidTestImplementation("androidx.test:core-ktx:1.5.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation("com.adevinta.android:barista:4.2.0") {
exclude(group = "org.jetbrains.kotlin") // Only if you already use Kotlin in your project
}
androidTestImplementation("androidx.test.espresso:espresso-web:3.5.1")
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.hotwire.turbo.demo

import android.view.View
import android.view.ViewGroup
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.web.sugar.Web.onWebView
import androidx.test.espresso.web.webdriver.DriverAtoms.findElement
import androidx.test.espresso.web.webdriver.DriverAtoms.getText
import androidx.test.espresso.web.webdriver.Locator
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed
import com.adevinta.android.barista.internal.matcher.DrawableMatcher.Companion.withDrawable
import dev.hotwire.turbo.demo.main.MainActivity
import junit.framework.TestCase.assertEquals
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TurboNativeFeatureTest {
@get:Rule
val activityScenarioRule =
activityScenarioRule<MainActivity>()

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.hotwire.turbo.demo",
appContext.packageName)
}

@Test
fun displayScreenTitle() {
onView(withId(R.id.app_bar_logo))
.check(matches(withDrawable(R.drawable.ic_turbo_logo)))
}

@Test
fun displayMainTurboView() {
onView(withId(R.id.app_bar_logo))
Thread.sleep(1000)
onWebView().forceJavascriptEnabled()
.withElement(findElement(Locator.LINK_TEXT, "Navigate to another webpage"))
}

fun nthChildOf(parentMatcher: Matcher<View>, childPosition: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("position $childPosition of parent ")
parentMatcher.describeTo(description)
}

public override fun matchesSafely(view: View): Boolean {
if (view.parent !is ViewGroup) return false
val parent = view.parent as ViewGroup

return (parentMatcher.matches(parent)
&& parent.childCount > childPosition
&& parent.getChildAt(childPosition) == view)
}
}
}
}