|
| 1 | +/* |
| 2 | + * Designed and developed by Duckie Team 2023. |
| 3 | + * |
| 4 | + * Licensed under the MIT. |
| 5 | + * Please see full license: https://github.com/duckie-team/quack-quack-android/blob/main/LICENSE |
| 6 | + */ |
| 7 | + |
| 8 | +@file:OptIn(ExperimentalQuackQuackApi::class) |
| 9 | + |
| 10 | +package team.duckie.quackquack.ui.plugin.interceptor |
| 11 | + |
| 12 | +import com.github.takahirom.roborazzi.RoborazziRule.Ignore as NoSnapshot |
| 13 | +import androidx.activity.ComponentActivity |
| 14 | +import androidx.compose.ui.Modifier |
| 15 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 16 | +import androidx.compose.ui.test.onRoot |
| 17 | +import androidx.compose.ui.unit.dp |
| 18 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 19 | +import com.github.takahirom.roborazzi.RoborazziRule |
| 20 | +import io.kotest.assertions.throwables.shouldThrowWithMessage |
| 21 | +import io.kotest.matchers.maps.shouldMatchExactly |
| 22 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 23 | +import io.kotest.matchers.shouldBe |
| 24 | +import org.junit.Rule |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | +import team.duckie.quackquack.material.QuackColor |
| 28 | +import team.duckie.quackquack.material.theme.QuackTheme |
| 29 | +import team.duckie.quackquack.ui.QuackTag |
| 30 | +import team.duckie.quackquack.ui.QuackTagStyle |
| 31 | +import team.duckie.quackquack.ui.TagStyleMarker |
| 32 | +import team.duckie.quackquack.ui.plugin.rememberQuackPlugins |
| 33 | +import team.duckie.quackquack.ui.util.ExperimentalQuackQuackApi |
| 34 | +import team.duckie.quackquack.util.compose.snapshot.test.SnapshotName |
| 35 | +import team.duckie.quackquack.util.compose.snapshot.test.snapshotPath |
| 36 | + |
| 37 | +@RunWith(AndroidJUnit4::class) |
| 38 | +class QuackInterceptorPluginTest { |
| 39 | + @get:Rule |
| 40 | + val compose = createAndroidComposeRule<ComponentActivity>() |
| 41 | + |
| 42 | + @get:Rule |
| 43 | + val roborazzi = RoborazziRule( |
| 44 | + composeRule = compose, |
| 45 | + captureRoot = compose.onRoot(), |
| 46 | + options = RoborazziRule.Options( |
| 47 | + outputFileProvider = { description, _, fileExtension -> |
| 48 | + val snapshotName = description.getAnnotation(SnapshotName::class.java)?.name ?: description.methodName |
| 49 | + snapshotPath( |
| 50 | + domain = "QuackInterceptorPlugin", |
| 51 | + snapshotName = snapshotName, |
| 52 | + isGif = fileExtension == "gif", |
| 53 | + ) |
| 54 | + }, |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + @SnapshotName("QuackTagRadiusStyleIntercepted") |
| 59 | + @Test |
| 60 | + fun `style intercept works fine`() { |
| 61 | + val map = mutableMapOf<String, Any?>() |
| 62 | + |
| 63 | + var interceptedStyle: QuackTagStyle<TagStyleMarker>? = null |
| 64 | + val interceptedRadius = Int.MAX_VALUE.dp |
| 65 | + |
| 66 | + compose.setContent { |
| 67 | + QuackTheme( |
| 68 | + plugins = rememberQuackPlugins { |
| 69 | + +QuackInterceptorPlugin.DesignToken { componentName, componentDesignToken, componentModifier, _ -> |
| 70 | + map["componentName"] = componentName |
| 71 | + map["componentDesignToken"] = componentDesignToken |
| 72 | + map["componentModifier"] = componentModifier |
| 73 | + |
| 74 | + (if (componentName == "QuackTag") { |
| 75 | + @Suppress("UNCHECKED_CAST") |
| 76 | + componentDesignToken as QuackTagStyle<TagStyleMarker> |
| 77 | + object : QuackTagStyle<TagStyleMarker> by componentDesignToken { |
| 78 | + override val radius = interceptedRadius |
| 79 | + override val colors = |
| 80 | + componentDesignToken.colors.copy( |
| 81 | + backgroundColor = QuackColor.Gray3, |
| 82 | + contentColor = QuackColor.Black, |
| 83 | + ) |
| 84 | + } |
| 85 | + } else { |
| 86 | + componentDesignToken |
| 87 | + }).also { intercepttedResult -> |
| 88 | + @Suppress("UNCHECKED_CAST") |
| 89 | + interceptedStyle = intercepttedResult as QuackTagStyle<TagStyleMarker> |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + ) { |
| 94 | + QuackTag( |
| 95 | + text = "Intercepted Tag", |
| 96 | + style = QuackTagStyle.Filled, |
| 97 | + onClick = {}, |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + map.shouldMatchExactly( |
| 103 | + "componentName" to { it shouldBe "QuackTag" }, |
| 104 | + "componentDesignToken" to { it.toString() shouldBe QuackTagStyle.Filled.toString() }, |
| 105 | + "componentModifier" to { it shouldBe Modifier }, |
| 106 | + ) |
| 107 | + interceptedStyle.shouldNotBeNull().radius shouldBe interceptedRadius |
| 108 | + } |
| 109 | + |
| 110 | + @NoSnapshot |
| 111 | + @Test |
| 112 | + fun InterceptedStyleTypeExceptionMessage() { |
| 113 | + shouldThrowWithMessage<IllegalStateException>(InterceptedStyleTypeExceptionMessage) { |
| 114 | + compose.setContent { |
| 115 | + QuackTheme( |
| 116 | + plugins = rememberQuackPlugins { |
| 117 | + +QuackInterceptorPlugin.DesignToken { _, _, _, _ -> Unit } |
| 118 | + }, |
| 119 | + ) { |
| 120 | + QuackTag( |
| 121 | + text = "", |
| 122 | + style = QuackTagStyle.Filled, |
| 123 | + onClick = {}, |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments