Merge "Make ApexManagerTest use resources that are not prebuilt" into rvc-dev

This commit is contained in:
Mohammad Samiul Islam
2020-04-29 13:21:52 +00:00
committed by Android (Google) Code Review
3 changed files with 23 additions and 20 deletions

View File

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

View File

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