|
| 1 | +package ai.openclaw.app.ui.chat |
| 2 | + |
| 3 | +import ai.openclaw.app.AndroidScreenshotFixture |
| 4 | +import ai.openclaw.app.AndroidScreenshotScene |
| 5 | +import ai.openclaw.app.MainViewModel |
| 6 | +import ai.openclaw.app.NodeApp |
| 7 | +import ai.openclaw.app.NodeRuntime |
| 8 | +import ai.openclaw.app.NodeRuntimeMode |
| 9 | +import ai.openclaw.app.SecurePrefs |
| 10 | +import ai.openclaw.app.ui.design.ClawDesignTheme |
| 11 | +import android.content.Context |
| 12 | +import android.provider.Settings |
| 13 | +import androidx.compose.foundation.layout.Box |
| 14 | +import androidx.compose.foundation.layout.size |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.draw.clipToBounds |
| 17 | +import androidx.compose.ui.platform.testTag |
| 18 | +import androidx.compose.ui.test.assertHasClickAction |
| 19 | +import androidx.compose.ui.test.assertIsDisplayed |
| 20 | +import androidx.compose.ui.test.assertTextEquals |
| 21 | +import androidx.compose.ui.test.getUnclippedBoundsInRoot |
| 22 | +import androidx.compose.ui.test.hasSetTextAction |
| 23 | +import androidx.compose.ui.test.junit4.v2.createComposeRule |
| 24 | +import androidx.compose.ui.test.onNodeWithContentDescription |
| 25 | +import androidx.compose.ui.test.onNodeWithTag |
| 26 | +import androidx.compose.ui.test.onNodeWithText |
| 27 | +import androidx.compose.ui.test.performClick |
| 28 | +import androidx.compose.ui.test.performScrollTo |
| 29 | +import androidx.compose.ui.test.performTextReplacement |
| 30 | +import androidx.compose.ui.unit.dp |
| 31 | +import androidx.lifecycle.SavedStateHandle |
| 32 | +import androidx.lifecycle.ViewModelStore |
| 33 | +import org.junit.After |
| 34 | +import org.junit.Assert.assertTrue |
| 35 | +import org.junit.Before |
| 36 | +import org.junit.Rule |
| 37 | +import org.junit.Test |
| 38 | +import org.junit.runner.RunWith |
| 39 | +import org.robolectric.RobolectricTestRunner |
| 40 | +import org.robolectric.RuntimeEnvironment |
| 41 | +import org.robolectric.annotation.Config |
| 42 | +import java.util.UUID |
| 43 | + |
| 44 | +@RunWith(RobolectricTestRunner::class) |
| 45 | +@Config(sdk = [34], qualifiers = "w360dp-h800dp-420dpi") |
| 46 | +class ChatComposerLayoutTest { |
| 47 | + @get:Rule |
| 48 | + val composeRule = createComposeRule() |
| 49 | + |
| 50 | + private lateinit var app: NodeApp |
| 51 | + private lateinit var prefs: SecurePrefs |
| 52 | + private lateinit var runtime: NodeRuntime |
| 53 | + private var originalRuntime: NodeRuntime? = null |
| 54 | + private val viewModelStore = ViewModelStore() |
| 55 | + private var originalAnimatorScale: String? = null |
| 56 | + |
| 57 | + @Before |
| 58 | + fun setUp() { |
| 59 | + app = RuntimeEnvironment.getApplication() as NodeApp |
| 60 | + prefs = SecurePrefs(app, app.getSharedPreferences("chat-composer-${UUID.randomUUID()}", Context.MODE_PRIVATE)) |
| 61 | + AndroidScreenshotFixture.configure(AndroidScreenshotScene.Chat) |
| 62 | + runtime = NodeRuntime(app, prefs, NodeRuntimeMode.ScreenshotFixture) |
| 63 | + originalRuntime = app.peekRuntime() |
| 64 | + setApplicationRuntime(runtime) |
| 65 | + originalAnimatorScale = Settings.Global.getString(app.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE) |
| 66 | + Settings.Global.putFloat(app.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 0f) |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + fun tearDown() { |
| 71 | + viewModelStore.clear() |
| 72 | + setApplicationRuntime(originalRuntime) |
| 73 | + runtime.disconnect() |
| 74 | + AndroidScreenshotFixture.configure(AndroidScreenshotScene.Home) |
| 75 | + Settings.Global.putString(app.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, originalAnimatorScale) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun slashSuggestionsKeepEditorAndStopVisibleAndLastSuggestionReachable() { |
| 80 | + showChat() |
| 81 | + val editor = composeRule.onNode(hasSetTextAction()) |
| 82 | + editor.performTextReplacement("/") |
| 83 | + editor.assertTextEquals("/") |
| 84 | + |
| 85 | + assertEditorAndStopVisible() |
| 86 | + val lastSuggestion = composeRule.onNodeWithText("/loop").performScrollTo().assertIsDisplayed() |
| 87 | + assertEditorAndStopVisible() |
| 88 | + lastSuggestion.performClick() |
| 89 | + editor.assertTextEquals("/loop ") |
| 90 | + assertEditorAndStopVisible() |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun normalTextAndShortSuggestionListsKeepComposerVisible() { |
| 95 | + showChat() |
| 96 | + val editor = composeRule.onNode(hasSetTextAction()) |
| 97 | + listOf("hello", "/help", "/unknown").forEach { input -> |
| 98 | + editor.performTextReplacement(input) |
| 99 | + editor.assertTextEquals(input) |
| 100 | + assertEditorAndStopVisible() |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun showChat() { |
| 105 | + val viewModel = MainViewModel(app, prefs, SavedStateHandle()) |
| 106 | + viewModelStore.put("chat", viewModel) |
| 107 | + viewModel.enterScreenshotFixtureMode(AndroidScreenshotScene.Chat) |
| 108 | + composeRule.setContent { |
| 109 | + ClawDesignTheme { |
| 110 | + // A portrait phone's remaining content viewport after its IME opens. |
| 111 | + Box(Modifier.size(width = 360.dp, height = 400.dp).clipToBounds().testTag("chat-viewport")) { |
| 112 | + ChatScreen( |
| 113 | + viewModel = viewModel, |
| 114 | + talkActive = false, |
| 115 | + showSidebarButton = true, |
| 116 | + onOpenSidebar = {}, |
| 117 | + onToggleTalk = {}, |
| 118 | + onOpenSessions = {}, |
| 119 | + onOpenDashboard = {}, |
| 120 | + onOpenGatewaySettings = {}, |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + composeRule.waitUntil { viewModel.chatCommands.value.size == 6 } |
| 126 | + } |
| 127 | + |
| 128 | + private fun assertEditorAndStopVisible() { |
| 129 | + val viewport = composeRule.onNodeWithTag("chat-viewport").getUnclippedBoundsInRoot() |
| 130 | + val editorNode = composeRule.onNode(hasSetTextAction()) |
| 131 | + val stopNode = composeRule.onNodeWithContentDescription("Stop") |
| 132 | + val editor = editorNode.getUnclippedBoundsInRoot() |
| 133 | + val stop = stopNode.getUnclippedBoundsInRoot() |
| 134 | + assertTrue("Editor must retain a visible line: $editor inside $viewport", editor.bottom > editor.top) |
| 135 | + assertTrue("Stop must retain its touch target: $stop inside $viewport", stop.bottom - stop.top >= 48.dp) |
| 136 | + for (bounds in listOf(editor, stop)) { |
| 137 | + assertTrue("Composer control must stay below the viewport top", bounds.top >= viewport.top) |
| 138 | + assertTrue("Composer control must stay above the viewport bottom", bounds.bottom <= viewport.bottom) |
| 139 | + } |
| 140 | + editorNode.assertIsDisplayed() |
| 141 | + stopNode.assertIsDisplayed().assertHasClickAction() |
| 142 | + } |
| 143 | + |
| 144 | + private fun setApplicationRuntime(value: NodeRuntime?) { |
| 145 | + NodeApp::class.java |
| 146 | + .getDeclaredField("runtimeInstance") |
| 147 | + .apply { isAccessible = true } |
| 148 | + .set(app, value) |
| 149 | + } |
| 150 | +} |
0 commit comments