@@ -36,6 +36,8 @@ import org.junit.Test
3636import org.junit.runner.RunWith
3737import org.robolectric.annotation.Config
3838import java.io.File
39+ import kotlin.test.assertFalse
40+ import kotlin.test.assertNotNull
3941import kotlin.test.junit5.JUnit5Asserter.assertNotNull
4042
4143@RunWith(AndroidJUnit4 ::class )
@@ -142,4 +144,49 @@ class AudioRecorderTest : RobolectricTest() {
142144 assertEquals(customFile.absolutePath, audioRecorder.currentFile?.absolutePath)
143145 verify { mockMediaRecorder.setOutputFile(customFile.absolutePath) }
144146 }
147+
148+ @Test
149+ fun `stop should delete temp file if not kept` () {
150+ val audioRecorder = createRecorder()
151+ audioRecorder.start()
152+
153+ val tempFile = audioRecorder.currentFile
154+ assertTrue(" Temp file should exist" , tempFile?.exists() ? : false )
155+
156+ audioRecorder.stop(keepFile = false )
157+ assertFalse(tempFile?.exists() ? : true )
158+ }
159+
160+ @Test
161+ fun `close() while recording temp file should delete the file` () {
162+ val audioRecorder = createRecorder()
163+ audioRecorder.start()
164+ val tempFile = audioRecorder.currentFile
165+
166+ assertTrue(" Temp file should exist" , tempFile?.exists() ? : false )
167+
168+ audioRecorder.close()
169+
170+ assertFalse(tempFile?.exists() ? : true , " Temp file should be cleaned up on close" )
171+ verify { mockMediaRecorder.release() }
172+ }
173+
174+ @Test
175+ fun `recording after a failed stop should still work and clean up` () {
176+ every { mockMediaRecorder.stop() } throws RuntimeException (" stop failed" )
177+
178+ val audioRecorder = createRecorder()
179+ audioRecorder.start()
180+ val firstFile = audioRecorder.currentFile
181+
182+ audioRecorder.stop()
183+
184+ assertFalse(firstFile?.exists() ? : true , " File should be deleted if recorder.stop() fails" )
185+ assertFalse(audioRecorder.isRecording)
186+
187+ // Ensure we can start again immediately
188+ audioRecorder.start()
189+ assertTrue(audioRecorder.isRecording)
190+ assertNotNull(audioRecorder.currentFile)
191+ }
145192}
0 commit comments