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