1 /* 2 * Copyright (C) 2014 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.targetprep; 18 19 import com.android.tradefed.build.DeviceBuildInfo; 20 import com.android.tradefed.build.IDeviceBuildInfo; 21 import com.android.tradefed.command.remote.DeviceDescriptor; 22 import com.android.tradefed.device.DeviceAllocationState; 23 import com.android.tradefed.device.ITestDevice; 24 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 25 import com.android.tradefed.result.ITestInvocationListener; 26 import com.android.tradefed.result.TestDescription; 27 import com.android.tradefed.testtype.InstrumentationTest; 28 29 import junit.framework.TestCase; 30 31 import org.easymock.EasyMock; 32 33 import java.util.HashMap; 34 35 /** 36 * Unit tests for {@link InstrumentationPreparer}. 37 */ 38 public class InstrumentationPreparerTest extends TestCase { 39 40 private InstrumentationPreparer mInstrumentationPreparer; 41 private ITestDevice mMockDevice; 42 private IDeviceBuildInfo mMockBuildInfo; 43 private InstrumentationTest mMockITest; 44 45 @Override 46 public void setUp() throws Exception { 47 super.setUp(); 48 mMockDevice = EasyMock.createMock(ITestDevice.class); 49 EasyMock.expect(mMockDevice.getSerialNumber()).andReturn("foo").anyTimes(); 50 mMockBuildInfo = new DeviceBuildInfo("0",""); 51 } 52 53 @Override 54 public void tearDown() throws Exception { 55 super.tearDown(); 56 } 57 58 public void testRun() throws Exception { 59 final String packageName = "packageName"; 60 final TestDescription test = new TestDescription("FooTest", "testFoo"); 61 mMockITest = 62 new InstrumentationTest() { 63 @Override 64 public void run(ITestInvocationListener listener) { 65 listener.testRunStarted(packageName, 1); 66 listener.testStarted(test); 67 listener.testEnded(test, new HashMap<String, Metric>()); 68 listener.testRunEnded(0, new HashMap<String, Metric>()); 69 } 70 }; 71 mInstrumentationPreparer = new InstrumentationPreparer() { 72 @Override 73 InstrumentationTest createInstrumentationTest() { 74 return mMockITest; 75 } 76 }; 77 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andReturn(new DeviceDescriptor("SERIAL", 78 false, DeviceAllocationState.Available, "unknown", "unknown", "unknown", "unknown", 79 "unknown")); 80 EasyMock.replay(mMockDevice); 81 mInstrumentationPreparer.setUp(mMockDevice, mMockBuildInfo); 82 EasyMock.verify(mMockDevice); 83 } 84 85 public void testRun_testFailed() throws Exception { 86 final String packageName = "packageName"; 87 final TestDescription test = new TestDescription("FooTest", "testFoo"); 88 mMockITest = 89 new InstrumentationTest() { 90 @Override 91 public void run(ITestInvocationListener listener) { 92 listener.testRunStarted(packageName, 1); 93 listener.testStarted(test); 94 listener.testFailed(test, null); 95 listener.testEnded(test, new HashMap<String, Metric>()); 96 listener.testRunEnded(0, new HashMap<String, Metric>()); 97 } 98 }; 99 mInstrumentationPreparer = new InstrumentationPreparer() { 100 @Override 101 InstrumentationTest createInstrumentationTest() { 102 return mMockITest; 103 } 104 }; 105 EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(new DeviceDescriptor( 106 "SERIAL", false, DeviceAllocationState.Available, "unknown", "unknown", "unknown", 107 "unknown", "unknown")); 108 EasyMock.replay(mMockDevice); 109 try { 110 mInstrumentationPreparer.setUp(mMockDevice, mMockBuildInfo); 111 fail("BuildError not thrown"); 112 } catch(final BuildError e) { 113 assertTrue("The exception message does not contain failed test names", 114 e.getMessage().contains(test.toString())); 115 // expected 116 } 117 EasyMock.verify(mMockDevice); 118 } 119 } 120