1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.virtualization.terminal 17 18 import android.app.Instrumentation 19 import android.content.Context 20 import android.content.Intent 21 import android.os.Bundle 22 import android.os.SystemProperties 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import androidx.test.platform.app.InstrumentationRegistry 25 import com.android.microdroid.test.common.DeviceProperties 26 import com.android.microdroid.test.common.MetricsProcessor 27 import java.io.IOException 28 import java.lang.Exception 29 import java.util.ArrayList 30 import org.junit.After 31 import org.junit.Assert 32 import org.junit.Before 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 36 @RunWith(AndroidJUnit4::class) 37 class TerminalAppTest { 38 private lateinit var instr: Instrumentation 39 private lateinit var targetContext: Context 40 private lateinit var properties: DeviceProperties 41 private val metricsProc = MetricsProcessor("avf_perf/terminal/") 42 43 @Before setupnull44 fun setup() { 45 instr = InstrumentationRegistry.getInstrumentation() 46 targetContext = instr.targetContext 47 properties = 48 DeviceProperties.create(DeviceProperties.PropertyGetter { SystemProperties.get(it) }) 49 installVmImage() 50 } 51 installVmImagenull52 private fun installVmImage() { 53 val INSTALL_TIMEOUT_MILLIS: Long = 300000 // 5 min 54 55 val intent = Intent(targetContext, InstallerActivity::class.java) 56 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 57 val activity = instr.startActivitySync(intent) 58 if (activity is InstallerActivity) { 59 Assert.assertTrue( 60 "Failed to install VM image", 61 activity.waitForInstallCompleted(INSTALL_TIMEOUT_MILLIS), 62 ) 63 } 64 } 65 66 @Test 67 @Throws(Exception::class) bootnull68 fun boot() { 69 val isNestedVirt = properties.isCuttlefish() || properties.isGoldfish() 70 val BOOT_TIMEOUT_MILLIS = 71 (if (isNestedVirt) 180000 else 30000).toLong() // 30 sec (or 3 min) 72 73 val intent = Intent(targetContext, MainActivity::class.java) 74 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 75 76 val start = System.currentTimeMillis() 77 val activity = instr.startActivitySync(intent) 78 if (activity is MainActivity) { 79 Assert.assertTrue( 80 "Failed to boot in 30s", 81 activity.waitForBootCompleted(BOOT_TIMEOUT_MILLIS), 82 ) 83 } 84 val delay = System.currentTimeMillis() - start 85 86 // TODO: measure multiple times? 87 val measurements: MutableList<Long?> = ArrayList<Long?>() 88 measurements.add(delay) 89 val stats = metricsProc.computeStats(measurements, "boot", "ms") 90 val bundle = Bundle() 91 for (entry in stats.entries) { 92 bundle.putDouble(entry.key, entry.value) 93 } 94 instr.sendStatus(0, bundle) 95 } 96 97 @After 98 @Throws(IOException::class) tearDownnull99 fun tearDown() { 100 PortsStateManager.getInstance(targetContext).clearEnabledPorts() 101 InstalledImage.getDefault(targetContext).uninstallFully() 102 } 103 } 104