Move isCuttlefish() to helper classes

We skip some tests when running on Cuttlefish, typically because nested
virtualization is too slow and the tests time out, but logic has been
duplicated across multiple test files. Clean things up by adding a new
common host/device helper VirtualizationTestHelper and make both the
MicrodroidHostTestCaseBase and MicrodroidDeviceTestBase share the same
logic.

Test: m MicrodroidTestApp ComposBenchmarkApp MicrodroidHostTestCases
Change-Id: Ibb9d25fc40818ffeb971d366dea409f8ee55ef48
diff --git a/tests/benchmark/src/java/com/android/microdroid/benchmark/MicrodroidBenchmarks.java b/tests/benchmark/src/java/com/android/microdroid/benchmark/MicrodroidBenchmarks.java
index dc085a4..3731e13 100644
--- a/tests/benchmark/src/java/com/android/microdroid/benchmark/MicrodroidBenchmarks.java
+++ b/tests/benchmark/src/java/com/android/microdroid/benchmark/MicrodroidBenchmarks.java
@@ -22,7 +22,6 @@
 
 import android.app.Instrumentation;
 import android.os.Bundle;
-import android.os.SystemProperties;
 import android.system.virtualmachine.VirtualMachineConfig;
 import android.system.virtualmachine.VirtualMachineConfig.DebugLevel;
 import android.system.virtualmachine.VirtualMachineException;
@@ -46,22 +45,11 @@
 
     @Rule public Timeout globalTimeout = Timeout.seconds(300);
 
-    private static final String KERNEL_VERSION = SystemProperties.get("ro.kernel.version");
-
     private static final String APEX_ETC_FS = "/apex/com.android.virt/etc/fs/";
     private static final double SIZE_MB = 1024.0 * 1024.0;
     private static final String MICRODROID_IMG_PREFIX = "microdroid_";
     private static final String MICRODROID_IMG_SUFFIX = ".img";
 
-    private boolean isCuttlefish() {
-        String productName = SystemProperties.get("ro.product.name");
-        return (null != productName)
-                && (productName.startsWith("aosp_cf_x86")
-                        || productName.startsWith("aosp_cf_arm")
-                        || productName.startsWith("cf_x86")
-                        || productName.startsWith("cf_arm"));
-    }
-
     @Parameterized.Parameters(name = "protectedVm={0}")
     public static Object[] protectedVmConfigs() {
         return new Object[] {false, true};
diff --git a/tests/helper/Android.bp b/tests/helper/Android.bp
index 679fbfe..e7760e2 100644
--- a/tests/helper/Android.bp
+++ b/tests/helper/Android.bp
@@ -3,11 +3,18 @@
 }
 
 java_library_static {
+    name: "VirtualizationTestHelper",
+    srcs: ["src/java/com/android/virt/**/*.java"],
+    host_supported: true,
+}
+
+java_library_static {
     name: "MicroroidDeviceTestHelper",
-    srcs: ["src/java/**/*.java"],
+    srcs: ["src/java/com/android/microdroid/**/*.java"],
     static_libs: [
         "androidx.test.runner",
         "androidx.test.ext.junit",
+        "VirtualizationTestHelper",
         "truth-prebuilt",
     ],
     libs: ["android.system.virtualmachine"],
diff --git a/tests/helper/src/java/com/android/microdroid/test/MicrodroidDeviceTestBase.java b/tests/helper/src/java/com/android/microdroid/test/MicrodroidDeviceTestBase.java
index 4d38f1f..a2c43d7 100644
--- a/tests/helper/src/java/com/android/microdroid/test/MicrodroidDeviceTestBase.java
+++ b/tests/helper/src/java/com/android/microdroid/test/MicrodroidDeviceTestBase.java
@@ -21,6 +21,7 @@
 
 import android.content.Context;
 import android.os.ParcelFileDescriptor;
+import android.os.SystemProperties;
 import android.sysprop.HypervisorProperties;
 import android.system.virtualizationservice.DeathReason;
 import android.system.virtualmachine.VirtualMachine;
@@ -33,6 +34,8 @@
 import androidx.annotation.CallSuper;
 import androidx.test.core.app.ApplicationProvider;
 
+import com.android.virt.VirtualizationTestHelper;
+
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -60,6 +63,10 @@
                 }).start();
     }
 
+    public static boolean isCuttlefish() {
+        return VirtualizationTestHelper.isCuttlefish(SystemProperties.get("ro.product.name"));
+    }
+
     // TODO(b/220920264): remove Inner class; this is a hack to hide virt APEX types
     protected static class Inner {
         private final boolean mProtectedVm;
diff --git a/tests/helper/src/java/com/android/virt/VirtualizationTestHelper.java b/tests/helper/src/java/com/android/virt/VirtualizationTestHelper.java
new file mode 100644
index 0000000..c6c0ad0
--- /dev/null
+++ b/tests/helper/src/java/com/android/virt/VirtualizationTestHelper.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * 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.android.virt;
+
+public abstract class VirtualizationTestHelper {
+    public static boolean isCuttlefish(String productName) {
+        return (null != productName)
+                && (productName.startsWith("aosp_cf_x86")
+                        || productName.startsWith("aosp_cf_arm")
+                        || productName.startsWith("cf_x86")
+                        || productName.startsWith("cf_arm"));
+    }
+}
diff --git a/tests/hostside/helper/Android.bp b/tests/hostside/helper/Android.bp
index f7caca2..af88bb6 100644
--- a/tests/hostside/helper/Android.bp
+++ b/tests/hostside/helper/Android.bp
@@ -10,4 +10,7 @@
         "tradefed",
         "truth-prebuilt",
     ],
+    static_libs: [
+        "VirtualizationTestHelper",
+    ],
 }
diff --git a/tests/hostside/helper/java/com/android/microdroid/test/MicrodroidHostTestCaseBase.java b/tests/hostside/helper/java/com/android/microdroid/test/MicrodroidHostTestCaseBase.java
index 635d54e..0712323 100644
--- a/tests/hostside/helper/java/com/android/microdroid/test/MicrodroidHostTestCaseBase.java
+++ b/tests/hostside/helper/java/com/android/microdroid/test/MicrodroidHostTestCaseBase.java
@@ -34,6 +34,7 @@
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.util.CommandResult;
 import com.android.tradefed.util.RunUtil;
+import com.android.virt.VirtualizationTestHelper;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -91,6 +92,10 @@
         android.tryRun("rm", "-rf", "/data/misc/virtualizationservice/*");
     }
 
+    protected boolean isCuttlefish() throws Exception {
+        return VirtualizationTestHelper.isCuttlefish(getDevice().getProperty("ro.product.name"));
+    }
+
     public static void testIfDeviceIsCapable(ITestDevice androidDevice) throws Exception {
         assumeTrue("Need an actual TestDevice", androidDevice instanceof TestDevice);
         TestDevice testDevice = (TestDevice) androidDevice;
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
index f3062c6..78fb53b 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
@@ -77,16 +77,6 @@
     @Rule public TestLogData mTestLogs = new TestLogData();
     @Rule public TestName mTestName = new TestName();
 
-    // TODO(b/176805428): remove this
-    private boolean isCuttlefish() throws Exception {
-        String productName = getDevice().getProperty("ro.product.name");
-        return (null != productName)
-                && (productName.startsWith("aosp_cf_x86")
-                        || productName.startsWith("aosp_cf_arm")
-                        || productName.startsWith("cf_x86")
-                        || productName.startsWith("cf_arm"));
-    }
-
     private int minMemorySize() throws DeviceNotAvailableException {
         CommandRunner android = new CommandRunner(getDevice());
         String abi = android.run("getprop", "ro.product.cpu.abi");