Add tests

This commit is contained in:
user5 2025-05-26 17:05:00 +03:00
parent 6bad590845
commit 8999932566
8 changed files with 90 additions and 38 deletions

View File

@ -291,10 +291,11 @@ class MainActivity : ComponentActivity() {
state = ButtonState.Chips
)
SecondaryAppButton(
AppButton(
label = "Популярное",
onClick = {},
state = ButtonState.Chips
state = ButtonState.Chips,
enabled = false
)
CartButton(

View File

@ -56,4 +56,8 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}

View File

@ -0,0 +1,65 @@
package com.example.core
import androidx.activity.ComponentActivity
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.core.component.button.AppButton
import com.example.core.component.button.ButtonState
import com.example.core.component.select.AppSelector
import com.example.core.component.select.Option
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ComponentsTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun test_input_error() {
}
@Test
fun test_app_selector() {
composeTestRule.setContent {
var selectedOption1 by remember { mutableStateOf<Option?>(null) }
AppSelector(
selectedOption = selectedOption1,
options = listOf(
Option(label = "Мужской"),
Option(label = "Женский")
),
onOptionSelect = { selectedOption1 = it },
hint = "Пол"
)
}
composeTestRule.onNodeWithText("Пол").performClick()
composeTestRule.onNodeWithTag("AppBottomSheet").assertIsDisplayed()
}
@Test
fun test_chips_button() {
composeTestRule.setContent {
AppButton(
label = "Популярное",
onClick = {},
state = ButtonState.Chips,
enabled = false
)
}
composeTestRule.onNodeWithText("Популярное").assertIsDisplayed()
}
}

View File

@ -1,24 +0,0 @@
package com.example.core
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.core.test", appContext.packageName)
}
}

View File

@ -29,6 +29,7 @@ fun AppButton(
enabled: Boolean = true
) {
Box(contentAlignment = Alignment.Center, modifier = modifier) {
val commonCondition = state is ButtonState.Chips
Button(
modifier = modifier
.then(state.width())
@ -38,17 +39,22 @@ fun AppButton(
enabled = enabled,
colors = ButtonDefaults.buttonColors().copy(
containerColor = CustomTheme.colors.accent,
disabledContainerColor = CustomTheme.colors.accentInactive
disabledContainerColor = if (commonCondition) CustomTheme.colors.inputBg else CustomTheme.colors.accentInactive
)
) {
}
Text(
text = label,
style = state.typography()
.copy(color = CustomTheme.colors.white, textAlign = TextAlign.Center),
.copy(
color = if (commonCondition && !enabled) CustomTheme.colors.description else CustomTheme.colors.white,
textAlign = TextAlign.Center
),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.then(state.width()).padding(horizontal = state.padding()),
modifier = Modifier
.then(state.width())
.padding(horizontal = state.padding()),
)
}
}

View File

@ -1,6 +1,5 @@
package com.example.core.component.button
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
@ -9,7 +8,6 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
@ -30,9 +28,6 @@ fun SecondaryAppButton(
modifier: Modifier = Modifier,
enabled: Boolean = true
) {
val textColor by animateColorAsState(
if (state is ButtonState.Chips) CustomTheme.colors.description else CustomTheme.colors.black
)
Box(contentAlignment = Alignment.Center, modifier = modifier) {
Button(
modifier = modifier
@ -49,7 +44,8 @@ fun SecondaryAppButton(
}
Text(
text = label,
style = state.typography().copy(color = textColor, textAlign = TextAlign.Center),
style = state.typography()
.copy(color = CustomTheme.colors.black, textAlign = TextAlign.Center),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier

View File

@ -63,7 +63,7 @@ fun EnterInputField(
var hasFocus by rememberSaveable { mutableStateOf(false) }
var showPassword by rememberSaveable { mutableStateOf(true) }
var showPassword by rememberSaveable { mutableStateOf(false) }
/**
* Установка цвета рамки в зависимости от состояния

View File

@ -19,7 +19,6 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@ -30,6 +29,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.example.core.theme.CustomIcons
import com.example.core.theme.CustomTheme
@ -54,7 +54,11 @@ fun AppSelector(
val scope = rememberCoroutineScope()
if (showSheet) {
AppBottomSheet(sheetState = sheetState, onDismissRequest = { showSheet = false }) {
AppBottomSheet(
sheetState = sheetState,
onDismissRequest = { showSheet = false },
modifier = Modifier.testTag("AppBottomSheet")
) {
Column(verticalArrangement = Arrangement.Center) {
options.forEach { option ->
Row(