Skip to content

Commit a5cd0e6

Browse files
committed
test: unit tests to verify file deletion
1 parent c014ad7 commit a5cd0e6

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

AnkiDroid/src/test/java/com/ichi2/anki/recorder/AudioRecorderTest.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import org.junit.Test
3636
import org.junit.runner.RunWith
3737
import org.robolectric.annotation.Config
3838
import java.io.File
39+
import kotlin.test.assertFalse
40+
import kotlin.test.assertNotNull
3941
import 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

Comments
 (0)