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