Cotton code
92 lines · 4.9 KB · KotlinRaw
package com.cottondb.messages.smsimport android.provider.Telephonyimport android.telephony.SmsManagerimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performClickimport com.cottondb.messages.warm.SmsPageimport com.cottondb.messages.warm.SmsWordsimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport org.robolectric.annotation.GraphicsMode/** SMS on Android (chat DECISIONS "SMS on Android"): what the phone hands the core, and the Texts (SMS) page. Invented. */@RunWith(RobolectricTestRunner::class)@GraphicsMode(GraphicsMode.Mode.NATIVE)@Config(sdk = [35], application = android.app.Application::class)class SmsTest {    @get:Rule val compose = createComposeRule()    /** A local number is read in the SIM's country; a sender with a name is not a number. */    @Test fun a_local_number_is_read_as_e164_and_a_named_sender_is_not() {        assertEquals("+919000061611", Sms.e164("09000061611", "IN"))        assertEquals("+919000061611", Sms.e164("+91 90000 61611", "US"))        assertNull(Sms.e164("JD-HDFCBK", "IN"))        val text = Sms.text("09000061611", "IN", "Reached", 1_763_900_000_000, out = false, live = true)        assertEquals("+919000061611", text["number"]!!.jsonPrimitive.content)        assertEquals("09000061611", text["from"]!!.jsonPrimitive.content)        assertEquals("true", text["live"]!!.jsonPrimitive.content)        assertNull(Sms.text("JD-HDFCBK", "IN", "Rs 500", 1L, out = false)["number"])    }    /** Android's store: in, sent and failed are read; drafts and texts still going are not. */    @Test fun the_store_reads_in_sent_and_failed_only() {        assertEquals(false to false, Sms.kindOf(Telephony.Sms.MESSAGE_TYPE_INBOX))        assertEquals(true to false, Sms.kindOf(Telephony.Sms.MESSAGE_TYPE_SENT))        assertEquals(true to true, Sms.kindOf(Telephony.Sms.MESSAGE_TYPE_FAILED))        assertNull(Sms.kindOf(Telephony.Sms.MESSAGE_TYPE_DRAFT))        assertNull(Sms.kindOf(Telephony.Sms.MESSAGE_TYPE_OUTBOX))    }    /** The radio's failures by the core's names. */    @Test fun a_failed_send_is_named_as_the_core_names_it() {        assertEquals("no_service", Sms.why(SmsManager.RESULT_ERROR_NO_SERVICE))        assertEquals("radio_off", Sms.why(SmsManager.RESULT_ERROR_RADIO_OFF))        assertEquals("limit", Sms.why(SmsManager.RESULT_ERROR_LIMIT_EXCEEDED))        assertEquals("generic", Sms.why(SmsManager.RESULT_ERROR_GENERIC_FAILURE))    }    /** A text in parts is told sent once, after its last part; a part that failed makes the whole text failed. */    @Test fun a_text_in_parts_answers_once_after_its_last_part() {        SmsSent.expect("m1", 3, "+919000061611", "long words", 5L)        assertNull(SmsSent.answered("m1", null))        assertNull(SmsSent.answered("m1", "no_service"))        val (why, going, done) = SmsSent.answered("m1", null)!!        assertTrue(done)        assertEquals("no_service", why)        assertEquals(Triple("+919000061611", "long words", 5L), SmsSent.parts(going))    }    /** A picture message's notice names its sender in its From field. */    @Test fun a_picture_messages_notice_names_its_sender() {        val address = "+919000061611/TYPE=PLMN".toByteArray()        val pdu = byteArrayOf(0x8C.toByte(), 0x82.toByte(), 0x98.toByte(), 'x'.code.toByte(), 0, 0x89.toByte(), (address.size + 2).toByte(), 0x80.toByte()) + address + byteArrayOf(0)        assertEquals("+919000061611", Sms.mmsSender(pdu))        assertNull(Sms.mmsSender(byteArrayOf(0x8C.toByte(), 0x82.toByte())))    }    /** The page: one paragraph and one button while Comms is not the SMS app; once it is, that and the way back. */    @Test fun the_page_asks_only_from_its_button() {        var used = 0        val heldState = androidx.compose.runtime.mutableStateOf(false)        val words = SmsWords("Texts (SMS)", "Back", "Make Comms the SMS app on this phone.", "Use Comms for texts", "Comms is the SMS app on this phone.", "Open Default apps")        compose.setContent { val held = heldState.value; SmsPage(words, held, reading = if (held) "Reading your texts… 500 so far" else null, onUse = { used++ }, onChange = {}, onBack = {}) }        compose.onNodeWithTag("you-sms-line").assertIsDisplayed()        assertEquals(0, used)        compose.onNodeWithTag("you-sms-use").performClick()        assertEquals(1, used)        heldState.value = true        compose.onNodeWithTag("you-sms-held").assertIsDisplayed()        compose.onNodeWithTag("you-sms-reading").assertIsDisplayed()        compose.onNodeWithTag("you-sms-change").assertIsDisplayed()    }}