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.tradefed.device.recovery;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import com.android.ddmlib.IDevice;
21 import com.android.tradefed.command.ICommandScheduler;
22 import com.android.tradefed.config.OptionSetter;
23 import com.android.tradefed.device.DeviceAllocationState;
24 import com.android.tradefed.device.DeviceManager.FastbootDevice;
25 import com.android.tradefed.device.IDeviceManager;
26 import com.android.tradefed.device.IManagedTestDevice;
27 import com.android.tradefed.device.ITestDevice;
28 
29 import org.easymock.Capture;
30 import org.easymock.EasyMock;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.junit.runners.JUnit4;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 /** Unit tests for {@link RunConfigDeviceRecovery}. */
40 @RunWith(JUnit4.class)
41 public class RunConfigDeviceRecoveryTest {
42 
43     private RunConfigDeviceRecovery mRecoverer;
44     private IManagedTestDevice mMockTestDevice;
45     private IDevice mMockIDevice;
46     private IDeviceManager mMockDeviceManager;
47     private ICommandScheduler mMockScheduler;
48 
49     @Before
setup()50     public void setup() throws Exception {
51         mMockTestDevice = EasyMock.createMock(IManagedTestDevice.class);
52         mMockIDevice = EasyMock.createMock(IDevice.class);
53         mMockDeviceManager = EasyMock.createMock(IDeviceManager.class);
54         mMockScheduler = EasyMock.createMock(ICommandScheduler.class);
55         EasyMock.expect(mMockTestDevice.getSerialNumber()).andStubReturn("serial");
56         EasyMock.expect(mMockTestDevice.getIDevice()).andStubReturn(mMockIDevice);
57         mRecoverer =
58                 new RunConfigDeviceRecovery() {
59                     @Override
60                     protected IDeviceManager getDeviceManager() {
61                         return mMockDeviceManager;
62                     }
63 
64                     @Override
65                     protected ICommandScheduler getCommandScheduler() {
66                         return mMockScheduler;
67                     }
68                 };
69         OptionSetter setter = new OptionSetter(mRecoverer);
70         setter.setOptionValue("recovery-config-name", "empty");
71     }
72 
73     @Test
testRecoverDevice_allocated()74     public void testRecoverDevice_allocated() {
75         List<IManagedTestDevice> devices = new ArrayList<>();
76         devices.add(mMockTestDevice);
77         EasyMock.expect(mMockTestDevice.getAllocationState())
78                 .andReturn(DeviceAllocationState.Allocated);
79         replay();
80         mRecoverer.recoverDevices(devices);
81         verify();
82     }
83 
84     @Test
testRecoverDevice_offline()85     public void testRecoverDevice_offline() throws Exception {
86         List<IManagedTestDevice> devices = new ArrayList<>();
87         devices.add(mMockTestDevice);
88         EasyMock.expect(mMockTestDevice.getAllocationState())
89                 .andReturn(DeviceAllocationState.Available);
90         ITestDevice device = EasyMock.createMock(ITestDevice.class);
91         EasyMock.expect(mMockDeviceManager.forceAllocateDevice("serial")).andReturn(device);
92 
93         mMockScheduler.execCommand(EasyMock.anyObject(), EasyMock.eq(device), EasyMock.anyObject());
94 
95         replay();
96         mRecoverer.recoverDevices(devices);
97         verify();
98     }
99 
100     /** Test that FastbootDevice are considered for recovery. */
101     @Test
testRecoverDevice_fastboot()102     public void testRecoverDevice_fastboot() throws Exception {
103         EasyMock.reset(mMockTestDevice);
104         EasyMock.expect(mMockTestDevice.getIDevice()).andStubReturn(new FastbootDevice("serial"));
105         EasyMock.expect(mMockTestDevice.getSerialNumber()).andStubReturn("serial");
106         List<IManagedTestDevice> devices = new ArrayList<>();
107         devices.add(mMockTestDevice);
108         EasyMock.expect(mMockTestDevice.getAllocationState())
109                 .andReturn(DeviceAllocationState.Available);
110         ITestDevice device = EasyMock.createMock(ITestDevice.class);
111         EasyMock.expect(mMockDeviceManager.forceAllocateDevice("serial")).andReturn(device);
112 
113         mMockScheduler.execCommand(EasyMock.anyObject(), EasyMock.eq(device), EasyMock.anyObject());
114 
115         replay();
116         mRecoverer.recoverDevices(devices);
117         verify();
118     }
119 
120     @Test
testRecoverDevice_run()121     public void testRecoverDevice_run() throws Exception {
122         List<IManagedTestDevice> devices = new ArrayList<>();
123         devices.add(mMockTestDevice);
124         EasyMock.expect(mMockTestDevice.getAllocationState())
125                 .andReturn(DeviceAllocationState.Available);
126 
127         ITestDevice device = EasyMock.createMock(ITestDevice.class);
128         EasyMock.expect(mMockDeviceManager.forceAllocateDevice("serial")).andReturn(device);
129 
130         Capture<String[]> captured = new Capture<>();
131         mMockScheduler.execCommand(
132                 EasyMock.anyObject(), EasyMock.eq(device), EasyMock.capture(captured));
133 
134         replay(device);
135         mRecoverer.recoverDevices(devices);
136         verify(device);
137 
138         String[] args = captured.getValue();
139         assertEquals("empty", args[0]);
140     }
141 
replay(Object... mocks)142     private void replay(Object... mocks) {
143         EasyMock.replay(mMockTestDevice, mMockIDevice, mMockDeviceManager, mMockScheduler);
144         EasyMock.replay(mocks);
145     }
146 
verify(Object... mocks)147     private void verify(Object... mocks) {
148         EasyMock.verify(mMockTestDevice, mMockIDevice, mMockDeviceManager, mMockScheduler);
149         EasyMock.verify(mocks);
150     }
151 }
152