package com.cottondb.days import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsNotSelected import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithTag import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import com.cottondb.days.pen.DaysCardLook import com.cottondb.days.pen.penFonts import com.cottondb.days.ui.GoogleGroup import com.cottondb.days.ui.GoogleNotice import com.cottondb.days.ui.FirstDay import com.cottondb.shell.LocalCardLook import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() 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() 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(), 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) } }