Skip to content

Commit

Permalink
js print test
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Sep 20, 2024
1 parent 89a09e6 commit b901872
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,41 @@ import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
import io.rebble.libpebblecommon.util.runBlocking
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.serialization.json.Json
import org.junit.Before
import org.junit.Test
import java.io.File

class WebViewJsRunnerTest {

private lateinit var context: Context
private val json = Json {ignoreUnknownKeys = true}
private val coroutineScope = CoroutineScope(Dispatchers.Default)
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
}

// Copies from the android_assets of the test to the sdcard so the WebView can access it
private fun assetsToSdcard(file: String): String {
val sdcardPath = context.getExternalFilesDir(null)!!.absolutePath
val testPath = "$sdcardPath/test"
File(testPath).mkdir()
val testFile = "$testPath/$file"
val assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets()
val inputStream = assetManager.open(file)
val outputStream = File(testFile).outputStream()
inputStream.copyTo(outputStream)
inputStream.close()
outputStream.close()
return testFile
}

@Test
fun test() = runBlocking {
val appInfo: PbwAppInfo = json.decodeFromString(
Expand Down Expand Up @@ -54,9 +76,10 @@ class WebViewJsRunnerTest {
}
""".trimIndent()
)
val printTestPath = "file:///android_asset/print_test.js"
val webViewJsRunner = WebViewJsRunner(context, appInfo, printTestPath)
val printTestPath = assetsToSdcard("print_test.js")
val webViewJsRunner = WebViewJsRunner(context, MutableStateFlow("dummy"), coroutineScope, appInfo, printTestPath)
webViewJsRunner.start()
delay(5000)
delay(1000)
webViewJsRunner.stop()
}
}

0 comments on commit b901872

Please sign in to comment.