package com.cottondb.meetings import android.app.Application import android.app.NotificationManager import android.content.Intent import android.content.pm.ServiceInfo import android.os.Looper import androidx.test.core.app.ApplicationProvider import com.cottondb.meetings.record.Capture import com.cottondb.meetings.record.RecorderService import com.cottondb.meetings.record.RecorderState import com.cottondb.meetings.record.Recording import com.cottondb.meetings.record.Take import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import 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() private val captures = mutableListOf() private lateinit var controller: ServiceController @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) } }