Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2016 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.ITestDevice;
     21 import com.android.tradefed.result.ITestInvocationListener;
     22 import com.android.tradefed.result.ITestLifeCycleReceiver;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import org.easymock.EasyMock;
     27 
     28 import java.util.concurrent.TimeUnit;
     29 
     30 
     31 /**
     32  * Unit tests for {@link VtsFuzzTest}.
     33  */
     34 public class VtsFuzzTestTest extends TestCase {
     35     private ITestInvocationListener mMockInvocationListener = null;
     36     private IShellOutputReceiver mMockReceiver = null;
     37     private ITestDevice mMockITestDevice = null;
     38     private VtsFuzzTest mTest;
     39 
     40     /**
     41      * Helper to initialize the various EasyMocks we'll need.
     42      */
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         mMockInvocationListener = EasyMock.createMock(ITestInvocationListener.class);
     47         mMockReceiver = EasyMock.createMock(IShellOutputReceiver.class);
     48         mMockITestDevice = EasyMock.createMock(ITestDevice.class);
     49         EasyMock.expect(mMockITestDevice.getSerialNumber()).andStubReturn("serial");
     50         mTest = new VtsFuzzTest() {
     51             @Override
     52             IShellOutputReceiver createResultParser(
     53                     String runName, ITestLifeCycleReceiver listener) {
     54                 return mMockReceiver;
     55             }
     56         };
     57     }
     58 
     59     /**
     60      * Helper that replays all mocks.
     61      */
     62     private void replayMocks() {
     63       EasyMock.replay(mMockInvocationListener, mMockITestDevice, mMockReceiver);
     64     }
     65 
     66     /**
     67      * Helper that verifies all mocks.
     68      */
     69     private void verifyMocks() {
     70       EasyMock.verify(mMockInvocationListener, mMockITestDevice, mMockReceiver);
     71     }
     72 
     73     /**
     74      * Test the run method with a normal input.
     75      */
     76     public void testRunNormalInput() throws DeviceNotAvailableException {
     77         final String fuzzerBinaryPath = VtsFuzzTest.DEFAULT_FUZZER_BINARY_PATH;
     78 
     79         mTest.setDevice(mMockITestDevice);
     80         EasyMock.expect(mMockITestDevice.doesFileExist(fuzzerBinaryPath)).andReturn(true);
     81         mMockITestDevice.executeShellCommand(
     82             EasyMock.contains(fuzzerBinaryPath),
     83             EasyMock.same(mMockReceiver),
     84             EasyMock.anyLong(),
     85             (TimeUnit)EasyMock.anyObject(),
     86             EasyMock.anyInt());
     87 
     88         replayMocks();
     89 
     90         mTest.setTargetClass("hal_conventional");
     91         mTest.setTargetType("gps");
     92         mTest.setTargetVersion(1.0f);
     93         mTest.setTargetComponentPath("libunittest_foo.so");
     94         mTest.run(mMockInvocationListener);
     95         verifyMocks();
     96     }
     97 
     98     /**
     99      * Test the run method with abnormal input data.
    100      */
    101     public void testRunAbnormalInput() throws DeviceNotAvailableException {
    102         final String fuzzerBinaryPath = VtsFuzzTest.DEFAULT_FUZZER_BINARY_PATH;
    103 
    104         mTest.setDevice(mMockITestDevice);
    105         EasyMock.expect(mMockITestDevice.doesFileExist(fuzzerBinaryPath)).andReturn(true);
    106 
    107         replayMocks();
    108 
    109         try {
    110             mTest.run(mMockInvocationListener);
    111         } catch (IllegalArgumentException e) {
    112             // expected
    113         }
    114 
    115         verifyMocks();
    116     }
    117 
    118     /**
    119      * Test the run method without any device.
    120      */
    121     public void testRunDevice() throws DeviceNotAvailableException {
    122         mTest.setDevice(null);
    123 
    124         replayMocks();
    125 
    126         try {
    127             mTest.run(mMockInvocationListener);
    128         } catch (IllegalArgumentException e) {
    129             // expected
    130         }
    131 
    132         verifyMocks();
    133     }
    134 }
    135