Skip to content

Commit

Permalink
✨ feat: Add icon refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jan 12, 2024
1 parent b230924 commit 890e77e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class TestMainActivity {
@get:Rule
val composeRule = createComposeRule()

@Test
fun testAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.caoccao.javet.shell", appContext.packageName)
}

@Test
fun testButtonExecute() {
V8Host.getV8Instance().createV8Runtime<V8Runtime>().use { v8Runtime ->
Expand All @@ -49,9 +56,12 @@ class TestMainActivity {
HomeScreen(v8Runtime = v8Runtime, stringBuilder = StringBuilder())
}
}
composeRule.onNodeWithTag("basicTextFieldCodeString").performTextInput("1 + 1")
composeRule.onNodeWithTag("elevatedButtonExecute").performClick()
composeRule.onNodeWithTag("textResult").assertTextEquals("\n> 1 + 1\n2")
val nodeBasicTextField = composeRule.onNodeWithTag("basicTextFieldCodeString")
val nodeElevatedButtonExecute = composeRule.onNodeWithTag("elevatedButtonExecute")
val nodeTextResult = composeRule.onNodeWithTag("textResult")
nodeBasicTextField.performTextInput("1 + 1")
nodeElevatedButtonExecute.performClick()
nodeTextResult.assertTextEquals("\n> 1 + 1\n2")
}
}

Expand All @@ -64,16 +74,38 @@ class TestMainActivity {
}
}
val nodeBasicTextField = composeRule.onNodeWithTag("basicTextFieldCodeString")
val nodeTextResult = composeRule.onNodeWithTag("textResult")
nodeBasicTextField.performTextInput("1 + 1\n")
nodeBasicTextField.performTextInput("\n")
composeRule.onNodeWithTag("textResult").assertTextEquals("\n> 1 + 1\n2")
nodeTextResult.assertTextEquals("\n> 1 + 1\n2")
}
}

@Test
fun testAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.caoccao.javet.shell", appContext.packageName)
fun testIconButtonRefresh() {
V8Host.getV8Instance().createV8Runtime<V8Runtime>().use { v8Runtime ->
composeRule.setContent {
JavetShellTheme {
HomeScreen(v8Runtime = v8Runtime, stringBuilder = StringBuilder())
}
}
val nodeIconButtonRefresh = composeRule.onNodeWithTag("iconButtonRefresh")
val nodeElevatedButtonExecute = composeRule.onNodeWithTag("elevatedButtonExecute")
val nodeBasicTextField = composeRule.onNodeWithTag("basicTextFieldCodeString")
val nodeTextResult = composeRule.onNodeWithTag("textResult")
nodeBasicTextField.performTextInput("const a = 1\n")
nodeElevatedButtonExecute.performClick()
nodeTextResult.assertTextEquals("\n> const a = 1\nundefined")
nodeBasicTextField.performTextInput("const a = 1\n")
nodeElevatedButtonExecute.performClick()
nodeTextResult.assertTextEquals(
"\n> const a = 1\nundefined" +
"\n> const a = 1\nSyntaxError: Identifier 'a' has already been declared"
)
nodeIconButtonRefresh.performClick()
nodeBasicTextField.performTextInput("const a = 1\n")
nodeElevatedButtonExecute.performClick()
nodeTextResult.assertTextEquals("V8 context is refreshed.\n> const a = 1\nundefined")
}
}
}
24 changes: 22 additions & 2 deletions android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
Expand Down Expand Up @@ -103,6 +107,8 @@ fun HomeScreen(
v8Runtime: V8Runtime? = null,
stringBuilder: StringBuilder = StringBuilder()
) {
var resultString by remember { mutableStateOf(stringBuilder.toString()) }
var codeString by remember { mutableStateOf("") }
Scaffold(
topBar = {
TopAppBar(
Expand All @@ -112,14 +118,28 @@ fun HomeScreen(
),
title = {
Text(text = stringResource(id = R.string.app_name))
},
actions = {
IconButton(
onClick = {
v8Runtime?.resetContext()
stringBuilder.clear().append("V8 context is refreshed.")
codeString = ""
resultString = ""
},
modifier = modifier.testTag("iconButtonRefresh")
) {
Icon(
Icons.Default.Refresh,
contentDescription = stringResource(id = R.string.icon_refresh)
)
}
}
)
},
) { paddingValues ->
val padding = 5.dp
val scrollState = rememberScrollState(0)
var resultString by remember { mutableStateOf(stringBuilder.toString()) }
var codeString by remember { mutableStateOf("") }
val executeCode = {
val trimmedCodeString = codeString.trim()
if (trimmedCodeString.isNotBlank()) {
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
<resources>
<string name="app_name">Javet Shell</string>
<string name="button_execute">Execute</string>
<string name="icon_refresh">Refresh</string>
</resources>

0 comments on commit 890e77e

Please sign in to comment.