package com.mogo.functions.test import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.LargeTest import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsManager import com.mogo.eagle.core.function.hmi.ui.MoGoHmiProvider import com.mogo.eagle.core.function.main.MainLauncherActivity import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import record_cache.RecordPanelOuterClass import java.text.SimpleDateFormat import java.util.* import java.util.concurrent.Executors import java.util.concurrent.TimeUnit import kotlin.random.Random @RunWith(AndroidJUnit4::class) @LargeTest class AutoPilotBadCaseTest { private lateinit var launch: ActivityScenario @Before fun launch() { launch = ActivityScenario.launch(MainLauncherActivity::class.java) } @ExperimentalCoroutinesApi @Test fun showBadCaseEntrance1(): Unit = runBlocking(Dispatchers.Main) { ensureMoGoHmiFragmentShow() delay(TimeUnit.MILLISECONDS.toSeconds(30)) var index = 0 (1 until 50) .map { it } .asFlow() .onEach { delay(TimeUnit.SECONDS.toMillis(5)) val builder = RecordPanelOuterClass.RecordPanel.newBuilder() builder.also { it.diskFree = (100 + index).toLong() it.duration = 60.0.toFloat() it.filename = "/user/general/record_$index.log" it.id = 10 + index it.key = index.toLong() it.stat = 100 it.type = 1 it.timestamp = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) index++ } // CallerDevaToolsManager.onReceiveBadCaseRecord(builder.build()) } .flowOn(Dispatchers.Default) .collect() delay(TimeUnit.HOURS.toMillis(2)) } @ExperimentalCoroutinesApi @Test fun showBadCaseEntrance2(): Unit = runBlocking(Dispatchers.Main) { ensureMoGoHmiFragmentShow() var index = 0 (1 until 50) .map { it } .asFlow() .onEach { if (index in 1..4) { delay(TimeUnit.SECONDS.toMillis(15)) } else { delay(Random(20).nextLong()) } val builder = RecordPanelOuterClass.RecordPanel.newBuilder() builder.also { it.diskFree = (100 + index).toLong() it.duration = 60.0.toFloat() it.filename = "/user/general/record_$index.log" it.id = 10 + index it.key = index.toLong() it.stat = 100 it.type = 1 it.timestamp = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) index++ } // CallerDevaToolsManager.onReceiveBadCaseRecord(builder.build()) } .flowOn(Dispatchers.Default) .collect() delay(TimeUnit.HOURS.toMillis(2)) } @ExperimentalCoroutinesApi @Test fun showBadCaseEntrance3(): Unit = runBlocking(Dispatchers.Main) { var index = 0 (1 until 50) .map { it } .asFlow() .onEach { delay(TimeUnit.SECONDS.toMillis(20)) val builder = RecordPanelOuterClass.RecordPanel.newBuilder() builder.also { it.diskFree = (100 + index).toLong() it.duration = 60.0.toFloat() it.filename = "/user/general/record_$index.log" it.id = 10 + index it.key = index.toLong() it.stat = 100 it.type = 1 it.timestamp = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) index++ } // CallerDevaToolsManager.onReceiveBadCaseRecord(builder.build()) } .flowOn(Dispatchers.Default) .collect() delay(TimeUnit.HOURS.toMillis(2)) } private suspend fun ensureMoGoHmiFragmentShow(): MoGoHmiProvider = suspendCancellableCoroutine { launch.onActivity { itx -> val executor = Executors.newSingleThreadScheduledExecutor() executor.scheduleAtFixedRate({ var find = itx.supportFragmentManager.fragments.find { it is MoGoHmiProvider } as? MoGoHmiProvider while (find == null) { find = itx.supportFragmentManager.fragments.find { it is MoGoHmiProvider } as? MoGoHmiProvider } while (!find.isResumed) { Thread.sleep(500) } it.resumeWith(Result.success(find)) try { Thread.sleep(500) executor.shutdownNow() } catch (e: Throwable) { e.printStackTrace() } }, 50, 500, TimeUnit.MILLISECONDS) } } }