Skip to content

Commit

Permalink
test: Add basic composeui integration testing module
Browse files Browse the repository at this point in the history
Signed-off-by: utzcoz <[email protected]>
  • Loading branch information
utzcoz committed Nov 20, 2024
1 parent 9888ec4 commit bcf831f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins {
alias(libs.plugins.robolectric.spotless)
alias(libs.plugins.robolectric.javadoc)
alias(libs.plugins.roborazzi) apply false
alias(libs.plugins.compose.compiler) apply false
}

allprojects {
Expand Down
11 changes: 11 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ androidx-test-monitor = "1.7.2"
androidx-test-runner = "1.6.2"
androidx-test-services = "1.5.0"

# https://developer.android.com/jetpack/androidx/releases/compose
androidx-compose-bom = "2024.10.01"

# for shadows/playservices/build.gradle
androidx-fragment-for-shadows = "1.2.0"
play-services-for-shadows = "17.0.0"
Expand Down Expand Up @@ -230,6 +233,13 @@ androidx-test-espresso-intents = { module = "androidx.test.espresso:espresso-int
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-ext-junit" }
androidx-test-ext-truth = { module = "androidx.test.ext:truth", version.ref = "androidx-test-ext-truth" }

androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "androidx-compose-bom" }
androidx-compose-ui = { module = "androidx.compose.ui:ui" }
androidx-compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" }
androidx-compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-compose-material3 = { module = "androidx.compose.material3:material3" }

androidx-fragment-for-shadows = { module = "androidx.fragment:fragment", version.ref = "androidx-fragment-for-shadows" }
play-services-auth-for-shadows = { module = "com.google.android.gms:play-services-auth", version.ref = "play-services-for-shadows" }
play-services-base-for-shadows = { module = "com.google.android.gms:play-services-base", version.ref = "play-services-for-shadows" }
Expand All @@ -256,6 +266,7 @@ jacoco = { id = "jacoco" }
java = { id = "java" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
robolectric-android-project = { id = "org.robolectric.gradle.AndroidProjectConfigPlugin" }
robolectric-deployed-java-module = { id = "org.robolectric.gradle.DeployedRoboJavaModulePlugin" }
robolectric-gradle-managed-device = { id = "org.robolectric.gradle.GradleManagedDevicePlugin" }
Expand Down
50 changes: 50 additions & 0 deletions integration_tests/composeui/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.robolectric.android.project)
alias(libs.plugins.robolectric.spotless)
}

android {
namespace = "org.robolectric.integrationtests.composeui"
compileSdk = 35

defaultConfig { minSdk = 21 }

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions { jvmTarget = "11" }

buildFeatures { compose = true }

testOptions {
targetSdk = 35
unitTests { isIncludeAndroidResources = true }
}

androidComponents {
beforeVariants(selector().all()) { variantBuilder ->
// composeui does not support AndroidTest now.
variantBuilder.enableAndroidTest = false
}
}
}

dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.material3)

debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.ui.test.manifest)

testImplementation(project(":robolectric"))
testImplementation(libs.junit4)
testImplementation(libs.truth)
testImplementation(libs.androidx.compose.ui.test.junit4)
}
2 changes: 2 additions & 0 deletions integration_tests/composeui/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.robolectric.integrationtests.composeui

import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.SemanticsProperties.EditableText
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

/** The test file for ComposeUI's Font compatibility with Robolectric. */
@RunWith(RobolectricTestRunner::class)
@Config(instrumentedPackages = ["androidx.loader.content"])
class FontTest {
@get:Rule val rule = createComposeRule()

@Test
fun `TextField's default value is expected`() {
rule.setContent { NumberInput() }

with(rule) {
onNodeWithTag(TAG_NUMBER_INPUT).fetchSemanticsNode().run {
assertThat(config[EditableText].text).isEqualTo(TEXT_CUSTOM_NUMBER)
}
}
}

@Composable
fun NumberInput() {
var text by remember { mutableStateOf(TextFieldValue(TEXT_CUSTOM_NUMBER)) }

TextField(
modifier = Modifier.semantics { testTag = TAG_NUMBER_INPUT },
value = text,
textStyle = TextStyle.Default,
onValueChange = { newText -> text = newText },
)
}

companion object {
const val TAG_NUMBER_INPUT = "number_input"
const val TEXT_CUSTOM_NUMBER = "123456789"
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include(
":errorprone",
":integration_tests:androidx",
":integration_tests:androidx_test",
":integration_tests:composeui",
":integration_tests:ctesque",
":integration_tests:dependency-on-stubs",
":integration_tests:jacoco-offline",
Expand Down

0 comments on commit bcf831f

Please sign in to comment.