Cotton code
125 lines · 7.1 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.getValueimport androidx.compose.runtime.setValueimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.assertIsNotSelectedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithTagimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.days.pen.DaysCardLookimport com.cottondb.days.pen.penFontsimport com.cottondb.days.ui.GoogleGroupimport com.cottondb.days.ui.GoogleNoticeimport com.cottondb.days.ui.FirstDayimport com.cottondb.shell.LocalCardLookimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * Connect and disconnect Google Calendar from inside Days (coordinator, 22 Nov 2026): settings drew Connect Google * Calendar whether or not Google was connected, never which account was, and nothing in Days disconnected it. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w411dp-h914dp-xhdpi", application = Application::class)class DaysGoogleGroupTest {    @get:Rule val compose = createComposeRule()    private fun answer(json: String): JsonObject = CottonCore.json.parseToJsonElement(json).jsonObject    private fun show(google: JsonObject, onConnect: () -> Unit = {}, onDisconnect: (String) -> Unit = {}, onKeep: (Boolean) -> Unit = {}, onSignIn: () -> Unit = {}) = compose.setContent {        CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) {            GoogleGroup(google, fallback = "line", onConnect = onConnect, onDisconnect = onDisconnect, onKeep = onKeep, onSignIn = onSignIn)        }    }    /** "Also keep my Days events in Google Calendar" (CONTRACT §16): under the account, off until turned on. */    @Test fun the_keep_switch_stands_under_an_account_and_a_tap_turns_it_on() {        val kept = mutableListOf<Boolean>()        show(answer("""{"line":"l","accounts":[{"email":"walk@example.test","says":"Up to date","trouble":false}],"connect":null,"unavailable":null,            "keep":{"label":"Also keep my Days events in Google Calendar","line":"Your own events go to Google too.","on":false}}"""), onKeep = { kept += it })        compose.onNodeWithTag("days-google-keep").assertIsNotSelected().performClick()        assertEquals(listOf(true), kept)    }    @Test fun nothing_connected_offers_connect_and_no_account() {        var connected = 0        show(answer("""{"line":"Bring in your Google calendars.","accounts":[],"connect":{"id":"google_connect","tool":"calendar","label":"Connect Google Calendar"},"unavailable":null}"""), onConnect = { connected++ })        assertEquals(0, compose.onAllNodesWithTag("days-google-account").fetchSemanticsNodes().size)        compose.onNodeWithText("Connect Google Calendar").performClick()        assertEquals(1, connected)    }    @Test fun a_connected_account_stands_and_its_disconnect_asks_before_it_goes() {        val gone = mutableListOf<String>()        show(answer("""{"line":"l","accounts":[{"email":"walk@example.test","says":"Up to date · 312 events","trouble":false,            "disconnect":{"id":"google_disconnect","tool":"calendar","account":"walk@example.test","label":"Disconnect",            "asks":"Disconnect walk@example.test? Those already here stay.","yes":"Disconnect"}}],"connect":null,"unavailable":null}"""),            onDisconnect = { gone += it })        compose.onNodeWithText("walk@example.test").assertIsDisplayed()        compose.onNodeWithText("Up to date · 312 events").assertIsDisplayed()        assertEquals("no Connect beside a connected account", 0, compose.onAllNodesWithTag("days-settings-connect").fetchSemanticsNodes().size)        compose.onNodeWithTag("days-google-disconnect").performClick()        assertEquals("asked first", emptyList<String>(), gone)        compose.onNodeWithText("Disconnect walk@example.test? Those already here stay.").assertIsDisplayed()        compose.onNodeWithTag("days-google-disconnect-yes", useUnmergedTree = true).performClick()        assertEquals(listOf("walk@example.test"), gone)    }    /** Signed out: why, and Sign in right there (store pass, 24 Nov 2026: the page was a dead end); never Connect. */    @Test fun signed_out_says_why_and_offers_sign_in() {        var signIn = 0        show(answer("""{"line":"l","accounts":[],"connect":null,"unavailable":"Sign in first, then connect Google Calendar.","signIn":"Sign in"}"""), onSignIn = { signIn++ })        compose.onNodeWithTag("days-google-unavailable").assertIsDisplayed()        assertEquals(0, compose.onAllNodesWithTag("days-settings-connect").fetchSemanticsNodes().size)        compose.onNodeWithTag("days-google-sign-in").assertIsDisplayed().performClick()        assertEquals(1, signIn)    }    @Test fun the_notice_stands_before_google_and_only_continue_goes_on() {        var went = 0        var not = 0        compose.setContent {            CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) {                GoogleNotice(                    answer("""{"title":"Connect Google Calendar","body":"Days will read the events on your Google account's main calendar.","go":"Continue to Google","notNow":"Not now"}"""),                    onGo = { went++ }, onNotNow = { not++ },                )            }        }        compose.onNodeWithText("Days will read the events on your Google account's main calendar.").assertIsDisplayed()        compose.onNodeWithText("Not now").performClick()        assertEquals(0 to 1, went to not)        compose.onNodeWithText("Continue to Google").performClick()        assertEquals(1, went)    }    private val lines = mapOf("empty.today" to "Nothing on today.", "empty.line" to "Add something, or bring in your calendar.",        "empty.line_alone" to "Add a to-do or an event.", "empty.add" to "Add something", "empty.connect" to "Connect Google Calendar")    @Test fun the_first_day_offers_the_calendar_only_while_it_can_be_connected() {        var connectable by androidx.compose.runtime.mutableStateOf(true)        compose.setContent {            CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) {                FirstDay({ lines.getValue(it) }, onAdd = {}, onConnect = if (connectable) ({}) else null)            }        }        compose.onNodeWithText("Add something, or bring in your calendar.").assertIsDisplayed()        compose.onNodeWithText("Connect Google Calendar").assertIsDisplayed()        connectable = false        compose.onNodeWithText("Add a to-do or an event.").assertIsDisplayed()        assertEquals(0, compose.onAllNodesWithTag("days-first-connect").fetchSemanticsNodes().size)    }}