Skip to content

Commit

Permalink
test: Add Designsystem ScreenshotTests
Browse files Browse the repository at this point in the history
  • Loading branch information
azrael8576 committed Nov 30, 2023
1 parent 1706e1e commit a5ec97b
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.wei.picquest.core.designsystem

import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.unit.dp
import com.wei.picquest.core.designsystem.component.PqBackground
import com.wei.picquest.core.testing.util.captureMultiTheme
import dagger.hilt.android.testing.HiltTestApplication
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode

@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(application = HiltTestApplication::class, sdk = [33], qualifiers = "480dpi")
@LooperMode(LooperMode.Mode.PAUSED)
class BackgroundScreenshotTests {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Test
fun atBackground_multipleThemes() {
composeTestRule.captureMultiTheme("Background") { description ->
PqBackground(Modifier.size(100.dp)) {
Text("$description background")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.wei.picquest.core.designsystem

import androidx.activity.ComponentActivity
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onRoot
import com.github.takahirom.roborazzi.captureRoboImage
import com.google.accompanist.testharness.TestHarness
import com.wei.picquest.core.designsystem.component.PqNavigationBar
import com.wei.picquest.core.designsystem.component.PqNavigationBarItem
import com.wei.picquest.core.designsystem.component.PqNavigationDrawer
import com.wei.picquest.core.designsystem.component.PqNavigationDrawerItem
import com.wei.picquest.core.designsystem.component.PqNavigationRail
import com.wei.picquest.core.designsystem.component.PqNavigationRailItem
import com.wei.picquest.core.designsystem.icon.PqIcons
import com.wei.picquest.core.designsystem.theme.PqTheme
import com.wei.picquest.core.testing.util.DefaultRoborazziOptions
import com.wei.picquest.core.testing.util.captureMultiTheme
import dagger.hilt.android.testing.HiltTestApplication
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode

@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(application = HiltTestApplication::class, sdk = [33], qualifiers = "480dpi")
@LooperMode(LooperMode.Mode.PAUSED)
class NavigationScreenshotTests() {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Test
fun navigationBar_multipleThemes() {
composeTestRule.captureMultiTheme("NavigationBar") {
Surface {
PqNavigationBarExample()
}
}
}

@Test
fun navigationBar_hugeFont() {
composeTestRule.setContent {
CompositionLocalProvider(
LocalInspectionMode provides true,
) {
TestHarness(fontScale = 2f) {
PqTheme {
PqNavigationBarExample("Looong item")
}
}
}
}
composeTestRule.onRoot()
.captureRoboImage(
"src/test/screenshots/NavigationBar" +
"/NavigationBar_fontScale2.png",
roborazziOptions = DefaultRoborazziOptions,
)
}

@Test
fun navigationRail_multipleThemes() {
composeTestRule.captureMultiTheme("NavigationRail") {
Surface {
PqNavigationRailExample()
}
}
}

@Test
fun navigationDrawer_multipleThemes() {
composeTestRule.captureMultiTheme("NavigationDrawer") {
Surface {
PqNavigationDrawerExample()
}
}
}

@Test
fun navigationDrawer_hugeFont() {
composeTestRule.setContent {
CompositionLocalProvider(
LocalInspectionMode provides true,
) {
TestHarness(fontScale = 2f) {
PqTheme {
PqNavigationDrawerExample("Loooooooooooooooong item")
}
}
}
}
composeTestRule.onRoot()
.captureRoboImage(
"src/test/screenshots/NavigationDrawer" +
"/NavigationDrawer_fontScale2.png",
roborazziOptions = DefaultRoborazziOptions,
)
}

@Composable
private fun PqNavigationBarExample(label: String = "Item") {
PqNavigationBar {
(0..2).forEach { index ->
PqNavigationBarItem(
selected = index == 0,
onClick = { },
icon = {
Icon(
imageVector = PqIcons.UpcomingBorder,
contentDescription = "",
)
},
selectedIcon = {
Icon(
imageVector = PqIcons.Upcoming,
contentDescription = "",
)
},
label = { Text(label) },
)
}
}
}

@Composable
private fun PqNavigationRailExample() {
PqNavigationRail {
(0..2).forEach { index ->
PqNavigationRailItem(
selected = index == 0,
onClick = { },
icon = {
Icon(
imageVector = PqIcons.UpcomingBorder,
contentDescription = "",
)
},
selectedIcon = {
Icon(
imageVector = PqIcons.Upcoming,
contentDescription = "",
)
},
)
}
}
}

@Composable
private fun PqNavigationDrawerExample(label: String = "Item") {
PqNavigationDrawer {
(0..2).forEach { index ->
PqNavigationDrawerItem(
selected = index == 0,
onClick = { },
icon = {
Icon(
imageVector = PqIcons.UpcomingBorder,
contentDescription = "",
)
},
selectedIcon = {
Icon(
imageVector = PqIcons.Upcoming,
contentDescription = "",
)
},
label = { Text(label) },
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.wei.picquest.core.testing.util

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestRule
import org.junit.rules.TestWatcher
import org.junit.runner.Description

/**
* A JUnit [TestRule] that sets the Main dispatcher to [testDispatcher]
* for the duration of the test.
*/
@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(),
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
}
}
Loading

0 comments on commit a5ec97b

Please sign in to comment.