Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions compose/snippets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ dependencies {
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.test.espresso.core)
androidTestImplementation(libs.androidx.test.espresso.contrib)
androidTestImplementation(libs.androidx.compose.ui.test)
debugImplementation(libs.androidx.compose.ui.tooling)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.compose.snippets.test

import android.view.View
import androidx.compose.material3.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
Expand All @@ -24,12 +25,23 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onRootWithViewInteraction
Comment thread
hiichoudhary marked this conversation as resolved.
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.v2.runComposeUiTest
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions.swipeLeft
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import org.hamcrest.CoreMatchers.allOf
import org.junit.Rule
import org.junit.Test

Expand Down Expand Up @@ -84,3 +96,50 @@ class Test2 {
}
// [END android_compose_test_v2_apis]
}

@OptIn(ExperimentalTestApi::class)
class OnRootWithViewInteractionApiSnippets {
private val recyclerViewId = View.generateViewId()
private val rootViewId = View.generateViewId()
private val viewPagerViewId = View.generateViewId()
private val fragmentRootViewId = View.generateViewId()
Comment thread
hiichoudhary marked this conversation as resolved.
// [START android_compose_test_onRootWithViewInteraction_api_1]
@Test
fun testComposeButtonInsideRecyclerViewItem() = runComposeUiTest {
// Scroll to the desired position using Espresso
Espresso.onView(withId(recyclerViewId))
.perform(RecyclerViewActions.scrollToPosition<MyViewHolder>(3))

// Define an Espresso ViewInteraction that uniquely identifies the row
val rowView = Espresso.onView(
allOf(
withId(rootViewId),
hasDescendant(withText("Item #3"))
)
)

// Scope the Compose search strictly to that specific row View
onRootWithViewInteraction(rowView)
.onNode(hasText("Like"))
.performClick()
Comment thread
hiichoudhary marked this conversation as resolved.
}
// [END android_compose_test_onRootWithViewInteraction_api_1]

// [START android_compose_test_onRootWithViewInteraction_api_2]
@Test
fun testComposeButtonInsideViewPagerItem() = runComposeUiTest {
// Swipe to the desired page using Espresso
Espresso.onView(withId(viewPagerViewId)).perform(swipeLeft())

// Identify the specific container view using Espresso
val fragmentB = Espresso.onView(withId(fragmentRootViewId))

// The generic text "Save" is now unique within this view scope
onRootWithViewInteraction(fragmentB)
.onNode(hasText("Save"))
.assertIsDisplayed()
}
// [END android_compose_test_onRootWithViewInteraction_api_2]
}

private class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ coil = "2.7.0"
# @keep
compileSdk = "37"
compose-latest = "1.10.5"
compose-test-latest = "1.12.0-alpha01"
composeUiTooling = "1.5.6"
coreSplashscreen = "1.2.0"
coroutines = "1.10.2"
Expand Down Expand Up @@ -155,8 +156,8 @@ androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime
androidx-compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose-latest" }
androidx-compose-ui-googlefonts = { module = "androidx.compose.ui:ui-text-google-fonts" }
androidx-compose-ui-graphics = { module = "androidx.compose.ui:ui-graphics" }
androidx-compose-ui-test = { module = "androidx.compose.ui:ui-test", version.ref = "compose-latest" }
androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose-latest" }
androidx-compose-ui-test = { module = "androidx.compose.ui:ui-test", version.ref = "compose-test-latest" }
Comment thread
hiichoudhary marked this conversation as resolved.
Outdated
androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose-test-latest" }
androidx-compose-ui-test-junit4-accessibility = { module = "androidx.compose.ui:ui-test-junit4-accessibility", version.ref = "androidx-compose-ui-test-junit4-accessibility" }
androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
Expand Down Expand Up @@ -214,6 +215,7 @@ androidx-registry-provider-play-services = { module = "androidx.credentials.regi
androidx-startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "androidx-startup-runtime" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
androidx-test-espresso-contrib = { module = "androidx.test.espresso:espresso-contrib", version.ref = "androidx-test-espresso" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-junit" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test" }
androidx-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "tiles" }
Expand Down
Loading