1 /* 2 * Copyright (C) 2018 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.assertEquals; 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.mock; 22 23 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner; 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.testtype.DeviceJUnit4ClassRunner; 30 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.io.File; 37 import java.io.IOException; 38 import java.nio.file.Files; 39 import java.util.Arrays; 40 41 /** 42 * Host side tests for the screenshot collector, this ensure that we are able to use the collectors 43 * in a similar way as the infra. 44 * 45 * Command: 46 * mm CollectorHostsideLibTest CollectorDeviceLibTest -j16 47 * tradefed.sh run commandAndExit template/local_min --template:map test=CollectorHostsideLibTest 48 */ 49 @RunWith(DeviceJUnit4ClassRunner.class) 50 public class ScreenshotCollectorHostTest extends BaseHostJUnit4Test { 51 private static final String TEST_APK = "CollectorDeviceLibTest.apk"; 52 private static final String PACKAGE_NAME = "android.device.collectors"; 53 private static final String AJUR_RUNNER = "android.support.test.runner.AndroidJUnitRunner"; 54 55 private static final String SCREENSHOT_COLLECTOR = 56 "android.device.collectors.ScreenshotListener"; 57 58 private RemoteAndroidTestRunner mTestRunner; 59 private IInvocationContext mContext; 60 61 @Before 62 public void setUp() throws Exception { 63 installPackage(TEST_APK); 64 assertTrue(isPackageInstalled(PACKAGE_NAME)); 65 mTestRunner = 66 new RemoteAndroidTestRunner(PACKAGE_NAME, AJUR_RUNNER, getDevice().getIDevice()); 67 mContext = mock(IInvocationContext.class); 68 doReturn(Arrays.asList(getDevice())).when(mContext).getDevices(); 69 doReturn(Arrays.asList(getBuild())).when(mContext).getBuildInfos(); 70 } 71 72 /** 73 * Test that ScreenshotListener collects screenshot and records to a file per test. 74 */ 75 @Test 76 public void testScreenshotListener() throws Exception { 77 mTestRunner.addInstrumentationArg("listener", SCREENSHOT_COLLECTOR); 78 mTestRunner.addInstrumentationArg("screenshot-format", "file:screenshot-log"); 79 80 CollectingTestListener listener = new CollectingTestListener(); 81 FilePullerDeviceMetricCollector collector = new FilePullerDeviceMetricCollector() { 82 @Override 83 public void processMetricFile(String key, File metricFile, DeviceMetricData runData) { 84 try { 85 assertTrue(metricFile.getName().contains("png")); 86 assertTrue(metricFile.length() > 0); 87 assertEquals("image/png", Files.probeContentType(metricFile.toPath())); 88 } catch (IOException e) { 89 throw new RuntimeException(e); 90 } finally { 91 assertTrue(metricFile.delete()); 92 } 93 } 94 @Override 95 public void processMetricDirectory(String key, File metricDirectory, 96 DeviceMetricData runData) { 97 } 98 }; 99 OptionSetter optionSetter = new OptionSetter(collector); 100 String pattern = String.format("%s_.*", SCREENSHOT_COLLECTOR); 101 optionSetter.setOptionValue("pull-pattern-keys", pattern); 102 collector.init(mContext, listener); 103 assertTrue(getDevice().runInstrumentationTests(mTestRunner, collector)); 104 } 105 } 106