1 /* 2 * Copyright (C) 2013 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 17 package com.android.tradefed.device; 18 19 import com.android.ddmlib.IShellOutputReceiver; 20 21 import org.easymock.EasyMock; 22 import org.easymock.IAnswer; 23 24 import java.util.concurrent.TimeUnit; 25 26 class MockTestDeviceHelper { 27 28 /** 29 * Helper method to build a response to a 30 * {@link ITestDevice#executeShellCommand(String, IShellOutputReceiver)} call. 31 * 32 * @param mockDevice the EasyMock created {@link ITestDevice} 33 * @param expectedCommand the shell command to expect or null to skip verification of command 34 * @param response the response to simulate 35 * @param asStub whether to set a single expectation or a stub expectation. A 'stub' expectation 36 * will return the same result for multiple calls to the method 37 */ 38 @SuppressWarnings("unchecked") 39 static void injectShellResponse(ITestDevice mockDevice, final String expectedCommand, 40 final String response, boolean asStub) throws Exception { 41 IAnswer<Object> shellAnswer = new IAnswer<Object>() { 42 @Override 43 public Object answer() throws Throwable { 44 IShellOutputReceiver receiver = (IShellOutputReceiver)EasyMock 45 .getCurrentArguments()[1]; 46 byte[] inputData = response.getBytes(); 47 receiver.addOutput(inputData, 0, inputData.length); 48 receiver.flush(); 49 return null; 50 } 51 }; 52 if (expectedCommand != null) { 53 mockDevice.executeShellCommand(EasyMock.eq(expectedCommand), 54 EasyMock.<IShellOutputReceiver>anyObject()); 55 } else { 56 mockDevice.executeShellCommand(EasyMock.<String>anyObject(), 57 EasyMock.<IShellOutputReceiver>anyObject()); 58 59 } 60 if (asStub) { 61 EasyMock.expectLastCall().andStubAnswer(shellAnswer); 62 } else { 63 EasyMock.expectLastCall().andAnswer(shellAnswer); 64 } 65 } 66 67 /** 68 * Helper method to build a response to a 69 * {@link ITestDevice#executeShellCommand(String, IShellOutputReceiver, long, TimeUnit, int)} 70 * call. 71 * 72 * @param mockDevice the EasyMock created {@link ITestDevice} 73 * @param expectedCommand the shell command to expect or null to skip verification of command 74 * @param expectedTimeout the shell timeout to expect 75 * @param expectedTimeUnit the shell timeout unit to expect 76 * @param expectedRetryAttempts the retry attempts to expect 77 * @param response the response to simulate 78 * @param asStub whether to set a single expectation or a stub expectation. A 'stub' expectation 79 * will return the same result for multiple calls to the method 80 */ 81 @SuppressWarnings("unchecked") 82 static void injectShellResponse(ITestDevice mockDevice, final String expectedCommand, 83 final long expectedTimeout, final TimeUnit expectedTimeUnit, 84 final int expectedRetryAttempts, final String response, boolean asStub) 85 throws Exception { 86 IAnswer<Object> shellAnswer = new IAnswer<Object>() { 87 @Override 88 public Object answer() throws Throwable { 89 IShellOutputReceiver receiver = (IShellOutputReceiver)EasyMock 90 .getCurrentArguments()[1]; 91 byte[] inputData = response.getBytes(); 92 receiver.addOutput(inputData, 0, inputData.length); 93 receiver.flush(); 94 return null; 95 } 96 }; 97 if (expectedCommand != null) { 98 mockDevice.executeShellCommand(EasyMock.eq(expectedCommand), 99 EasyMock.<IShellOutputReceiver> anyObject(), EasyMock.eq(expectedTimeout), 100 EasyMock.eq(expectedTimeUnit), EasyMock.eq(expectedRetryAttempts)); 101 } else { 102 mockDevice.executeShellCommand(EasyMock.<String> anyObject(), 103 EasyMock.<IShellOutputReceiver> anyObject(), EasyMock.eq(expectedTimeout), 104 EasyMock.eq(expectedTimeUnit), EasyMock.eq(expectedRetryAttempts)); 105 } 106 if (asStub) { 107 EasyMock.expectLastCall().andStubAnswer(shellAnswer); 108 } else { 109 EasyMock.expectLastCall().andAnswer(shellAnswer); 110 } 111 } 112 } 113