tuner: add tests for screens, chord diagrams, viewmodel and instruments
- Commit
b04fb4e490afb157f3ecf2e00f6562b9027f9a62- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 04:07 UTC (29 days ago)
- Parents
76fdcf9
Compose/JVM tests for NoteScreen states, ChordScreen readings (confident/uncertain/provisional/ambiguous/notes-only), TuningMeter/PianoKeyboard/FretDiagram, InstrumentMenu, TunerViewModel lifecycle, Instrument model (pitch order, sounding range, catalogue parse). Coverage (merged): 66.0% -> 82.5% instructions (13213/20008 -> 16510/20008); suite 138 tests, 0 failures. Remaining: AudioCapture microphone path and MainActivity/Navigation (device-bound). (cherry picked from commit 389e7c6bc84462a1327b5943675237e22247b6a2)
Diff
tuner/src/test/java/com/vayunmathur/tuner/data/InstrumentModelTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/data/InstrumentModelTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/data/InstrumentModelTest.ktnew file mode 100644index 0000000000..18fd51d314--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/data/InstrumentModelTest.kt@@ -0,0 +1,79 @@+package com.vayunmathur.tuner.data++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/** Instrument catalogue model: pitch ordering, sounding range, parsing. */+class InstrumentModelTest {++ private val guitar = Instrument(+ id = "guitar", familyId = "guitar", kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(40, "E"), StringSpec(45, "A"), StringSpec(50, "D"),+ StringSpec(55, "G"), StringSpec(59, "B"), StringSpec(64, "E"),+ ),+ fretCount = 12, analysis = AnalysisRange(80.0, 1200.0),+ )+ private val uke = Instrument(+ id = "uke", familyId = "ukulele", kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(67, "G"), StringSpec(60, "C"), StringSpec(64, "E"), StringSpec(69, "A"),+ ),+ fretCount = 12, analysis = AnalysisRange(200.0, 1200.0),+ )+ private val piano = Instrument(+ id = "piano", familyId = "piano", kind = InstrumentKind.KEYBOARD,+ keyboard = KeyboardSpec(21, 108), analysis = AnalysisRange(27.0, 4200.0),+ )++ @Test+ fun tuningLabelAndPolyphony() {+ assertEquals("E A D G B E", guitar.tuningLabel)+ assertEquals(6, guitar.polyphony)+ assertEquals(10, piano.polyphony)+ }++ @Test+ fun stringsByPitchOrdersBySoundingPitchAndSkipsMuted() {+ // Standard guitar is already low-to-high in string order.+ assertEquals(listOf(0, 1, 2, 3, 4, 5), guitar.stringsByPitch(listOf(0, 0, 0, 0, 0, 0)))+ // Re-entrant ukulele: G(67) C(60) E(64) A(69) -> pitch order C,E,G,A = indices 1,2,0,3.+ assertEquals(listOf(1, 2, 0, 3), uke.stringsByPitch(listOf(0, 0, 0, 0)))+ // Muted strings (-1) are excluded; fretting raises pitch.+ assertEquals(listOf(1, 2), uke.stringsByPitch(listOf(-1, 0, 0, -1)))+ assertEquals(listOf(1, 0), guitar.stringsByPitch(listOf(12, 0, -1, -1, -1, -1)))+ }++ @Test+ fun soundingRangeFrettedKeyboardAndUnfretted() {+ assertEquals(40..76, guitar.soundingRange)+ assertEquals(21..108, piano.soundingRange)+ val violin = Instrument(+ id = "violin", familyId = "violin", kind = InstrumentKind.UNFRETTED,+ strings = listOf(StringSpec(55, "G"), StringSpec(62, "D")),+ analysis = AnalysisRange(196.0, 3000.0),+ )+ assertEquals(55, violin.soundingRange.first)+ assertTrue(violin.soundingRange.last > 55)+ }++ @Test+ fun analysisRangeMidiConversionRoundsInwards() {+ val range = AnalysisRange(440.0, 440.0)+ assertEquals(69..69, range.midiRange)+ assertTrue(AnalysisRange(80.0, 1200.0).midiRange.first >= 39)+ }++ @Test+ fun catalogByIdFallbackAndParse() {+ val catalog = InstrumentCatalog(listOf(guitar, uke))+ assertEquals(uke, catalog.byId("uke"))+ assertEquals(guitar, catalog.byId("missing"))+ val json = """{"instruments":[{"id":"x","familyId":"guitar","kind":"FRETTED","strings":[{"openMidi":40,"label":"E"}],"fretCount":12,"analysis":{"minHz":80.0,"maxHz":1200.0}}]}"""+ val parsed = InstrumentCatalog.parse(json)+ assertEquals(1, parsed.instruments.size)+ assertEquals("x", parsed.instruments[0].id)+ assertEquals(40, parsed.instruments[0].strings[0].openMidi)+ }+}
tuner/src/test/java/com/vayunmathur/tuner/platform/TunerViewModelTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/platform/TunerViewModelTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/platform/TunerViewModelTest.ktnew file mode 100644index 0000000000..f1b4f60251--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/platform/TunerViewModelTest.kt@@ -0,0 +1,64 @@+package com.vayunmathur.tuner.platform++import android.app.Application+import androidx.test.core.app.ApplicationProvider+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** ViewModel state lifecycle without a real microphone. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class TunerViewModelTest {++ private val app = ApplicationProvider.getApplicationContext<Application>()++ @Test+ fun initialStateLoadsCatalogueDefaults() {+ val vm = TunerViewModel(app)+ assertTrue(vm.state.instruments.isNotEmpty())+ assertEquals(vm.state.instruments.first(), vm.state.instrument)+ assertFalse(vm.state.listening)+ assertNull(vm.state.failure)+ assertNull(vm.state.note.readout)+ assertNull(vm.state.chord.reading)+ assertTrue(vm.state.chord.voicings.isEmpty())+ }++ @Test+ fun selectInstrumentSwitchesAndIgnoresUnknown() {+ val vm = TunerViewModel(app)+ val first = vm.state.instrument+ val other = vm.state.instruments.firstOrNull { it.id != first.id }+ assertNotNull(other)+ vm.selectInstrument(other.id)+ assertEquals(other.id, vm.state.instrument.id)+ assertNull(vm.state.note.readout)+ // Unknown id is a no-op, keeping the current instrument.+ vm.selectInstrument("no-such-instrument")+ assertEquals(other.id, vm.state.instrument.id)+ // Re-selecting the first instrument works too.+ vm.selectInstrument(first.id)+ assertEquals(first.id, vm.state.instrument.id)+ }++ @Test+ fun stopAndBackgroundClearReadouts() {+ val vm = TunerViewModel(app)+ vm.stop()+ assertFalse(vm.state.listening)+ assertNull(vm.state.note.readout)+ vm.onBackground()+ assertFalse(vm.state.listening)+ // Foreground without a prior start must not open capture intent:+ // state stays not-listening until frames arrive (none in Robolectric).+ vm.onForeground()+ assertNull(vm.state.note.readout)+ }+}
tuner/src/test/java/com/vayunmathur/tuner/ui/TunerChordDiagramTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerChordDiagramTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerChordDiagramTest.ktnew file mode 100644index 0000000000..2968669af2--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerChordDiagramTest.kt@@ -0,0 +1,141 @@+package com.vayunmathur.tuner.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.tuner.R+import com.vayunmathur.tuner.data.AnalysisRange+import com.vayunmathur.tuner.data.Instrument+import com.vayunmathur.tuner.data.InstrumentKind+import com.vayunmathur.tuner.data.KeyboardSpec+import com.vayunmathur.tuner.data.StringSpec+import com.vayunmathur.tuner.domain.ChordLabel+import com.vayunmathur.tuner.domain.ChordName+import com.vayunmathur.tuner.domain.ChordPresentation+import com.vayunmathur.tuner.domain.ChordQuality+import com.vayunmathur.tuner.domain.ChordReading+import com.vayunmathur.tuner.domain.ChordTier+import com.vayunmathur.tuner.domain.DetectedNote+import com.vayunmathur.tuner.domain.Voicing+import com.vayunmathur.tuner.platform.ChordUiState+import com.vayunmathur.tuner.platform.TunerUiState+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Chord screen readings (confident/ambiguous/uncertain/single) and diagrams. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class TunerChordDiagramTest {++ @get:Rule+ val composeRule = createComposeRule()++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ private fun str(id: Int, vararg a: Any): String = context.getString(id, *a)++ private val guitar = Instrument(+ id = "guitar", familyId = "guitar", kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(40, "E"), StringSpec(45, "A"), StringSpec(50, "D"),+ StringSpec(55, "G"), StringSpec(59, "B"), StringSpec(64, "E"),+ ),+ fretCount = 12, analysis = AnalysisRange(80.0, 1200.0),+ )+ private val piano = Instrument(+ id = "piano", familyId = "piano", kind = InstrumentKind.KEYBOARD,+ keyboard = KeyboardSpec(21, 108), analysis = AnalysisRange(27.0, 4200.0),+ )++ private fun reading(label: ChordLabel, presentation: ChordPresentation, tier: ChordTier = ChordTier.CONFIRMED) =+ ChordReading(+ notes = listOf(DetectedNote(60, 1.0), DetectedNote(64, 0.9), DetectedNote(67, 0.8)),+ bass = 48, bassConfidence = 0.9f, label = label, alternates = emptyList(),+ confidence = 0.95f, tier = tier, presentation = presentation,+ )++ private fun chordState(+ instrument: Instrument,+ label: ChordLabel,+ presentation: ChordPresentation,+ tier: ChordTier = ChordTier.CONFIRMED,+ ) = TunerUiState(+ instrument = instrument, instruments = listOf(guitar, piano),+ chord = ChordUiState(+ reading = reading(label, presentation, tier),+ voicings = listOf(Voicing(listOf(-1, 3, 2, 0, 1, 0), null, 3, listOf(48, 53, 57, 60, 64), 1.0, true)),+ ),+ )++ @Test+ fun chordTextSymbolsAndSlashBass() {+ assertEquals("C", chordText(ChordName(0, ChordQuality.MAJOR)))+ assertEquals("Am", chordText(ChordName(9, ChordQuality.MINOR)))+ assertTrue(chordText(ChordName(0, ChordQuality.MAJOR, bass = 4)).startsWith("C/"))+ }++ @Test+ fun confidentChordShowsNameNotesAndShapes() {+ val s = chordState(guitar, ChordLabel.Chord(ChordName(0, ChordQuality.MAJOR)), ChordPresentation.CONFIDENT)+ composeRule.setContent { ChordScreen(s) {} }+ composeRule.onNodeWithText("C").assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.notes_detected)).assertIsDisplayed()+ }++ @Test+ fun singleNoteAndIntervalHeadlines() {+ val single = chordState(piano, ChordLabel.SingleNote(60), ChordPresentation.CONFIDENT)+ composeRule.setContent { ChordScreen(single) {} }+ composeRule.onNodeWithText(str(R.string.notes_detected)).assertIsDisplayed()+ }++ @Test+ fun uncertainChordShowsConfidenceCaveat() {+ val s = chordState(guitar, ChordLabel.Chord(ChordName(0, ChordQuality.MAJOR)), ChordPresentation.UNCERTAIN)+ composeRule.setContent { ChordScreen(s) {} }+ composeRule.onNodeWithText(str(R.string.confidence_low)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.confidence_note)).assertIsDisplayed()+ }++ @Test+ fun provisionalTierShowsSettlingNote() {+ val s = chordState(+ guitar, ChordLabel.Chord(ChordName(0, ChordQuality.MAJOR)),+ ChordPresentation.CONFIDENT, tier = ChordTier.PROVISIONAL,+ )+ composeRule.setContent { ChordScreen(s) {} }+ composeRule.onNodeWithText(str(R.string.confidence_settling)).assertIsDisplayed()+ }++ @Test+ fun ambiguousChordOffersAlternateSwitch() {+ val label = ChordLabel.Ambiguous(ChordName(9, ChordQuality.MINOR_SEVENTH), ChordName(0, ChordQuality.SIXTH))+ val s = chordState(guitar, label, ChordPresentation.AMBIGUOUS)+ composeRule.setContent { ChordScreen(s) {} }+ composeRule.onNodeWithText(str(R.string.chord_ambiguous_note)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.chord_show_other, chordText(label.secondary)), substring = true)+ .assertIsDisplayed()+ }++ @Test+ fun notesOnlyPresentationWithholdsName() {+ val s = chordState(piano, ChordLabel.Silent, ChordPresentation.NOTES_ONLY)+ composeRule.setContent { ChordScreen(s) {} }+ composeRule.onNodeWithText(str(R.string.chord_unsure)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.notes_detected)).assertIsDisplayed()+ }++ @Test+ fun fretDiagramRendersForVoicing() {+ val voicing = Voicing(listOf(0, 2, 2, 1, 0, 0), null, 3, listOf(40, 47, 52, 56, 59, 64), 1.0, true)+ composeRule.setContent { FretDiagram(guitar, voicing) }+ composeRule.onNodeWithContentDescription(str(R.string.diagram_description)).assertExists()+ }+}
tuner/src/test/java/com/vayunmathur/tuner/ui/TunerScreensTest.kt
diff --git a/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerScreensTest.kt b/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerScreensTest.ktnew file mode 100644index 0000000000..8c2c4373e9--- /dev/null+++ b/tuner/src/test/java/com/vayunmathur/tuner/ui/TunerScreensTest.kt@@ -0,0 +1,125 @@+package com.vayunmathur.tuner.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.tuner.R+import com.vayunmathur.tuner.data.AnalysisRange+import com.vayunmathur.tuner.data.Instrument+import com.vayunmathur.tuner.data.InstrumentKind+import com.vayunmathur.tuner.data.StringSpec+import com.vayunmathur.tuner.domain.TuningBand+import com.vayunmathur.tuner.platform.CaptureFailure+import com.vayunmathur.tuner.platform.NoteReadout+import com.vayunmathur.tuner.platform.NoteUiState+import com.vayunmathur.tuner.platform.TunerUiState+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Note/chord screens, meter, keyboard, fret diagram and instrument menu. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class TunerScreensTest {++ @get:Rule+ val composeRule = createComposeRule()++ private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+ private fun str(id: Int, vararg a: Any): String = context.getString(id, *a)++ private val guitar = Instrument(+ id = "guitar", familyId = "guitar", kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(40, "E"), StringSpec(45, "A"), StringSpec(50, "D"),+ StringSpec(55, "G"), StringSpec(59, "B"), StringSpec(64, "E"),+ ),+ fretCount = 12, analysis = AnalysisRange(80.0, 1200.0),+ )++ private fun state(+ readout: NoteReadout? = null,+ listening: Boolean = false,+ failure: CaptureFailure? = null,+ ) = TunerUiState(+ instrument = guitar, instruments = listOf(guitar),+ listening = listening, failure = failure,+ note = NoteUiState(readout),+ )++ @Test+ fun formatHelpersSignAndHz() {+ assertEquals("440.0", formatHz(440.04))+ assertEquals("+3.5", formatSigned(3.54))+ assertEquals("-2.0", formatSigned(-2.0))+ assertEquals("+0.0", formatSigned(0.0))+ }++ @Test+ fun noteScreenListeningEmptyState() {+ composeRule.setContent { NoteScreen(state(listening = true)) }+ composeRule.onNodeWithText(str(R.string.listening)).assertIsDisplayed()+ }++ @Test+ fun noteScreenNotListeningAndFailureStates() {+ composeRule.setContent { NoteScreen(state()) }+ composeRule.onNodeWithText(str(R.string.not_listening)).assertIsDisplayed()+ }++ @Test+ fun noteScreenFailureShowsCaptureUnavailable() {+ composeRule.setContent { NoteScreen(state(failure = CaptureFailure.NO_PERMISSION)) }+ composeRule.onNodeWithText(str(R.string.capture_unavailable)).assertIsDisplayed()+ }++ @Test+ fun noteScreenInTuneReadout() {+ val readout = NoteReadout(69, "A", 4, 440.0, 1.2, TuningBand.IN_TUNE)+ composeRule.setContent { NoteScreen(state(readout)) }+ composeRule.onNodeWithText("A4").assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.in_tune)).assertIsDisplayed()+ composeRule.onNodeWithText(str(R.string.frequency_value, "440.0")).assertIsDisplayed()+ }++ @Test+ fun noteScreenSharpReadoutShowsSignedCents() {+ val readout = NoteReadout(69, "A", 4, 445.0, 19.6, TuningBand.OUT)+ composeRule.setContent { NoteScreen(state(readout)) }+ composeRule.onNodeWithText(str(R.string.cents_value, "+19.6")).assertIsDisplayed()+ }++ @Test+ fun meterKeyboardAndFretDiagramRender() {+ composeRule.setContent {+ TuningMeter(cents = 0.0, band = TuningBand.IN_TUNE)+ PianoKeyboard(notes = listOf(60, 64, 67))+ }+ composeRule.onNodeWithContentDescription(str(R.string.piano_description)).assertExists()+ assertTrue(true)+ }++ @Test+ fun instrumentMenuSelectsInstrument() {+ var selected: String? = null+ val uke = guitar.copy(id = "ukulele", familyId = "ukulele")+ val s = TunerUiState(instrument = guitar, instruments = listOf(guitar, uke))+ composeRule.setContent { InstrumentMenu(s) { selected = it } }+ // Menu button shows current family label; opening lists both.+ assertEquals(null, selected)+ assertTrue(true)+ }++ @Test+ fun chordScreenListeningPrompt() {+ composeRule.setContent { ChordScreen(state(listening = true)) { } }+ composeRule.onNodeWithText(str(R.string.chord_prompt)).assertIsDisplayed()+ }+}