diff --git a/android/app/src/androidTest/java/com/caoccao/javet/shell/TestMainActivity.kt b/android/app/src/androidTest/java/com/caoccao/javet/shell/TestMainActivity.kt index b5f2d3c..5ce5e8a 100644 --- a/android/app/src/androidTest/java/com/caoccao/javet/shell/TestMainActivity.kt +++ b/android/app/src/androidTest/java/com/caoccao/javet/shell/TestMainActivity.kt @@ -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().use { v8Runtime -> @@ -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") } } @@ -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().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") + } } } \ No newline at end of file diff --git a/android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt b/android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt index 5caedef..253a7c5 100644 --- a/android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt +++ b/android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt @@ -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 @@ -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( @@ -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()) { diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 56d2b4f..cf49fdd 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -18,4 +18,5 @@ Javet Shell Execute + Refresh \ No newline at end of file