Skip to content

Commit c2416a3

Browse files
bhanu-dev82angrezichatterboxandrewtavis
authored
Refactor/keyboard UI manager (#542)
* Refactor: Extract UI management to KeyboardUIManager * updated tests to reflect the refactore of generalkeyboardime * feat:move keyboard test to keyboard test directory * Misc comment fixes for new changes * fix:shift key not working * Nit: Minor fix to comment --------- Co-authored-by: Gautham Mohanraj <62511283+angrezichatterbox@users.noreply.github.com> Co-authored-by: angrezichatterbox <gouthammohanraj@gmail.com> Co-authored-by: Andrew Tavis McAllister <andrew.t.mcallister@gmail.com>
1 parent 2bd2870 commit c2416a3

14 files changed

Lines changed: 2192 additions & 2024 deletions

File tree

app/src/androidTest/kotlin/be/scri/helpers/KeyboardTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/helpers/KeyboardTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class KeyboardTest {
5959
}
6060
every { mockInputConnection.deleteSurroundingText(1, 0) } returns true
6161

62-
keyHandler.handleKey(KeyboardBase.KEYCODE_DELETE, "en")
62+
keyHandler.handleKey(KeyboardBase.Companion.KEYCODE_DELETE, "en")
6363

6464
assert(currentText.length == initialText.length - 1) {
6565
"Expected length ${initialText.length - 1}, but got ${currentText.length}"
@@ -81,7 +81,7 @@ class KeyboardTest {
8181
mockInputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER))
8282
}
8383

84-
keyHandler.handleKey(KeyboardBase.KEYCODE_ENTER, "en")
84+
keyHandler.handleKey(KeyboardBase.Companion.KEYCODE_ENTER, "en")
8585

8686
verify(exactly = 1) { mockIME.handleKeycodeEnter() }
8787
verify(exactly = 1) {

app/src/androidTest/kotlin/be/scri/helpers/data/PluralFormsManagerTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/helpers/data/PluralFormsManagerTest.kt

File renamed without changes.

app/src/androidTest/kotlin/be/scri/services/GeneralKeyboardIMETest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/services/GeneralKeyboardIMETest.kt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,47 @@ class GeneralKeyboardIMETest {
1919

2020
@Before
2121
fun setUp() {
22-
inputConnection =
23-
mockk {
24-
every { getTextBeforeCursor(any(), any()) } returns ""
25-
}
22+
inputConnection = mockk(relaxed = true)
23+
24+
ime = mockk(relaxed = true)
2625

27-
ime =
28-
mockk(relaxed = true) {
29-
every { getCurrentInputConnection() } returns inputConnection
26+
every { ime.currentInputConnection } returns inputConnection
3027

31-
every { hasTextBeforeCursor } answers {
32-
val text = inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0)?.toString() ?: ""
33-
text.isNotEmpty() && text.trim() != "." && text.trim() != ""
34-
}
28+
every { ime.hasTextBeforeCursor } answers {
29+
val ic = ime.currentInputConnection
30+
if (ic == null) {
31+
false
32+
} else {
33+
val text = ic.getTextBeforeCursor(Int.MAX_VALUE, 0)?.toString() ?: ""
34+
text.isNotEmpty() && text.trim() != "." && text.trim() != ""
3535
}
36+
}
3637
}
3738

3839
@Test
3940
fun hasTextBeforeCursor_returnsTrue_whenTextExists() {
40-
every { inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0) } returns "hello"
41+
every { inputConnection.getTextBeforeCursor(any(), any()) } returns "hello"
4142

42-
println("Result: ${ime.hasTextBeforeCursor}")
4343
assertTrue(ime.hasTextBeforeCursor)
4444
}
4545

4646
@Test
4747
fun hasTextBeforeCursor_returnsFalse_whenTextIsEmpty() {
48-
every { inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0) } returns ""
48+
every { inputConnection.getTextBeforeCursor(any(), any()) } returns ""
4949

5050
assertFalse(ime.hasTextBeforeCursor)
5151
}
5252

5353
@Test
5454
fun hasTextBeforeCursor_returnsFalse_whenTextIsPeriod() {
55-
every { inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0) } returns ". "
55+
every { inputConnection.getTextBeforeCursor(any(), any()) } returns ". "
56+
57+
assertFalse(ime.hasTextBeforeCursor)
58+
}
5659

60+
@Test
61+
fun hasTextBeforeCursor_returnsFalse_whenInputConnectionIsNull() {
62+
every { ime.currentInputConnection } returns null
5763
assertFalse(ime.hasTextBeforeCursor)
5864
}
5965
}

app/src/androidTest/kotlin/be/scri/ui/screens/about/AboutUtilInstrumentedTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/ui/screens/about/AboutUtilInstrumentedTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ class AboutUtilInstrumentedTest {
297297
println("Testing ExternalLinks constants...")
298298

299299
// Test that external links are properly defined.
300-
assertThat(be.scri.ui.screens.about.ExternalLinks.GITHUB_SCRIBE).isNotEmpty()
301-
assertThat(be.scri.ui.screens.about.ExternalLinks.GITHUB_ISSUES).isNotEmpty()
302-
assertThat(be.scri.ui.screens.about.ExternalLinks.GITHUB_RELEASES).isNotEmpty()
303-
assertThat(be.scri.ui.screens.about.ExternalLinks.MATRIX).isNotEmpty()
304-
assertThat(be.scri.ui.screens.about.ExternalLinks.MASTODON).isNotEmpty()
300+
assertThat(ExternalLinks.GITHUB_SCRIBE).isNotEmpty()
301+
assertThat(ExternalLinks.GITHUB_ISSUES).isNotEmpty()
302+
assertThat(ExternalLinks.GITHUB_RELEASES).isNotEmpty()
303+
assertThat(ExternalLinks.MATRIX).isNotEmpty()
304+
assertThat(ExternalLinks.MASTODON).isNotEmpty()
305305

306306
println("ExternalLinks constants test passed!")
307307
}

app/src/androidTest/kotlin/be/scri/ui/screens/settings/ScribeItemListTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/ui/screens/settings/ScribeItemListTest.kt

File renamed without changes.

app/src/androidTest/kotlin/be/scri/ui/screens/settings/SettingsScreenInstallKeyboardButtonTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/ui/screens/settings/SettingsScreenInstallKeyboardButtonTest.kt

File renamed without changes.

app/src/androidTest/kotlin/be/scri/ui/screens/settings/SettingsScreenInstrumentedTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/ui/screens/settings/SettingsScreenInstrumentedTest.kt

File renamed without changes.

app/src/androidTest/kotlin/be/scri/ui/screens/settings/SettingsUtilTest.kt renamed to app/src/androidTestKeyboards/kotlin/be/scri/ui/screens/settings/SettingsUtilTest.kt

File renamed without changes.

app/src/main/java/be/scri/helpers/KeyHandler.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,7 @@ class KeyHandler(
201201
*/
202202

203203
private fun handleDeleteKey() {
204-
val isCommandBarActive =
205-
ime.currentState == ScribeState.TRANSLATE ||
206-
ime.currentState == ScribeState.CONJUGATE ||
207-
ime.currentState == ScribeState.PLURAL
208-
209-
ime.handleDelete(isCommandBarActive, ime.isDeleteRepeating()) // Pass the actual repeating status
204+
ime.handleDelete(ime.isDeleteRepeating()) // pass the actual repeating status
210205

211206
if (ime.currentState == ScribeState.IDLE) {
212207
val currentWord = ime.getLastWordBeforeCursor()
@@ -356,8 +351,8 @@ class KeyHandler(
356351
ScribeState.TRANSLATE,
357352
ScribeState.CONJUGATE,
358353
ScribeState.PLURAL,
359-
-> true // Use command bar for actual commands
360-
else -> false // Use main input field for IDLE and SELECT_COMMAND
354+
-> true // use command bar for actual commands
355+
else -> false // use main input field for IDLE and SELECT_COMMAND
361356
}
362357

363358
ime.handleElseCondition(code, ime.keyboardMode, isCommandBarActive)

0 commit comments

Comments
 (0)