1 /* 2 * Copyright (C) 2019 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.collectors; 17 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertNull; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 24 import com.android.tradefed.config.OptionSetter; 25 import com.android.tradefed.device.metric.DeviceMetricData; 26 import com.android.tradefed.device.metric.FilePullerDeviceMetricCollector; 27 import com.android.tradefed.invoker.IInvocationContext; 28 import com.android.tradefed.result.CollectingTestListener; 29 import com.android.tradefed.result.TestRunResult; 30 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 31 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 32 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 import java.io.File; 39 import java.io.FileInputStream; 40 import java.io.IOException; 41 import java.net.URLConnection; 42 import java.util.Arrays; 43 44 /** 45 * Host side tests for the logcat collector, this ensure that we are able to use the collector in a 46 * similar way as the infra. 47 * 48 * <p>Command: mm CollectorHostsideLibTest CollectorDeviceLibTest -j16 49 * 50 * <p>tradefed.sh run commandAndExit template/local_min --template:map test=CollectorHostsideLibTest 51 */ 52 @RunWith(DeviceJUnit4ClassRunner.class) 53 public class LogcatOnFailureCollectorHostTest extends BaseHostJUnit4Test { 54 private static final String TEST_APK = "CollectorDeviceLibTest.apk"; 55 private static final String PACKAGE_NAME = "android.device.collectors"; 56 private static final String AJUR_RUNNER = "androidx.test.runner.AndroidJUnitRunner"; 57 58 private static final String LOGCAT_COLLECTOR = 59 "android.device.collectors.LogcatOnFailureCollector"; 60 61 private IInvocationContext mContext; 62 private DeviceTestRunOptions mOptions = null; 63 64 @Before setUp()65 public void setUp() throws Exception { 66 installPackage(TEST_APK); 67 assertTrue(isPackageInstalled(PACKAGE_NAME)); 68 mOptions = new DeviceTestRunOptions(PACKAGE_NAME); 69 mOptions.setRunner(AJUR_RUNNER); 70 mOptions.addInstrumentationArg("listener", LOGCAT_COLLECTOR); 71 mOptions.setTestClassName("android.device.tests.TestEvents"); 72 mOptions.setDisableIsolatedStorage(true); 73 74 mContext = mock(IInvocationContext.class); 75 doReturn(Arrays.asList(getDevice())).when(mContext).getDevices(); 76 doReturn(Arrays.asList(getBuild())).when(mContext).getBuildInfos(); 77 } 78 79 @Test testCollect()80 public void testCollect() throws Exception { 81 TestFilePullerDeviceMetricCollector collector = new TestFilePullerDeviceMetricCollector(); 82 OptionSetter optionSetter = new OptionSetter(collector); 83 String pattern = String.format("%s_.*", LOGCAT_COLLECTOR); 84 optionSetter.setOptionValue("pull-pattern-keys", pattern); 85 collector.init(mContext, new CollectingTestListener()); 86 mOptions.addExtraListener(collector); 87 mOptions.setCheckResults(false); 88 runDeviceTests(mOptions); 89 TestRunResult results = getLastDeviceRunResults(); 90 assertFalse(results.isRunFailure()); 91 // Ensure we actually collected something 92 assertTrue(collector.mCollectedOnce); 93 } 94 95 private class TestFilePullerDeviceMetricCollector extends FilePullerDeviceMetricCollector { 96 97 public boolean mCollectedOnce = false; 98 99 @Override processMetricFile(String key, File metricFile, DeviceMetricData runData)100 public void processMetricFile(String key, File metricFile, DeviceMetricData runData) { 101 try { 102 assertTrue(metricFile.getName().endsWith(".txt")); 103 String mime = 104 URLConnection.guessContentTypeFromStream(new FileInputStream(metricFile)); 105 assertNull(mime); // guessContentTypeFromStream returns null for text/plain 106 } catch (IOException e) { 107 throw new RuntimeException(e); 108 } finally { 109 assertTrue(metricFile.delete()); 110 } 111 mCollectedOnce = true; 112 } 113 114 @Override processMetricDirectory( String key, File metricDirectory, DeviceMetricData runData)115 public void processMetricDirectory( 116 String key, File metricDirectory, DeviceMetricData runData) {} 117 } 118 } 119