Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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 
     17 package android.dumpsys.cts;
     18 
     19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
     20 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
     21 import com.android.ddmlib.testrunner.TestResult.TestStatus;
     22 import com.android.tradefed.build.IBuildInfo;
     23 import com.android.tradefed.device.CollectingOutputReceiver;
     24 import com.android.tradefed.device.DeviceNotAvailableException;
     25 import com.android.tradefed.device.ITestDevice;
     26 import com.android.tradefed.log.LogUtil.CLog;
     27 import com.android.tradefed.result.CollectingTestListener;
     28 import com.android.tradefed.result.TestDescription;
     29 import com.android.tradefed.result.TestResult;
     30 import com.android.tradefed.result.TestRunResult;
     31 import com.android.tradefed.testtype.DeviceTestCase;
     32 import com.android.tradefed.testtype.IBuildReceiver;
     33 
     34 import java.io.FileNotFoundException;
     35 import java.util.Map;
     36 import java.util.Set;
     37 import java.util.regex.Matcher;
     38 import java.util.regex.Pattern;
     39 
     40 import javax.annotation.Nonnull;
     41 import javax.annotation.Nullable;
     42 
     43 public class BaseDumpsysTest extends DeviceTestCase implements IBuildReceiver {
     44     protected static final String TAG = "DumpsysHostTest";
     45 
     46     private static final String TEST_RUNNER = "androidx.test.runner.AndroidJUnitRunner";
     47 
     48     /**
     49      * A reference to the device under test.
     50      */
     51     protected ITestDevice mDevice;
     52 
     53     protected IBuildInfo mCtsBuild;
     54 
     55     @Override
     56     protected void setUp() throws Exception {
     57         super.setUp();
     58         mDevice = getDevice();
     59     }
     60 
     61     @Override
     62     public void setBuild(IBuildInfo buildInfo) {
     63         mCtsBuild = buildInfo;
     64     }
     65 
     66     protected static long assertInteger(String input) {
     67         try {
     68             return Long.parseLong(input);
     69         } catch (NumberFormatException e) {
     70             fail("Expected an integer but found \"" + input + "\"");
     71             // Won't be hit, above throws AssertException
     72             return -1;
     73         }
     74     }
     75 
     76     protected static long assertNonNegativeInteger(String input) {
     77         try {
     78             final long result = Long.parseLong(input);
     79             assertTrue("Expected non-negative, but was: " + result, result >= 0);
     80 
     81             return result;
     82         } catch (NumberFormatException e) {
     83             fail("Expected an integer but found \"" + input + "\"");
     84             // Won't be hit, above throws AssertException
     85             return -1;
     86         }
     87     }
     88 
     89     protected static long assertPositiveInteger(String input) {
     90         try {
     91             final long result = Long.parseLong(input);
     92             assertTrue("Expected positive, but was: " + result, result > 0);
     93 
     94             return result;
     95         } catch (NumberFormatException e) {
     96             fail("Expected an integer but found \"" + input + "\"");
     97             // Won't be hit, above throws AssertException
     98             return -1;
     99         }
    100     }
    101 
    102     protected static void assertMinAvgMax(String min, String avg, String max, boolean checkAvg) {
    103         final long lMin = assertNonNegativeInteger(min);
    104         final long lAvg = assertNonNegativeInteger(avg);
    105         final long lMax = assertNonNegativeInteger(max);
    106 
    107         if (checkAvg) {
    108             assertTrue("min [" + min + "] <= avg [" + avg + "]", lMin <= lAvg);
    109             assertTrue("avg [" + avg + "] <= max [" + max + "]", lAvg <= lMax);
    110         } else {
    111             // There was a bug in the average calculation, so we can't check the average
    112             // from the last N hour stats, which may be generated on with the buggy logic.
    113             assertTrue("min [" + min + "] <= max [" + max + "]", lMin <= lMax);
    114         }
    115     }
    116 
    117     protected static void assertLesserOrEqual(String lesser, String greater) {
    118         final long lLesser = assertNonNegativeInteger(lesser);
    119         final long lGreater = assertNonNegativeInteger(greater);
    120 
    121         assertTrue("[" + lesser + "] <= [" + greater + "]", lLesser <= lGreater);
    122     }
    123 
    124     protected static double assertDouble(String input) {
    125         try {
    126             return Double.parseDouble(input);
    127         } catch (NumberFormatException e) {
    128             fail("Expected a double but found \"" + input + "\"");
    129             return -1;
    130         }
    131     }
    132 
    133     protected static void assertSeenTag(Set<String> seenTags, String tag) {
    134         assertTrue("No line starting with \"" + tag + ",\"", seenTags.contains(tag));
    135     }
    136 
    137 
    138     /**
    139      * Install a device side test package.
    140      *
    141      * @param appFileName Apk file name, such as "CtsNetStatsApp.apk".
    142      * @param grantPermissions whether to give runtime permissions.
    143      */
    144     protected void installPackage(String appFileName, boolean grantPermissions)
    145             throws FileNotFoundException, DeviceNotAvailableException {
    146         CLog.d("Installing app " + appFileName);
    147         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
    148         final String result = getDevice().installPackage(
    149                 buildHelper.getTestFile(appFileName), true, grantPermissions);
    150         assertNull("Failed to install " + appFileName + ": " + result, result);
    151     }
    152 
    153     /**
    154      * Run a device side test.
    155      *
    156      * @param pkgName Test package name, such as "com.android.server.cts.netstats".
    157      * @param testClassName Test class name; either a fully qualified name, or "." + a class name.
    158      * @param testMethodName Test method name.
    159      * @throws DeviceNotAvailableException
    160      */
    161     protected void runDeviceTests(@Nonnull String pkgName,
    162             @Nullable String testClassName, @Nullable String testMethodName)
    163             throws DeviceNotAvailableException {
    164         if (testClassName != null && testClassName.startsWith(".")) {
    165             testClassName = pkgName + testClassName;
    166         }
    167 
    168         RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(
    169                 pkgName, TEST_RUNNER, getDevice().getIDevice());
    170         if (testClassName != null && testMethodName != null) {
    171             testRunner.setMethodName(testClassName, testMethodName);
    172         } else if (testClassName != null) {
    173             testRunner.setClassName(testClassName);
    174         }
    175 
    176         CollectingTestListener listener = new CollectingTestListener();
    177         assertTrue(getDevice().runInstrumentationTests(testRunner, listener));
    178 
    179         final TestRunResult result = listener.getCurrentRunResults();
    180         if (result.isRunFailure()) {
    181             throw new AssertionError("Failed to successfully run device tests for "
    182                     + result.getName() + ": " + result.getRunFailureMessage());
    183         }
    184         if (result.getNumTests() == 0) {
    185             throw new AssertionError("No tests were run on the device");
    186         }
    187 
    188         if (result.hasFailedTests()) {
    189             // build a meaningful error message
    190             StringBuilder errorBuilder = new StringBuilder("On-device tests failed:\n");
    191             for (Map.Entry<TestDescription, TestResult> resultEntry :
    192                     result.getTestResults().entrySet()) {
    193                 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) {
    194                     errorBuilder.append(resultEntry.getKey().toString());
    195                     errorBuilder.append(":\n");
    196                     errorBuilder.append(resultEntry.getValue().getStackTrace());
    197                 }
    198             }
    199             throw new AssertionError(errorBuilder.toString());
    200         }
    201     }
    202 
    203     /**
    204      * Execute the given command, and find the given pattern and return the resulting
    205      * {@link Matcher}.
    206      */
    207     protected Matcher execCommandAndFind(String command, String pattern) throws Exception {
    208         final CollectingOutputReceiver receiver = new CollectingOutputReceiver();
    209         getDevice().executeShellCommand(command, receiver);
    210         final String output = receiver.getOutput();
    211         final Matcher matcher = Pattern.compile(pattern).matcher(output);
    212         assertTrue("Pattern '" + pattern + "' didn't match. Output=\n" + output, matcher.find());
    213         return matcher;
    214     }
    215 
    216     /**
    217      * Execute the given command, find the given pattern, and return the first captured group
    218      * as a String.
    219      */
    220     protected String execCommandAndGetFirstGroup(String command, String pattern) throws Exception {
    221         final Matcher matcher = execCommandAndFind(command, pattern);
    222         assertTrue("No group found for pattern '" + pattern + "'", matcher.groupCount() > 0);
    223         return matcher.group(1);
    224     }
    225 }
    226