Skip to content

Commit

Permalink
Add default theme test
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Dec 12, 2024
1 parent b058d86 commit fd45f1b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ androidx-lifecycle = "2.6.1"
androidx-navigation = "2.7.7"
androidx-test-espresso-core = "3.5.1"
androidx-test-ext-junit = "1.2.1"
androidx-test-core = "1.6.1"
kim = "0.17.7"

dropbox-differ = "0.3.0"
Expand Down Expand Up @@ -86,6 +87,7 @@ androidx-navigation-ui-ktx = { module = "androidx.navigation:navigation-ui-ktx",
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso-core" }
androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-ext-junit" }
androidx-test-ext-junit-ktx = { module = "androidx.test.ext:junit-ktx", version.ref = "androidx-test-ext-junit" }
androidx-test-core-ktx = { group = "androidx.test", name = "core-ktx", version.ref = "androidx-test-core" }
kim = { module = "com.ashampoo:kim", version.ref = "kim" }

android-tools-build-gradle = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down
1 change: 1 addition & 0 deletions sample-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ dependencies {
androidTestImplementation libs.androidx.test.ext.junit
testImplementation libs.androidx.compose.material
testImplementation libs.androidx.test.espresso.core
testImplementation libs.androidx.test.core.ktx
testImplementation libs.robolectric
testImplementation(libs.kim)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.takahirom.roborazzi.sample

import android.app.Application
import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.compose.material3.Text
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onRoot
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.takahirom.roborazzi.RoborazziRule
import com.github.takahirom.roborazzi.captureRoboImage
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.junit.runner.RunWith
import org.robolectric.Shadows
import org.robolectric.annotation.GraphicsMode

class DefaultThemeActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(android.R.style.Theme_Material_Light)
super.onCreate(savedInstanceState)
}
}

@RunWith(AndroidJUnit4::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class DefaultThemeComposeTest {
@get:Rule(order = 1)
val addActivityToRobolectricRule = object : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val appContext: Application = ApplicationProvider.getApplicationContext()
val activityInfo = ActivityInfo().apply {
name = DefaultThemeActivity::class.java.name
packageName = appContext.packageName
}
Shadows.shadowOf(appContext.packageManager).addOrUpdateActivity(activityInfo)
}
}

@get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<DefaultThemeActivity>()

@get:Rule
val roborazziRule = RoborazziRule(
composeRule = composeTestRule,
captureRoot = composeTestRule.onRoot(),
)

@Test
fun composable() {
composeTestRule.setContent {
Text("a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz")
}
composeTestRule.onRoot().captureRoboImage()
}
}

0 comments on commit fd45f1b

Please sign in to comment.