Merge "Make ApexManagerTest use resources that are not prebuilt" into rvc-dev am: cd99932241 am: d4af8c13ac am: 387cd16920

Change-Id: I30d69dad713169ddb933f24b97a8a9642e458304
This commit is contained in:
Mohammad Samiul Islam
2020-04-29 14:18:13 +00:00
committed by Automerger Merge Worker
3 changed files with 23 additions and 20 deletions

View File

@@ -99,6 +99,7 @@ android_test {
":PackageParserTestApp1",
":PackageParserTestApp2",
":PackageParserTestApp3",
":apex.test",
],
resource_zips: [":FrameworksServicesTests_apks_as_resources"],
}

View File

@@ -36,7 +36,6 @@ import android.apex.ApexSessionParams;
import android.apex.IApexService;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.FileUtils;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
@@ -44,7 +43,6 @@ import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.android.frameworks.servicestests.R;
import com.android.server.pm.parsing.PackageParser2;
import com.android.server.pm.parsing.TestPackageParser2;
@@ -52,11 +50,12 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@SmallTest
@Presubmit
@@ -64,6 +63,7 @@ import java.io.InputStream;
public class ApexManagerTest {
private static final String TEST_APEX_PKG = "com.android.apex.test";
private static final String TEST_APEX_FILE_NAME = "apex.test.apex";
private static final int TEST_SESSION_ID = 99999999;
private static final int[] TEST_CHILD_SESSION_ID = {8888, 7777};
private ApexManager mApexManager;
@@ -274,7 +274,7 @@ public class ApexManagerTest {
}
private ApexInfo[] createApexInfo(boolean isActive, boolean isFactory) {
File apexFile = copyRawResourceToFile(TEST_APEX_PKG, R.raw.apex_test);
File apexFile = extractResource(TEST_APEX_PKG, TEST_APEX_FILE_NAME);
ApexInfo apexInfo = new ApexInfo();
apexInfo.isActive = isActive;
apexInfo.isFactory = isFactory;
@@ -308,27 +308,29 @@ public class ApexManagerTest {
return params;
}
/**
* Copies a specified {@code resourceId} to a temp file. Returns a non-null file if the copy
* succeeded
*/
File copyRawResourceToFile(String baseName, int resourceId) {
File outFile;
// Extracts the binary data from a resource and writes it to a temp file
private static File extractResource(String baseName, String fullResourceName) {
File file;
try {
outFile = File.createTempFile(baseName, ".apex");
file = File.createTempFile(baseName, ".apex");
} catch (IOException e) {
throw new AssertionError("CreateTempFile IOException" + e);
}
try (InputStream is = mContext.getResources().openRawResource(resourceId);
FileOutputStream os = new FileOutputStream(outFile)) {
assertThat(FileUtils.copy(is, os)).isGreaterThan(0L);
} catch (FileNotFoundException e) {
throw new AssertionError("File not found exception " + e);
try (
InputStream in = ApexManager.class.getClassLoader()
.getResourceAsStream(fullResourceName);
OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
if (in == null) {
throw new IllegalArgumentException("Resource not found: " + fullResourceName);
}
byte[] buf = new byte[65536];
int chunkSize;
while ((chunkSize = in.read(buf)) != -1) {
out.write(buf, 0, chunkSize);
}
return file;
} catch (IOException e) {
throw new AssertionError("IOException" + e);
throw new AssertionError("Exception while converting stream to file" + e);
}
return outFile;
}
}