Skip to content

Commit af6a073

Browse files
authored
Merge pull request #56 from superus8r/feature/DROID-17/AddAutoScrollSwitchToSettings
DROID-17_Add Auto-Scroll Switch to Settings Bottom Sheet
2 parents ae4ad5a + 90dd528 commit af6a073

25 files changed

+831
-237
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ dependencies {
291291

292292
// Instrumented Test Libraries
293293
androidTestImplementation(composeBom)
294+
androidTestImplementation(libs.compose.ui.test.junit4)
295+
debugImplementation(libs.compose.ui.test.manifest)
294296
androidTestImplementation(libs.coroutines.test)
295297
androidTestImplementation(libs.androidx.test.core)
296298
androidTestImplementation(libs.androidx.test.ext.junit)

app/src/androidTest/java/org/kabiri/android/usbterminal/common/FrequentMocks.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/src/androidTest/java/org/kabiri/android/usbterminal/data/repository/UserSettingRepositoryAndroidTest.kt

Lines changed: 0 additions & 91 deletions
This file was deleted.

app/src/androidTest/java/org/kabiri/android/usbterminal/domain/GetCustomBaudRateUseCaseAndroidTest.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/src/androidTest/java/org/kabiri/android/usbterminal/domain/SetCustomBaudRateUseCaseAndroidTest.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.kabiri.android.usbterminal.ui.setting
2+
3+
import androidx.activity.ComponentActivity
4+
import androidx.compose.ui.test.assertIsDisplayed
5+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
6+
import androidx.compose.ui.test.onNodeWithText
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
8+
import io.mockk.every
9+
import io.mockk.mockk
10+
import kotlinx.coroutines.flow.flowOf
11+
import org.hamcrest.CoreMatchers.notNullValue
12+
import org.hamcrest.MatcherAssert.assertThat
13+
import org.junit.Rule
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.kabiri.android.usbterminal.R
17+
import org.kabiri.android.usbterminal.ui.theme.UsbTerminalTheme
18+
19+
@RunWith(AndroidJUnit4::class)
20+
class SettingContentAndroidTest {
21+
@get:Rule
22+
val composeRule = createAndroidComposeRule<ComponentActivity>()
23+
24+
private fun showContet(viewModel: SettingViewModel) {
25+
composeRule.setContent {
26+
UsbTerminalTheme {
27+
SettingContent(
28+
settingViewModel = viewModel,
29+
onDismiss = {},
30+
)
31+
}
32+
}
33+
}
34+
35+
@Test
36+
fun settingContent_displaysExpectedTexts() {
37+
// arrange
38+
val context = composeRule.activity
39+
assertThat(context, notNullValue())
40+
val viewModel = mockk<SettingViewModel>(relaxed = true)
41+
every { viewModel.currentBaudRate } returns flowOf(9600)
42+
every { viewModel.currentAutoScroll } returns flowOf(true)
43+
44+
// act
45+
showContet(viewModel)
46+
47+
// assert
48+
composeRule.onNodeWithText(context.getString(R.string.settings_title)).assertIsDisplayed()
49+
composeRule
50+
.onNodeWithText(context.getString(R.string.settings_subtitle))
51+
.assertIsDisplayed()
52+
53+
// Baud rate label (ensure unmerged tree so label is found)
54+
composeRule
55+
.onNodeWithText(
56+
context.getString(R.string.settings_label_baud_rate),
57+
useUnmergedTree = true,
58+
).assertIsDisplayed()
59+
60+
// Buttons and bottom text
61+
composeRule
62+
.onNodeWithText(context.getString(R.string.settings_bt_reset_default))
63+
.assertIsDisplayed()
64+
composeRule
65+
.onNodeWithText(context.getString(R.string.settings_bt_dismiss_sheet))
66+
.assertIsDisplayed()
67+
composeRule
68+
.onNodeWithText(context.getString(R.string.settings_bottom_text))
69+
.assertIsDisplayed()
70+
71+
// Auto-scroll label
72+
composeRule
73+
.onNodeWithText(context.getString(R.string.settings_label_auto_scroll))
74+
.assertIsDisplayed()
75+
}
76+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.kabiri.android.usbterminal.ui.setting
2+
3+
import androidx.activity.ComponentActivity
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.test.assertIsDisplayed
7+
import androidx.compose.ui.test.assertIsOff
8+
import androidx.compose.ui.test.assertIsOn
9+
import androidx.compose.ui.test.hasAnySibling
10+
import androidx.compose.ui.test.hasText
11+
import androidx.compose.ui.test.isToggleable
12+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
13+
import androidx.compose.ui.test.onNodeWithText
14+
import androidx.test.ext.junit.runners.AndroidJUnit4
15+
import org.junit.Rule
16+
import org.junit.Test
17+
import org.junit.runner.RunWith
18+
import org.kabiri.android.usbterminal.R
19+
import org.kabiri.android.usbterminal.ui.theme.UsbTerminalTheme
20+
21+
@RunWith(AndroidJUnit4::class)
22+
class SettingSwitchItemAndroidTest {
23+
@get:Rule
24+
val composeRule = createAndroidComposeRule<ComponentActivity>()
25+
26+
private fun showContet(enabled: Boolean = true) {
27+
composeRule.setContent {
28+
UsbTerminalTheme {
29+
SettingSwitchItem(
30+
enabled = enabled,
31+
onToggle = {},
32+
modifier = Modifier.fillMaxSize(),
33+
)
34+
}
35+
}
36+
}
37+
38+
@Test
39+
fun autoScrollSwitch_displaysLabel() {
40+
// arrange
41+
val context = composeRule.activity
42+
43+
// act
44+
showContet()
45+
46+
// assert
47+
composeRule
48+
.onNodeWithText(context.getString(R.string.settings_label_auto_scroll))
49+
.assertIsDisplayed()
50+
}
51+
52+
@Test
53+
fun autoScrollSwitch_isOn_whenEnabledTrue() {
54+
// arrange
55+
val context = composeRule.activity
56+
57+
// act
58+
showContet(enabled = true)
59+
60+
// assert
61+
composeRule
62+
.onNode(
63+
hasAnySibling(
64+
hasText(context.getString(R.string.settings_label_auto_scroll)),
65+
).and(isToggleable()),
66+
).assertIsOn()
67+
}
68+
69+
@Test
70+
fun autoScrollSwitch_isOff_whenEnabledFalse() {
71+
// arrange
72+
val context = composeRule.activity
73+
74+
// act
75+
showContet(enabled = false)
76+
77+
// assert
78+
composeRule
79+
.onNode(
80+
hasAnySibling(
81+
hasText(context.getString(R.string.settings_label_auto_scroll)),
82+
).and(isToggleable()),
83+
).assertIsOff()
84+
}
85+
}

app/src/main/java/org/kabiri/android/usbterminal/MainActivity.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ class MainActivity : AppCompatActivity() {
5252
// make the text view scrollable:
5353
tvOutput.movementMethod = ScrollingMovementMethod()
5454

55+
var autoScrollEnabled = true
56+
lifecycleScope.launch {
57+
settingViewModel.currentAutoScroll.collect { enabled ->
58+
autoScrollEnabled = enabled
59+
}
60+
}
61+
5562
lifecycleScope.launch {
5663
viewModel.getLiveOutput()
5764
viewModel.output.collect {
5865
tvOutput.apply {
5966
text = it
60-
scrollToLastLine()
67+
if (autoScrollEnabled) scrollToLastLine()
6168
}
6269
}
6370
}

app/src/main/java/org/kabiri/android/usbterminal/data/repository/ArduinoRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.combine
1515
import kotlinx.coroutines.launch
1616
import org.kabiri.android.usbterminal.R
1717
import org.kabiri.android.usbterminal.domain.IGetCustomBaudRateUseCase
18-
import org.kabiri.android.usbterminal.model.defaultBaudRate
18+
import org.kabiri.android.usbterminal.model.DEFAULT_BAUD_RATE
1919
import javax.inject.Inject
2020

2121
/**
@@ -44,7 +44,7 @@ internal class ArduinoRepository
4444
private val getBaudRate: IGetCustomBaudRateUseCase,
4545
): IArduinoRepository {
4646

47-
private var currentBaudRate = defaultBaudRate // Default value
47+
private var currentBaudRate = DEFAULT_BAUD_RATE // Default value
4848

4949
private val _messageFlow = MutableStateFlow("")
5050
override val messageFlow: Flow<String>

0 commit comments

Comments
 (0)