Home | History | Annotate | Download | only in suite
      1 /*
      2  * Copyright (C) 2017 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.targetprep.suite;
     17 
     18 import static org.junit.Assert.*;
     19 import static org.mockito.Mockito.doReturn;
     20 import static org.mockito.Mockito.times;
     21 import static org.mockito.Mockito.verify;
     22 
     23 import com.android.tradefed.build.IBuildInfo;
     24 import com.android.tradefed.build.IDeviceBuildInfo;
     25 import com.android.tradefed.device.ITestDevice;
     26 import com.android.tradefed.targetprep.TargetSetupError;
     27 import com.android.tradefed.util.FileUtil;
     28 
     29 import org.easymock.EasyMock;
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.mockito.Mockito;
     33 
     34 import java.io.File;
     35 import java.io.FileNotFoundException;
     36 import java.util.HashMap;
     37 import java.util.Map;
     38 
     39 /** Unit test for {@link SuiteApkInstaller} */
     40 public class SuiteApkInstallerTest {
     41 
     42     private SuiteApkInstaller mPreparer;
     43     private IBuildInfo mMockBuildInfo;
     44     private ITestDevice mMockDevice;
     45 
     46     @Before
     47     public void setUp() {
     48         mPreparer = new SuiteApkInstaller();
     49         mMockBuildInfo = Mockito.mock(IBuildInfo.class);
     50         mMockDevice = Mockito.mock(ITestDevice.class);
     51     }
     52 
     53     /**
     54      * Test that when there is no $ANDROID_TARGET_OUT_TESTCASES defined and no ROOT_DIR, we throw an
     55      * exception because we have nowhere to look for the files.
     56      */
     57     @Test
     58     public void testGetTestsDir_noVar_noRootDir() {
     59         mPreparer =
     60                 new SuiteApkInstaller() {
     61                     @Override
     62                     String getEnvVariable() {
     63                         return null;
     64                     }
     65                 };
     66         doReturn(new HashMap<String, String>()).when(mMockBuildInfo).getBuildAttributes();
     67         try {
     68             mPreparer.getTestsDir(mMockBuildInfo);
     69             fail("Should have thrown an exception.");
     70         } catch (FileNotFoundException expected) {
     71             // expected
     72         }
     73     }
     74 
     75     /**
     76      * Test that when there is no $ANDROID_TARGET_OUT_TESTCASES defined but a ROOT_DIR is defined,
     77      * we return the ROOT_DIR location.
     78      */
     79     @Test
     80     public void testGetTestsDir_noVar() throws Exception {
     81         mPreparer =
     82                 new SuiteApkInstaller() {
     83                     @Override
     84                     String getEnvVariable() {
     85                         return null;
     86                     }
     87                 };
     88         File tmpDir = FileUtil.createTempDir("suite-apk-installer");
     89         try {
     90             Map<String, String> attributes = new HashMap<>();
     91             attributes.put("ROOT_DIR", tmpDir.getAbsolutePath());
     92             doReturn(attributes).when(mMockBuildInfo).getBuildAttributes();
     93             File res = mPreparer.getTestsDir(mMockBuildInfo);
     94             assertNotNull(res);
     95             assertEquals(tmpDir.getAbsolutePath(), res.getAbsolutePath());
     96         } finally {
     97             FileUtil.recursiveDelete(tmpDir);
     98         }
     99     }
    100 
    101     /**
    102      * Tests that when $ANDROID_TARGET_OUT_TESTCASES is defined it is returned, we do not check
    103      * ROOT_DIR.
    104      */
    105     @Test
    106     public void testGetLocalPathForFilename_withVariable() throws Exception {
    107         File varDir = FileUtil.createTempDir("suite-apk-installer-var");
    108         File apk = FileUtil.createTempFile("testapk", ".apk", varDir);
    109         try {
    110             mPreparer =
    111                     new SuiteApkInstaller() {
    112                         @Override
    113                         String getEnvVariable() {
    114                             return varDir.getAbsolutePath();
    115                         }
    116                     };
    117             File res =
    118                     mPreparer.getLocalPathForFilename(mMockBuildInfo, apk.getName(), mMockDevice);
    119             verify(mMockBuildInfo, times(0)).getBuildAttributes();
    120             assertNotNull(res);
    121             assertEquals(apk.getAbsolutePath(), res.getAbsolutePath());
    122         } finally {
    123             FileUtil.recursiveDelete(varDir);
    124         }
    125     }
    126 
    127     /**
    128      * Tests that when $ANDROID_TARGET_OUT_TESTCASES is defined but is not a directory, we check and
    129      * return ROOT_DIR instead.
    130      */
    131     @Test
    132     public void testGetTestsDir_notDir() throws Exception {
    133         File varDir = FileUtil.createTempFile("suite-apk-installer-var", ".txt");
    134         File tmpDir = FileUtil.createTempDir("suite-apk-installer");
    135         File apkFile = FileUtil.createTempFile("apk-test", ".apk", tmpDir);
    136         try {
    137             mPreparer =
    138                     new SuiteApkInstaller() {
    139                         @Override
    140                         String getEnvVariable() {
    141                             return varDir.getAbsolutePath();
    142                         }
    143                     };
    144 
    145             Map<String, String> attributes = new HashMap<>();
    146             attributes.put("ROOT_DIR", tmpDir.getAbsolutePath());
    147             doReturn(attributes).when(mMockBuildInfo).getBuildAttributes();
    148             File res =
    149                     mPreparer.getLocalPathForFilename(
    150                             mMockBuildInfo, apkFile.getName(), mMockDevice);
    151             assertNotNull(res);
    152             assertEquals(apkFile.getAbsolutePath(), res.getAbsolutePath());
    153         } finally {
    154             FileUtil.recursiveDelete(varDir);
    155             FileUtil.recursiveDelete(tmpDir);
    156         }
    157     }
    158 
    159     /**
    160      * Test that {@link SuiteApkInstaller#getLocalPathForFilename(IBuildInfo, String, ITestDevice)}
    161      * returns the apk file when found.
    162      */
    163     @Test
    164     public void testGetLocalPathForFileName() throws Exception {
    165         File tmpApk = FileUtil.createTempFile("suite-apk-installer", ".apk");
    166         mPreparer =
    167                 new SuiteApkInstaller() {
    168                     @Override
    169                     protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
    170                         return tmpApk.getParentFile();
    171                     }
    172                 };
    173         try {
    174             File apk =
    175                     mPreparer.getLocalPathForFilename(
    176                             mMockBuildInfo, tmpApk.getName(), mMockDevice);
    177             assertEquals(tmpApk.getAbsolutePath(), apk.getAbsolutePath());
    178         } finally {
    179             FileUtil.deleteFile(tmpApk);
    180         }
    181     }
    182 
    183     /**
    184      * Test that {@link SuiteApkInstaller#getLocalPathForFilename(IBuildInfo, String, ITestDevice)}
    185      * throws an exception when the apk file is not found.
    186      */
    187     @Test
    188     public void testGetLocalPathForFileName_noFound() throws Exception {
    189         File tmpApk = FileUtil.createTempFile("suite-apk-installer", ".apk");
    190         mPreparer =
    191                 new SuiteApkInstaller() {
    192                     @Override
    193                     protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
    194                         return tmpApk.getParentFile();
    195                     }
    196                 };
    197         try {
    198             mPreparer.getLocalPathForFilename(mMockBuildInfo, "no_exist", mMockDevice);
    199             fail("Should have thrown an exception.");
    200         } catch (TargetSetupError expected) {
    201             // expected
    202         } finally {
    203             FileUtil.deleteFile(tmpApk);
    204         }
    205     }
    206 
    207     /**
    208      * Test that {@link SuiteApkInstaller#getLocalPathForFilename(IBuildInfo, String, ITestDevice)}
    209      * returns the apk file located in IDeviceBuildInfo.getTestsDir().
    210      */
    211     @Test
    212     public void testGetLocalPathForFileName_testsDir() throws Exception {
    213         mPreparer =
    214                 new SuiteApkInstaller() {
    215                     @Override
    216                     protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
    217                         return null;
    218                     }
    219                 };
    220         IDeviceBuildInfo deviceBuildInfo = EasyMock.createMock(IDeviceBuildInfo.class);
    221         File tmpDir = null;
    222         try {
    223             tmpDir = FileUtil.createTempDir("test");
    224             File tmpApk = FileUtil.createTempFile("suite-apk-installer", ".apk", tmpDir);
    225             EasyMock.expect(deviceBuildInfo.getTestsDir()).andReturn(tmpDir);
    226             EasyMock.replay(deviceBuildInfo);
    227             File apk =
    228                     mPreparer.getLocalPathForFilename(
    229                             deviceBuildInfo, tmpApk.getName(), mMockDevice);
    230             assertEquals(tmpApk.getAbsolutePath(), apk.getAbsolutePath());
    231             EasyMock.verify(deviceBuildInfo);
    232         } finally {
    233             FileUtil.recursiveDelete(tmpDir);
    234         }
    235     }
    236 }
    237