1 /*
2  * Copyright (C) 2016 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.tradefed.testtype.suite;
17 
18 import com.android.tradefed.device.DeviceNotAvailableException;
19 import com.android.tradefed.device.ITestDevice;
20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
21 import com.android.tradefed.result.ByteArrayInputStreamSource;
22 import com.android.tradefed.result.ITestInvocationListener;
23 import com.android.tradefed.result.InputStreamSource;
24 import com.android.tradefed.result.LogDataType;
25 import com.android.tradefed.result.TestDescription;
26 import com.android.tradefed.util.IRunUtil;
27 
28 import org.easymock.EasyMock;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 
38 /** Unit tests for {@link com.android.tradefed.testtype.suite.TestFailureListener} */
39 @RunWith(JUnit4.class)
40 public class TestFailureListenerTest {
41 
42     private TestFailureListener mFailureListener;
43     private ITestInvocationListener mMockListener;
44     private ITestDevice mMockDevice;
45     private List<ITestDevice> mListDevice;
46 
47     @Before
setUp()48     public void setUp() {
49         mMockListener = EasyMock.createMock(ITestInvocationListener.class);
50         mMockDevice = EasyMock.createStrictMock(ITestDevice.class);
51         mListDevice = new ArrayList<>();
52         mListDevice.add(mMockDevice);
53         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("SERIAL");
54         // Create base failure listener with all option ON and default logcat size.
55         mFailureListener = new TestFailureListener(mListDevice, true, true, true, true, -1);
56         mFailureListener.setLogger(mMockListener);
57     }
58 
59     /** Test that on testFailed all the collection are triggered. */
60     @Test
61     @SuppressWarnings("MustBeClosedChecker")
testTestFailed()62     public void testTestFailed() throws Exception {
63         TestDescription testId = new TestDescription("com.fake", "methodfake");
64         final String trace = "oups it failed";
65         final long startDate = 1479917040l; // Wed Nov 23 16:04:00 GMT 2016
66         final byte[] fakeData = "fakeData".getBytes();
67         InputStreamSource fakeSource = new ByteArrayInputStreamSource(fakeData);
68         EasyMock.expect(mMockDevice.getDeviceDate()).andReturn(startDate);
69         // Screenshot routine
70         EasyMock.expect(mMockDevice.getScreenshot()).andReturn(fakeSource);
71         mMockListener.testLog(
72                 EasyMock.eq(testId.toString() + "-SERIAL-screenshot"),
73                 EasyMock.eq(LogDataType.PNG),
74                 EasyMock.eq(fakeSource));
75         // Bugreport routine - testLog is internal to it.
76         EasyMock.expect(mMockDevice.logBugreport(EasyMock.anyObject(), EasyMock.anyObject()))
77                 .andReturn(true);
78         // logcat routine
79         EasyMock.expect(mMockDevice.getLogcatSince(EasyMock.eq(startDate))).andReturn(fakeSource);
80         mMockListener.testLog(
81                 EasyMock.eq(testId.toString() + "-SERIAL-logcat"),
82                 EasyMock.eq(LogDataType.LOGCAT),
83                 EasyMock.eq(fakeSource));
84         // Reboot routine
85         EasyMock.expect(mMockDevice.getProperty(EasyMock.eq("ro.build.type")))
86                 .andReturn("userdebug");
87         mMockDevice.reboot();
88         EasyMock.replay(mMockListener, mMockDevice);
89         mFailureListener.testStarted(testId);
90         mFailureListener.testFailed(testId, trace);
91         mFailureListener.testEnded(testId, new HashMap<String, Metric>());
92         EasyMock.verify(mMockListener, mMockDevice);
93     }
94 
95     /**
96      * Test that testFailed behave properly when device is unavailable: collection is attempted and
97      * properly handled.
98      */
99     @Test
100     @SuppressWarnings("MustBeClosedChecker")
testTestFailed_notAvailable()101     public void testTestFailed_notAvailable() throws Exception {
102         mFailureListener =
103                 new TestFailureListener(mListDevice, false, true, true, true, -1) {
104                     @Override
105                     IRunUtil getRunUtil() {
106                         return EasyMock.createMock(IRunUtil.class);
107                     }
108                 };
109         mFailureListener.setLogger(mMockListener);
110         TestDescription testId = new TestDescription("com.fake", "methodfake");
111         final String trace = "oups it failed";
112         final byte[] fakeData = "fakeData".getBytes();
113         InputStreamSource fakeSource = new ByteArrayInputStreamSource(fakeData);
114         DeviceNotAvailableException dnae = new DeviceNotAvailableException();
115         EasyMock.expect(mMockDevice.getDeviceDate()).andThrow(dnae);
116         // Screenshot routine
117         EasyMock.expect(mMockDevice.getScreenshot()).andThrow(dnae);
118         // logcat routine
119         EasyMock.expect(mMockDevice.getLogcat(EasyMock.anyInt())).andReturn(fakeSource);
120         mMockListener.testLog(
121                 EasyMock.eq(testId.toString() + "-SERIAL-logcat"),
122                 EasyMock.eq(LogDataType.LOGCAT),
123                 EasyMock.eq(fakeSource));
124         // Reboot routine
125         EasyMock.expect(mMockDevice.getProperty(EasyMock.eq("ro.build.type")))
126                 .andReturn("userdebug");
127         mMockDevice.reboot();
128         EasyMock.expectLastCall().andThrow(dnae);
129         EasyMock.replay(mMockListener, mMockDevice);
130         mFailureListener.testStarted(testId);
131         mFailureListener.testFailed(testId, trace);
132         mFailureListener.testEnded(testId, new HashMap<String, Metric>());
133         EasyMock.verify(mMockListener, mMockDevice);
134     }
135 
136     /**
137      * Test when a test failure occurs and it is a user build, no reboot is attempted.
138      */
139     @Test
testTestFailed_userBuild()140     public void testTestFailed_userBuild() throws Exception {
141         mFailureListener = new TestFailureListener(mListDevice, false, false, false, true, -1);
142         mFailureListener.setLogger(mMockListener);
143         final String trace = "oups it failed";
144         TestDescription testId = new TestDescription("com.fake", "methodfake");
145         EasyMock.expect(mMockDevice.getProperty(EasyMock.eq("ro.build.type"))).andReturn("user");
146         EasyMock.replay(mMockListener, mMockDevice);
147         mFailureListener.testStarted(testId);
148         mFailureListener.testFailed(testId, trace);
149         mFailureListener.testEnded(testId, new HashMap<String, Metric>());
150         EasyMock.verify(mMockListener, mMockDevice);
151     }
152 
153     /**
154      * Test when a test failure occurs during a multi device run. Each device should capture the
155      * logs.
156      */
157     @Test
testFailed_multiDevice()158     public void testFailed_multiDevice() throws Exception {
159         ITestDevice device2 = EasyMock.createMock(ITestDevice.class);
160         mListDevice.add(device2);
161         mFailureListener = new TestFailureListener(mListDevice, false, false, false, true, -1);
162         mFailureListener.setLogger(mMockListener);
163         final String trace = "oups it failed";
164         TestDescription testId = new TestDescription("com.fake", "methodfake");
165         EasyMock.expect(mMockDevice.getProperty(EasyMock.eq("ro.build.type"))).andReturn("debug");
166         mMockDevice.reboot();
167         EasyMock.expect(device2.getSerialNumber()).andStubReturn("SERIAL2");
168         EasyMock.expect(device2.getProperty(EasyMock.eq("ro.build.type"))).andReturn("debug");
169         device2.reboot();
170 
171         EasyMock.replay(mMockListener, mMockDevice, device2);
172         mFailureListener.testStarted(testId);
173         mFailureListener.testFailed(testId, trace);
174         mFailureListener.testEnded(testId, new HashMap<String, Metric>());
175         EasyMock.verify(mMockListener, mMockDevice, device2);
176     }
177 }
178