1 /* 2 * Copyright (C) 2010 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; 17 18 import com.android.ddmlib.IShellOutputReceiver; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.IFileEntry; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 23 import com.android.tradefed.result.ITestInvocationListener; 24 25 import junit.framework.TestCase; 26 27 import org.easymock.Capture; 28 import org.easymock.EasyMock; 29 30 import java.util.HashMap; 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * Unit tests for {@link NativeStressTest}. 35 */ 36 public class NativeStressTestTest extends TestCase { 37 38 private static final String RUN_NAME = "run-name"; 39 private ITestInvocationListener mMockListener; 40 private Capture<HashMap<String, Metric>> mCapturedMetricMap; 41 private NativeStressTest mNativeTest; 42 private ITestDevice mMockDevice; 43 private IFileEntry mMockStressFile; 44 45 /** 46 * {@inheritDoc} 47 */ 48 @Override 49 protected void setUp() throws Exception { 50 super.setUp(); 51 mNativeTest = new NativeStressTest(); 52 mMockListener = EasyMock.createMock(ITestInvocationListener.class); 53 mCapturedMetricMap = new Capture<HashMap<String, Metric>>(); 54 // expect this call 55 mMockListener.testRunStarted(RUN_NAME, 0); 56 mMockListener.testRunEnded(EasyMock.anyLong(), EasyMock.capture(mCapturedMetricMap)); 57 mMockDevice = EasyMock.createMock(ITestDevice.class); 58 mMockStressFile = EasyMock.createNiceMock(IFileEntry.class); 59 EasyMock.expect(mMockDevice.getFileEntry((String)EasyMock.anyObject())).andReturn( 60 mMockStressFile); 61 EasyMock.expect(mMockStressFile.isDirectory()).andReturn(Boolean.FALSE); 62 EasyMock.expect(mMockStressFile.getName()).andStubReturn(RUN_NAME); 63 EasyMock.expect(mMockStressFile.getFullEscapedPath()).andStubReturn(RUN_NAME); 64 65 mNativeTest.setDevice(mMockDevice); 66 EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn("serial"); 67 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.contains("chmod"))).andReturn(""); 68 } 69 70 /** 71 * Test a run where --iterations has not been specified. 72 */ 73 public void testRun_missingIterations() throws DeviceNotAvailableException { 74 try { 75 mNativeTest.run(mMockListener); 76 fail("IllegalArgumentException not thrown"); 77 } catch (IllegalArgumentException e) { 78 // expected 79 } 80 } 81 82 /** 83 * Test a run with default values. 84 */ 85 public void testRun() throws DeviceNotAvailableException { 86 mNativeTest.setNumIterations(100); 87 mMockDevice.executeShellCommand(EasyMock.contains("-s 0 -e 99"), (IShellOutputReceiver) 88 EasyMock.anyObject(), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), 89 EasyMock.anyInt()); 90 replayMocks(); 91 mNativeTest.run(mMockListener); 92 verifyMocks(); 93 } 94 95 /** 96 * Test a stress test execution with two runs. 97 */ 98 public void testRun_twoRuns() throws DeviceNotAvailableException { 99 mNativeTest.setNumIterations(100); 100 mNativeTest.setNumRuns(2); 101 mMockDevice.executeShellCommand(EasyMock.contains("-s 0 -e 99"), (IShellOutputReceiver) 102 EasyMock.anyObject(), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), 103 EasyMock.anyInt()); 104 mMockDevice.executeShellCommand(EasyMock.contains("-s 100 -e 199"), (IShellOutputReceiver) 105 EasyMock.anyObject(), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), 106 EasyMock.anyInt()); 107 108 replayMocks(); 109 mNativeTest.run(mMockListener); 110 verifyMocks(); 111 } 112 113 /** 114 * Test that stress test results are still reported even if device becomes not available 115 */ 116 public void testRun_deviceNotAvailable() throws DeviceNotAvailableException { 117 mNativeTest.setNumIterations(100); 118 mMockDevice.executeShellCommand(EasyMock.contains("-s 0 -e 99"), (IShellOutputReceiver) 119 EasyMock.anyObject(), EasyMock.anyLong(), (TimeUnit)EasyMock.anyObject(), 120 EasyMock.anyInt()); 121 EasyMock.expectLastCall().andThrow(new DeviceNotAvailableException()); 122 123 replayMocks(); 124 try { 125 mNativeTest.run(mMockListener); 126 fail("DeviceNotAvailableException not thrown"); 127 } catch (DeviceNotAvailableException e) { 128 // expected 129 } 130 verifyMocks(); 131 } 132 133 private void replayMocks() { 134 EasyMock.replay(mMockListener, mMockDevice, mMockStressFile); 135 } 136 137 private void verifyMocks() { 138 EasyMock.verify(mMockListener, mMockDevice); 139 } 140 } 141