127 lines · 5.6 KB · KotlinRaw
package com.cottondb.meetingsimport android.app.Applicationimport android.app.NotificationManagerimport android.content.Intentimport android.content.pm.ServiceInfoimport android.os.Looperimport androidx.test.core.app.ApplicationProviderimport com.cottondb.meetings.record.Captureimport com.cottondb.meetings.record.RecorderServiceimport com.cottondb.meetings.record.RecorderStateimport com.cottondb.meetings.record.Recordingimport com.cottondb.meetings.record.Takeimport org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNotNullimport org.junit.Assert.assertTrueimport org.junit.Beforeimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.Robolectricimport org.robolectric.RobolectricTestRunnerimport org.robolectric.Shadows.shadowOfimport org.robolectric.annotation.Configimport org.robolectric.android.controller.ServiceController/** * The recording service's life on a (Robolectric) phone: a start puts it in the foreground with the microphone's * type and an ongoing notice, Pause and Mark reach the take, a swipe from recents leaves it recording, Stop ends the * take on disk and takes the service out of the foreground. The microphone is a stand-in handing made frames. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = Application::class)class RecorderServiceTest { private class Frames : Capture { override val sampleRate = 16_000 override val channels = 1 override val bitrate = 32_000 var frame: ((ByteArray, Int, Int) -> Unit)? = null override fun start(frame: (ByteArray, Int, Int) -> Unit, lost: (Throwable?) -> Unit) { this.frame = frame } override fun stop() {} fun emit(n: Int) = repeat(n) { frame!!(ByteArray(80), 0, 80) } } private val app get() = ApplicationProvider.getApplicationContext<Application>() private val captures = mutableListOf<Frames>() private lateinit var controller: ServiceController<RecorderService> @Before fun fresh() { Recording.reset() app.filesDir.resolve("meetings").deleteRecursively() Recording.capture = { Frames().also(captures::add) } controller = Robolectric.buildService(RecorderService::class.java).create() } @After fun forget() { Recording.reset() } private fun send(action: String, kind: String? = null) { val intent = Intent(app, RecorderService::class.java).setAction(action) kind?.let { intent.putExtra(RecorderService.KIND, it) } controller.withIntent(intent).startCommand(0, 1) shadowOf(Looper.getMainLooper()).idle() } private val state get() = Recording.recorder(app).state.value @Test fun a_start_is_a_microphone_foreground_service_with_an_ongoing_notice() { send(RecorderService.START, Take.KIND_MEMO) val service = shadowOf(controller.get()) assertFalse(service.isForegroundStopped) assertEquals(ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE, controller.get().foregroundServiceType) val notice = service.lastForegroundNotification assertNotNull(notice) assertTrue("ongoing: the person cannot swipe a recording's notice away", notice.flags and android.app.Notification.FLAG_ONGOING_EVENT != 0) assertEquals("Pause, Mark, Stop", 3, notice.actions.size) assertEquals(RecorderState.Phase.Recording, state.phase) assertEquals(Take.KIND_MEMO, state.kind) } @Test fun pause_mark_and_a_swipe_from_recents_reach_the_take_and_stop_ends_it() { send(RecorderService.START) captures[0].emit(20) send(RecorderService.MARK) assertEquals(listOf(20 * 64L), state.marks) send(RecorderService.PAUSE) assertEquals(RecorderState.Phase.Paused, state.phase) val paused = shadowOf(app.getSystemService(NotificationManager::class.java)).allNotifications.last() assertFalse("paused: the time stands still", paused.extras.getBoolean(android.app.Notification.EXTRA_SHOW_CHRONOMETER)) send(RecorderService.RESUME) captures.last().emit(5) controller.get().onTaskRemoved(Intent()) assertEquals("swiped away: still recording", RecorderState.Phase.Recording, state.phase) assertFalse(shadowOf(controller.get()).isStoppedBySelf) val take = state.take!! send(RecorderService.STOP) val deadline = System.currentTimeMillis() + 5_000 while (!shadowOf(controller.get()).isStoppedBySelf && System.currentTimeMillis() < deadline) { Thread.sleep(20); shadowOf(Looper.getMainLooper()).idle() } assertTrue(shadowOf(controller.get()).isStoppedBySelf) assertTrue(shadowOf(controller.get()).isForegroundStopped) assertEquals(RecorderState.Phase.Idle, state.phase) assertEquals(take, state.stoppedTake) val story = Recording.takes(app).all().single { it.id == take }.read() assertTrue(story.stopped) assertEquals(25 * 64L, story.durationMs) assertEquals(listOf(20 * 64L), story.marks) assertEquals(1, story.pauses.size) } @Test fun a_second_start_while_recording_is_the_same_take() { send(RecorderService.START) val first = state.take send(RecorderService.START) assertEquals(first, state.take) assertEquals(1, captures.size) } @Test fun restarted_by_the_system_with_nothing_recording_it_goes_away() { controller.withIntent(Intent(app, RecorderService::class.java)).startCommand(0, 1) shadowOf(Looper.getMainLooper()).idle() assertTrue(shadowOf(controller.get()).isStoppedBySelf) }}