Home | History | Annotate | Download | only in util
      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.util;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertNotNull;
     20 import static org.junit.Assert.assertNull;
     21 
     22 import com.android.tradefed.build.DeviceBuildInfo;
     23 import com.android.tradefed.targetprep.AltDirBehavior;
     24 
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.junit.runners.JUnit4;
     28 
     29 import java.io.File;
     30 import java.util.ArrayList;
     31 
     32 /** Unit tests for {@link BuildTestsZipUtils}. */
     33 @RunWith(JUnit4.class)
     34 public class BuildTestsZipUtilsTest {
     35 
     36     /**
     37      * Tests that when the search is unsuccessful across tests dir, we return null, and no exception
     38      * is thrown.
     39      */
     40     @Test
     41     public void testGetApkFile_notFound() throws Exception {
     42         DeviceBuildInfo buildInfo = new DeviceBuildInfo();
     43         File testDir = FileUtil.createTempDir("test-dir-build-tests");
     44         try {
     45             buildInfo.setTestsDir(testDir, "1");
     46             File apkFile =
     47                     BuildTestsZipUtils.getApkFile(
     48                             buildInfo,
     49                             "TestApk.apk",
     50                             new ArrayList<>(),
     51                             AltDirBehavior.FALLBACK,
     52                             true,
     53                             null);
     54             assertNull(apkFile);
     55         } finally {
     56             FileUtil.recursiveDelete(testDir);
     57         }
     58     }
     59 
     60     /** Tests that when the search finds the file in the tests dir, the file found is returned. */
     61     @Test
     62     public void testGetApkFile_fromTestDir() throws Exception {
     63         DeviceBuildInfo buildInfo = new DeviceBuildInfo();
     64         File testDir = FileUtil.createTempDir("test-dir-build-tests");
     65         try {
     66             buildInfo.setTestsDir(testDir, "1");
     67             File apkDir = new File(testDir, "TestApk");
     68             apkDir.mkdir();
     69             File apk = new File(apkDir, "TestApk.apk");
     70             apk.createNewFile();
     71             File apkFile =
     72                     BuildTestsZipUtils.getApkFile(
     73                             buildInfo,
     74                             "TestApk.apk",
     75                             new ArrayList<>(),
     76                             AltDirBehavior.FALLBACK,
     77                             true,
     78                             null);
     79             assertNotNull(apkFile);
     80             assertEquals(apk, apkFile);
     81         } finally {
     82             FileUtil.recursiveDelete(testDir);
     83         }
     84     }
     85 
     86     /**
     87      * Tests that when the search finds the file in the sub testcases dir we still properly find it
     88      * and return it.
     89      */
     90     @Test
     91     public void testGetApkFile_fromTestDir_testCase() throws Exception {
     92         DeviceBuildInfo buildInfo = new DeviceBuildInfo();
     93         File testDir = FileUtil.createTempDir("test-dir-build-tests");
     94         try {
     95             buildInfo.setTestsDir(testDir, "1");
     96             File testcasedir = new File(testDir, "testcases");
     97             testcasedir.mkdir();
     98             File apkDir = new File(testcasedir, "TestApk");
     99             apkDir.mkdir();
    100             File apk = new File(apkDir, "TestApk.apk");
    101             apk.createNewFile();
    102             File apkFile =
    103                     BuildTestsZipUtils.getApkFile(
    104                             buildInfo,
    105                             "TestApk.apk",
    106                             new ArrayList<>(),
    107                             AltDirBehavior.FALLBACK,
    108                             true,
    109                             null);
    110             assertNotNull(apkFile);
    111             assertEquals(apk, apkFile);
    112         } finally {
    113             FileUtil.recursiveDelete(testDir);
    114         }
    115     }
    116 }
    117