Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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)
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ appcompat = "1.7.1"
coil = "2.7.0"
# @keep
compileSdk = "37"
compose-latest = "1.10.5"
compose-latest = "1.12.0-alpha01"
Comment thread
kkuan2011 marked this conversation as resolved.
composeUiTooling = "1.5.6"
coreSplashscreen = "1.2.0"
coroutines = "1.10.2"
Expand Down Expand Up @@ -214,6 +214,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