135 lines · 7.7 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.compose.runtime.getValueimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.messages.chat.Avatarimport com.cottondb.messages.chat.Foundimport com.cottondb.messages.chat.StarredListimport com.cottondb.messages.chat.TimerCardimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** Starred and disappearing messages on Android (CONTRACT.md): the core's answers read and drawn. Invented chats and words. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = Application::class)class ChatStarredTimerTest { @get:Rule val compose = createAndroidComposeRule<ComponentActivity>() private fun json(s: String) = CottonCore.json.parseToJsonElement(s).jsonObject private val timer = """{"seconds":604800,"title":"Disappearing messages","label":"Disappearing messages: 7 days", "note":"Disappearing can't stop anyone keeping a message another way, such as a screenshot.", "choices":[{"seconds":0,"label":"Off","on":false},{"seconds":86400,"label":"24 hours","on":false}, {"seconds":604800,"label":"7 days","on":true},{"seconds":7776000,"label":"90 days","on":false}]}""" // RULE: The timer row says the length it stands at; its sheet offers the four lengths and the note, and a choice is handed back. @Test fun the_timer_row_offers_the_lengths_and_says_what_it_cannot_stop() { val card = TimerCard.of(json(timer)) assertEquals("7 days", card.length) var set: Int? = null compose.setContent { WarmTheme(night = false) { var open by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(false) } TimerRow(card) { open = true } if (open) TimerSheet(card, "Close", { open = false }) { set = it } } } compose.onNodeWithText("7 days").assertIsDisplayed() compose.onNodeWithTag("chat-timer").performClick() compose.onNodeWithTag("chat-timer-note").assertExists() compose.onNodeWithTag("chat-timer-86400").performClick() assertEquals(86400, set) } // RULE: Tapped on a person's page, Disappearing messages opens its sheet over the page (walk, 21 Nov 2026: the app // closed at the tap, the sheet measured inside the page's scrolling column with no height to stand in). @Test fun the_timer_sheet_opens_over_a_persons_page() { val card = TimerCard.of(json(timer)) var set: Int? = null val words = PersonWords("Back", "Message", "Call", "Video", "Between you", "Nothing between you yet.", { "Calls themselves $it" }, "Number", "Mute", "Unmute", "Mute notifications", "Block", "Unblock", "You blocked them.", "Close") compose.setContent { WarmTheme(night = false) { PersonPage(PersonView("Odile Example", null, Avatar("OE", 1, null), "+15550100123", "", emptyList(), emptyList(), false, false, emptyList(), emptyList()), words, {}, {}, {}, {}, {}, {}, {}, timer = card, onTimer = { set = it }) } } compose.onNodeWithTag("chat-timer").performClick() compose.onNodeWithTag("chat-timer-note").assertIsDisplayed() compose.onNodeWithTag("chat-timer-86400").performClick() assertEquals(86400, set) } // RULE (owner, FEEDBACK-2 B.1, 23 Nov 2026: he could not find the row): Disappearing messages stands under the page's // buttons, above Between you, on every person's page, a WhatsApp chat's (no number) too, whose sheet says it is only // his copy. It stood under Between you, below the fold on a chat with pictures. @Test fun the_timer_row_stands_above_between_you_on_a_whatsapp_chats_page() { val own = TimerCard.of(json(timer.replace("Disappearing can't stop anyone keeping a message another way, such as a screenshot.", "Only your copy in Cotton. WhatsApp keeps its own."))) val words = PersonWords("Back", "Message", "Call", "Video", "Between you", "Nothing between you yet.", { "Calls themselves $it" }, "Number", "Mute", "Unmute", "Mute notifications", "Block", "Unblock", "You blocked them.", "Close") val between = listOf("Photos" to 40, "Videos" to 3, "Voice messages" to 12, "Files" to 5, "Links" to 9) compose.setContent { WarmTheme(night = false) { PersonPage(PersonView("Bimal Example", null, Avatar("BE", 2, null), null, "", emptyList(), between, false, false, emptyList(), emptyList()), words, {}, {}, {}, {}, {}, {}, {}, timer = own, onTimer = {}) } } val row = compose.onNodeWithTag("chat-timer").fetchSemanticsNode().boundsInRoot val heading = compose.onNodeWithText("Between you", ignoreCase = true, useUnmergedTree = true).fetchSemanticsNode().boundsInRoot org.junit.Assert.assertTrue("the row ($row) stands above Between you ($heading)", row.bottom <= heading.top) compose.onNodeWithTag("chat-timer").assertIsDisplayed().performClick() compose.onNodeWithText("Only your copy in Cotton. WhatsApp keeps its own.").assertIsDisplayed() } // RULE: The same on a group's page. @Test fun the_timer_sheet_opens_over_a_groups_page() { val card = TimerCard.of(json(timer)) var set: Int? = null compose.setContent { WarmTheme(night = false) { GroupPage( GroupView("Hill Walkers", 3, 64, "member", emptySet(), emptyList(), emptyList()), Avatar("HW", 1, null), { it }, "Back", "Close", onBack = {}, onAdd = {}, onRename = {}, onPicture = {}, onLeave = {}, onMember = {}, status = "3 members", timer = card, onTimer = { set = it }, ) } } compose.onNodeWithTag("chat-timer").performClick() compose.onNodeWithTag("chat-timer-note").assertIsDisplayed() compose.onNodeWithTag("chat-timer-0").performClick() assertEquals(0, set) } // RULE: The starred list draws each message as a search result, newest starred first; a tap hands the message back; empty says so. @Test fun the_starred_list_draws_and_opens_its_messages() { val list = StarredList.of(json("""{"title":"Starred messages","messages":[ {"thread":"t-asha","message":"m-2","name":"Asha Example","picture":{"initials":"AE","tint":1},"group":false,"mine":true,"from":"You","line":"platform four","when":"10:42"}, {"thread":"t-asha","message":"m-1","name":"Asha Example","picture":{"initials":"AE","tint":1},"group":false,"mine":false,"line":"the train is at seven","when":"10:40"}]}""")) assertEquals(listOf("m-2", "m-1"), list.found.map { it.message }) assertNull(list.empty) var opened: Found? = null compose.setContent { WarmTheme(night = false) { StarredSheet(list, "Close", {}, {}) { opened = it } } } compose.onNodeWithText("Starred messages").assertIsDisplayed() compose.onNodeWithTag("chat-found-m-1").performClick() assertEquals("m-1", opened?.message) assertEquals("Nothing starred yet.", StarredList.of(json("""{"title":"Starred messages","messages":[],"empty":"Nothing starred yet."}""")).empty) }}