Home | History | Annotate | Download | only in testdefs
      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.testdefs;
     17 
     18 import com.android.tradefed.device.DeviceNotAvailableException;
     19 import com.android.tradefed.device.ITestDevice;
     20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     21 import com.android.tradefed.result.ITestInvocationListener;
     22 import com.android.tradefed.testtype.InstrumentationTest;
     23 import com.android.tradefed.testtype.MockInstrumentationTest;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.easymock.Capture;
     28 import org.easymock.EasyMock;
     29 import org.easymock.IAnswer;
     30 
     31 import java.io.File;
     32 import java.io.FileOutputStream;
     33 import java.io.IOException;
     34 import java.util.HashMap;
     35 
     36 /**
     37  * Unit tests for {@link XmlDefsTest}.
     38  */
     39 public class XmlDefsTestTest extends TestCase {
     40 
     41     private static final String TEST_PATH = "foopath";
     42     private static final String TEST_DEF_DATA = XmlDefsParserTest.FULL_DATA;
     43     private static final String TEST_PKG = XmlDefsParserTest.TEST_PKG;
     44     private static final String TEST_COVERAGE_TARGET = XmlDefsParserTest.TEST_COVERAGE_TARGET;
     45     private ITestDevice mMockTestDevice;
     46     private ITestInvocationListener mMockListener;
     47     private XmlDefsTest mXmlTest;
     48     private MockInstrumentationTest mMockInstrumentationTest;
     49 
     50     /**
     51      * {@inheritDoc}
     52      */
     53     @Override
     54     protected void setUp() throws Exception {
     55         super.setUp();
     56 
     57         mMockTestDevice = EasyMock.createMock(ITestDevice.class);
     58         EasyMock.expect(mMockTestDevice.getSerialNumber()).andReturn("foo").anyTimes();
     59         mMockListener = EasyMock.createMock(ITestInvocationListener.class);
     60         mMockInstrumentationTest = new MockInstrumentationTest();
     61 
     62         mXmlTest = new XmlDefsTest() {
     63             @Override
     64             InstrumentationTest createInstrumentationTest() {
     65                 return mMockInstrumentationTest;
     66             }
     67         };
     68         mXmlTest.setDevice(mMockTestDevice);
     69     }
     70 
     71     /**
     72      * Test the run normal case. Simple verification that expected data is passed along, etc.
     73      */
     74     public void testRun() throws DeviceNotAvailableException {
     75         mXmlTest.addRemoteFilePath(TEST_PATH);
     76 
     77         injectMockXmlData();
     78         mMockListener.testRunStarted(TEST_PKG, 0);
     79         Capture<HashMap<String, Metric>> captureMetrics = new Capture<HashMap<String, Metric>>();
     80         mMockListener.testRunEnded(EasyMock.anyLong(), EasyMock.capture(captureMetrics));
     81         EasyMock.replay(mMockTestDevice, mMockListener);
     82         mXmlTest.run(mMockListener);
     83         assertEquals(mMockListener, mMockInstrumentationTest.getListener());
     84         assertEquals(TEST_PKG, mMockInstrumentationTest.getPackageName());
     85         assertEquals(
     86                 TEST_COVERAGE_TARGET,
     87                 captureMetrics
     88                         .getValue()
     89                         .get(XmlDefsTest.COVERAGE_TARGET_KEY)
     90                         .getMeasurements()
     91                         .getSingleString());
     92     }
     93 
     94     private void injectMockXmlData() throws DeviceNotAvailableException {
     95         // TODO: it would be nice to mock out the file objects, so this test wouldn't need to do
     96         // IO
     97         mMockTestDevice.pullFile(EasyMock.eq(TEST_PATH), (File)EasyMock.anyObject());
     98         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
     99             @Override
    100             public Object answer() throws Throwable {
    101              // simulate the pull file by dumping data into local file
    102                 FileOutputStream outStream;
    103                 try {
    104                     outStream = new FileOutputStream((File)EasyMock.getCurrentArguments()[1]);
    105                     outStream.write(TEST_DEF_DATA.getBytes());
    106                     outStream.close();
    107                     return true;
    108                 } catch (IOException e) {
    109                     fail(e.toString());
    110                 }
    111                 return false;
    112             }
    113         });
    114     }
    115 
    116     /**
    117      * Test a run that was aborted then resumed
    118      */
    119     public void testRun_resume() throws DeviceNotAvailableException {
    120         mXmlTest.addRemoteFilePath(TEST_PATH);
    121         // turn off sending of coverage for simplicity
    122         mXmlTest.setSendCoverage(false);
    123         injectMockXmlData();
    124         mMockInstrumentationTest.setException(new DeviceNotAvailableException());
    125         EasyMock.replay(mMockTestDevice, mMockListener);
    126         try {
    127             mXmlTest.run(mMockListener);
    128             fail("DeviceNotAvailableException not thrown");
    129         } catch (DeviceNotAvailableException e) {
    130             // expected
    131         }
    132         // verify InstrumentationTest.run was called
    133         assertEquals(mMockListener, mMockInstrumentationTest.getListener());
    134         mMockInstrumentationTest.setException(null);
    135         mMockInstrumentationTest.clearListener();
    136         // resume test run, on a different device
    137         ITestDevice newTestDevice = EasyMock.createMock(ITestDevice.class);
    138         mXmlTest.setDevice(newTestDevice);
    139         mXmlTest.run(mMockListener);
    140         // verify InstrumentationTest.run was called again, with same listener + different device
    141         assertEquals(mMockListener, mMockInstrumentationTest.getListener());
    142         assertEquals(newTestDevice, mMockInstrumentationTest.getDevice());
    143     }
    144 
    145     /**
    146      * Test that IllegalArgumentException is thrown when attempting run without setting device.
    147      */
    148     public void testRun_noDevice() throws Exception {
    149         mXmlTest.addRemoteFilePath(TEST_PATH);
    150         mXmlTest.setDevice(null);
    151         try {
    152             mXmlTest.run(mMockListener);
    153             fail("IllegalArgumentException not thrown");
    154         } catch (IllegalArgumentException e) {
    155             // expected
    156         }
    157         assertNull(mMockInstrumentationTest.getPackageName());
    158     }
    159 
    160     /**
    161      * Test that IllegalArgumentException is thrown when attempting run without setting any file
    162      * paths.
    163      */
    164     public void testRun_noPath() throws Exception {
    165         try {
    166             mXmlTest.run(mMockListener);
    167             fail("IllegalArgumentException not thrown");
    168         } catch (IllegalArgumentException e) {
    169             // expected
    170         }
    171         assertNull(mMockInstrumentationTest.getPackageName());
    172     }
    173 }
    174