Skip to content

Commit

Permalink
🧪 test: Add TestMainActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jan 11, 2024
1 parent eabe40f commit 5326dcf
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,49 @@

package com.caoccao.javet.shell

import androidx.test.platform.app.InstrumentationRegistry
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.assertValueEquals
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import com.caoccao.javet.interop.V8Host
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.shell.ui.theme.JavetShellTheme
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
class TestMainActivity {
@get:Rule
val composeRule = createComposeRule()

@Test
fun testExecute() {
V8Host.getV8Instance().createV8Runtime<V8Runtime>().use { v8Runtime ->
composeRule.setContent {
JavetShellTheme {
HomeScreen(v8Runtime = v8Runtime, stringBuilder = StringBuilder())
}
}
composeRule.onNodeWithTag("basicTextFieldCodeString").performTextInput("1 + 1")
composeRule.onNodeWithTag("elevatedButtonExecute").performClick()
composeRule.onNodeWithTag("textResult").assertTextEquals("\n> 1 + 1\n2")
}
}

@Test
fun useAppContext() {
fun testAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.caoccao.javet.shell", appContext.packageName)
Expand Down
64 changes: 36 additions & 28 deletions android/app/src/main/java/com/caoccao/javet/shell/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.caoccao.javet.interop.V8Host
Expand All @@ -57,20 +59,20 @@ import com.caoccao.javet.utils.JavetOSUtils
import com.caoccao.javet.values.V8Value

class MainActivity : ComponentActivity() {
private lateinit var v8Runtime: V8Runtime
private var v8Runtime: V8Runtime? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
v8Runtime = V8Host.getV8Instance().createV8Runtime()
val now = v8Runtime.getExecutor("new Date()").executeZonedDateTime()
val now = v8Runtime?.getExecutor("new Date()")?.executeZonedDateTime()
val stringBuilder = StringBuilder().append(
"""Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
|
| Javet v${JavetLibLoader.LIB_VERSION} supports Android (arm, arm64, x86 and x86_64) ABI >= 24.
|
| OS Name = ${JavetOSUtils.OS_NAME}
| OS Arch = ${JavetOSUtils.OS_ARCH}
| V8 = ${v8Runtime.version}
| V8 = ${v8Runtime?.version}
| Now = $now
|
""".trimMargin("| ")
Expand All @@ -82,23 +84,24 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
HomeScreen(v8Runtime, stringBuilder)
HomeScreen(v8Runtime = v8Runtime, stringBuilder = stringBuilder)
}
}
}
}

override fun onDestroy() {
v8Runtime.close()
v8Runtime?.close()
v8Runtime = null
super.onDestroy()
}
}

@Composable
fun HomeScreen(
v8Runtime: V8Runtime,
stringBuilder: java.lang.StringBuilder,
modifier: Modifier = Modifier
modifier: Modifier = Modifier,
v8Runtime: V8Runtime? = null,
stringBuilder: StringBuilder = StringBuilder()
) {
Scaffold(
topBar = {
Expand All @@ -108,7 +111,7 @@ fun HomeScreen(
titleContentColor = MaterialTheme.colorScheme.primary,
),
title = {
Text("Javet Shell")
Text(text = stringResource(id = R.string.app_name))
}
)
},
Expand All @@ -117,6 +120,24 @@ fun HomeScreen(
val scrollState = rememberScrollState(0)
var resultString by remember { mutableStateOf(stringBuilder.toString()) }
var codeString by remember { mutableStateOf("") }
val executeFunction = {
val trimmedCodeString = codeString.trim()
if (!trimmedCodeString.isNullOrBlank()) {
try {
v8Runtime!!
.getExecutor(codeString)
.execute<V8Value>()
.use { v8Value ->
stringBuilder.append("\n> $trimmedCodeString\n$v8Value")
}
codeString = ""
} catch (t: Throwable) {
stringBuilder.append("\n> $trimmedCodeString\n${t.message}")
} finally {
resultString = stringBuilder.toString()
}
}
}
LaunchedEffect(resultString) {
scrollState.scrollTo(scrollState.maxValue)
}
Expand All @@ -136,6 +157,7 @@ fun HomeScreen(
.verticalScroll(scrollState)
.padding(padding)
.fillMaxSize()
.testTag("textResult")
)
}
Column(
Expand All @@ -144,27 +166,14 @@ fun HomeScreen(
.weight(0.3F)
) {
ElevatedButton(
onClick = {
try {
v8Runtime
.getExecutor(codeString)
.execute<V8Value>()
.use { v8Value ->
stringBuilder.append("\n> $codeString\n$v8Value")
}
codeString = ""
} catch (t: Throwable) {
stringBuilder.append("\n> $codeString\n${t.message}")
} finally {
resultString = stringBuilder.toString()
}
},
onClick = executeFunction,
shape = RoundedCornerShape(10),
modifier = modifier
.fillMaxWidth()
.padding(start = padding, end = padding)
.testTag("elevatedButtonExecute")
) {
Text(text = "Execute")
Text(text = stringResource(id = R.string.button_execute))
}
BasicTextField(
value = codeString,
Expand All @@ -173,6 +182,7 @@ fun HomeScreen(
.fillMaxSize()
.padding(padding)
.background(color = Color.LightGray)
.testTag("basicTextFieldCodeString")
)
}
}
Expand All @@ -183,8 +193,6 @@ fun HomeScreen(
@Composable
fun JavetShellPreview() {
JavetShellTheme {
V8Host.getV8Instance().createV8Runtime<V8Runtime>().use { v8Runtime ->
HomeScreen(v8Runtime, StringBuilder())
}
HomeScreen()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.shell.ui.theme

import androidx.compose.ui.graphics.Color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.shell.ui.theme

import android.app.Activity
Expand Down
16 changes: 16 additions & 0 deletions android/app/src/main/java/com/caoccao/javet/shell/ui/theme/Type.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.shell.ui.theme

import androidx.compose.material3.Typography
Expand Down
16 changes: 16 additions & 0 deletions android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. caoccao.com Sam Cao
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
Expand Down
20 changes: 19 additions & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. caoccao.com Sam Cao
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<string name="app_name">JavetShell</string>
<string name="app_name">Javet Shell</string>
<string name="button_execute">Execute</string>
</resources>
16 changes: 16 additions & 0 deletions android/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024. caoccao.com Sam Cao
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>

<style name="Theme.JavetShell" parent="android:Theme.Material.Light.NoActionBar" />
Expand Down

0 comments on commit 5326dcf

Please sign in to comment.