From 17d5475d0f458a93f39eddd7a92d461a5f23c128 Mon Sep 17 00:00:00 2001 From: Brian Yencho Date: Tue, 29 Aug 2023 13:07:55 -0500 Subject: [PATCH] Add BaseComposeTest for facilitating compose-layer testing (#14) --- .../bitwarden/example/ExampleComposeTest.kt | 19 +++------------- .../bitwarden/example/ui/BaseComposeTest.kt | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 app/src/test/java/com/x8bit/bitwarden/example/ui/BaseComposeTest.kt diff --git a/app/src/test/java/com/x8bit/bitwarden/example/ExampleComposeTest.kt b/app/src/test/java/com/x8bit/bitwarden/example/ExampleComposeTest.kt index 524d117f6..4f71f23ff 100644 --- a/app/src/test/java/com/x8bit/bitwarden/example/ExampleComposeTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/example/ExampleComposeTest.kt @@ -1,30 +1,17 @@ package com.x8bit.bitwarden.example import androidx.compose.material3.Button -import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick -import dagger.hilt.android.testing.HiltTestApplication +import com.x8bit.bitwarden.example.ui.BaseComposeTest import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue -import org.junit.Rule import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.annotation.Config /** - * Example showing that Compose tests using "junit" imports and Roboelectric work. + * Example showing that Compose tests using "junit" imports and Robolectric work. */ -@Config( - application = HiltTestApplication::class, - sdk = [Config.NEWEST_SDK], -) -@RunWith(RobolectricTestRunner::class) -class ExampleComposeTest { - @get:Rule - val composeTestRule = createComposeRule() - +class ExampleComposeTest : BaseComposeTest() { @Test fun `the onClick callback should be correctly triggered when performing a click`() { var isClicked = false diff --git a/app/src/test/java/com/x8bit/bitwarden/example/ui/BaseComposeTest.kt b/app/src/test/java/com/x8bit/bitwarden/example/ui/BaseComposeTest.kt new file mode 100644 index 000000000..9f4e9a068 --- /dev/null +++ b/app/src/test/java/com/x8bit/bitwarden/example/ui/BaseComposeTest.kt @@ -0,0 +1,22 @@ +package com.x8bit.bitwarden.example.ui + +import androidx.compose.ui.test.junit4.createComposeRule +import dagger.hilt.android.testing.HiltTestApplication +import org.junit.Rule +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A base class that can be used for performing Compose-layer testing using Robolectric, Compose + * Testing, and JUnit 4. + */ +@Config( + application = HiltTestApplication::class, + sdk = [Config.NEWEST_SDK], +) +@RunWith(RobolectricTestRunner::class) +abstract class BaseComposeTest { + @get:Rule + val composeTestRule = createComposeRule() +}