Skip to content

Commit 5933ac3

Browse files
authored
Merge pull request #51 from superus8r/tests/Improve-test-coverage
Refactor: Improve test coverage on MainActivityViewModel
2 parents b7754eb + 4dce806 commit 5933ac3

File tree

5 files changed

+539
-193
lines changed

5 files changed

+539
-193
lines changed

app/build.gradle.kts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.io.FileInputStream
2-
import java.util.*
2+
import java.util.Base64
3+
import java.util.Properties
34

45
plugins {
56
id("com.android.application")
@@ -110,6 +111,35 @@ tasks.register<JacocoReport>("jacocoTestReport") {
110111
csv.required.set(false)
111112
}
112113

114+
val fileFilter = listOf(
115+
"**/R.class",
116+
"**/MainActivity.*",
117+
"**/R$*.class", "**/BuildConfig.*", "**/Manifest*.*",
118+
"**/*Test*.*", "android/**/*.*",
119+
"**/Dagger*.*", "**/*_Hilt*.*", "**/*Hilt*.*",
120+
)
121+
val kotlinDebugTree = fileTree(layout.buildDirectory.dir("tmp/kotlin-classes/debug")) { exclude(fileFilter) }
122+
val mainKotlinSrc = layout.projectDirectory.dir("src/main/kotlin")
123+
sourceDirectories.from(files(mainKotlinSrc))
124+
classDirectories.from(files(kotlinDebugTree))
125+
executionData.from(fileTree(layout.buildDirectory) {
126+
include(
127+
"outputs/managed_device_code_coverage/**/*.ec",
128+
"outputs/unit_test_code_coverage/**/*.exec",
129+
)
130+
})
131+
}
132+
133+
tasks.register<JacocoReport>("jacocoUiOnly") {
134+
135+
dependsOn("pixel2api30DebugAndroidTest")
136+
137+
reports {
138+
xml.required.set(true)
139+
html.required.set(true)
140+
csv.required.set(false)
141+
}
142+
113143
val fileFilter = listOf(
114144
"**/R.class", "**/R$*.class", "**/BuildConfig.*", "**/Manifest*.*",
115145
"**/*Test*.*", "android/**/*.*",
@@ -123,7 +153,6 @@ tasks.register<JacocoReport>("jacocoTestReport") {
123153
classDirectories.from(files(javaDebugTree, kotlinDebugTree))
124154
executionData.from(fileTree(layout.buildDirectory) {
125155
include(
126-
"outputs/unit_test_code_coverage/**/*.exec",
127156
"outputs/managed_device_code_coverage/**/*.ec",
128157
"outputs/managed_device_code_coverage/**/*.exec"
129158
)

app/src/androidTest/java/org/kabiri/android/usbterminal/MainActivityAndroidTest.kt

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ import org.junit.Rule
1515
import org.junit.Test
1616
import org.junit.runner.RunWith
1717

18-
1918
@RunWith(AndroidJUnit4::class)
2019
internal class MainActivityAndroidTest {
21-
2220
@get:Rule
2321
var rule = activityScenarioRule<MainActivity>()
2422

2523
private fun ensureMenuIsAccessible(
2624
menuItemId: Int,
2725
onVisible: () -> Unit,
28-
onOverflow: () -> Unit
26+
onOverflow: () -> Unit,
2927
) {
3028
try {
3129
// Try to find the menu item first
@@ -49,57 +47,53 @@ internal class MainActivityAndroidTest {
4947
}
5048

5149
@Test
52-
fun checkActionMenuItemSettingsIsDisplayed() = ensureMenuIsAccessible(
53-
menuItemId = R.id.actionSettings,
54-
onVisible = {
55-
56-
// assert
57-
onView(withId(R.id.actionSettings)).check(matches(isDisplayed()))
58-
},
59-
onOverflow = {
60-
61-
// assert
62-
onView(withText(R.string.title_settings)).check(matches(isDisplayed()))
63-
}
64-
)
50+
fun checkActionMenuItemSettingsIsDisplayed() =
51+
ensureMenuIsAccessible(
52+
menuItemId = R.id.actionSettings,
53+
onVisible = {
54+
// assert
55+
onView(withId(R.id.actionSettings)).check(matches(isDisplayed()))
56+
},
57+
onOverflow = {
58+
// assert
59+
onView(withText(R.string.title_settings)).check(matches(isDisplayed()))
60+
},
61+
)
6562

6663
@Test
67-
fun checkActionMenuItemConnectIsDisplayed() = ensureMenuIsAccessible(
68-
menuItemId = R.id.actionSettings,
69-
onVisible = {
70-
71-
// assert
72-
onView(withId(R.id.actionConnect)).check(matches(isDisplayed()))
73-
},
74-
onOverflow = {
75-
76-
// assert
77-
onView(withText(R.string.title_connect)).check(matches(isDisplayed()))
78-
}
79-
)
64+
fun checkActionMenuItemConnectIsDisplayed() =
65+
ensureMenuIsAccessible(
66+
menuItemId = R.id.actionSettings,
67+
onVisible = {
68+
// assert
69+
onView(withId(R.id.actionConnect)).check(matches(isDisplayed()))
70+
},
71+
onOverflow = {
72+
// assert
73+
onView(withText(R.string.title_connect)).check(matches(isDisplayed()))
74+
},
75+
)
8076

8177
@Test
82-
fun checkActionMenuItemDisconnectIsDisplayed() = ensureMenuIsAccessible(
83-
menuItemId = R.id.actionSettings,
84-
onVisible = {
85-
86-
// assert
87-
onView(withId(R.id.actionDisconnect)).check(matches(isDisplayed()))
88-
},
89-
onOverflow = {
90-
91-
// assert
92-
onView(withText(R.string.title_disconnect)).check(matches(isDisplayed()))
93-
}
94-
)
78+
fun checkActionMenuItemDisconnectIsDisplayed() =
79+
ensureMenuIsAccessible(
80+
menuItemId = R.id.actionSettings,
81+
onVisible = {
82+
// assert
83+
onView(withId(R.id.actionDisconnect)).check(matches(isDisplayed()))
84+
},
85+
onOverflow = {
86+
// assert
87+
onView(withText(R.string.title_disconnect)).check(matches(isDisplayed()))
88+
},
89+
)
9590

9691
@Test
9792
fun clickingSettingsOpensSettingsBottomSheet() {
9893
// arrange
9994
ensureMenuIsAccessible(
10095
menuItemId = R.id.actionSettings,
10196
onVisible = {
102-
10397
// act
10498
onView(withId(R.id.actionSettings)).perform(click())
10599

@@ -112,7 +106,7 @@ internal class MainActivityAndroidTest {
112106

113107
// assert
114108
onView(withId(R.id.composeViewSettingContent)).check(matches(isDisplayed()))
115-
}
109+
},
116110
)
117111
}
118112
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,21 @@ import androidx.core.view.WindowInsetsCompat
1919
import androidx.lifecycle.lifecycleScope
2020
import dagger.hilt.android.AndroidEntryPoint
2121
import kotlinx.coroutines.launch
22-
import org.kabiri.android.usbterminal.util.scrollToLastLine
2322
import org.kabiri.android.usbterminal.ui.setting.SettingModalBottomSheet
2423
import org.kabiri.android.usbterminal.ui.setting.SettingViewModel
24+
import org.kabiri.android.usbterminal.util.scrollToLastLine
2525
import org.kabiri.android.usbterminal.viewmodel.MainActivityViewModel
2626

27+
private const val TAG = "MainActivity"
28+
2729
@AndroidEntryPoint
2830
class MainActivity : AppCompatActivity() {
29-
30-
companion object {
31-
private const val TAG = "MainActivity"
32-
}
33-
3431
private val viewModel by viewModels<MainActivityViewModel>()
3532
private val settingViewModel by viewModels<SettingViewModel>()
3633

3734
override fun onCreate(savedInstanceState: Bundle?) {
3835
super.onCreate(savedInstanceState)
36+
viewModel.startObservingUsbDevice()
3937
setContentView(R.layout.activity_main)
4038

4139
// avoid system navbar or soft keyboard overlapping the content.
@@ -67,9 +65,11 @@ class MainActivity : AppCompatActivity() {
6765
fun sendCommand() {
6866
val input = etInput.text.toString()
6967
// append the input to console
70-
if (viewModel.serialWrite(input))
71-
etInput.setText("") // clear the terminal input.
72-
else Log.e(TAG, "The message was not sent to Arduino")
68+
if (viewModel.serialWrite(input)) {
69+
etInput.setText("")
70+
} else {
71+
Log.e(TAG, "The message was not sent to Arduino")
72+
}
7373
}
7474

7575
// send the command to device when the button is clicked.
@@ -83,7 +83,9 @@ class MainActivity : AppCompatActivity() {
8383
event.action == KeyEvent.ACTION_DOWN)) {
8484
sendCommand()
8585
true
86-
} else false
86+
} else {
87+
false
88+
}
8789
}
8890
}
8991

0 commit comments

Comments
 (0)