package com.cottondb.messages.calls import com.cottondb.calling.CallPush import com.cottondb.messages.chat.Item import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.Words import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import java.io.File /** Calls in Cotton Chat on Android draw the core's answers and decide nothing: these read them as the screens do. Every name is invented. */ class ChatCallsTest { private fun answer(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } /** The call screen is the core's: its name, status or live clock, its controls in order, a ring's two keys. */ @Test fun the_call_screen_is_read_as_answered() { val live = CallView.of(answer("""{"shown":true,"phase":"live","name":"Asha Example","picture":{"initials":"AE","tint":3},"liveAt":1000,"video":true,"camera":true, "controls":[{"id":"mute","label":"Mute","on":false},{"id":"speaker","label":"Speaker","on":true},{"id":"video","label":"Camera","on":true},{"id":"flip","label":"Flip","on":false},{"id":"end","label":"End","on":false}], "bar":"Asha Example · tap to return","endedFor":2000}""")) assertTrue(live.shown && live.video && live.camera) assertNull(live.status) assertEquals(1000L, live.liveAt) assertEquals(listOf("mute", "speaker", "video", "flip", "end"), live.controls.map { it.id }) assertTrue(live.controls.first { it.id == "speaker" }.on) assertEquals("AE", live.avatar.initials) val ring = CallView.of(answer("""{"shown":true,"phase":"ringing_in","name":"Asha Example","picture":{"initials":"AE","tint":3},"status":"Cotton voice call","video":false,"camera":false,"controls":[], "ring":{"title":"Cotton voice call","answer":"Answer","decline":"Decline"},"endedFor":2000}""")) assertEquals("Answer", ring.ring?.answer) assertTrue(ring.controls.isEmpty()) assertFalse(CallView.of(answer("""{"shown":false,"phase":"idle","name":"","picture":{"initials":"","tint":0},"video":false,"camera":false,"controls":[],"endedFor":2000}""")).shown) } /** A live call's clock: m:ss, then h:mm:ss past an hour, as the core says a call's length. */ @Test fun the_clock_reads_as_the_core_says_lengths() { assertEquals("0:07", CallView.clock(7)) assertEquals("12:05", CallView.clock(725)) assertEquals("1:02:09", CallView.clock(3729)) } /** The Calls tile's rows, newest first as given, missed ones marked, paged by `next`. */ @Test fun the_calls_tile_is_read_as_answered() { val page = CallsPage.of(answer("""{"rows":[ {"call":"c1","name":"Asha Example","picture":{"initials":"AE","tint":3},"thread":"t1","incoming":true,"video":false,"missed":true,"group":false,"line":"Missed voice call","when":"10:42"}, {"call":"c2","name":"+1 555-010-0077","picture":{"initials":"#","tint":1},"incoming":false,"video":true,"missed":false,"group":false,"line":"Video call · 3:12","when":"Yesterday"}], "next":"x","words":{"title":"Calls","empty":"No calls yet.","back":"Call back"}}""")) assertEquals(listOf("c1", "c2"), page.rows.map { it.call }) assertTrue(page.rows[0].missed && page.rows[0].incoming) assertNull(page.rows[1].thread) assertEquals("x", page.next) assertEquals("Calls", page.title) } /** A thread's header carries the core's call buttons, and a call's line its call to ring back. */ @Test fun a_thread_carries_its_call_buttons_and_lines() { val words = Words { _, key -> key } val page = ThreadPage.of(answer("""{"thread":"t1","header":{"name":"Asha Example","picture":{"initials":"AE","tint":3},"group":false,"source":"cotton", "calls":[{"video":false,"label":"Call"},{"video":true,"label":"Video call"}]}, "items":[{"item":"event","message":"m-call","text":"Missed video call","call":{"call":"c1","video":true,"missed":true,"incoming":true,"time":"10:42","back":"Call back"}}, {"item":"event","message":"m-joined","text":"Bimal joined"}], "unread":0,"actions":{}}"""), words) assertEquals(listOf(false to "Call", true to "Video call"), page.header?.calls) val events = page.items.filterIsInstance() assertEquals("c1", events[0].call?.call) assertTrue(events[0].call!!.missed && events[0].call!!.video) assertNull(events[1].call) } /** An imported thread's header has no call buttons. */ @Test fun an_imported_thread_offers_no_call() { val page = ThreadPage.of(answer("""{"thread":"t2","header":{"name":"Old number","picture":{"initials":"ON","tint":1},"group":false,"source":"sms"},"items":[],"unread":0,"actions":{}}"""), Words { _, key -> key }) assertTrue(page.header!!.calls.isEmpty()) } /** A ring's push and a chat's push come through Chat's one Firebase service: a call's is the engine's, a chat's is not. */ @Test fun a_ring_is_the_engines_and_a_chat_is_not() { assertTrue(CallPush.of(mapOf("kind" to "ring", "room" to "0123456789abcdef0123456789abcdef", "video" to "1")) is CallPush.Ring) assertTrue(CallPush.of(mapOf("kind" to "stop", "room" to "0123456789abcdef0123456789abcdef", "stop" to "answered_elsewhere")) is CallPush.Stop) assertNull(CallPush.of(mapOf("kind" to "chat"))) } /** Chat's manifest declares what Telecom and the ring need, as Cotton Calling's did. */ @Test fun the_manifest_holds_what_a_call_needs() { val manifest = File(repo, "apps/android/app/src/messages/AndroidManifest.xml").readText() listOf( "android.permission.RECORD_AUDIO", "android.permission.CAMERA", "android.permission.MANAGE_OWN_CALLS", "android.permission.USE_FULL_SCREEN_INTENT", "android.permission.FOREGROUND_SERVICE_PHONE_CALL", "com.cottondb.calling.CallConnectionService", "com.cottondb.calling.CallService", "com.cottondb.calling.CallActionReceiver", "android.permission.BIND_TELECOM_CONNECTION_SERVICE", ).forEach { assertTrue("$it declared", manifest.contains(it)) } assertTrue(File(repo, "apps/android/app/flavours.json").readText().substringAfter("\"messages\"").substringBefore("]").contains("stream-webrtc-android")) } }